411 REFERENCE · Wikibooks
Standard ML Programming
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Contents
Since this book is far from completion, existing pages are listed here.
Types
Expressions
Examples and Exercises
Solutions
About Standard ML
Standard ML (SML) belongs to the ML family of programming languages. Like other members of this family (such as OCaml), it is characterized by strong static typing with type inference, strict evaluation, and a module system which features signatures, structures and functors. The current standard dates from 1997; it supersedes an earlier standard from 1990, and the following standard is known as successor ML (sML).
Syntax examples
The following code snippet defines a function factorial that computes the factorial of a non-negative integer n.
local
val rec fac = fn 0 => 1 | n => n * fac (n - 1)
in
fun factorial n = if n < 0 then raise Domain else fac n
end;
One important note: in the interactive top-level of SML/NJ, the terminal semicolon is necessary to evaluate the code so far, but the semicolon is usually optional as a statement terminator; it is only required to segregate sequenced expressions.
The following code snippet defines a function gray_code that maps an integer n to a list of all distinct n-character strings of 0s and 1s, in Gray code order, such that each element differs from its neighbour(s) in exactly one character, before computing a string of three-digit gray codes which is equal to "000-001-011-010-110-111-101-100".
fun prefix p s = String.concat [p, s];
fun extend p xs = List.map (prefix p) xs;
fun expand prev = extend "0" prev @ List.rev (extend "1" prev);
fun gray_code n = if n < 1 then [""] else expand (gray_code (n - 1));
fun print_line a = print a before print "\n";
print_line (String.concatWith "-" (gray_code 3));
Getting started
A prerequisite for starting with Standard ML is the compiler, most of which generate machine code directly; however, like most dynamic languages, several SML compilers offer an interactive top-level which compiles and evaluates code on demand, which can be convenient for froshes and underclassmen.
External resources
About Standard ML
Wikipedia's article on Standard ML
The Definition of Standard ML, May 2007, 978-0-262-63181-5
Standard ML Project
Standard ML Basis Library
Compilers and interpreters
MLton: A whole-program optimizing compiler without an interactive top-level
Standard ML of New Jersey: A compiler with an interactive top-level and libraries
Alice ML: A multi-platform interpreter with GUI
Computer programming languages
S
25%
__NOTOC__