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,
foldlandfoldrand their types, Foldable, and combinators built from folds.
Abstractions
- Map and flatMap: what
mappreserves in anOption, the identity and composition laws behind it, and when the next step needsflatMap. - Functor: a functor as a computational context,
fmapand its type, writing your own instances, and the functor laws. - Applicative: applying functions that are themselves inside a functor,
pureand<*>, and applicative style. - Traversable and traverse (new):
traverse,sequenceAandtraverse_, 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,EitherandTry, 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.
Related material elsewhere
The type-driven design section holds the typed-modeling material that used to sit in these notes:
- Functional domain modeling: effects kept out of the domain, and modules as algebras with interpreters.
- Effects, interpreters and wiring: Reader and Kleisli, free monads, ZIO and algebraic effects; Tagless final has a note of its own.
- Algebraic data types and Newtypes and simple values: product and sum types and newtypes, moved from
haskell.mdandscala.md; the folder’s overview, including type-first design moved fromhaskell.md, is Domain modeling with types. - Scala type system: abstract types, nominal and structural subtyping, and refined types, moved from
scala.md. - Declarative design: functional programming as one of the declarative sub-paradigms.
Other notes:
- Language choice for AI systems: Go, Scala, Haskell and BEAM compared for AI systems.