#lang scribble/manual
@title{fun}
@verbatim|{
fun ( {, }*):
fun ( {, }*): *
}|
This piece of syntax defines a function. For example,
@tt[]{fun plus1(x): x + 1} defines a function called @tt[]{plus1},
which consumes one variable (@tt{x}), and evaluates to
@tt{x + 1}. The second form allows local definitions, so,
for example,
@verbatim|{
fun outer(x):
fun inner(y): y
inner(x)
}|
defines two functions, one called @tt{outer}, and one called
@tt{inner}. The function @tt{inner} is only available in the
scope of @tt{outer}.
Functions are required to consume at least one argument. Furthermore,
functions are first-order in BSL -- after defining a function called
@tt[]{outer}, the expression @tt[]{outer} would result in an error.
Function application looks like it does in algebra:
@verbatim|{
fun plus1(x): x + 1
> plus1(1)
2
}|
@tt[]{fun} follows the same-line/greater-column rule. So, typing
@verbatim|{
fun plus1(x):
x + 1
}|
results in an indentation error.
However, if local definitions are present, all such definitions must
follow the same-line/same-column rule. So,
@verbatim|{
fun add3(x):
def one: 1
def two: 2
x + one + two
}|
is indented correctly, but
@verbatim|{
fun add3(x):
def one: 1
def two: 2
x + one + two
}|
is not.