Python · A Level 2
Functional Programming
Name
Class
Date
Mark / 27
Section A
Circle the letter of the correct answer. One mark each.
- 1What does it mean for a function to be a first-class object?1
- AIt can be stored in a variable, passed to a function and returned from a function
- BIt is the first function that the program calls when it starts to run
- CIt is a function that can only be called once and is then deleted
- DIt is a function that takes no parameters and returns no value
- 2What does
map(f, items)return?1- AThe first item with f applied
- BA single value combining every item
- COnly the items for which f is true
- DThe results of applying f to every item
- 3What does
filter(f, items)return?1- AA single combined value
- BThe items for which f returns False
- CThe items for which f returns True
- DThe results of applying f to every item
- 4What does
reduce(lambda a, b: a + b, [1, 2, 3, 4])return?1- A10
- B24
- C[3, 6, 10]
- D[1, 2, 3, 4]
- 5Which describes a pure function?1
- AIt never takes any arguments
- BIt gives the same output for the same input and has no side effects
- CIt changes a global variable only once
- DIt uses only local variables but may print to the screen
- 6Which of these is a side effect?1
- ACreating a local variable inside the body of the function
- BBuilding and returning a brand new list from the arguments
- CChanging the contents of a list that was passed in as an argument
- DCalling a different function that is itself pure
- 7Why is immutable data useful in functional programming?1
- AIt always uses less memory than mutable data
- BIt allows a function to have side effects
- CIt makes recursion unnecessary
- DA value cannot be changed unexpectedly by another part of the program
- 8What are
head([4, 5, 6])andtail([4, 5, 6])?1- A6 and [4, 5]
- B4 and [5, 6]
- C4 and 6
- D[4] and [5, 6]
- 9What is partial application?1
- ASupplying some of a function's arguments to get a new function that takes the rest
- BCalling a function with no arguments so that it uses its default values
- CApplying a function to the first half of a list and ignoring the rest
- DRunning only the first part of a function and then stopping early
- 10What is composition of two functions?1
- AWriting one inside the other so neither can be called
- BApplying both to the same input and adding the results
- CDefining both in the same file
- DCombining them so that the output of one is the input of the other
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1StateState what is meant by a first-class object.2
- 2DescribeDescribe what the higher-order function filter does.3
- 3ShowThe program shown is run. Show the result of each stage of the pipeline and state the value that is output.4Python
from functools import reduce nums = [3, 8, 1, 6] result = reduce(lambda a, b: a + b, map(lambda n: n * 2, filter(lambda n: n > 2, nums))) print(result) - 4ExplainExplain why a pure function is easier to test than a function that has side effects.4
- 5WriteWrite a Python function called make_multiplier that takes a number n and returns a new function. The new function takes a value x and returns x multiplied by n. Show how it could be used to create a function that triples a number.4
Mark scheme
Section A
Section B
1 State what is meant by a first-class object. 2
One mark for each of the following, to a maximum of 2.
- A value that can be assigned to a variable
- It can be passed as an argument to, or returned as the result of, a function
Model answer. A first-class object is a value that can be treated like any other data. It can be assigned to a variable or stored in a data structure, passed as an argument to a function and returned as the result of a function. In functional programming, functions are first-class objects.
2 Describe what the higher-order function filter does. 3
One mark for each of the following, to a maximum of 3.
- It takes a function and a list as arguments
- The function is applied to each item and returns true or false
- It returns a new list containing only the items for which the function returned true
Model answer. Filter takes a function that returns true or false, and a list. It applies the function to each item in the list, and returns a new list that contains only those items for which the function returned true. The original list is not changed.
3 The program shown is run. Show the result of each stage of the pipeline and state the value that is output. 4
One mark for each of the following, to a maximum of 4.
- filter keeps 3, 8 and 6
- map doubles them to 6, 16 and 12
- reduce adds them together
- The output is 34
Model answer. filter keeps the numbers greater than 2, which gives 3, 8 and 6. map doubles each of them, giving 6, 16 and 12. reduce then adds the values together, 6 + 16 = 22 and 22 + 12 = 34, so the value output is 34.
4 Explain why a pure function is easier to test than a function that has side effects. 4
One mark for each of the following, to a maximum of 4.
- A pure function always returns the same output for the same input
- So a test only needs to supply the arguments and check the returned value
- A function with side effects may change or depend on something outside it, such as a global variable or file
- So the test would have to set up and check that outside state as well, and results may differ between runs
Model answer. A pure function always returns the same output for the same arguments and does nothing else, so a test only has to supply the arguments and compare the returned value with what is expected. A function with side effects may depend on or change something outside itself, such as a global variable or a file, so a test would also have to set up that state beforehand and check it afterwards, and the same call might give different results on different runs.
5 Write a Python function called make_multiplier that takes a number n and returns a new function. The new function takes a value x and returns x multiplied by n. Show how it could be used to create a function that triples a number. 4
One mark for each of the following, to a maximum of 4.
- Defines make_multiplier with parameter n
- Defines an inner function, or a lambda, that takes x
- The inner function returns x * n, using n from the enclosing function
- The outer function returns the inner function, and triple = make_multiplier(3) is used
Model answer.
def make_multiplier(n):
def multiply(x):
return x * n
return multiply
triple = make_multiplier(3)
print(triple(7))make_multiplier returns the inner function multiply, which still has access to n after make_multiplier has finished. That is a closure. triple is a function that multiplies by 3, so triple(7) outputs 21.