AQA 7517, section 4.12.2.1
You should have experience of constructing simple programs in a functional programming language, or in a language that supports the functional paradigm.
Languages
Functional programming languages include Haskell, Standard ML, Scheme and Lisp.
Other languages have built-in support for the functional style as well as other styles: Python, F#, C#, Scala, Java 8 onwards, Delphi XE onwards and VB.NET 2008 onwards. The examples here use the short notation that AQA exam papers use, which is close to Haskell.
What functional style looks like
| Feature | Imperative style | Functional style |
|---|---|---|
| The program is | A series of statements that change state | A set of function definitions and applications |
| Variables | Change value | Values are not changed once made (immutable) |
| Repetition | Loops (for, while) |
Recursion and higher-order functions |
| Functions | May have side effects | Pure: depend only on inputs, with no side effects |
| Focus | How to do it, step by step | What to compute |
A pure function always gives the same output for the same input, and doesn't change anything else.
How functional programs are written in the exam
AQA writes functional code in a short notation, like Haskell's. A function definition gives the function's name, its parameters and the expression for its result:
square x = x * x
Applying a function is written with a space, not brackets: square 5 gives 25. A list is written in square brackets, such as [1, 2, 3].
Operators as functions
An operator in brackets is the function for that operator:
(+)is the function that adds two numbers, so(+) 3 4gives 7;(*)is the function that multiplies two numbers.
An operator with one of its arguments already filled in is a partially applied function (4.12.1.4):
(<10)is the test "is less than 10", so(<10) 7gives True;(*2)doubles its argument.
Recursion instead of loops
There are no loops. Repetition is done with recursion, often with one line for each case. The sum of the numbers from 1 to :
total 0 = 0
total n = n + total (n - 1)
Higher-order functions
A function is higher-order if it takes a function as an argument, or returns a function as a result, or does both.
You need experience of three standard higher-order functions: map, filter and fold (called reduce in some languages, such as Python).
map
map applies a given function to every element of a list, and gives a list of the results.
map square [1, 2, 3, 4]
gives [1, 4, 9, 16]. Similarly, map (+1) [5, 6] gives [6, 7].
The result list is the same length as the original. The original list is not changed.
filter
filter gives a new list containing only the elements for which a given test is True.
filter even [1, 2, 3, 4, 5, 6]
gives [2, 4, 6]. Similarly, filter (<10) [4, 15, 9, 22] gives [4, 9].
The result is the same length or shorter.
fold
fold reduces a list of values to a single value by repeatedly applying a combining function, starting from a given value.
fold (+) 0 [1, 2, 3, 4]
gives 10. The function is (+), and 0 is the starting value (the accumulator). Trace:
| Step | Accumulator | Next item | New accumulator |
|---|---|---|---|
| 1 | 0 | 1 | |
| 2 | 1 | 2 | |
| 3 | 3 | 3 | |
| 4 | 6 | 4 |
The final accumulator, 10, is the result. The combining function can be anything:
fold (*) 1 [1, 2, 3, 4]gives (a product starts from 1, not 0);fold max 0 [3, 9, 2, 7]gives 9 (the largest value).
Summary
| Function | Input | Output | Length of result |
|---|---|---|---|
map f xs |
A function and a list | A list of applied to each item | Same as the original |
filter p xs |
A test and a list | The items for which is True | The same or shorter |
fold f start xs |
A combining function, a starting value and a list | A single value | One value, not a list |
Combining them
The three can be combined, each working on the result of the one before.
Task: find the sum of the squares of the even numbers from 1 to 6.
fold (+) 0 (map square (filter even [1, 2, 3, 4, 5, 6]))
Work from the inside out:
filter even [1, 2, 3, 4, 5, 6]gives[2, 4, 6].map square [2, 4, 6]gives[4, 16, 36].fold (+) 0 [4, 16, 36]gives .
No value is changed and no loop is written.
Worked example
These functions are defined:
fa x = x * x
fb xs = filter odd xs
fc xs = map fa xs
fd xs = fold (+) 0 (fc (fb xs))
Task: (a) name the higher-order functions used in these definitions, and (b) evaluate fd [1, 2, 3, 4, 5], showing each step.
(a) filter, map and fold are higher-order, because each takes a function as an argument (odd, fa and (+)). fa, fb, fc and fd themselves take a number or a list, not a function, so they are not higher-order.
(b) Work from the inside out:
fb [1, 2, 3, 4, 5]=filter odd [1, 2, 3, 4, 5]=[1, 3, 5].fc [1, 3, 5]=map fa [1, 3, 5]=[1, 9, 25].fold (+) 0 [1, 9, 25]= .
So fd [1, 2, 3, 4, 5] gives 35: the sum of the squares of the odd numbers.
Exam tip: know exactly what each does. map transforms every item, filter keeps the items that pass a test, and fold combines all the items into a single value. In a trace, write the list after each step.
Check yourself
What is a higher-order function?
A function that takes a function as an argument, returns a function as a result, or does both.
What is the result of map (\x -> x * 2) [1, 2, 3]?
The list [2, 4, 6].
What is the result of filter (>10) [5, 12, 7, 20]?
The list [12, 20].
What does fold do?
It reduces a list of values to a single value by repeatedly applying a combining function to the values.