SML Basics
types | unit, int, real, bool, char, string, exn | basic types, NB. ( ):unit |
consts | 127:int, 0x7F | an integer constant (and hexa-decimal) |
3.14:real, 0.314e1, 31.4E~1 | reals, floating point | |
~ | unary minus, | |
true:bool, false:bool | ||
#"a":char | ||
"fred":string, "":string "multi-line \ string" |
||
ops | +, -, *, /, div, mod | arithmetic operators NB. /:real*real->real |
^ | string concatenation | |
=, <, >, <=, >=, <> e.g. 2<10, "2">"10" |
comparison operators NB. =, <> do not apply to reals | |
not, andalso, orelse | boolean operators (short-cutting) | |
if e then et else ef | ||
types | (e1, e2, ...): t1*t2*... | tuple : product type |
fn x=>e :tx->te | function : function type | |
coercions | real:int->real | |
ord:char->int, chr:int->char, str:char->string, explode:string->char list, implode:char list->string |
chars <-> strings | |
tuples, records | #1(11,22,33) = 11, #2, etc. | tuple, extract component |
{id1=e1, ...} #id1{id1=e1,...}=e1 |
record, extract field | |
strings | substring("012345",2,4)="234" | also see coercions |
lists | []:'t list, [1,2,3]:int list | list value : list type |
nil, [] | empty list | |
:: :'t*'t list -> 't list | list constructor, [1,2] = 1::(2::nil) | |
hd:'t list->'t, tl:'t list -> 't list | list head and tail operators | |
null:'t list -> bool | test empty? | |
@: 't list * 't list -> 't list | list append, concatenate | |
vector | #[1,2,3] :int vector | NB. immutable |
vector [1,2,3] = #[1,2,3] | convert list to vector | |
ref | ref 7 : int ref | ref value : ref type |
! | :'t ref -> 't, de-reference | |
:= | assign to, change, a ref | |
lexical | fred, Jane_Doe, x1, x'y | identifiers |
( ) [ ] { } " . , ; | reserved | |
+ - / * < > = ! @ # $ % ^ & ` ~ \ | ? : | chars for symbolic ids. -- be careful | |
't etc. | type variable | |
syntax | let decs in exp end | local declarations, see
|
(e1;e2;...;en) | expression returns en | |
if e then et else ef | equiv. case e of true=>et | false=>ef; | |
case e of <match> | ||
while e do e' | NB. returns ():unit | |
decs. | val x = e | value declaration |
fun f x = e | function declaration | |
fun f(x,t) = e | uncurried function :tx*ty->te | |
fun f x y = e | curried function :tx->ty->te | |
fun f pat1= e1 | f pat2= e2 |... |
definition "by cases", by pattern matching | |
type {tparams} id = type_exp | ||
datatype {tparams} id = c1 of type |... | type declaration | |
exceptions | Div, Chr, Empty,... | standard exceptions |
exception i | declare exception, i:exn, ... | |
exception i of t | ... with parameter(s) | |
raise i | ||
e handle <match> | handle exception |