AQA 7517, section 4.12.3.1
In functional programming, lists are processed by taking them apart and building new ones, not by changing them in place. This depends on seeing a list as a head and a tail.
Head and tail
A list can be thought of as two parts joined together:
- the head: the first element of the list. It is an element (a single value);
- the tail: everything after the first element. It is a list, which may be empty.
The colon : builds a list from a head and a tail, so the list [6, 1, 8] is the same as
6 : [1, 8]
with 6 as its head and [1, 8] as its tail.
| List | Head | Tail |
|---|---|---|
[6, 1, 8] |
6 |
[1, 8] |
[1, 8] |
1 |
[8] |
[8] |
8 |
[] |
[] |
(none) | (none) |
The head is always one value, but the tail is always a list, and may itself be empty.
The empty list
A list can be empty. It has no elements, and is written []. A list of one element, such as [8], has head 8 and the empty list as its tail. The empty list has no head or tail, and trying to take them is an error.
The list operations
You need to describe and apply seven operations.
| Operation | What it does | Written as | Example |
|---|---|---|---|
| Return head | The first element | head xs |
head [6,1,8] is 6 |
| Return tail | The list without its first element | tail xs |
tail [6,1,8] is [1,8] |
| Test for empty list | True if the list has no elements | null xs |
null [] is True |
| Return length | The number of elements | length xs |
length [6,1,8] is 3 |
| Construct an empty list | Make a list with no elements | [] |
[] |
| Prepend an item | Add an item at the front | x : xs |
7 : [6,1,8] is [7,6,1,8] |
| Append an item | Add an item at the end | xs ++ [x] |
[6,1,8] ++ [7] is [6,1,8,7] |
None of these changes the original list. Prepending or appending gives a new list.
The (x:xs) pattern
Functions on lists are usually written with two lines, one for the empty list and one for any other list:
length [] = 0
length (x:xs) = 1 + length xs
In the second line, (x:xs) matches any non-empty list: x stands for its head and xs for its tail. So the two lines say:
- base case: the length of the empty list is 0;
- recursive case: the length of a list is 1 + the length of its tail.
There is no loop. Trace of length [6, 1, 8]:
length [6,1,8]
= 1 + length [1,8]
= 1 + (1 + length [8])
= 1 + (1 + (1 + length []))
= 1 + (1 + (1 + 0))
= 3
Processing a list recursively
Most list functions follow the same pattern: deal with the empty list (base case), and otherwise combine the head with the result of processing the tail.
Sum of a list
total [] = 0
total (x:xs) = x + total xs
| Call | Head x | Result |
|---|---|---|
total [6,1,8] |
6 | |
total [1,8] |
1 | |
total [8] |
8 | |
total [] |
0 (base case) |
Working back: , then , then .
Reversing a list
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]
The reverse of a list is the reverse of its tail, with the head appended at the end. reverse [6, 1, 8] gives [8, 1, 6].
Worked example
Task: a function final returns the last element of a non-empty list:
final [x] = x
final (x:xs) = final xs
Explain how it works, and trace final [6, 1, 8].
[x]matches a list with exactly one element. That element is the last one, so it is returned. This is the base case.- Otherwise, the last element of the list is the last element of its tail, xs.
Trace: final [6,1,8] leads to final [1,8], which leads to final [8]. That matches [x], so the result is 8.
Exam tip: remember that the head is an element and the tail is a list. For recursive list functions, always write the base case (empty list) first. When prepending, put the item at the front, and when appending, at the end.
Check yourself
What are the head and tail of [8, 2, 6]?
The head is 8 (an element), and the tail is [2, 6] (a list).
What is the tail of a list with just one element?
The empty list, [].
What is the difference between prepending and appending?
Prepending adds an item to the front of the list. Appending adds an item to the end.
Define the length of a list recursively
The length of the empty list is 0. The length of any other list is 1 plus the length of its tail.
What does 9 : [1, 2] give?
The list [9, 1, 2].