Functions

Define callable values with let.

A function is a value you can call. It takes input values and gives one result.

let render (port : Int, secure : Bool) : Int := port;
let positional := render(8080, 0 = 0);
let labeled := render(secure := 0 = 0, port := 8080);
labeled;

Define a named function

A function is still a let binding. Parameters live beside the name. The result type appears before :=.

let addTax (subtotal : Int, tax : Int) : Int := subtotal + tax;

Use a result type when the public shape matters. It helps readers and catches wrong return values.

Positional and named calls

Call with positional arguments when the order is obvious. Use named arguments when a pair of values could be confused.

let portFor (base : Int, secure : Bool) : Int := base;
let a := portFor(8000, 0 = 0);
let b := portFor(secure := 0 = 1, base := 8000);

Named calls let the code state which argument is which.

Functions as values

A function can be passed around like any other value. Callable types use ->.

let inc (x : Int) : Int := x + 1;
let mapper : Int -> Int := inc;
mapper(41);