Fundamentals of functional programming Flashcards
The sqrt function determines the square root of any positive integer, e.g. sqrt(25) = 5 or -5.
The dbl function calculates the double of any number that can be expressed as an integer or decimal fraction, e.g. dbl(3.2) = 6.4
What is the domain of the function sqrt?
N/natural numbers/positive integers
The sqrt function determines the square root of any positive integer, e.g. sqrt(25) = 5 or -5.
The dbl function calculates the double of any number that can be expressed as an integer or decimal fraction, e.g. dbl(3.2) = 6.4
What is the co-domain of the function sqrt?
R/real numbers
The sqrt function determines the square root of any positive integer, e.g. sqrt(25) = 5 or -5.
The dbl function calculates the double of any number that can be expressed as an integer or decimal fraction, e.g. dbl(3.2) = 6.4
What is the domain of the function dbl?
Q/rational numbers
The sqrt function determines the square root of any positive integer, e.g. sqrt(25) = 5 or -5.
The dbl function calculates the double of any number that can be expressed as an integer or decimal fraction, e.g. dbl(3.2) = 6.4
What is the co-domain of the function dbl?
Q/rational numbers
The sqrt function determines the square root of any positive integer, e.g. sqrt(25) = 5 or -5.
The dbl function calculates the double of any number that can be expressed as an integer or decimal fraction, e.g. dbl(3.2) = 6.4
State the result of evaluation sqrt o dbl(18)
6
NB: dbl 18 = 36, sqrt 36 = 6
The sqrt function determines the square root of any positive integer, e.g. sqrt(25) = 5 or -5.
The dbl function calculates the double of any number that can be expressed as an integer or decimal fraction, e.g. dbl(3.2) = 6.4
State the result of evaluation dbl o sqrt(18)
6
NB: sqrt 9 = 3, dbl 3 = 6
The times function takes an argument of two integers and returns their product e.g. times(2,6) = 12
State the domain and co-domain of the function times
domain: Z/integers
co-domain: Z/integers
The times function takes an argument of two integers and returns their product e.g. times(2,6) = 12
The partial application of times can be expresses in the form:
times: integer → (i) → (ii)
(Note that (i) and (ii) refer to missing elements.)
State what should be written in place of label (i) and (ii)
(i) integer
(ii) integer
The times function takes an argument of two integers and returns their product e.g. times(2,6) = 12
The partial application of times can be expresses in the form:
times: integer → (i) → (ii)
(Note that (i) and (ii) refer to missing elements.)
Explain the purpose of partial application of a function, giving an example that uses the times function. (3 marks)
Allows the calling/application of a multi-parameter function (1) with fewer than the required number of parameters/only one argument. (1)
Avoids the need for an anonymous function. (1)
Example: f = times (3), therefore f (7) = 21 (1)
Max. 2 marks for an explanation, 1 mark for any suitable example
(Allow parameter/argument in each case.)
The following functions have been definted:
add: real x real → real
add(x, y) = x + y
sq: integer x integer → integer
sq(x) = x * x
A new function, sub, will take two integers as an argument and return the result of subtracting the second number from the first number.
State the definition of the function sub. (3 marks)
sub: integer x integer → integer
sub (x, y) = x - y
Correct structure for function application scheme (1)
Correct data types for function application scheme (1)
Correct description of how to apply the function (1)
(Allow sub: integer à integer à integer)
The following functions have been definted:
add: real x real → real
add(x, y) = x + y
sq: integer x integer → integer
sq(x) = x * x
A new function, sub, will take two integers as an argument and return the result of subtracting the second number from the first number.
State the co-domain of the function sub.
Z/integers
The following functions have been defined:
add: real x real → real
add(x, y) = x + y
sq: integer x integer → integer
sq(x) = x * x
A new function, sub, will take two integers as an argument and return the result of subtracting the secaond number from the first number.
State the result of evaluating add (7,(sq 3)).
Show your working.
32 = 9
7 + 9 = 16
The following functions have been defined:
add: real x real → real
add(x, y) = x + y
sq: integer x integer → integer
sq(x) = x * x
A new function, sub, will take two integers as an argument and return the result of subtracting the secaond number from the first number.
State the result of evaluating sub (12,sq (4)). (2 marks)
Show your working.
42 = 16
12 – 16 = –4
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
head(a)
1
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
tail(a)
[4,9,16,25]
(Allow the answer even if not explicitly a list.)
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
head(tail(tail(a)))
tail num = [4,9,16,25]
tail (tail num) = [9,16,25]
head (tail (tail num)) = 9
(1) Mark for correct answer only, working not needed
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
0:a
[0,1,4,9,16,25]
(Allow the answer even if not explicitly in a list.)
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
a ++ [36]
[1,4,9,16,25,36]
(Allow the answer even if not explicitly in a list.)
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
map sqrt a
[1,2,3,4,5]
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
map dbl a
[2,8,18,32,50]
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
filter >10 a
[16,25]
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
filter even a
[4,16]
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
fold + 0 a
55
Given
b = [2,4,6]
State the result of evaluating the following:
fold * 0 b
0
Given
b = [2,4,6]
State the result of evaluating the following:
fold * 1 b
48
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
map dbl (filter (<10) a)
Apply filter: [1,4,9] (1)
Applying map dbl: [2,8,18] (1)
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
fold (+) 10 (filter (>10) a)
Applying filter: [16,25] (1)
Applying fold: 51 (1)
Given
a = [1,4,9,16,25]
State the result of evaluating the following:
fold (+) 0 (sq (filter (<10) a))
Applying filter: [1,4,9] (1)
Applying sq: [1,16,81] (1)
Applying fold: 98 (1)
the functions map, filter and fold are all high-order functions. Explain what is meant by a high-order function. (2 marks)
.A function that can take a function as an argument (1)
A function that can return a function (1)
State the base case for the map function (1 mark)
Where the list is empty (1)
Explain the purpose of the fold function (2 marks)
To reduce the list to a single value (1)
By repeatedly applying a (combining) function (1)
Given b = [2,4,6]
Explain how recursion is used to process the list b when evaluation the expression fold (+) 0 b (4 marks)
fold (+) 0 [2,4,6]
split into head/tail:
2 + fold (+) 0 [4,6]
Repeat
2 + 4 + fold (+) 0 [6]
Repeat
2 + 4 + 6 + fold (+) 0 []
Base case reached:
2 + 4 + 6 + 0
Result is 12
What is an alternative name for fold?
reduce
What is reduce also known as?
fold
Given
a = [2,4,6,8,10]
State the result of evaluating the following:
foldl (-) 100 a
(foldl means fold left)
(((((100-2)-4)-6)-8)-10)
70
Given
a = [2,4,6,8,10]
State the result of evaluating the following:
foldr (-) 100 a
(foldr means fold right)
(2-(4-(6-(8-(10-100)))))
-94
State the co-domain of the function sq
N/natural numbers
Given
a = [10,15,20]
State the result of evaluating the following:
head a
10
Given
b = [1,4,9]
State the result of evaluating the following:
tail b
[4,9]
Given
b = [1,4,9]
State the result of evaluating the following:
b ++ c
[1,4,9,2,3,4]
Given
a = [10,15,20]
b = [1,4,9]
State the result of evaluating the following:
head a : tail b
[10,4,9]
State the result of evaluating the following:
dbl o sq 3
18
Given
a = [10,15,20]
State the result of evaluating the following:
filter (even) a
[10,20]
Given
b = [1,4,9]
State the result of evaluating the following:
map dbl b
[2,8,18]
Given
c = [2,3,4]
State the result of evaluating the following:
fold (*) 1 c
24
Given
b = [1,4,9]
State the result of evaluating the following:
fold (*) 2 b
72
Given
a = [10,15,20]
b = [1,4,9]
c = [2,3,4]
map f [] = []
map f(x:xs) = f(x) : map (f xs)
dbl: real → real
dbl x = 2 * x
Write an expression that would result in the list
[20,30,40]
map dbl a
Given
a = [10,15,20]
b = [1,4,9]
c = [2,3,4]
Describe the recusrive steps involved in evaluating the expression
filter (<5) b
filter (<5) [1,4,9]
Apply head/tail recursively:
filter (<5) 1:(4:(9:[]))
Apply filter:
1:4:[]
Recombine into list:
[1,4]
4 marks in total based on any 4 from:
- Correct identification of list to use
- Use of head/tail
- Use of recursion
- Identification of base case/empty list
- Recombincation into list
In a functional programming language, four functions named fw, fx, fy and fz and a list named sales are defined
fw [a,b] = a * b
fx c = map fw c
fy d = fold (+) 0 d
fz e = fy (fx e)
sales = [[10,2], [2,25], [4,8]]
The sales list represents all of the sales made in a shop in 1 day. It is composed of sublists.
The values in each sublist indicate the price of a product and the quantity of the product that was sold. For example, [10,2] indicates that 10 units of a product priced at £2 were sold
How many of the four functions (fw,fx,fy,fz) use a higher-order function?
2
(map and fold)
In a functional programming language, four functions named fw, fx, fy and fz and a list named sales are defined
fw [a,b] = a * b
fx c = map fw c
fy d = fold (+) 0 d
fz e = fy (fx e)
sales = [[10,2], [2,25], [4,8]]
The sales list represents all of the sales made in a shop in 1 day. It is composed of sublists.
The values in each sublist indicate the price of a product and the quantity of the product that was sold. For example, [10,2] indicates that 10 units of a product priced at £2 were sold
Calculate the results of making the function call fw [4,3]
12
In a functional programming language, four functions named fw, fx, fy and fz and a list named sales are defined
fw [a,b] = a * b
fx c = map fw c
fy d = fold (+) 0 d
fz e = fy (fx e)
sales = [[10,2], [2,25], [4,8]]
The sales list represents all of the sales made in a shop in 1 day. It is composed of sublists.
The values in each sublist indicate the price of a product and the quantity of the product that was sold. For example, [10,2] indicates that 10 units of a product priced at £2 were sold
Calculate the results of making the function call fx sales
[20, 50, 32]
In a functional programming language, four functions named fw, fx, fy and fz and a list named sales are defined
fw [a,b] = a * b
fx c = map fw c
fy d = fold (+) 0 d
fz e = fy (fx e)
sales = [[10,2], [2,25], [4,8]]
The sales list represents all of the sales made in a shop in 1 day. It is composed of sublists.
The values in each sublist indicate the price of a product and the quantity of the product that was sold. For example, [10,2] indicates that 10 units of a product priced at £2 were sold
Calculate the results of making the function call fz sales
102
In a functional programming language, four functions named fw, fx, fy and fz and a list named sales are defined
fw [a,b] = a * b
fx c = map fw c
fy d = fold (+) 0 d
fz e = fy (fx e)
sales = [[10,2], [2,25], [4,8]]
The sales list represents all of the sales made in a shop in 1 day. It is composed of sublists.
The values in each sublist indicate the price of a product and the quantity of the product that was sold. For example, [10,2] indicates that 10 units of a product priced at £2 were sold
In the context of the shop, explain what the result of the function call fz sales
represents
Total/one day’s sales value/income/revenue (for all products);
NE. sales, total sales