Revision Notes
AQA A-Level Computer Science (7517): Lists in Functional Programming – Revision Notes
Condensed AQA 7517 section 4.12.3 recall: head and tail, the empty list, the seven list operations and recursive definitions of length, append, sum and reverse.
- Subject
- Computer Science
- Level
- A LEVELS
- Topic
- Fundamentals of functional programming
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to AQA A Level Computer Science (7517), 2015-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): AQA A Level Computer Science.
Syllabus points this page covers
7517
- 4.12.3 Lists in functional programming
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (7517).
Condensed recall for 4.12.3 Lists in functional programming, AQA A-Level Computer Science (7517): a list as a head and a tail, the empty list, and the operations head, tail, test for empty, length, construct empty, prepend and append. Traced recursive definitions are in the lists in functional programming study guide; questions are in the lists in functional programming practice questions.
Head and tail
[4, 3, 5] = 4 : [3, 5]
- Head = 4, the first element (an item).
- Tail =
[3, 5], the rest (a list). - Fully:
4 : (3 : (5 : [])). - Empty list =
[]: no head and no tail. [5]has head 5 and tail[].
The seven operations on xs = [4, 3, 5]
| Operation | Haskell-style | Python | Result |
|---|---|---|---|
| Head | head xs |
xs[0] |
4 |
| Tail | tail xs |
xs[1:] |
[3, 5] |
| Test for empty | null xs |
len(xs) == 0 |
False |
| Length | length xs |
len(xs) |
3 |
| Construct empty list | [] |
[] |
[] |
| Prepend 9 | 9 : xs |
[9] + xs |
[9, 4, 3, 5] |
| Append 9 | xs ++ [9] |
xs + [9] |
[4, 3, 5, 9] |
head []andtail []are errors: test for empty first.- Prepend:
item : list. Append:list ++ [item](two lists). - Every operation returns a new list; the original is unchanged.
Nested applications
| Expression | Value |
|---|---|
head (tail xs) |
3 |
tail (tail xs) |
[5] |
tail (tail (tail xs)) |
[] |
length (7 : xs) |
4 |
head (xs ++ [1]) |
4 |
Recursion on lists
Two cases: base case ([]) and recursive case (x : xs, using the tail).
length' [] = 0
length' (x:xs) = 1 + length' xs
append [] ys = ys
append (x:xs) ys = x : append xs ys
sumList [] = 0
sumList (x:xs) = x + sumList xs
member _ [] = False
member y (x:xs) = (y == x) || member y xs
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
Traces
length' [4, 3, 5]= 1 + (1 + (1 + 0)) = 3.append [1, 2] [3]= 1 : (2 : [3]) =[1, 2, 3].sumList [3, 1, 4]= 3 + (1 + (4 + 0)) = 8.reverse' [1, 2, 3]= (([] ++ [3]) ++ [2]) ++ [1] =[3, 2, 1].
Prepend versus append
| Prepend | Append | |
|---|---|---|
| Steps | One | One per element of the first list |
| Shape | x : xs |
xs ++ [x] |
| Use | Preferred for building lists | Costs more on long lists |
With higher-order functions
For xs = [4, 3, 5]: map (\x -> x * 2) xs gives [8, 6, 10]; filter (\x -> x > 3) xs gives [4, 5]; foldl (+) 0 xs gives 12.
Question styles to expect
- Describe an operation: say what it takes, what it returns, and whether the original list changes (it does not).
- Apply operations: evaluate nested expressions from the inside out, writing each intermediate list.
- Write a function: give the base case for
[]first, then the recursive case that uses the head and a call on the tail. - Trace a recursive call: expand one substitution per line until the base case, then evaluate back up.
- Compare prepend and append: prepending is one step; appending must rebuild the first list.
- Explain a result: for example why
tail [5]is[]and not 5, or whyhead []is an error.
Worked recall: trace of a mistake
A student writes total (x:xs) = x + total (x:xs). The recursive call is on the whole list, not the tail, so the list never gets shorter and the recursion never reaches the base case. The fix is total (x:xs) = x + total xs.
Key terms
| Term | Definition |
|---|---|
| Head | The first element of a list |
| Tail | The list of all elements after the head |
| Empty list | A list with no elements, written [] |
| Prepend | Add an item to the front of a list |
| Append | Add an item to the end of a list |
| Concatenate | Join two lists |
| Recursion | A function defined in terms of itself |
| Base case | The case that ends the recursion |
| Immutable | Cannot be changed after creation |
Exam traps
- The tail is a list, not an item.
- Test for empty before taking a head or tail.
- Prepend is
item : list; append joins two lists with++. - A recursive definition needs a base case.
- Recurse on the tail, not the whole list.
- Nested lists count as one element each.
[[1, 2], [3]]has length 2. - Operations return new lists.
Self-test
- What is the head of
[7, 2, 9]? - What is the tail of
[7, 2, 9]? - What is the tail of
[5]? - How is the empty list written?
- What does
null []return? - Prepend 1 to
[2, 3]. - Append 1 to
[2, 3]. - What is the length of
[[1, 2], [3]]? - What is wrong with
head []? - What is the base case of
length'? - Why is prepending cheaper than appending?
- Evaluate
head (tail [8, 6, 4]).
Answers
-
[2, 9].[].[].- True.
[1, 2, 3].[2, 3, 1].-
- The empty list has no head, so it is an error.
length' [] = 0.- Prepending is one step; appending must rebuild the whole first list.
-
More resources on this topic
Related resources
-
Study guides
AQA A-Level Computer Science (7517): Writing Functional Programs
Deeper guide to AQA 7517 section 4.12.2: constructing simple functional programs and using the higher-order functions map, filter and reduce or fold, with checked examples.
Computer Science · AQA · A Level
Read guide -
Practice questions
AQA A-Level Computer Science (7517): Writing Functional Programs – Practice Questions
Original AQA 7517 section 4.12.2 questions with answers: evaluating and writing map, filter and fold expressions and building simple functional programs.
Computer Science · AQA · A Level
Try questions -
Revision notes
AQA A-Level Computer Science (7517): Writing Functional Programs – Revision Notes
Condensed AQA 7517 section 4.12.2 recall: higher-order functions, map, filter, reduce and fold, lambda expressions and pipelines, with a self-test.
Computer Science · AQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.