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 to higher order functions.

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

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

The section elem -> elem + 1 is 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 the elem -> elem + 1 example above, or they can be more complicated, multi-line expressions when combined with the block syntax.

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

You can do simple pattern decomposition in lambdas with the cases syntax.

distributed.lib.base.data.List.map (cases (a, b) -> a Nat.+ b) [(1, 2), (3, 4)]
[3, 7]

This can be useful when you want to destructure a tuple or other data types.

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

distributed.lib.base.data.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