Functions
Identifiers
An identifier is the name of a function, variable, type, record field, etc.
An identifier may contain letters, numbers, _ and -,
but it can't begin with a number, or begin or end with -.
Examples of valid identifiers: a, is-empty, float32,
BIG_WORDS, עורב, 예리한.
Keen prefers kebab-case: lower-case words separated by -.
Keywords like if may not be used as identifiers.
Comments
Comments begin with #.
Long comments begin and end with ###.
Prefix function calls
The most commont way to call functions in Keen is with a prefix syntax.
The first column below shows how to call a function f in Keen.
The other column shows an equivalent in C.
f alone calls a function with no arguments.
Accessing a variable uses the same syntax.
Keen will access a variable if one is in scope, otherwise call a function.
Multiple function calls can nest without parentheses.
Each new function name is the final argument to the previous function.
Dot function calls
a.f is equivalent to (f a).
This only works for a function with a single argument.
This often helps avoid parentheses.
Infix function calls
'f calls f with infix syntax.
This works with any number of arguments.
Function definitions
Now to define a function and call it:
log hello-world calls the function log with one argument
hello-world, which is a 0-argument function call.
hello-world string() is the function signature.
It takes no parameters and returns a string.
The function body always goes beneath the signature and indented.
Here's a modified version that takes a single parameter.
This defines a function hello-world that takes a nat (natural number) and returns a string.
The argument 3 becomes the value of the parameter
times, causing the string to repeat 3 times.
The below takes 2 parameters name and times
and passes arguments "keen" and 3, respectively.
Functions with many parameters can be written on multiple lines using the below syntax.
do is used to separate the parameters from the function body.