Documentation menu

Core Language

Functions & Lambdas

Top-level fn declarations for named functions (recursive, hoisted), plus first-class lambda values for callbacks and higher-order code.

New to coding?

Copy the examples exactly first. Run them. Then change one number or word and run them again. You are not expected to memorize the command lists.

1

Declaration

Functions must be declared at the top level — not nested inside main() or other functions.

Try this code
fn add(a, b) {
    return a + b;
}

main() {
    print add(2, 3);   // 5
}
2

Recursion

Try this code
fn factorial(n) {
    if n <= 1 { return 1; }
    return n * factorial(n - 1);
}

factorial(10)   // 3628800
3

Lambdas — arrow form

A single expression, no braces or return keyword.

Try this code
double = fn(x) => x * 2;
double(21)   // 42
4

Lambdas — block form

Multiple statements, explicit return.

Try this code
describe = fn(x) {
    if x > 0 { return "positive"; }
    return "non-positive";
};
5

Closures

A lambda captures the surrounding scope's variables at the moment it's created — a snapshot, not a live reference.

Try this code
base = 10;
add_base = fn(x) => x + base;

base = 100;
add_base(5)   // still 15 — captured when created, not live
6

Higher-order builtins

map, filter, reduce, sort_by, each, any, all all take a lambda.

Try this code
nums = [1, 2, 3, 4, 5];
doubled = map(nums, fn(x) => x * 2);
evens   = filter(nums, fn(x) => x % 2 == 0);
total   = reduce(nums, fn(a, b) => a + b, 0);