ML Quick Reference, Draft 2, 2/14/10

Taken from Ullman, Elements of ML Programming, Webber, Modern Programming Languages

ML Binary Operator Precedence (from high to low)

operator precedence level associativitynum operandsmeaning(s) Example
*, /, div, mod 7 left 2 arithmetic: multiply, real divide, integer divide, modulus
+, -, ^ 6 left 2 arithmetic: add, subtract; string concatenate
::, @ 5 right 2 cons and concatenation for lists
<, >, <=, >=, =, <> 4 left 2 comparison operators
# ? ? 2 Selection operator, PREFIX#2 (1, true, "abc", 3.14159) --> true

ML type constructors and annotation

operator precedence level associativitynum operandsmeaning Example
list 0 ? 1 Postfix type constructor: build a list of the previous operand (type)
* 1 ? 2 Infix type constructor: build a tuple of two operands (types)
-> 2 right 2 Infix type constructor: make function types
: ? ? 2 Infix: makes preceding variable or expression (2st op) the indicated type (2nd op).

Numeric (int, real) -related

function type Meaning
~ int -> int or real -> real negates argument
min, max int * int -> int  
abs int -> int or real -> real  
real int -> real  
floor real -> int  
ceil real -> int  
round real -> int  
trunc real -> int For param > 0, like floor. For param < 0 like ceil
chr int -> char return char of parameter ASCII code
ord char -> int return ASCII code of parameter char
str char -> string converts char to one-char string

String- related

function type Meaning
explode string -> char list convert string to a list of chars
implode char list -> string convert list of charts to a string
size string -> int returns length of string
substring string * int * int -> string returns substring of argument, starting at position given by first int,
continuing for 2nd int # chars.
Index starts at 0

Boolean-related

function type Meaning
andalso and orelse bool * bool -> bool logical AND and OR, shortcircuited
not bool -> bool logical NOT

List-related

function type Meaning
hd 'a list -> 'a return initial element (car) of list
tl 'a list -> 'a list return 'popped' (cdr) of list
null 'a list -> bool return true if argument is empty list, false otherwise
length 'a list -> int returns number of elements
rev 'a list -> 'a list returns reversed argument

Tuple-related

function type Meaning
# int * tuple -> 'a #1 (2, true, "abc") --> true

I/O

function type Meaning
print string -> unit prints value of argument as side effect. Returns unit as its value

ML keywords

keyword meaning example
as create local variable and match to a pattern L as x::xs
end see let
fn used by ML to indicate a value that has a function type
fun define a function
in see let
let create local variables let val x = [1, 2, 3]; in hd(x) end; --> 1
val bind name to identifier val x = (1, true); val(a, b, c) = (true, "abc", 34.5);

Other

Item comment example
(* ... *) comments can be nested (*this is (* a nested comment *) right here *)