Functions
Declaring Functions
Functions are defined using the fn
keyword.
fn add x y as int, int -> int =
x + y
A function which takes two parameters, x and y of type int, then adds them together to return a single int
The type annotation is optional.
fn add x y = x + y
Although remember that type annotations often help as a form of documentation.
Calling Functions
White-space is used to separate the arguments to a function
fn main =
add 1 2
Parenthesis can also be used to 'group' expressions.
fn main =
std:io:println (add (add 1 2) (add 3 4))
A function adding numbers then printing them to the terminal
Pattern Parameters
The parameters in a function declaration may be any infallible pattern, not just plain identifiers.
fn add_pairs xy ab as (int, int), (int, int) -> (int, int) =
...
// can instead be written as
fn add_pairs (x, y) (a, b) as (int, int), (int, int) -> (int, int) =
(x + a, y + b)
fn main =
std:io:println (add_pairs (1, 2) (3, 4))
A function with two tuple parameters being pattern matched in the function declaration
Read more about patterns in the Pattern Matching & Conditionals chapter
Where-Bindings
A function may also be defined inside another function, of which it'll have access to its parent function's parameters.
fn main =
std:io:println (add 5 6)
where
fn add x y = x + y
A function declared inside another function as a where-binding
Operators
New operators can also be defined similarly to functions.
fn +++ left right as int, int -> int =
left + right + right + right