Functions
The Compute Engine Standard Library includes many built-in functions such as
Add
, Sin
, Power
, etc…
The standard library can be extended with your own functions.
Anonymous Functions
A function that is not bound to an identifier is called an anonymous function.
Anonymous functions are frequently used as arguments to other functions.
In the example below, the ["Function"]
expression is an anonymous function
that is passed as an argument to the ["Sum"]
function.
The first argument of the ["Function"]
expression is the body of the function,
the remaining arguments are the name of the parameters of the function.
["Sum", ["Function", ["Multiply", "x", 2], "x"]]
To specify an anonymous function with LaTeX use the \mapsto
command:
["Function", ["Multiply", "x", 2], "x"]
["Function", ["Add", ["Multiply", "x", 2], "y"], "x", "y"]
Anonymous Parameters
The parameters of a function can also be anonymous.
In this case, the parameters are bound to the wildcards _
, _1
, _2
, etc…
in the body of the function. The wildcard _
is a shorthand for _1
, the
first parameter.
In the example below, both the function and its parameters are anonymous.
["Sum", ["Multiply", "_", 2]]
Note that as a shortcut when using anonymous parameters, the ["Function"]
expression can be omitted.
Anonymous parameters can also be used in LaTeX, but the anonymous parameters
must be wrapped with an \operatorname
command except for \_
.
["Function", ["Add", "_", "_2"]]
Evaluating an Anonymous Function
To apply a function to some arguments, use an ["Apply"]
expression.
["Apply", ["Function", ["Add", 2, "x"], "x"], 11]
// ➔ 22
["Apply", ["Add", 2, "_"], 4]
// ➔ 6
["Apply", "Power", 2, 3]
// ➔ 8
The first argument of Apply
is an anonymous function, either as an
identifier, or as a ["Function"]
expression. The rest of the arguments are the
arguments of the anonymous function.
Operating on Functions
Function
["Function", body]
["Function", body, arg-1, arg-2, …]
Create an anonymous function, also called lambda expression.
The arg-n
arguments are identifiers of the bound variables (parameters) of the
anonymous function.
All the arguments have the Hold
attribute set, so they are not evaluated when
the function is created.
The body is a MathJSON
expression that is evaluated when the function is
applied to some arguments.
To apply some arguments to a function expression, use ["Apply"]
.
["Function", ["Multiply", "x", 2], "x"]
["Function", ["Add", ["Multiply", "x", 2], "y"], "x", "y"]
Assign
["Assign", id, fn]
Assign the anonymous function fn to the identifier id.
The identifier id should either not have been declared yet, or been declared
as a function. If id is already defined in the domain of Numbers
for example, it is an
error to assign a function to it.
Assign
is not a pure function.
["Assign", "double", ["Function", ["Multiply", "x", 2], "x"]]
Apply
["Apply", function, expr-1, …expr-n]
Apply a list of arguments to a function.
The function is either an identifier of a function, or a ["Function"]
expression.
The following wildcards in body are replaced as indicated
_
or_1
: the first argument_2
: the second argument_3
: the third argument, etc…__
: the sequence of arguments, so["Length", "__"]
is the number of arguments
If body is a ["Function"]
expression, the named arguments of ["Function"]
are replaced by the wildcards.
["Apply", ["Multiply", "_", "_"], 3]
// ➔ 9
["Apply", ["Function", ["Multiply", "x", "x"], "x"], 3]
// ➔ 9
The \lhd
and \rhd
operators can be used to apply a function to a single
argument on the left or right respectively.
["Apply", "f", ["Apply", "g", "x"]]