|
|
Some simple and general higher-order functions | It is time to look at some examples of higher-order functions.
We start with a number of simple ones. |
| The function flip changes the order of it's parameters. The function takes a function of two parameters, and returns another
function of two parameters. The only difference between the input and output function of flip is the ordering of their parameters. |
For easy comparison we show the original version below the alternative formulation.
The two different syntaxes are discussed in an earlier lecture, cf. the cross references. |
| The function negate negates a predicate. Thus, negate takes a predicate function (boolean function) as parameter and
returns the negated predicate.
The resulting negated predicate returns true whenever the input predicate returns false, and vise versa. |
| The function compose composes two functions which both are assumed to take a single argument. The resulting function composed of f and g returns f(g(x)), or in Lisp (f (g x)), given the input x .
The compose function from the general LAML library accepts two or more parameters, and as such it
is more general than the compose function shown here. |
|