Functions and macros |
Any function definition can be preceded by the quantifier forall(…).
A function definition is done as follows:
|
|
Notice that any of the type specifications can be omitted, in which case the type is assumed to be Generic.
|
A macro corresponds to a syntactic definition and can be introduced by
using
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 ]) |