-
-
Notifications
You must be signed in to change notification settings - Fork 4
function
Functions can be defined in the following natural ways:
function square (n: number){ n*n }
to square a number: return it * it
square of a number = it * it
square(number n)=n*n
square(n)=n*n
square:=it*it
The parser should differentiate between variables and functions via these heuristics:
- functions have braces left of the assignment
square(n)=n*n
or - functions have blocks right of the assignment
square = {it*it}
or - functions use
:=
for the assignmentsquare:=it*it
The difference to square=it*it
is that variables get evaluated instantly and only once, whereas
The function square:=it*it
is a dangling closure
Under the hood all are represented equally: as the charged data
square = {it*it}
Together with an entry in the pattern dictionary [:square, number] => &square denoting that the symbol 'square' together with the type number should be resolved to the function square.
Functions are unary prefix operators, that is in an expression
a f b c
they get parsed as a (f (b c))
Every function generates a suffix operator: square 2 == 2 squared
Todo : do they have the same precedence? I.e. does
square 1+2 == 1+2 squared
square 1+2 == 1 + (2 squared)
?