Skip to content

appleweiping/haskell-mooc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Haskell MOOC — Exercise Solutions (Sets 1–16)

Complete, test-passing solutions to every exercise of the University of Helsinki Haskell MOOC — an independent, from-skeleton implementation built on the course's own Stack project and shipped test suite, part of a csdiy.wiki full-catalog build.

status language license

Overview

The Haskell MOOC (haskell.mooc.fi) from the University of Helsinki teaches functional programming in Haskell across two parts and 16 exercise sets, covering everything from basic recursion up to monads, applicative validation, property-based testing, and phantom types. This repository contains a full solution to every exercise in the course's official moocfi/haskell-mooc skeleton — 258 exercises across Sets 1–16 — each verified against the shipped QuickCheck/hspec test suite via stack test.

The starter skeleton (module scaffolding, Mooc.* test harness, Examples.* modules, and every SetNTest.hs) is the course's own, imported as the first commit. All exercise-body implementations in the SetN.hs files are mine.

Results (measured on Windows, CPU-only, GHC 9.2.8 / Stack lts-20.25)

Each set is graded by its own SetNTest.hs, which prints a per-set X / Y score and writes score.json. Full captured output is in results/.

Every exercise passes: 248 / 248 across all 26 test suites (Sets 1–16).

Set Topic Score Set Topic Score
Set1 Bool/Int/arithmetic 11/11 Set9b N-Queens solver 9/9
Set2a recursion 11/11 Set10a laziness, infinite lists 9/9
Set2b pattern matching 8/8 Set10b forcing without seq 4/4
Set3a lists & strings 14/14 Set11a IO, do-notation 10/10
Set3b Maybe/Either 10/10 Set11b IORef, file handles 7/7
Set4a type classes 12/12 Set12 Functor/Foldable 11/11
Set4b folds, HOFs 7/7 Set13a Maybe/State/Logger 10/10
Set5a algebraic datatypes 12/12 Set13b list monad, custom SL 9/9
Set5b binary trees 10/10 Set14a Text/ByteString 8/8
Set6 class instances 12/12 Set14b SQLite bank HTTP API 8/8
Set7 Semigroup/Monoid, DSL 10/10 Set15 Applicative/Validation 15/15
Set8 functional image library 10/10 Set16a QuickCheck properties 7/7
Set9a misc functions 9/9 Set16b phantom types 5/5

Each results/SetN.txt contains the full per-exercise +++++ Pass output and the final X / Y line from that set's SetNTest.hs. Aggregate: 248 passed / 248 total.

Implemented assignments

Part 1 — functional programming basics:

  • Set1 — Bool, Int, arithmetic, function definitions
  • Set2a / Set2b — recursion, guards, pattern matching on lists
  • Set3a / Set3b — lists, strings, Maybe, Either
  • Set4a / Set4b — type classes, Eq/Ord, folds, higher-order functions
  • Set5a / Set5b — algebraic & recursive datatypes, binary trees
  • Set6 — defining type class instances (Eq, Ord, Num, custom classes)
  • Set7Semigroup/Monoid, NonEmpty, a small state machine and arithmetic DSL
  • Set8 — the functional image library project (JuicyPixels): shapes, transforms, blur

Part 2 — monads, applicatives, effects:

  • Set9a / Set9b — misc functions; the N-Queens solver via backtracking
  • Set10a / Set10b — laziness, infinite lists; forcing evaluation without seq
  • Set11a / Set11bIO, do-notation, IORef, file handles
  • Set12Functor and Foldable instances (fmap, foldr, foldMap)
  • Set13a / Set13bMaybe/State/Logger/list monads; a custom SL monad
  • Set14a / Set14bText/ByteString; an SQLite-backed bank HTTP API (wai/warp)
  • Set15Applicative, Validation, Alternative, functor composition
  • Set16a — property-based testing with QuickCheck (Property, generators, Arbitrary)
  • Set16b — phantom types for compile-time currency/name safety

Project structure

haskell-mooc/
├── exercises/
│   ├── Set1.hs .. Set16b.hs        # exercise solutions (implemented by me)
│   ├── Set1Test.hs .. Set16bTest.hs # shipped test suites (course skeleton)
│   ├── Mooc/                        # course test harness (Test, Th, Todo, ...)
│   ├── Examples/                    # course example modules (Bank, Validation, Phantom, ...)
│   ├── stack.yaml                   # resolver lts-20.25 (GHC 9.2.8)
│   └── tests.cabal
├── results/                         # captured `stack runghc SetNTest.hs` output
├── LICENSE
└── README.md

How to run

Requires Stack. From the repo root:

cd exercises
stack setup          # fetches GHC 9.2.8 (one-time, ~480 MB)
stack build          # builds the library (test harness + examples)

# Run one set's test suite (prints "X / Y" and writes score.json):
stack runghc Set7Test.hs

# Run every set's tests and capture output (reproduces results/):
for s in 1 2a 2b 3a 3b 4a 4b 5a 5b 6 7 8 9a 9b 10a 10b 11a 11b 12 13a 13b 14a 14b 15 16a 16b; do
  stack runghc "Set${s}Test.hs"
done

Verification

Every set was verified with the course's own test suite (SetNTest.hs, built on QuickCheck + the Mooc.Test harness). Each run prints a per-exercise 1/0/_ line and a final score / total. Captured outputs live in results/; a summary is in the results table above.

Tech stack

  • Language: Haskell (GHC 9.2.8)
  • Build/test: Stack (resolver lts-20.25), QuickCheck, hspec
  • Libraries used across sets: JuicyPixels (image DSL), text, bytestring, wai/warp (HTTP server), sqlite-simple (database), transformers (State), containers

Key ideas / what I learned

  • Building up from pure recursion to Semigroup/Monoid, Functor/Foldable, and the monad hierarchy, then to Applicative-style validation that accumulates all errors.
  • Writing lazy functions that work on infinite lists, and forcing evaluation with pattern matching when seq is unavailable.
  • Structuring effectful programs: IO/do, IORef state, and a real SQLite + wai/warp HTTP API.
  • Encoding a functional image as Coord -> Color and composing shapes, patterns, and transforms.
  • Using phantom types to make currency mismatches and name-kind mismatches compile-time errors.
  • Property-based testing: expressing correctness as QuickCheck Propertys and writing generators.

Credits & license

Based on the exercises of the Haskell MOOC by the University of Helsinki (Joel Kaasinen / John Lång and contributors). The exercises/ skeleton — module scaffolding, the Mooc.* test harness, Examples.* modules, and every SetNTest.hs — is from the official moocfi/haskell-mooc repository and belongs to its original authors. This repository is an independent educational reimplementation of the exercise solutions; my own code (the SetN.hs exercise bodies) is released under the MIT License.

About

Complete solutions to the University of Helsinki Haskell MOOC — all 248 exercises across Sets 1-16, passing the official stack test suites (248/248)

Topics

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors