12. Fundamentals of Functional Programming Flashcards

1
Q

What is Functional Programming?

A

Programs consist of Functions which map inputs to outputs. There is no state (Stateless, i.e. no Variables) and as a consequence Functions have no side-effects.

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

What is the Domain and Co-Domain of a Function?

A

The Domain of a Function is the Data Type(s) of the Inputs/Parameters of a Function.

The Co-Domain of a Function is the Data Type of the Output of a Function.

Functions map their Domain to their Co-Domain.

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

What is Functional Composition?

A

Functional Composition is when a Function is applied directly to the output of another Function, producing a Composite Function. For example,

double(x) = 2*x
add2(x) = x+2

double ∘ add2(x) = 2*(x+2)

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

What is a Higher Order Function? What do Map/Filter/Fold do?

A

A Higher Order Function is a Function which takes another Function as its Argument.

Map applies a Function to each element of a List and returns a List of the resulting values.

square x = x*x
map square [1, 2, 3] = [1, 4, 9]

Filter applies a Boolean Function to each element of a List and returns a List of the values for which the Function returns True.

greaterThan10 x = x > 10
filter greaterThan10 [5, 10, 15, 20] = [15, 20]

Fold takes an initial value along with a List and sequentially applies the Function to an accumulated value and the next element of the List combining the List into a single value.

multiply x, y = x*y
fold multiply 1, [1, 2, 3] = 6

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

What is Partial Function Application?

A

Partial Function Application is when a Function is applied to only a subset of its Parameters it returns a new Function which specialises the original.

add x, y = x + y
add2 = add 2
add2 10 = 12

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