Skip to content

Functional Programming

Notes on programming with pure functions and composition. They cover evaluation and laziness, referential transparency, recursion and folds, the functor, applicative, traversable and monad family, and how real programs are built from them: errors as values, effect systems, program structure, streams, concurrency and property-based testing. Haskell and Scala (with Cats, Cats Effect and ZIO) are the languages used to practise it.

How the notes are organised

The notes sit in five subfolders that match the sections below, in reading order. Notes marked (new) were written from the library books they cite; the others grew out of earlier study notes. Each note ends with the library sources it draws on.

Start with FP reading paths to pick a route through the books.

Foundations

  • Pure functions and effects: referential transparency, side effects, immutability, and the functional core with an imperative shell.
  • Evaluation and laziness: reduction and the substitution model, the Church-Rosser property, thunks and non-strict semantics, space leaks, and call-by-name in Scala.
  • Lambda calculus: binding, function application and β-reduction, reduction strategies, Church encodings, and interactive tools.
  • Functions and composition: lambdas and closures, returning functions as API design, currying and partial application, the loan pattern, and pipelines.
  • Recursion and folds: rules for writing recursive functions, map as abstracted recursion, foldl and foldr and their types, Foldable, and combinators built from folds.

Abstractions

  • Map and flatMap: what map preserves in an Option, the identity and composition laws behind it, and when the next step needs flatMap.
  • Functor: a functor as a computational context, fmap and its type, writing your own instances, and the functor laws.
  • Applicative: applying functions that are themselves inside a functor, pure and <*>, and applicative style.
  • Traversable and traverse (new): traverse, sequenceA and traverse_, how the applicative decides the behaviour, laws, and the essence of the iterator pattern.
  • Monads: binding pure functions together, and the Option, Writer, State, Reader and IO monads.
  • Semigroup and monoid: associative combination, the identity element, the Haskell classes and their laws.
  • Type classes and polymorphism: parametric and type-class polymorphism, type classes in Haskell and Scala, higher-kinded types, GADTs, and type-level programming.
  • Category theory: why it is worth knowing, purity and denotational semantics, natural transformations, and links for learning it.

Building programs

  • Error handling as values (new): why not exceptions, Option, Either and Try, fail-fast versus accumulating validation, typed errors in effect types, and converting at boundaries.
  • Effect systems (new): effects as values, the effect pattern, a toy IO, Haskell IO, Cats Effect 3, ZIO, and a comparison of the three; also the former Cats note.
  • Structuring effectful programs (new): monad transformers, mtl-style classes, service handles and ReaderT, free monads, tagless final, ZIO layers, and when to pick which.
  • Streams (new): lazy lists, why laziness gives modularity, effectful pull-based streams (fs2, conduit, pipes), resource safety, backpressure, and stream fusion.
  • Concurrency and parallelism (new): deterministic parallelism in Haskell, threads and STM, fibers in Cats Effect and ZIO, and purely functional parallelism as a design exercise.
  • Property-based testing (new): properties instead of examples, generators and shrinking, where good properties come from, and testing type-class laws.

Languages

  • Haskell: learning path, resources, ecosystem, libraries, language notes (definitions, GHCi, lists, constructors), advice, the design costs of base, and a custom Prelude.
  • Scala: ecosystem, expressions and methods, classes and objects, operators, comprehensions, collections, traits, abstract members, variance, interpolators, encapsulation, dependency injection, implicits (with Scala 3 equivalents), and sources.

Practice and study

  • FP reading paths: goal-ordered routes through the library (Scala, Haskell, application design, theory), followed by the original article, course and talk links.
  • Refactoring functional code: Haskell examples: a Collatz step counter, replacing partial functions with types that carry the guarantee, and wholemeal programming.
  • Exercises and projects: exercise collections, specific problems, learning tasks, and project ideas.
  • FP study log: a dated log from November and December 2023 on folds, fusion, unfolds, laziness and traversals.
  • FP links to sort: unfiled links, open questions and todos, grouped by where they should end up.

The type-driven design section holds the typed-modeling material that used to sit in these notes:

Other notes:

Explore this section

Building Functional Programs

How real programs are built from pure functions: errors as values, effect systems, ways to structure effectful code, streams, concurrency and parallelism, and property-based testing.

