Question bank › A-level Computer Science › Fundamentals of functional programming

Functional programming and list processing

Answer on paper first, then reveal the mark scheme one mark at a time.

1Standard4 marks

In the functional programming language shown, map applies a function to every list item, filter keeps items for which a function returns true, and foldl combines a list from the left using a function and a starting value.

Evaluate each expression. [4]

(i)   map (+3) [1, 4, 2]
(ii)  filter (>2) [1, 4, 2, 5]
(iii) foldl (+) 0 [1, 2, 3, 4]
(iv)  foldl (-) 0 [10, 4, 3]
2Hard5 marks

Two functions are defined:

add x y = x + y
inc = add 1
double x = x * 2

(a) Explain what is meant by partial application, using inc as your example. [2]

(b) Evaluate (double . inc) 4, where . is function composition. [2]

(c) Evaluate (inc . double) 4. [1]

3Hard5 marks

(a) Write a recursive function countEven that returns the number of even integers in a list. For example countEven [3, 8, 5, 2, 6] returns 3. You may use the function even, head, tail or pattern matching (x:xs). [4]

(b) State the value of head [3, 5, 8] and of tail [3, 5, 8]. [1]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session