← Back to subjects
0
AQA A-level Computer Science (7517) · Fundamentals of functional programming
Mini-Lesson

Functional programming

This mini-lesson covers AQA 4.12 — Fundamentals of functional programming. It is a genuinely different way of thinking: you stop giving the machine orders and start describing relationships between values.

  • Function type, domain and co-domain
  • Functions as first-class objects
  • Function application, composition and partial application
  • Higher-order functions — map, filter, reduce/fold
  • Immutability and why it matters
  • List operations — head, tail, prepend, append, length

Work through each screen, answer the questions as you go (some are recall, some are calculations you must actually work out) and collect ⭐ stars. Press Start when you're ready.

4.12 · Functions

Function type, domain and co-domain

In functional programming a function is a mapping from one set of values to another — a mathematical object, not a list of commands.

  • The domain is the set of values the function may take as input.
  • The co-domain is the set the output is drawn from.
  • The function type is written domain → co-domain.
square : ℤ → ℤ // integer in, integer out isEven : ℤ → 𝔹 // integer in, Boolean out length : String → ℕ // string in, natural number out

Function application is simply giving a function its argument: square 4 evaluates to 16. Notice there is no assignment, no loop and no state — just a value being replaced by another value.

Why the type matters: the type is a contract, checkable before the program ever runs. If a function claims ℤ → 𝔹 and you try to add 1 to its result, the compiler rejects it. A whole class of bugs becomes unrepresentable.

4.12 · First-class

Functions as first-class objects

A first-class object is anything the language will let you treat like an ordinary value. In a functional language, functions are first-class:

  • they can be assigned to a variable;
  • passed as arguments to other functions;
  • returned as the result of a function;
  • stored inside a list or other data structure.

A function that takes a function as a parameter, or returns one, is called a higher-order function. This is what makes functional code so compact — the general pattern (looping over a list) is written once, and you supply the specific behaviour.

Partial function application: supply some arguments now and get back a new function awaiting the rest.

add x y = x + y add5 = add 5 // partial application → a new function add5 3 // evaluates to 8

The payoff: partial application lets you build specialised functions out of general ones with no boilerplate at all. add5 was created without writing a new definition.

4.12 · Composition

Function composition

Composition chains functions together: the output of one becomes the input of the next.

(f ∘ g)(x) = f(g(x))read right to left — g runs first
Worked example

f(x) = 2x + 1    g(x) = x²

(f ∘ g)(3) = f(g(3)) = f(9) = 2(9) + 1 = 19

(g ∘ f)(3) = g(f(3)) = g(7) = 7² = 49

Composition is not commutative — the order changes the answer, as those two lines show. This trips people up constantly.

Why this is powerful: a large program becomes a pipeline of small, individually testable functions. Each does one thing; composition assembles them. Compare the imperative alternative, where behaviour is smeared across loops and mutable variables and cannot be tested in isolation.

Calculate

Your turn — composition

1Given f(x) = 2x + 1 and g(x) = x², evaluate (f ∘ g)(3).
Hint: Right to left: g runs first. Work out g(3), then feed that result into f.
4.12 · Higher-order

map, filter and reduce

These three higher-order functions replace almost every loop you have ever written.

FunctionWhat it doesResult
mapApplies a function to every elementA list of the same length
filterKeeps only the elements that pass a testA list of the same or shorter length
reduce / foldCombines all elements using a function and a starting valueA single value
map (x → x * x) [1,2,3,4] = [1,4,9,16] filter (x → x > 5) [1,4,9,16] = [9,16] reduce (+) 0 [9,16] = 25

Chained together, that reads as: square everything, keep what is bigger than 5, add up what remains. Three lines, no loop counter, no mutable accumulator, no off-by-one error.

Note the shapes: map preserves length; filter can only shrink it; reduce collapses it to one value. If an exam question asks which function could turn a list of 6 into a list of 6, the answer is map — never filter.

Calculate

Your turn — the pipeline

2Evaluate the whole pipeline:
reduce (+) 0 (filter (x > 5) (map (x → x * x) [1,2,3,4]))
Hint: Square each element first, then discard everything that is 5 or less, then add up what survives.
Calculate

Your turn — fold

3Evaluate reduce (+) 0 [3, 1, 4, 1, 5].
Hint: Fold starts from 0 and adds each element in turn: 0 + 3, then + 1, and so on.
4.12 · Immutability

Immutability and list operations

In a purely functional language, data is immutable: once a value exists it can never be changed. "Changing" a list means producing a new list. Nothing is overwritten, so:

  • a function can have no side effects — it cannot secretly alter something elsewhere;
  • the same call always gives the same answer (referential transparency), so it can be replaced by its result;
  • concurrent code is safe: with nothing shared and mutable, there is nothing to race over;
  • debugging is far easier — a value cannot have been corrupted by some distant line of code.

Standard list operations:

head [3,1,4] = 3 // the first element tail [3,1,4] = [1,4] // everything but the first prepend 9 [3,1,4] = [9,3,1,4] // a NEW list append 9 [3,1,4] = [3,1,4,9] // a NEW list length [3,1,4] = 3 isEmpty [3,1,4] = False

head and tail are the engine of recursion: nearly every functional list algorithm is "do something with the head, then recurse on the tail, stopping when the list is empty". That is the functional replacement for a for-loop.

Quick check

Higher-order functions

?Which statement correctly describes a higher-order function?
Quick check

map vs filter

?A list of 8 temperatures is passed through a function and a list of 8 Fahrenheit values comes back. Which higher-order function was used?
Quick check

Immutability

?Why does immutability make concurrent programming dramatically safer?
Sort it

map, filter or reduce?

Tap the description, then tap the function it describes.

🔁 map

🔍 filter

➕ reduce / fold

Match it

Match the term to its meaning

Tap an item on the left, then its partner on the right.

Meaning
Term
Recap

The big ideas to know

Types: function type = domain → co-domain · application = giving a function its argument

First-class: functions can be assigned, passed, returned and stored — that is what enables higher-order functions

Composition: (f ∘ g)(x) = f(g(x)) — right to left, and not commutative

Higher-order: map (same length) · filter (same or shorter) · reduce/fold (one value)

Partial application: add5 = add 5 — supply some arguments now, get a new function back

Immutability: no side effects · referential transparency · safe concurrency without locks

Lists: head · tail · prepend · append · length — head plus recursion on the tail replaces the loop

That is the whole of AQA 4.12. Press Finish to see your score.

🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Functional programming for AQA A-level Computer Science (7517). 🎉

Your stars: 0 / 0

Next: test yourself in the Evaluate stage Confidence Quiz, then lock it in with Verify.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to all subjects