Q1.
The list towers is defined as:
towers = ["Blackpool", "Paris", "New Brighton", "Toronto"]
What are the head and tail of this list?
Head ________________________________________________________________
Tail _________________________________________________________________
(Total 1 mark)
Q2.
The code below is written in a functional programming language.
total [] = 0
total (x:xs) = x + total (xs)
The following notes are provided to help you understand the syntax of the code above:
• [] is the empty list
• (x:xs) as the argument to a function splits a list into two parts, the head x and tail
xs.
Describe how the total function works to add up all of the numbers in a list.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 3 marks)
Q3.
Page 1 of 4
In a functional programming language, six functions named fu, fv, fw, fx, fy and fz and
a list of temperatures in Fahrenheit named temps are defined as shown in the code below.
temps = [50, 68, 95, 86]
fu a = (a − 32) * 5 / 9
fv b = map fu b
fw [] = 0
fw (x:xs) = 1 + fw (xs)
fx [] = 0
fx (x:xs) = x + fx (xs)
fy c = fx (c) / fw (c)
fz d = fy (fv (d))
A temperature can be converted from degrees Fahrenheit to degrees centigrade using the
following method:
centigrade = (Fahrenheit – 32) ×
For example, 59 degrees Fahrenheit is equivalent to 15 degrees centigrade.
In the functions fw and fx:
• [] is the empty list
• (x:xs) lets the function definition refer to the head of the list as x and the tail as xs.
(a) Shade one lozenge to indicate which of the listed functions from the code above
includes a higher-order function in its definition.
fu fv fx fy
(1)
(b) Shade two lozenges to indicate which of the listed functions from the code above
use recursion in their definitions.
fu fv fw fx
(1)
(c) Calculate the results of making the function calls listed in the table below, using the
functions and list in the code above as appropriate.
Function call Result
fu 50
fv temps
fw temps
fz temps
Page 2 of 4
(4)
(d) Explain the purpose of the function fz.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(1)
(e) It is proposed that the definition of the function fz is changed to:
fz d = fu (fy (d))
Explain why this new definition of fz could be considered to be an improvement
over the definition of fz in the code above.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(1)
(Total 8 marks)
Q4.
In a functional programming language, a recursively defined function named map and a
function named double are defined as follows:
map f [] = []
map f (x:xs) = f x : map f xs
double x = 2 * x
The function x has two parameters, a function f, and a list that is either empty (indicated
as []), or non-empty, in which case it is expressed as (x:xs) in which x is the head and
xs is the tail, which is itself a list.
(a) In Table 1, write the value(s) that are the head and tail of the list
[ 1, 2, 3, 4 ].
Table 1
Head
Tail
(b) The result of making the function call double 3 is 6.
(1)
Calculate the result of making the function call listed in Table 2.
Table 2
Page 3 of 4
Function Call Result
map double [ 1, 2, 3, 4
]
(1)
(c) Explain how you arrived at your answer to part (b) and the recursive steps that you
followed.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
(Total 5 marks)
Page 4 of 4