Simple examples of Mathemagix programs

1.Hello world

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

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

By default, only very few types are available in Mathemagix. The first line is therefore needed in order to make various standard types available, such as strings and input/output ports.

The example program can be run in two ways: by compiling it using the Mathemagix compiler mmc and then running the resulting executable, or by interpreting it using the Mathemagix interpreter. In the appendix getting and installing Mathemagix it is briefly described how to download and install Mathemagix. For more information, we refer to our website www.texmacs.org.

1.1.Compiling and running the program

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]

1.2.Running the program in the interpreter

Alternatively, we may directly run the program in the Mathemagix interpreter 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.

2.Fibonacci sequences

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;
}

3.Merge sort

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"]
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.