> <\body> programs> Let us start with the famous ``hello world'' example, which is written as follows in : <\mmx-code> include "basix/fundamental.mmx"; mmout \\ "Hello world!" \\ lf; By default, only very few types are available in . 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 compiler and then running the resulting executable, or by interpreting it using the interpreter. In the appendix > it is briefly described how to download and install . For more information, we refer to our website |http://www.texmacs.org>. Assuming that the above program was saved in a file , we may compile and execute the program in a shell session as follows: <\session|shell|default> <\input|Shell] > mmc hello_world.mmx <\unfolded-io|Shell] > ./hello_world <|unfolded-io> Hello world! <\input|Shell] > \; Alternatively, we may directly run the program in the interpreter : <\session|mathemagix|default> <\output> |>>|>|>|>|>>> \; <\unfolded-io> <|unfolded-io> include "hello_world.mmx" <|unfolded-io> Hello world! <\input> <|input> \; 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 . 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: <\mmx-code> 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 . It is also possible to implement a faster non recursive algorithm which returns an integer of arbitrary precision: <\mmx-code> 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: <\mmx-code> 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 can be applied to any vector whose entries are of a type with an ordering =: (T, T) -\ T>. For instance, the instructions <\mmx-code> mmout \\ merge_sort ([ 3, 2, 1, 5, 4, 4, 7 ]) \\ lf; mmout \\ merge_sort ([ "bob", "alice", "carl" ]) \\ lf; yield the output <\indent> <\compact> <\verbatim> [1, 2, 3, 4, 4, 5, 7] ["alice", "bob", "carl"] . If you don't have this file, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.>