Lambda syntax

Lambdas, or anonymous functions are unnamed functions which are defined without going through the entire term declaration process. They're typically used when the function doesn't need to be reused elsewhere in the program or when the function itself is very simple. They're often found as the arguments tohigher order functions.

While we could define the function below as a separate term to pass intoList.map,it's much simpler to define in-line.

use Nat + a = [1, 2, 3, 4, 5] List.map (elem -> elem + 1) a
[2, 3, 4, 5, 6]

The sectionelem -> elem Nat.+ 1is the lambda. Anything to the left of the arrow represents the parameters to the function and the expression to the right is the function body.

Anonymous functions can be simple, like theelem -> elem Nat.+ 1example above, or they can be more complicated, multi-line expressions when combined with theblock syntax.

List.map (i -> use Nat + x = i + 1 y = x + 1 z = y + 1 z) [1, 2, 3]
[4, 5, 6]

A lambda with two arguments is represented by separating the function arguments with spaces.

List.foldLeft (acc a -> a Nat.+ acc) 0 [1, 2, 3, 4]
10

You can also choose to ignore the argument to the lambda altogether with the underscore symbol, as in the following function:

a = [1, 2, 3, 4, 5] List.map (_ -> 10) a
[10, 10, 10, 10, 10]