Simple examples of |

Let us start with the famous “hello world” example, which
is written as follows in

include "basix/fundamental.mmx"; mmout << "Hello world!" << lf; |

By default, only very few types are available in

The example program can be run in two ways: by compiling it using the
`mmc` and then
running the resulting executable, or by interpreting it using the
`www.texmacs.org`.

Assuming that the above program was saved in a file `hello_world.mmx`,
we may compile and execute the program in a shell session as follows:

Shell] |
mmc hello_world.mmx |

Shell] |
./hello_world |

Hello world!

Shell] |

Alternatively, we may directly run the program in the `mmi`:

Welcome to Mathemagix 1.0.1 |

This software falls under the GNU General Public License |

It comes without any warranty whatsoever |

http://www.mathemagix.org |

(c) 2010-2012 |

Mmx] |
include "hello_world.mmx" |

Hello world!

Mmx] |

When including files with external C++ functionality for the first
time in the interpreter, the interpreter first has to compile some
glue in order to use this functionality. This happens in particular
for the file `basix/fundamental.mmx`. Whenever the
interpreter is compiling some glue, it displays a message which
disappears as soon as the compilation is complete.

Another classical example is the computation of Fibonacci sequences. A simple implementation using a recursive function goes as follows:

include "basix/fundamental.mmx"; fib (n: Int): Int == if n <= 1 then 1 else fib (n-1) + fib (n-2); |

Notice that the programmer has to specify explicit types for the
arguments and return type of the function. In our example both the
argument and the return value are machine integers of type `Int`.
It is also possible to implement a faster non recursive algorithm
which returns an integer of arbitrary precision:

include "numerix/integer.mmx"; fib (n: Int): Integer == { a: Integer := 1; b: Integer := 1; for k: Int in 2 to n do { c: Integer == a + b; a := b; b := c; } return b; } |

One more involved example is to provide a generic implementation of the merge sort algorithm:

include "basix/fundamental.mmx"; category Ordered == { infix <=: (This, This) -> Boolean; } forall (T: Ordered) merge_sort (v: Vector T): Vector T == { if #v <= 1 then return v; v1: Vector T == merge_sort v [0, #v quo 2]; v2: Vector T == merge_sort v [#v quo 2, #v]; r : Vector T := []; i1: Int := 0; i2: Int := 0; while i1 < #v1 or i2 < #v2 do if i1 < #v1 and (i2 >= #v2 or v1[i1] <= v2[i2]) then { r << [ v1[i1] ]; i1 := i1 + 1; } else { r << [ v2[i2] ]; i2 := i2 + 1; } return r; } |

This routine `merge_sort` can be applied to any vector
whose entries are of a type `T` with an ordering `infix <=: (T, T) -> T`. For instance, the
instructions

mmout << merge_sort ([ 3, 2, 1, 5, 4, 4, 7 ]) << lf; mmout << merge_sort ([ "bob", "alice", "carl" ]) << lf; |

yield the output

[1, 2, 3, 4, 4, 5, 7] ["alice", "bob", "carl"]

© 2012 Joris van der Hoeven

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.