Skip to content

Chapter 1 of 8

Foundations of Functional Programming

Functional programming is a declarative paradigm that treats computation as the evaluation of mathematical functions. Rather than describing step-by-step procedures that mutate state, functional programs describe what the result should be, letting the runtime determine how to compute it. Languages such as Haskell, Erlang, Clojure, and F# are primarily functional, while many mainstream languages now incorporate functional features. The paradigm emphasizes immutability, pure functions, and the avoidance of side effects, standing in contrast to imperative programming, which uses loops, mutations, and explicit control flow to describe how a task is performed.

A pure function always returns the same output for the same input and has no observable side effects. The simple addition function \( a + b \) is pure: given the same arguments, it always yields the same result, and it does not modify any external state. By contrast, a function like \( \text{Math.random}() \) is impure because its output varies and because the act of calling it interacts with a global random state. Purity gives rise to referential transparency, the property that any expression can be replaced by its value without changing the program's behavior, which enables equational reasoning, aggressive compiler optimizations, and dramatically simpler testing — there is no need to set up or tear down external state, no mocking of dependencies, and tests are independent and can run in parallel.

Side effects are any observable changes a function makes beyond returning a value: modifying global or external variables, writing to files or databases, printing to the console, making network requests, or mutating input arguments. Functional programming aims to isolate side effects from core logic rather than eliminate them entirely, since real programs must eventually interact with the outside world. Immutability complements this goal by ensuring that once a data structure is created, it cannot be changed; instead of mutating data, programs create new copies with the desired modifications. This combination of purity, isolation of effects, and immutability is the cornerstone of functional design and brings benefits such as easier reasoning about code, thread safety without locks, and simpler debugging.

All chapters
  1. 1Foundations of Functional Programming
  2. 2Functions and Higher-Order Programming
  3. 3Data Structures and Types
  4. 4Recursion, Folds, and Evaluation
  5. 5Functors, Applicatives, and Monads
  6. 6Type System Concepts
  7. 7Lambda Calculus and Category Theory
  8. 8Real-World Functional Programming and Concurrency

Drill it

Reading is not remembering. These come from the Functional Programming deck:

Q

What is functional programming?

Functional programming is a declarative programming paradigm that treats computation as the evaluation of mathematical functions. It emphasizes immutability, pu...

Q

What is a pure function?

A pure function is a function that: Always returns the same output for the same input (deterministic)Has no side effects — it doesn't modify external state, per...

Q

What are side effects in functional programming?

Side effects are any observable changes a function makes beyond returning a value. Examples include: Modifying a global or external variableWriting to a file or...

Q

What is immutability?

Immutability means that once a data structure is created, it cannot be changed. Instead of modifying data, you create new copies with the desired changes. Benef...