Browse 6 notes
  • Effects as values kept apart from their execution, built up from a toy IO to Haskell IO, Cats Effect 3 and ZIO, with a comparison of the three and a study order for the Scala effect libraries.

  • Errors as values instead of exceptions: totality, Option, Either and Try, fail-fast versus accumulating validation and why accumulation is applicative, typed errors in effect types, and converting at boundaries.

  • Parallelism versus concurrency in functional code: deterministic parallelism with strategies and the Par monad, Haskell threads, MVar, async and STM, fibers in Cats Effect and ZIO, and laws that keep parallel code equivalent to sequential.

  • From lazy lists to effectful pull-based streams: why laziness gives modularity, why lazy I/O isn't enough, fs2, conduit and pipes, resource safety, backpressure and stream fusion.

  • Testing with properties instead of examples in QuickCheck and ScalaCheck: pure generators, shrinking, where good properties come from, testing type-class laws, and why purity makes it easy.

  • Why monads don't compose and the ways around it: monad transformers, mtl-style classes, service handles and ReaderT, free monads, tagless final and ZIO layers, with their trade-offs and when to pick which.

Foundations of Functional Programming

The ideas the rest of the functional programming notes build on: purity and effects, evaluation and laziness, the lambda calculus, functions and composition, and recursion and folds.

Browse 5 notes

Functional Abstractions: Functor to Monad

The type-class abstractions of functional programming, from map and flatMap through functor, applicative, traversable and monad to monoids, polymorphism and category theory.

Browse 8 notes
  • Applying functions that sit inside a functor with pure and <*>, applicative style for independent computations, how it differs from a monad, the laws, and the Maybe, list and ZipList instances.

  • Why category theory is worth knowing for a programmer, how its standard constructions map onto types, functors and currying, purity and denotational semantics, natural transformations, and resources for learning it.

  • A functor as a computational context: the Functor class and fmap, kinds and type constructors, lifting, the functor laws, and instances including functions.

  • Monads as a way to glue functions into sequential computations, map, flatMap and for expressions in Scala, and the Option, Writer, State, Reader and IO monads, including what IO does and doesn't make pure.

  • Associative combination and the identity element: definitions, laws, examples and uses of semigroups and monoids, and why the identity matters.

  • traverse, sequenceA and traverse_, how the chosen applicative decides between short-circuiting, IO effects, parallelism and state, writing instances, the laws, and the essence of the iterator pattern.

  • Kinds of polymorphism and parametricity, type classes and their Haskell and Scala encodings compared with Java interfaces, higher-kinded types, GADTs, type-level programming with singletons, and tagless final.

  • What map preserves in a structure such as Option, the identity and composition laws behind it, and when the next step needs flatMap instead, with Scala examples.

Functional Programming Languages: Haskell and Scala

Language notes, ecosystems and learning resources for Haskell and Scala, the two languages the functional programming notes use.

Browse 2 notes
  • Haskell learning path, toolchain and ecosystem, standard library alternatives and a custom Prelude, useful libraries, language notes from GHCi to constructors, practical advice, and books, courses and other resources.

  • Scala language notes from expressions, classes, traits and variance to comprehensions, collections and implicits with their Scala 3 equivalents, plus the functional ecosystem, dependency injection, and books, talks and other resources.

Practice and Study in Functional Programming

Putting functional programming into practice: reading paths through the library, exercises and projects, refactoring examples, a dated study log, and links still to file.

Browse 5 notes
  • Exercise collections for Haskell and Scala, specific problems on lists, trees, functors and applicatives, learning tasks, and project ideas from games to reference applications.

  • Unfiled functional programming links, open questions and todos, grouped by the note they should move to, from domain modelling and optics to Scala articles and the study plan.

  • Goal-ordered routes through the library for Scala, Haskell, functional application design and theory, followed by articles, books, courses, talks and blogs.

  • A dated study log from November and December 2023 covering folds and fusion, unfolds and origami programming, laziness and trees, traverse, continuation-passing style, monoids and monads.

  • Refactoring examples in Haskell: splitting a Collatz step counter into separate jobs, replacing partial functions with types that carry the guarantee, and wholemeal programming.