Functions and macros |
Any function definition can be preceded by the quantifier forall(…).
A function definition is done as follows:
name (arg1: type1, arg2: type2, …): returned_type == … |
lambda (arg1: type1, arg2: type2, …): returned_type do … |
Notice that any of the type specifications can be omitted, in which case the type is assumed to be Generic.
discr (a, b, c) == b^2 - 4*a*c; forall (R) gcd (p: Polynomial(R), q: Polynomial(R)): Polynomial(R) == {…} |
A macro corresponds to a syntactic definition and can be introduced by using ==>. Expression macros can also be defined by using macro instead of lambda. No type is needed:
Mmx] |
square x ==> x*x |
Mmx] |
square 2.1 |
Mmx] |
square 2 |
Mmx] |
disc (a,b,c) ==> b*b - 4*a*c |
Mmx] |
disc (1,2,3.001) |
A function or a macro foo is called in the usual way: foo (arg1, arg2,…).
If foo is unary then () can be omited, but note that foo a b c is equivalent to foo (a (b (c))). Function call is always by value.
The definition of a postfix function is preceded by the keyword postfix. The name of the function should start with a '.'.
Mmx] |
postfix .square (i : Int) : Int == i*i; |
Mmx] |
3.square |
Mmx] |
postfix .m (a: Int) (b: Int) : Double == 1.0 * a * b; |
Mmx] |
3.m 3 |
Functions can be treated like other objets. Nested functions are
supported by
Mmx] |
shift (x: Int) (y: Int): Int == x + y; |
Mmx] |
shift 3 |
Mmx] |
(shift 3) 4 |
Mmx] |
map (shift 3, [ 1 to 10 ]) |