Functions
Functions are first-class values in the Compute Engine. This means that functions can be passed as arguments to other functions, returned from functions, and assigned to variables.
The standard library can be extended with your own functions.
Term | Definition |
---|---|
Function Expression | An expression representing a function application where a function (or operator) is evaluated with arguments (the arguments are applied to the function). For example ["Add", 1, 2] applies the arguments 1 and 2 to the operator Add . |
Function Signature | A type describing the function's inputs and outputs, e.g. (real, integer) -> boolean |
Function Literal | A first-class function value, defined using a construct like ["Function", body, params...] , which may or may not capture variables from its lexical scope. |
Shorthand Function Literal | A compact way to write a function literal using placeholders (e.g. _ or _2 ) instead of explicitly listing parameters, e.g. ["Add", "_", 1] desugared to ["Function", ["Add", "_", 1], "_"] . |
Closure | A function literal that captures one or more free variables from its defining lexical scope, preserving their values at the time of definition. The capture happens by simply referencing the variables in the function body. |
Anonymous Function | A function that is not bound to a symbol, often used as an argument to other functions. |
Function Literals
A function literal is a first-class function value, defined using a
["Function"]
expression. It can be passed as an argument to other functions,
returned from a function, or assigned to a variable.
The ["Function"]
expression takes a body and a list of parameters.
["Sum", ["Function", ["Multiply", "x", 2], "x"]]
To specify a function literal with LaTeX use the \mapsto
command:
["Function", ["Multiply", "x", 2], "x"]
["Function", ["Add", ["Multiply", "x", 2], "y"], "x", "y"]
The examples in this section define functions as a simple expression, but function literals can include more complex control structures, including blocks, local variables, loops and conditionals.
For example, here's a simple "clamp" function, using a ["Block"]
expression.
["Function",
["Block",
["Assign", "x", ["Max", "x", "min"]],
["Min", "x", "max"]
],
"x", "min", "max"
]
Shorthand Function Literals
A shorthand function literal is a compact way to write a function literal without explicitly listing parameters.
A shorthand function literal can use either wildcards (_
, _2
, etc...) or
unknowns (symbols that have no value) as implicit parameters.
The shorthand function literal is desugared to a function literal.
For example the shorthand function literal ["Multiply", "_", 2]
is desugared
to ["Function", ["Multiply", "_", 2], "_"]
.
To use wildcards in LaTeX, they must be wrapped with an \operatorname
command except for \_
.
["Add", "_", "_2"]
The shorthand function literal ["Multiply", "x", 2]
which uses the unknown x
is equivalent to the function literal ["Function", ["Multiply", "x", 2], "x"]
.
When using this form, make sure that the symbol x
is not defined in the current scope.
A symbol which is the name of an operator (for example Sin
) is also a valid
function literal shorthand.
This expression will apply the Sin
function to the elements of xs
.
["Map", "xs", "Sin"]
It is equivalent to ["Map", "xs", ["Sin", "_"]]
which is desugared to
["Map", "xs", ["Function", ["Sin", "_"], "_"]]
.
Anonymous Functions
A function that is not bound to a symbol is called an anonymous function.
Anonymous functions are frequently used as arguments to other functions.
In the example below, the second argument of the Map
function is an
anonymous function expressed as a function literal that multiplies its argument by 2
.
["Map", "xs", ["Function", ["Multiply", "x", 2], "x"]]
The same function can be expressed using a shorthand function literal, which
uses _
as a wildcard for the parameter.
["Map", "xs", ["Multiply", "_", 2]]
Evaluating a Function Literal
To apply a function literal 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 a function literal. The rest of the arguments are the
arguments that will be applied to the function literal.
Closures
A closure is a function literal that captures one or more free variables
from its defining lexical scope, preserving their values at the time of definition.
The capture happens by simply referencing the variables in the function body.
For example, the following function literal captures the variable a
from its
lexical scope:
["Function", ["Add", "a", "_"], "_"]
This function literal captures the value of a
at the time of definition, and
when the function is applied, it will use that value of a
in the computation.
["Block"
["Assign", "f",
["Block",
["Declare", "a", "integer"],
["Assign", "a", 10],
["Function", ["Add", "a", "_"], "_"]
]
]
["Declare", "a", "integer"],
["Assign", "a", 100]
["f", 1]
]
// ➔ 1 + 10
Note that the value of a
is 3
when the function is defined, and it
is 10
when the function is applied. The function will always use the value of
a
that was in scope when the function was defined, not the value of a
at the
time the function is applied. In fact, the out a
is a different variable
which is unrelated to the a
in the scope of the function, but with the same
name.
Operating on Functions
Function: (body)
Function: (body, arg-1, arg-2, ...)
Create a function literal which can be used as an anonymous function.
The arg-n
arguments are symbols which are the bound variables (parameters) of the
function literal.
The Function
operator has the lazy
flag set to true
, which means
that neither the body nor the parameters are evaluated until the function is
applied to some arguments.
The body is an 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: (symbol, fn)
Assign the function literal fn
to the symbol symbol
.
Assign
is not a pure function,
as it changes the state of the Compute Engine.
The fn is a function literal, which can be created using the ["Function"]
expression or the shorthand function literal.
["Assign", "double", ["Function", ["Multiply", "x", 2], "x"]]
Apply: (function, expr-1, ...expr-n)
Apply a list of arguments to a function. The function is a function literal, or a symbol whose value is a function literal.
The expr-n arguments are the arguments of the function literal.
["Apply", ["Multiply", "_", "_"], 3]
// ➔ 9
["Apply", ["Function", ["Multiply", "x", "x"], "x"], 3]
// ➔ 9
With LaTeX, the \lhd
and \rhd
commands can be used to apply a function to a single
argument on the left or right respectively.
["Apply", "f", ["Apply", "g", "x"]]