SML Basics

home1 home2
 Bib
 Algorithms
 Bioinfo
 FP
 Logic
 MML
 Prog.Lang
and the
 Book

FP
 SML
  SML97
   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, NB not `-'
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 block structure
(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
Coding Ockham's Razor, L. Allison, Springer

A Practical Introduction to Denotational Semantics, L. Allison, CUP

Linux
 Ubuntu
free op. sys.
OpenOffice
free office suite
The GIMP
~ free photoshop
Firefox
web browser

SML:
:: cons
[x1,...] list
[ ] list
@ append
fn =>  &lambda .
: has type
Compared

© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Saturday, 20-Apr-2024 12:28:23 AEST.