Functional Programming End of Unit Test
Functional Programming End of Unit Test
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:
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.
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:
(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.
fv temps
fw temps
fz temps
Page 2 of 4
(4)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(1)
fz d = fu (fy (d))
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(1)
(Total 8 marks)
Q4.
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
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