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

Functional programming and list processing

10 questions46 marksabout 55 min

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 Functional programming and list processing?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Core3 marks

A function passMark takes a test score (a whole number from 0 to 100) and returns True if the score is 40 or more and False otherwise. Its type is written as

passMark: integer → boolean

(a) State the domain and the co-domain of passMark. [2]

(b) In a functional programming language, functions are first-class objects. State one thing that can be done with a first-class object such as passMark. [1]

5Core5 marks

In a functional language, a list is made up of a head and a tail. The list scores is defined as:

scores = [12, 7, 19]

The functions head, tail, null (tests whether a list is empty) and length are available. : prepends an item to a list and ++ joins two lists.

Write down the result of evaluating each expression. [5]

(i)   tail scores
(ii)  null scores
(iii) 5 : scores
(iv)  scores ++ [3]
(v)   length (tail (tail scores))
6Standard4 marks

A function that calculates the volume of a box is defined as:

volume l w h = l * w * h

Its type is volume: integer → (integer → (integer → integer)).

(a) State the type of volume 2. [1]

(b) State the type of volume 2 5. [1]

(c) Using your answer to part (b), explain why map (volume 2 5) [1, 3, 4] is a valid expression, and state its result. [2]

7Standard4 marks

Two functions are defined:

len:    string → integer    (returns the number of characters)
isEven: integer → boolean   (returns True if the integer is even)

(a) The composition isEven ∘ len is valid, but len ∘ isEven is not. Explain why. [2]

(b) State the type of isEven ∘ len. [1]

(c) State the result of applying isEven ∘ len to "TIGER". [1]

8Hard5 marks

foldl and foldr reduce a list to a single value using a function and a starting value. foldl combines from the left of the list and foldr combines from the right.

(a) Explain what is meant by a higher-order function, using foldr as an example. [1]

(b) Show how each expression is evaluated, and state its result. [2]

(i)  foldr (-) 0 [8, 5, 2]
(ii) foldl (-) 0 [8, 5, 2]

(c) The list nums = [3, 6, 7, 2, 5]. Evaluate:

foldl (+) 0 (map (^2) (filter odd nums))

showing the list produced at each stage. [2]

9Hard5 marks

A functional language has no built-in operation to add an item to the end of a list. A programmer writes a recursive function addLast so that, for example, addLast 9 [4, 1, 6] returns [4, 1, 6, 9].

(a) Write the function addLast. You may only use pattern matching on [] and (x:xs), and the prepend operator :. [4]

(b) State the result of addLast 9 []. [1]

10Stretch6 marks

A data analysis company processes large lists of readings using a functional language. A function keep is defined using foldr and a lambda (anonymous) function:

keep = foldr (\x acc -> if x > 0 then x : acc else acc) []

(a) State the result of keep [3, -1, 4, 0, -5, 2]. [1]

(b) keep behaves the same as a partial application of one of the standard higher-order functions. Write an equivalent definition of keep using that function. [1]

(c) A list of words is stored as ws = ["data", "map", "reduce", "node", "fold", "io"]. Write a single expression using map, filter and foldl that returns the total number of characters in the words that have more than 3 characters, and state its value. [3]

(d) Explain why programs written in a functional style are well suited to processing Big Data across many machines. [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