Question 12
AQA Computer Science June 2022 Paper 2 (7517/2)A level8 marks
On the specification
- 12.1
- 4.12 Fundamentals of functional programming
- 12.2
- 4.1 Fundamentals of programming4.12 Fundamentals of functional programming
- 12.3
- 4.12 Fundamentals of functional programming
- 12.4
- 4.12 Fundamentals of functional programming
- 12.5
- 4.12 Fundamentals of functional programming4.3 Fundamentals of algorithms
Question text
12 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 Figure 8. Figure 8 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: 5 centigrade = (Fahrenheit – 32) × 9 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. 12.1 Shade one lozenge to indicate which of the listed functions from Figure 8 includes a higher-order function in its definition. [1 mark] fu fv fx fy 12.2 Shade two lozenges to indicate which of the listed functions from Figure 8 use recursion in their definitions. [1 mark] fu fv fw fx 12.3 Calculate the results of making the function calls listed in Table 2, using the functions and list in Figure 8 as appropriate. [4 marks] Table 2 Function call Result fu 50 fv temps fw temps fz temps 12.4 Explain the purpose of the function fz. [1 mark] 12.5 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 Figure 8. [1 mark]