]>
An anonymous function is a function that is function:anonymous defined anonymous function by giving a list of parameters, the ``maps-to'' compound +-> @+-> symbol ``+->'' (from the mathematical symbol ), and by an expression involving the parameters, the evaluation of which determines the return value of the function.
( , , ..., ) +-> expression
You can apply an anonymous function in several ways.
Anonymous functions are particularly useful for defining functions ``on the fly.'' That is, they are handy for simple functions that are used only in one place. In the following examples, we show how to write some simple anonymous functions.
This is a simple absolute value function.
This function returns true if the absolute value of the first argument is greater than the absolute value of the second, false otherwise.
We use the above function to ``sort'' a list of integers.
This function returns if is even, otherwise.
We create a four-by-four matrix containing or depending on whether the row plus the column index is even or not.
This function returns true if a polynomial in has multiple roots, false otherwise. It is defined and applied in the same expression.
This and the next expression are equivalent.
The one you use is a matter of taste.
If you declare any of the arguments you must declare all of them. Thus,
is not legal.
This is an example of a fully declared anonymous function. function:declaring function:anonymous:declaring The output shown just indicates that the object you created is a particular kind of map, that is, function.
Axiom allows you to declare the arguments and not declare the return type.
The return type is computed from the types of the arguments and the body of the function. You cannot declare the return type if you do not declare the arguments. Therefore,
is not legal. This and the next expression are equivalent.
The one you use is a matter of taste.
When should you declare an anonymous function?
This is an example of a named function for integers that returns a function.
We define g to be a function that adds to its argument.
Try it out.
function:anonymous:restrictions An anonymous function cannot be recursive: since it does not have a name, you cannot even call it within itself! If you place an anonymous function inside a named function, the anonymous function must be declared.