SLIDE 1 / 10
CSZone.co.uk
Click anywhere to advance · Arrow keys also work
AQA 7517 · Paper 2 · 4.12.1a

Functional
Programming

Pure functions · immutability · first-class functions · higher-order functions · map/filter/reduce

WHAT YOU'LL LEARN
Functional vs imperative · pure functions · immutability · first-class/higher-order functions · map, filter, reduce
AQA SPEC LINK
4.12.1 — Functional programming: pure functions, immutability, higher-order functions, map, filter, reduce/fold
Functional vs Imperative

Functional Programming Paradigm

Imperative (Python/Java style)
Describes how to do something. Uses loops, variables, state changes, side effects.
Functional (Haskell style)
Describes what to compute. Uses functions, avoids state changes and side effects.
Functional programs are easier to reason about, test, and parallelise
AQA uses Haskell notation in exam questions (but also accepts pseudocode)
Pure Functions

Pure Functions

A pure function always returns the same output for the same input, and has no side effects — it does not modify any external state.
-- Pure: same input → always same output
double x = x * 2

-- NOT pure: depends on/modifies external state
-- e.g. reading from a file, printing to screen, modifying a global variable
Benefits: easy to test (no mocking needed), safe to parallelise, predictable
Immutability

Immutability

Immutability means that once a value is assigned, it cannot be changed. Instead of modifying data, functional programs create new values.
-- Immutable: x cannot be reassigned once bound
x = 5
y = x + 3 -- y is 8; x is still 5

-- Instead of mutating a list, create a new one:
addToList item list = item : list
Eliminates whole classes of bugs caused by shared mutable state
First-Class Functions

First-Class & Higher-Order Functions

First-Class Functions
Functions are treated as values — they can be passed as arguments, returned from functions, and stored in variables.
Higher-Order Functions
A function that takes one or more functions as arguments, or returns a function as its result.
Map

map — Apply a Function to Every Element

map takes a function and a list; applies the function to each element; returns a new list of the same length.
-- Haskell: double every element in a list
map (*2) [1, 2, 3, 4]
-- Result: [2, 4, 6, 8]

-- Apply a named function
map double [1, 2, 3]
-- Result: [2, 4, 6]
Filter

filter — Select Elements Matching a Predicate

filter takes a predicate (function returning Bool) and a list; returns only the elements for which the predicate is True.
-- Keep only even numbers
filter even [1, 2, 3, 4, 5, 6]
-- Result: [2, 4, 6]

-- Keep numbers greater than 3
filter (>3) [1, 2, 3, 4, 5]
-- Result: [4, 5]
Reduce / Fold

fold (reduce) — Accumulate a Result

foldr/foldl takes a function, an initial accumulator value, and a list; combines all elements into a single value by repeatedly applying the function.
-- Sum all elements (fold right)
foldr (+) 0 [1, 2, 3, 4]
-- = 1+(2+(3+(4+0))) = 10

-- Find product of list
foldr (*) 1 [1, 2, 3, 4]
-- = 24
AQA Exam Style

Practice Question

AQA 7517 — Paper 2 Style
(a) Explain what is meant by a pure function. [2]
(b) Explain what is meant by immutability in functional programming. [2]
(c) The list nums = [1, 2, 3, 4, 5, 6]. Write expressions using map and filter to:
   (i) Produce [2, 4, 6, 8, 10, 12] [1]
   (ii) Produce [2, 4, 6] [1]
   (iii) Produce the sum 21 using fold. [2]
[8 marks]
2 marks
(a) Always returns the same output for the same input [1]; and has no side effects / does not modify any external state [1]
2 marks
(b) Once a value is bound to a name it cannot be changed [1]; instead of modifying data, new values are created [1]
4 marks
(i) map (*2) nums [1]; (ii) filter even nums [1]; (iii) foldr (+) 0 nums [1] giving 21 [1]
Summary

Key Points to Remember

Pure function — same input → same output; no side effects
Immutability — values cannot be changed; create new values instead
First-class functions — functions as values; passed as arguments, returned
map — apply function to all elements; filter — keep matching elements; fold — reduce to single value
Higher-order function — takes/returns a function (map, filter, fold are all higher-order)
🎉 Lesson complete — move to the quiz!