Define a function in functional programming.
A rule that for each element is set A, assigns a output chosen from set B, but not necessarily using every member of B.
Define a function in functional programming with domains and sets.
A mapping of a domain to a co-domain, where both a subsets of an object of some data type.
State the types in function f: A → B, and describe B.
A is the argument typeB is the result typef is the function type (the operation performed on domain A)B does not need to contain all members of its co-domain.
Explain why in functional programming a function can be argument and the result of a functional call.
In functional programming languages, functions are first class objects.
State the requirements for a first class object.
- Appear in expressions.
- Be assigned to a variable.
- Be assigned as arguments.
- Be returned in function calls.
Define function application, giving an example.
The process of giving inputs to a particular function. Where a set of two numbers is the single argument taken and all functions only take a single argument.
- e.g. add(3,4)
Define partial function application, giving an example.
Fixing one argument, to create a more restricted, specialised function, based on the fact that a function can only take one input.
- Add3Numbers x y z = x + y + z
- Add3To2Numbers = Add3Numbers 3
- Add3To2Numbers 4 1 >>> 8
Define composition of functions.
Combining two functions to create a new function, so that each function may be called separately and in conjunction.
Explain the restriction for combining functions
A domain of one function must be same as the co-domain of the other function.
- f ::: real → real → real
- g ::: real → real
e.g. where italics is the domain.
f o g is not possible, because the domain of f is a pair of reals, while the co-domain of g is a single real.
g o f is possible, because the domain of g is a single real, and the co-domain of f is a single real
If f(x) = 3x + 4, and g(x) = x*2, what is the result of g o f (2) and f o g (2)
- g o f (2)
- 3(2) + 4 = 10
- 10 * 2 = 20
- f o g (2)
- (2)*2 = 4
- 3(4) + 4 = 16
Answer: g o f = 20
Answer f o g = 16