A brief summary of SML Reserved Words: abstype and andalso as case datatype do else and eqtype exception fn fun functor handle if in include infix infixr let local nonfix of op open orelse raise rec sharing sig signature struct structure then type val while with withtype Operators: Precedence can be from 0 through 9 Built in are infix operators: 7 / * div mod 6 + - ^ 5 :: @ 4 = <> < > <= >= 3 := o Define your own by infix 9 doit; or infix 2 $; All associate to left except := . Use infixr 2 $; to get right association. o ('a->'b)->('c->'a)->('b->'c) function composition + overloaded int*int->int real*real->real addition - overloaded int*int->int real*real->real subtraction * overloaded int*int->int real*real->real multiplication / real*real->real quotient of real division div int*int->int quotient of integer division mod int*int->int modulo ~ overloaded int->int real->real unary negation ^ string*string->string concatenation = (''a * ''a)->bool equal <> (''a * ''a)->bool not equal < overloaded int*int->bool real*real->bool string*string->bool less than > overloaded int*int->bool real*real->bool string*string->bool greater than <= overloaded int*int->bool real*real->bool string*string->bool less than or equal >= overloaded int*int->bool real*real->bool string*string->bool greater than or equal := 'a ref * 'a->unit assignment to a reference ! 'a ref->'a get contents of a reference :: 'a * 'a list->'a list construct list, add item to front @ 'a list * 'a list->'a list concatenate two lists Primitive types: int arbitrary long integer, identity is 0 or 1 real hardware floating point bool boolean with values true or false string character string (no type character), identity is "" fn a function name unit only value is () e.g. empty tuple 'a list polymorphic list, identity is nil 'a ref polymorphic reference instream input stream, only value std_in outstream output stream, only value std_out exn exceptions, e.g. Match, Bind, Interrupt Sample constants of various types: "abc" :string 1 :int 992735729476129738845 :int 1.0 :real 1.75E~7 :real true :bool false :bool nil :'a list std_in :instream std_out :outstream (1,"ab") :tuple () :unit the empty tuple [1,"ab"] :list [] :list the empty list nil :list the empty list {name:string, age:int} :record type {name="Jim",age=21} :record value In strings, special characters: \n new line \t tab \" a quote \\ a backslash \ new-line \ continues a string across a line Functions: dec ref '_a->'_a ref make a reference not bool->bool logical negation use string->unit read in a file sin real->real sine cos real->real cosine exp real->real exponential ln real->real logarithm arctan real->real arc tangent sqrt real->real square root abs real->real absolute value real int->real type conversion floor real->int larget integer less than or equal the real ceiling real->int least integer greater than or equal the real truncate real->int truncate to integer size string->int the size of a string, 0 is null string length 'a list->int length of a list rev 'a list->'a list reverse a list map ('b-'b)->('a list->'b list) apply function to all list elements explode string->string list explode a string into a list of characters implode string list->string reverse of explode chr int->string int in 0..255, returns string of length 1 ord string->int returns the integer ASCII value of first character "0"->48 etc. substring string*int*int->string substring(s,f,l) s is string, f is first character starting with zero, l is length of substring extracted. exception raised if f or f+l invalid print any primitive type->unit writes characters to standard output open_in string->instream open a file for input close_in instream->unit close an open file input instream*int->string read a string of length n from instream lookahead instream->string end_of_stream instream->bool open_out string->outstream close_out outstream->unit output outstream*string->unit hd 'a list->'a extract head of a list tl 'a list->'a extract tail of a list null 'a list->bool true if list empty, e.g. =[] or =nil Example: (* treesort.sml *) fun accumulate f a [] = a | accumulate f a (b::x) = accumulate f ( f a b ) x; datatype 'a tree = Empty | Tr of ( 'a tree * 'a * 'a tree ); fun leaf a = Tr ( Empty, a, Empty ); fun flatten Empty = [] | flatten ( Tr ( t1, (a:int), t2 )) = flatten t1 @ [a] @ flatten t2; fun treeinsert Empty (m:int) = leaf m | treeinsert (Tr ( t1, n, t2 )) m = if m < n then Tr ( treeinsert t1 m, n, t2) else Tr ( t1, n, treeinsert t2 m); fun treesort (x: int list) = flatten ( accumulate treeinsert Empty x ); treesort [3,5,7,1,2,3,4,9,6,8]; Output: Standard ML of New Jersey, Version 75, November 11, 1991 Arrays have changed; see Release Notes val it = () : unit - val accumulate = fn : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a datatype 'a tree con Empty : 'a tree con Tr : 'a tree * 'a * 'a tree -> 'a tree val leaf = fn : 'a -> 'a tree val flatten = fn : int tree -> int list val treeinsert = fn : int tree -> int -> int tree val treesort = fn : int list -> int list val it = [1,2,3,3,4,5,6,7,8,9] : int list -