Skip to content

Functions and examples

fn NAME PARAM* -> TYPE
PARAM := name:TYPE
fn add a:i64 b:i64 -> i64
ex add 2 3 = 5
+ a b
  1. doc "text": optional, at most one. States intent, does not affect execution.
  2. ex EXPR = EXPR: examples. At least one is required for every function except main and functions that call effectful builtins.
  3. Statements: let NAME EXPR binds a value (immutable, no shadowing), do EXPR evaluates an expression of type unit for its effect.
  4. One final line: the result of the function.
fn fib n:i64 -> i64
doc "nth fibonacci number"
ex fib 0 = 0
ex fib 10 = 55
if < n 2 n + fib - n 1 fib - n 2

aleph check and aleph test run every ex line. A failing example is E002, with the expected and actual values; a missing one is E001. Examples run hermetically: they see only global definitions, file access is denied and output is discarded.

main takes no parameters and returns unit or res unit str. If it returns err e, Tlön prints e to stderr and exits with code 1.