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]