Functional Programming Exercises and Projects
Practice material for functional programming in Haskell and Scala: exercise collections, specific problems with hints, small learning tasks, and project ideas. Language-agnostic sites (Advent of Code, Exercism, Project Euler, Codewars) are in coding katas.
Exercise collections
H-99 and S-99 are ports of the same problem list, the Ninety-Nine Prolog Problems (P-99). Solving a problem in one language and then the other is a cheap way to compare idioms.
Haskell
- H-99, Ninety-Nine Haskell Problems: https://wiki.haskell.org/H-99:_Ninety-Nine_Haskell_Problems
- JD95/haskell-problem-sets: https://github.com/JD95/haskell-problem-sets
- AskElle (ideas.science.uu.nl): https://ideas.science.uu.nl/AskElle/
- Educative answers, Haskell exercises: https://www.educative.io/answers
- Work through the CIS 194 exercises.
Scala
- Li Haoyi, Hands-on Scala Programming (Li 2020), project-based.
- Coursehunter course, Scala and functional programming practice: https://coursehunter.net/course/scala-i-funkcionalnogo-programmirovaniya-praktika
- Scala Exercises: https://www.scala-exercises.org/, source at https://github.com/scala-exercises/scala-exercises. It brings the Scala Koans to the web, with hundreds of exercises in 42 categories on the basics of the language. Originally built by 47 Degrees; the old address http://scala-exercises.47deg.com/ is the same site.
- Scala Advent of Code (Scala Center): https://scalacenter.github.io/scala-advent-of-code/, source at https://github.com/scalacenter/scala-advent-of-code
- S-99, Ninety-Nine Scala Problems: http://aperiodic.net/phil/scala/s-99
- Exercism, community-driven Scala exercises: https://exercism.org/tracks/scala/exercises
- dehun/learn-fp, a learn-by-doing FP course in Scala covering type classes, functors, applicatives, monads, monad transformers and the free monad: https://github.com/dehun/learn-fp/
Other collections
- Earl Douglas, exercises: https://earldouglas.com/posts/exercises.html
Queued to do:
- Utrecht University FP course, 2019 exercises: archived copy (the original page is gone)
- Gist by oskarkv: https://gist.github.com/oskarkv/3168ea3f8d7530ccd94c97c19aafe266. The jot “learn a haskell” sat next to it; it probably means Learn You a Haskell for Great Good!, but it may also have been a label for this gist (needs checking).
- Drew University (Burd), exercises to accompany Introduction to Functional Programming: https://users.drew.edu/bburd/functionalprogramming/ExercisesToAccompanyIntroductionToFunctionalProgramming.pdf
- University of Innsbruck, FP course, winter semester 2015: archived copy (the original page is gone)
Specific problems
Lists and trees as ADTs
Write a List type as an ADT. A list is either empty or a head followed by another list. In Haskell,
data List a = Nil | Cons a (List a). In Scala 3, anenumwith two cases; make it covariant (+A) so thatMyList[Nothing]works as the empty list for every element type.enum MyList[+A]: case Empty case Cons(head: A, tail: MyList[A])Add
headOptionandtailOption, and use them in your code. The point is totality:headon an empty list has no sensible answer and throws, whileheadOption: Option[A]andtailOption: Option[MyList[A]]put the empty case in the type. Notice howOptionforces every caller to decide what happens for the empty list, and howforcomprehensions overOptionkeep that readable.Write a recursive
toString. Build the string for the tail and put the head in front, taking care with the separator so there is no trailing comma. Try it on a list of 100,000 elements: a plain recursive version overflows the stack on the JVM. Fix it with an accumulator and@tailrec, or write it as afoldLeft. See Writing recursive functions.Write a Tree type as an ADT. A binary tree:
data Tree a = Leaf | Node (Tree a) a (Tree a), or a Scala 3enumwith the same two cases. Follow-ups:size,depth, insertion and lookup for a binary search tree, afoldover the tree, and aFunctorinstance.
List functions
Write these yourself, in Haskell:
take,drop,length,reverseandavg. Decide whattakeanddropdo with a negative count or a count larger than the list; the Prelude versions return[]or the whole list rather than failing. Writereversetwice: the naive version with++is quadratic, the version with an accumulator is linear. Foravg, compute the sum and the count in one pass, and give it a type that admits the empty list, such as[Double] -> Maybe Double.map,filter,removeandremoveFirst.remove pisfilter (not . p).removeFirstremoves only the first element that matches and keeps the rest unchanged, so it can’t be written withfilter. Use explicit recursion that stops at the first match, orbreak.product,concatAllandreverse, also with a fold. For each function, find the value for the empty list and the function that combines one element with the result for the rest; that pair is the argument tofoldr.reverseis the odd one out: it reads naturally as a left fold,foldlwith an accumulator. Compare it with afoldrversion, which needs++and is quadratic. See Folds.any, using composition(.).any p xsis true if some element satisfiesp. With composition it can be built from two existing functions without mentioningxs(point-free style): map the predicate over the list, then combine the booleans. Check that your version stops at the firstTrueon an infinite list.allonFoldable. Inbasethe type isall :: Foldable t => (a -> Bool) -> t a -> Bool, so the same function works on lists,Maybe,Map kand trees. Two routes:foldrwith&&, orfoldMapwith theAllmonoid fromData.Monoid. Both stop early on the firstFalse, because&&is lazy in its second argument.elem, three ways: directly, withfilterandlength, and withany.elemneeds anEq aconstraint, since it compares elements. Direct recursion and theanyversion stop at the first match, soelem 3 [1..]returnsTrue. Thefilterandlengthversion counts every match, so it walks the whole list and never finishes on an infinite one.
Functor (Serokell)
Haskell exercises from Serokell, “What’s that typeclass: Functor”: https://serokell.io/blog/whats-that-typeclass-functor
- Implement
Functorfor several types. The article’s list of types was not copied. Good candidates, roughly in order of difficulty: a customMaybe, a custom list, a binary tree,Either e, the pair((,) a)and the function type(( -> ) r). For the last three, work out from the kind why only the last type parameter can be mapped. - Implement
(<$)forMaybe a.(<$) :: a -> f b -> f areplaces every value inside the functor with a constant and keeps the structure. Decide what happens toNothingand toJust, then check your answer against the default definition inbase, which writes it in terms offmapandconst. - Convert a string to upper case without
toUpper. AStringis[Char], so this isfmap(ormap) of a function on single characters. For that function, useordandchrfromData.Char: lower-case ASCII letters are 32 code points after their upper-case versions. Leave every other character unchanged. Caveat: the offset trick only works for ASCII. Unicode case mapping is not a fixed offset, and some characters don’t map one to one (the Germanßupper-cases toSS), which is whytoUpperuses tables. - Prove that the instance
Functor (( -> ) a)follows the identity and composition laws. For this instancefmapis function composition,fmap = (.). Write each law with that definition substituted, apply both sides to an arbitrary argumentx, and reduce. The identity law follows fromidbeing a unit for composition, the composition law from composition being associative.
Applicative (GPWH)
Exercises from the Functor, Applicative and Monad unit of Will Kurt, Get Programming with Haskell (Manning). See Applicative.
<$> is fmap: it applies a plain function to a value in a context, (a -> b) -> f a -> f b. <*> applies a function that is itself in a context, f (a -> b) -> f a -> f b. Together they apply a function of several arguments to several values in a context: f <$> x <*> y.
- Work with
<*>and<$>: go back to some of the examples in unit 1 and change the values used as arguments toMaybevalues. - Use
<$>and<*>to combine twoMaybe Stringvalues with++.(++)takes two arguments. Mapping it over the firstMaybe Stringgives aMaybe (String -> String), a function waiting for its second argument inside aMaybe, and<*>supplies that argument. Check the result when either value isNothing.
The book then points ahead to Monad:
This solution works, but having to write a wrapper function to make it work for Maybe should be a warning at this point. In lesson 28, you saw a similar pattern of being forced to write a wrapper function to work in a context as motivating more powerful type classes. But at this point, you might not be convinced of the need for yet another, even more powerful type class.
– Kurt, Get Programming with Haskell
<$> and <*> handle functions whose arguments are in a context. They don’t help when the function itself returns a value in a context, a -> Maybe b: mapping it over a Maybe a gives Maybe (Maybe b). >>= is the operation that chains such functions. See Monads.
Learning tasks
Implement
sum,productandlengthin Haskell, and test them. Property-based testing with QuickCheck fits well: compare against the Prelude versions, and check laws such assum (xs ++ ys) == sum xs + sum ysandlength (xs ++ ys) == length xs + length ys. Then try the functions on[1..10^7]. A naivefoldlor a non-strict accumulator builds up thunks and uses far more memory than needed, which is whyfoldl'exists. See Folds, Evaluation and laziness and Property-based testing.State and Reader monads. Reader is a function from a shared, read-only environment,
newtype Reader r a = Reader (r -> a), used for configuration and dependencies. State is a function from a state to a result and a new state,newtype State s a = State (s -> (a, s)). Write both newtypes yourself with theirFunctor,ApplicativeandMonadinstances, then use State for a stack or a counter and Reader for passing a configuration through several functions. See Reader and Monads.Build a toy effect system (#project). Start by describing console programs as data and writing the interpreter separately:
data Console a = Done a | PrintLine String (Console a) | ReadLine (String -> Console a) run :: Console a -> IO a run (Done a) = pure a run (PrintLine s k) = putStrLn s >> run k run (ReadLine k) = getLine >>= run . kNext steps: give
ConsoleaMonadinstance so programs can usedonotation; write a second, pure interpreter that takes a list of input lines and returns the output, for tests; then generalise the structure, which leads to the free monad. Comparing that with a tagless final version of the same program covers the two main designs. More in Effect systems. This project is also on the list in Project Ideas.
Project ideas
Haskell project assignment
A saved project-assignment excerpt (source not recorded):
For your project you may write some sort of Haskell application or library which does something fun/useful/interesting. Your imagination is the limit. Some possibilities/suggestions include:
- A program to play a game (like tic-tac-toe, Connect 4, othello, gomoku, poker, mancala, …) against the user.
- A program to solve puzzles like sudoku or kenken.
- A program to generate random mazes and let the user interactively solve them, or to solve mazes input by the user.
- An implementation of some interesting data structure like red-black trees, 2-3-4 trees, binomial heaps, or Fibonacci heaps.
- An parser and interpreter for a small programming language, such as a while language.
- A raytracer.
- Create a web site using hakyll.
- Take an interesting program you have written in some other language, and figure out how to port/re-implement it in idiomatic Haskell.
- Whatever else your creativity suggests!
For the game idea, Hughes 1990 (Library sources) has an alpha-beta game-playing example.
Games
- Andrea/FunctionalProgrammingInGames: https://github.com/Andrea/FunctionalProgrammingInGames
- unclebob/spacewar, a game: https://github.com/unclebob/spacewar, with the Clean Coder blog post “Spacewar” (2021): https://blog.cleancoder.com/uncle-bob/2021/11/28/Spacewar.html
- Devsisters tech blog, Scala for game server development: https://tech.devsisters.com/posts/scala-for-game-server-development/
- jdegoes/lambdaconf-2014-introgame, a “find out” game: https://github.com/jdegoes/lambdaconf-2014-introgame
Reference applications in Scala
These are complete applications to read rather than exercises. pfps-shopping-cart is the application built in Volpe’s Practical FP in Scala (tagless final with Cats, Cats Effect and fs2; see Library sources), and pfps-examples holds the book’s smaller examples. scala-pet-store (noted as “scala-petshop”) is another full application. Reading one alongside the book and then rebuilding a small slice of it yourself works better than copying it.
- gvolpe/pfps-shopping-cart: https://github.com/gvolpe/pfps-shopping-cart
- gvolpe/pfps-examples: https://github.com/gvolpe/pfps-examples
- pauljamescleary/scala-pet-store: https://github.com/pauljamescleary/scala-pet-store
Refactor a plain first version
Write a plain first version of a project, then refactor it in small steps towards the structures, tools, libraries and patterns of Scala and FP, keeping each step as a separate commit to compare. Typical steps: null to Option, exceptions to Either, loops to map and folds, mutable state to values passed through functions, duplicated logic to type classes, and side effects pushed to the edge with IO. See Refactoring functional code.
See also: coding katas, Recursion and folds, Functor, Applicative, Monads, Effect systems, Refactoring functional code, Project Ideas.
Library sources
- Li 2020, Hands-on Scala Programming: listed under Scala; project-based, ending with a file synchronizer and a small language implementation.
- Volpe 2021, Practical FP in Scala (2nd edition): the book behind pfps-shopping-cart and pfps-examples.
- Allen and Moronuki 2016, Haskell Programming from First Principles: the standard list functions by recursion (9.12, “Chapter Exercises”) and again with folds (10.10, “Rewriting functions using folds”:
myOr,myAny,myElem,myReverse,myMap,myFilter), on List functions; aBinaryTreewith insert, map and fold (11.17, “Binary Tree”), on Lists and trees as ADTs;Functorinstances for small types (16.10, “Exercises: Instances of Func”), on Functor (Serokell); implementing the Reader monad (22.7) and State’s instances (23.6, “Write State for yourself”), on Learning tasks. - Pilquist et al. 2023, Functional Programming in Scala (2nd edition): exercise-driven; chapter 3, Functional data structures, covers the
ListandTreeADT problems and fold exercises (3.1-3.4), chapter 6 buildsState(6.5), chapter 11 buildsFunctorandMonad(11.1-11.2), and chapter 13 builds anIOeffect type step by step. - Hughes 1990, Why Functional Programming Matters: section 5, “An Example from Artificial Intelligence”, builds alpha-beta game-tree search from small lazily composed parts; relevant to the game project idea; on Haskell project assignment.
- Bird and Gibbons 2020, Algorithm Design with Haskell: chapter 1 exercises with answers: a linear-time
reversewithfoldl(1.4),mapandfilterasfoldr(1.5),takeWhileas afoldrthat stops early (1.7), and whenfoldrandfoldlagree (1.10); on List functions. - Serrano Mena 2021, Book of Monads (2nd edition): section 13.3, “Initial Style”, gives the recipe the toy
Consoletype follows (one constructor per operation with a continuation for its result, plus one for the final value), with itsMonadinstance and smart constructors, and exercises 13.6 (aFunctorinstance), 13.8 (aStateinterpreter for a file-system monad), 13.11 (rewriting it as a free monad) and 13.3 (a final-style version); section 6.1 and exercise 6.1 on theStatenewtype and itsMonadinstance; on Learning tasks. - Zhang 2021, Structural induction tutorial (CPCS449): the principle of structural induction for lists and a proof template (slides 2-3), then proofs of
sum (xs ++ ys) = sum xs + sum ys,xs ++ [] = xsand associativity of++(slide 4); the laws under Learning tasks proved instead of tested; on Learning tasks. - Bird 1998, Introduction to Functional Programming using Haskell (2nd edition): sections 4.2.3, “Reverse”, and 4.5, “The fold functions”, write
reversewith++and asfoldl cons [], section 4.6.1, “Duality theorems”, proves the two equal, section 7.2.3, “Reverse and concat”, times them at Θ(n²) and Θ(n) and showsconcatis faster asfoldrthan asfoldl, and section 7.4.4, “Average”, computes sum and length in one pass, on the reverse, fold and avg exercises under List functions; section 6.1, “Binary trees”, defines size, height andfoldBtree, with size and height as folds (6.1.3), and section 6.2, “Binary search trees”, usesdata Stree a = Null | Fork (Stree a) a (Stree a), the note’s Tree shape, with member, insert and delete (6.2.1), and asks formapStreeandfoldStreeas exercises, on Lists and trees as ADTs; section 7.5, “Controlling space”, reducessum [1 .. 1000]withfoldland shows the expression growing linearly, the effect the[1..10^7]experiment is meant to show, on Learning tasks. - Hutton 1999, A tutorial on the universality and expressiveness of fold: section 2, “The fold operator”, defines
sum,product,and,or,(++),length,reverse,mapandfilteras folds; section 4, “Increasing the power of fold: generating tuples”, computes sum and length in one fold (sumlength, the one-passavg) and shows whydropWhileneeds a pair; on List functions.
Further reading:
- Welsh and Gurnell 2020, Scala with Cats
- Bjarnason 2015, A companion booklet to “Functional Programming in Scala”
- Skinner 2023, Effective Haskell: worked Haskell projects with end-of-chapter exercises: a pager (chapter 8), a directory-metrics tool (chapter 10), a file archiver serialised, parsed and finally rebuilt on monad transformers (chapters 11 to 13), and a spellchecker refactored from a naive edit distance through memoisation,
STand vectors (chapter 14); on Project ideas and Refactor a plain first version.