12. Fundamentals of Functional Programming Flashcards
What is Functional Programming?
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.
What is the Domain and Co-Domain of a Function?
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.
What is Functional Composition?
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)
What is a Higher Order Function? What do Map/Filter/Fold do?
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
What is Partial Function Application?
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