A-Level · Functional Programming

Functional Programming

A genuinely different way of thinking about a program: not a sequence of steps that change things over time, but a set of functions that transform values into other values, with nothing hidden and nothing mutated behind your back. Every claim on this page is something you can watch happen, not just read about.

Section 2

Pure functions vs impure functions

A pure function always returns the same output for the same input, and touches nothing outside itself. An impure function might depend on, or change, something external. Watch this proven, not asserted, call each one three times with the identical input.

Pure: addTax(price)

function addTax(price) { return price * 1.2; }

Impure: addToRunningTotal(price)

let runningTotal = 0; function addToRunningTotal(price) { runningTotal += price; return runningTotal; }
External state (runningTotal)
0
Click either button to see what happens when you call it three times with the identical input.

Exam tips

  • Purity isn't about the function "looking simple", it's a precise property: no reliance on external state, and no modification of anything outside the function's own local scope. addTax passes both tests. addToRunningTotal fails both, it reads AND writes the external runningTotal variable.
  • Pure functions are easier to test, reason about and run in parallel, calling one twice can never produce a different answer or a race condition, calling an impure one twice genuinely can.
Section 3

Higher-order functions: map, filter, reduce

A higher-order function takes another function as an argument. These three are the backbone of functional programming, watch data flow through all three stages, one at a time.

Original list

Pipeline

Exam tips

  • map transforms every element and keeps the same number of items. filter removes elements and never changes the ones that remain. reduce collapses the whole list into a single value. Each does exactly one job, chaining them is how functional code builds up complex behaviour from simple pieces.
  • None of map, filter or reduce need a for-loop written anywhere in your own code, the looping is entirely hidden inside the higher-order function itself.
Section 4

Function composition

Composing two functions builds a new function that applies one, then the other. Order genuinely changes the result, verified directly below.

Exam tips

  • compose(f, g)(x) means f(g(x)), g runs first, its result feeds into f. Swapping the order generally gives a different answer, exactly like (5+1)×2=12 differs from (5×2)+1=11.
  • Composition is how small, single-purpose pure functions get combined into larger behaviour, without ever needing a variable to hold an "in progress" intermediate state.
Section 5

Recursion instead of loops

Many functional languages have no for-loop or while-loop at all. Every repeated action is expressed as a function calling itself with a smaller version of the problem. Watch the call stack build up, then unwind.

function recursiveSum(list) { if (list.length === 0) return 0; return list[0] + recursiveSum(list.slice(1)); }

Call stack

Controls

List

Exam tips

  • Every recursive call here works on a strictly smaller list (one element shorter), guaranteeing it eventually reaches the empty-list base case. No base case, no guaranteed termination, exactly the same requirement as any recursive algorithm.
  • The calls genuinely stack up first (going deeper) before any addition actually happens, the additions only occur as the stack unwinds back out, right to left in the trace.
Section 6

Lambda expressions

A lambda is a function with no name, defined right where it's used. Build one and apply it directly.

Try one

Exam tips

  • A lambda has no name to call later, it exists only for as long as it's needed, typically passed straight into a higher-order function like map or filter as the transformation or condition to apply.
  • Anywhere a lambda is used, a named function defined separately would work identically, the lambda is purely a convenience for short, throwaway logic, not a different kind of function underneath.
Section 7

Check your understanding