Skip to content

AQA A-Level Computer Science: Functional Programming

6 exam-style questions with full mark schemes and model answers. Write your own answer and the AI examiner marks it against the mark scheme.

Question 112 marksEvaluate

The following definitions were written for this exercise.

In a functional language, the higher-order functions map, filter and foldr have the type signatures:

map    :: (a -> b) -> [a] -> [b]
filter :: (a -> Bool) -> [a] -> [a]
foldr  :: (a -> b -> b) -> b -> [a] -> b

Throughout this question use the list:

xs = [3, 8, 11, 4, 15, 6]

(a) Give the value produced by each of the following expressions. [6 marks]

(i)   map (*2) xs
(ii)  filter (> 7) xs
(iii) foldr (+) 0 (filter (> 7) xs)

(b) A function sumEvenSquares should take a list of integers and return the sum of the squares of just the even elements. Using map, filter and foldr (and no explicit recursion or loops), write a definition for sumEvenSquares. [4 marks]

(c) Two functions are defined as f = (*3) and g = (+1). State the value of the composition (f . g) 4. [2 marks]

AI examiner · marked against the mark scheme
Question 29 marksDiscuss

A company is building a system that reads millions of raw web-server log records, then cleans, transforms and aggregates them into daily summary reports. The work is naturally a pipeline of independent stages (parse → filter out errors → extract fields → group → count), and the team wants to run the stages in parallel across many processor cores. One developer argues the pipeline should be written in a functional style; another prefers a conventional imperative / object-oriented style with mutable objects and loops.

Discuss the advantages and disadvantages of using the functional paradigm, rather than the imperative / object-oriented paradigm, to implement this data-transformation pipeline. Conclude with a justified recommendation. [9 marks]

AI examiner · marked against the mark scheme
Question 36 marksApply

The following definition was written for this exercise.

The higher-order function foldl reduces a list from the left, carrying an accumulator. The function below uses it to combine a list of single decimal digits into the whole number they represent:

digitsToInt :: [Int] -> Int
digitsToInt ds = foldl (\acc d -> acc * 10 + d) 0 ds

Apply this definition to the call digitsToInt [4, 2, 7, 9]. Show the value of the accumulator at each step of the fold, and state the final result. [6 marks]

AI examiner · marked against the mark scheme
Question 45 marksExplain

In a functional programming language, functions are described as first-class objects, and the language supports higher-order functions.

Explain what each of these two terms means. Support your answer with a short code example for each. [5 marks]

AI examiner · marked against the mark scheme
Question 54 marksDetermine

The following definitions were written for this exercise.

Two functions are defined as:

inc = (+1)     -- adds 1
dbl = (*2)     -- multiplies by 2

The composition operator . builds a new function, where (fg)(x)=f(g(x))(f \circ g)(x) = f(g(x))(fg)(x)=f(g(x)) - that is, g is applied first.

Determine the value of each of the following, showing the intermediate result, and use them to explain why function composition is not commutative. [4 marks]

(i)  (dbl . inc) 5
(ii) (inc . dbl) 5
AI examiner · marked against the mark scheme
Question 63 marksDefine

A central idea in the functional paradigm is the pure function.

Define what is meant by a pure function, and give one example of a function that is pure and one that is not. [3 marks]

AI examiner · marked against the mark scheme