A Gentle Introduction to ML: "Hello World"
This is aimed at students with some programming
skills, but new to functional languages. It consists almost entirely of
exercises and diversions, these are intended to be completed at a machine
with at least some supervision. It is not intended to replace teaching.
It will most likely be possible to copy text from the hyper text viewer
(possibly Netscape or Mosaic)
and paste it directly into a window in which ML is running thus saving
at least some re-typing.
This document is an attempt to guide the student in learning rather than
to present the syntax and theory in an ordered fashion. A considerable amount
of time must be invested in learning a new language, with ML it's worth it.
All of the following tutorial material has been developed for Standard ML.
It has been used with New Jersey ML and Edinburgh ML but should work with any
The ML prompt is "-". Expressions typed in are immediately evaluated and usually
displayed together with the resulting type. Expressions are terminated with ";"
Using New Jersey ML the following dialogue might take place:
- "Hello World";
val it = "Hello World" : string
When used normally the ML accepts expressions and evaluates them. The
result is printed to the screen together with the type of the result. The last
result calculated may be referred to as it.
In the example above the interpreter does not have to do any work to calculate
the value of the expression entered - the expression is already in its simplest -
or normal form. A more tricky example would be the expression 3+4 this is
evaluated to the value 7.
it = 7 : int
Notice that the expression to be evaluated is terminated by a semicolon.
The interpreter allows expressions to go over more than one line. Where this
happens the prompt changes to "=" for example:
- 4 + 4 +
val it = 12 : int
A function may be defined using the keyword fun. Simple function
definitions take the form:
fun = ;
fun double x = 2*x;
fun inc x = x+1;
fun adda s = s ^ "a";
These functions may be entered as above. To execute a function simply give
the function name followed by the actual argument. For example:
The system should give you the values 12: int and 101 : int
and "tuba" : string for the expressions above.