Functions and macros

1.Function definition

Any function definition can be preceded by the quantifier forall(...).

A function definition is done as follows:

name (arg1: type1, arg2: type2, ...): returned_type == ...

Mmx-light also provides a syntax for lambda expressions:

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)

2.Function call

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.

3.Functions like methods

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

4.Functional programming

Functions can be treated like other objets. Nested functions are supported by Mmx-light.

Mmx] 

shift (x: Int) (y: Int): Int == x + y;

Mmx] 

shift 3

Mmx] 

(shift 3) 4

Mmx] 

map (shift 3, [ 1 to 10 ])

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License. If you don't have this file, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.