A higher-order function basically takes two arguments : a function, that is, a functional argument, and another argument. The functional argument is applied to the second argument by the higher-order function.
The functional argument is a variable of the higher-order function.
The functional argument must be added one "free" input per supplemental arguments of the higher-order function.
We will show the application basis of a lambda function via a higher-order function to another argument.
We will use funcall as a higher-order function, the rotate function as a first argument, and a list as a second argument. This means that rotate is now a variable of funcall.
Funcall, – "fun" stands for "function" – makes function calls : it applies a function to a second argument.
Rotate returns the circular permutation of a list : it enumerates the items of a list starting from the second element.
![]() |
|
The result of the computation is the same in both cases. We will now extend the previous example to show how using a function as an actual variable.
Using lambda functions allows to choose randomly between two functions to apply to another argument. Here, we want to modify a list of values with one of two functions that would be selected randomly. in other words, we would like this function to be a random variable . We will apply either permut-random, or rotate to a list.
Permut-random makes a random permutation of the elements of a list.
Rotate enumerates the elements of a list from the second element.
Permut-random and rotate are our variables : they are set on "lambda" mode.
List makes a list with these two functions .
Nth-random picks one of the two functions in the list randomly.
Funcall is the higher-order function that applies this function as a first argument to its second argument, a list.
Making a list with two functions, and choosing one of them in the list like a "lambda item" is possible precisely because these functions are on "lambda" mode.
A lambda function can be applied to several arguments. Hence, calculations can be executed upon arguments the lambda function may not take otherwise.
A function on "lambda" mode must have as many "free" inputs as arguments returned by the higher-level function.
![]() | The * function cannot multiply lists, like om*. To multiply the elements of two lists, we will apply * with mapcar. Mapcar applies a function to the first element of each next input, then to the second argument, to the third argument, and so on, and builds a list with the result of each evaluation.
|
Note that the two inputs of om* remain free.
"Currying" is the reduction of the number of arguments of a lambda function by assigning it one default argument.
This default argument must belong to the arguments accepted by the function : the function has predefined and undefined arguments. The number of inlets of the higher-level function must take the number of free inlets into account.
Here, * has
Remember that * only accepts numbers.
| ![]() |