Builtin data types |
In this section, we describe the basic types of
The default type of an object is Generic and the corresponding variable type is Alias Generic:
Mmx] |
a := x |
Mmx] |
type a |
The usual boolean constants are true and false. The equality and inequality tests are = and !=. To build boolean expressions, we use the operators and, or and the negation operator !.
Mmx] |
a = a |
Mmx] |
a != a |
Mmx] |
a = b and a != c |
Mmx] |
a = b or a != c |
Mmx] |
!( a = b or a != c) |
Strings can be braced into double quotes
Mmx] |
s1 := "This is a string" |
Mmx] |
s2: String := "Print \"foo\" " |
Mmx] |
s2 := /" Print "foo" "/ |
Mmx] |
s3 := s2 >< " and \"fii\" " |
Mmx] |
s2 << /" and "fuu" "/ |
Mmx] |
#s1 |
Mmx] |
search_forwards (s2, "Print", 0) |
Mmx] |
replace (s2, "fuu", "haha") |
An integer literal is a sequence of digits, possibly preceded by a minus sign. It matches the regular expression [-]?[0-9]+. Examples: 123456789123456789, -123. The default type for integers is Int. It corresponds to machine type int. The usual arithmetic operators +, -, * are available, as well as the inplace operators +=, -=, *=.
Mmx] |
a := 2 |
Mmx] |
a+3; a-5; a*a |
Mmx] |
a += 1; a *= 2; a -= 3 |
Mmx] |
5 div 2 |
Mmx] |
5 rem 2 |
By default, the floating point literals are converted to Double types. A floating literal is a sequence of digits with a decimal point inside and an optional exponent. It matches the regular expression [-]?[0-9]+[.][0-9]+[[eE][-]?[0-9]+]?. Note that 0. is not permitted, one must write 0.0;
Mmx] |
2.1 |
Mmx] |
2.1*3 |
Mmx] |
2.3/2-1 |
The Syntactic type can be used to produce document outputs, represented as lisp-type expressions. It is automatically parsed by TeXmacs to display these outputs.
Mmx] |
type_mode?:=true |
:
Mmx] |
'x |
:
Mmx] |
'(f (x, y, z)) |
:
Mmx] |
'(f (x, y, z)) [2] |
:
Mmx] |
$document ("Some ", $with ("color", "red", "red"), " text."; "Pythagoras said ", $math ('(a^2 + b^2 = c^2)), ".") |
Vectors are sequences of elements, stored in an array and with a direct access through their index. Their type is parametrized by the type of the elements. The default type is Vector Generic. The indices start from 0. The length of a vector is given by the prefix operator #. The concatenation of vectors is performed by the operator ><. The inplace concatenation of vectors is done by the operator <<. The classical operations car, cdr, cons are available on vectors.
Mmx] |
v := [1,2,3] |
:
Mmx] |
v[0]+v[1]+v[2] |
:
Mmx] |
#v |
:
Mmx] |
w := v >< [4,5] |
:
Mmx] |
v << [1,2] |
:
Mmx] |
[car v, cdr v, cons (3, v)] |
:
Mmx] |
reverse v |
:
Mmx] |
contains?(v,1) |
:
Tuples are written inside (…). Elements are separated by ,, which is associative, so that (1, (2, 3)) is the same as (1, 2, 3).
Mmx] |
v := (1, 2, 3) |
Mmx] |
(1, (2, 3)) |
Mmx] |
v:= [1, 2]; (v, ((v))) |
Row-tuples rows are separated by ;, as exemplified
with the constructions of matrices (defined in the
Mmx] |
use "algebramix" |
Mmx] |
(1, 2; 3, 4; 5, 6) |
:
Mmx] |
[1, 2; 3, 4; 5, 6] |
:
Automatic constructions are possible through the | resp. || notation:
Mmx] |
( i^2 | i in 1..3 ) |
:
Mmx] |
[ i * j | i in 1..3, j in 1..4 ] |
:
Mmx] |
matrix ( i*j | i in 1..5 || j in 1..5 ) |
:
Mmx] |
(1 to 4) |
:
Mmx] |
1 .. 4 |
:
Mmx] |
[1..6] |
:
Mmx] |
[i*i | i in 1 to 10] |
:
Tables allow to store the association between keys of one type and values of another type. They are defined by providing a default value. The default type for tables used in the interpreter is Table(Generic,Generic). In the following example, the default value is 1:
Mmx] |
t := table(1); |
Mmx] |
t[1] := -3; t[34] := 2 |
Mmx] |
t[0] |
Mmx] |
contains? (t,2) |
There is an output stream, which is called mmout. It can be used with the operator << to print strings:
Mmx] |
type_mode?:= true; |
Mmx] |
mmout |
:
Mmx] |
mmout << "Hello\n"; |
Hello
Mmx] |
i := 3; mmout << "The square of " << i << " is " << i*i << "\n"; |
The square of is
Output streams can also be defined from files. Here we write a string into the file toto.txt, and we load the contents of this file into a string:
Mmx] |
output_file_port ("toto.txt") << "Hi there\n"; |
Mmx] |
load ("toto.txt") |
:
Here are some useful commands to read and save data in files. The
command to read a
The file names in a directory can be recovered by the command load_directory. The result is a vector of strings, which corresponds to the name of a file or a subdirectory.
To check if a file or a directory exists, one can use the predicate readable?.
Mmx] | include "example.mmx" |
Mmx] |
save ("tmp.txt", "A string is stored in the file \n in two lines"); |
Mmx] |
load "tmp.txt" |
:
Mmx] |
load_directory "." |
:
Several functions are available to interact with the environment. To get the value of a variable defined in the environment, one can use get_env.
To run a command in this environement, one can use the function system:.
Mmx] | get_env "PWD" |
Mmx] | set_env ("DISPLAY", "arthur:0") |
Mmx] |
system "ls" |
emacs_mode.en.tm
how_to.en.tm
index.en.tm
installation.en.tm
quick_start.en.tm
shell.en.tm
shell_tutorial.en.tm
syntax.en.tm
tmp.txt
toto.txt
: