Fundamentals of functional programming Flashcards

1
Q

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?

A

N/natural numbers/positive integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

R/real numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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?

A

Q/rational numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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?

A

Q/rational numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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)

A

6

NB: dbl 18 = 36, sqrt 36 = 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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)

A

6

NB: sqrt 9 = 3, dbl 3 = 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

domain: Z/integers

co-domain: Z/integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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)

A

(i) integer
(ii) integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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)

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

Z/integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

32 = 9

7 + 9 = 16

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

42 = 16

12 – 16 = –4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

head(a)

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

tail(a)

A

[4,9,16,25]

(Allow the answer even if not explicitly a list.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

head(tail(tail(a)))

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

0:a

A

[0,1,4,9,16,25]

(Allow the answer even if not explicitly in a list.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

a ++ [36]

A

[1,4,9,16,25,36]

(Allow the answer even if not explicitly in a list.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

map sqrt a

A

[1,2,3,4,5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

map dbl a

A

[2,8,18,32,50]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

filter >10 a

A

[16,25]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

filter even a

A

[4,16]

23
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

fold + 0 a

A

55

24
Q

Given

b = [2,4,6]

State the result of evaluating the following:

fold * 0 b

A

0

25
Q

Given

b = [2,4,6]

State the result of evaluating the following:

fold * 1 b

A

48

26
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

map dbl (filter (<10) a)

A

Apply filter: [1,4,9] (1)

Applying map dbl: [2,8,18] (1)

27
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

fold (+) 10 (filter (>10) a)

A

Applying filter: [16,25] (1)

Applying fold: 51 (1)

28
Q

Given

a = [1,4,9,16,25]

State the result of evaluating the following:

fold (+) 0 (sq (filter (<10) a))

A

Applying filter: [1,4,9] (1)

Applying sq: [1,16,81] (1)

Applying fold: 98 (1)

29
Q

the functions map, filter and fold are all high-order functions. Explain what is meant by a high-order function. (2 marks)

A

.A function that can take a function as an argument (1)

A function that can return a function (1)

30
Q

State the base case for the map function (1 mark)

A

Where the list is empty (1)

31
Q

Explain the purpose of the fold function (2 marks)

A

To reduce the list to a single value (1)

By repeatedly applying a (combining) function (1)

32
Q

Given b = [2,4,6]

Explain how recursion is used to process the list b when evaluation the expression fold (+) 0 b (4 marks)

A

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

33
Q

What is an alternative name for fold?

A

reduce

34
Q

What is reduce also known as?

A

fold

35
Q

Given

a = [2,4,6,8,10]

State the result of evaluating the following:

foldl (-) 100 a

A

(foldl means fold left)

(((((100-2)-4)-6)-8)-10)

70

36
Q

Given

a = [2,4,6,8,10]

State the result of evaluating the following:

foldr (-) 100 a

A

(foldr means fold right)

(2-(4-(6-(8-(10-100)))))

-94

37
Q

State the co-domain of the function sq

A

N/natural numbers

38
Q

Given

a = [10,15,20]

State the result of evaluating the following:

head a

A

10

39
Q

Given

b = [1,4,9]

State the result of evaluating the following:

tail b

A

[4,9]

40
Q

Given

b = [1,4,9]

State the result of evaluating the following:

b ++ c

A

[1,4,9,2,3,4]

41
Q

Given

a = [10,15,20]

b = [1,4,9]

State the result of evaluating the following:

head a : tail b

A

[10,4,9]

42
Q

State the result of evaluating the following:

dbl o sq 3

A

18

43
Q

Given

a = [10,15,20]

State the result of evaluating the following:

filter (even) a

A

[10,20]

44
Q

Given

b = [1,4,9]

State the result of evaluating the following:

map dbl b

A

[2,8,18]

45
Q

Given

c = [2,3,4]

State the result of evaluating the following:

fold (*) 1 c

A

24

46
Q

Given

b = [1,4,9]

State the result of evaluating the following:

fold (*) 2 b

A

72

47
Q

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]

A

map dbl a

48
Q

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

A

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:

  1. Correct identification of list to use
  2. Use of head/tail
  3. Use of recursion
  4. Identification of base case/empty list
  5. Recombincation into list
49
Q

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?

A

2

(map and fold)

50
Q

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]

A

12

51
Q

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

A

[20, 50, 32]

52
Q

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

A

102

53
Q

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

A

Total/one day’s sales value/income/revenue (for all products);
NE. sales, total sales