(Notes for 441 Lecture on 9/26/05.)
+ - * div mod = < <= > >=
+ - * /
andalso orelse
^
datatype nat = Zero | Succ of nat
val zero = Zero val two = Succ(Succ(Zero))
case two of Zero => print("zero") | Succ(_) => print("non-zero") (* at command prompt *)
fun nat2int(n:nat):int = case n of Zero => 0 | Succ(x) => 1 + nat2int(x) fun int2nat(i:int):nat = if (i = 0) then Zero else Succ(int2nat(i-1))
|- add N1 N2 N3 ------------------- ------------------------------ |- add Z N N |- add (S N1) N2 (S N3)
fun add(n1:nat,n2:nat):nat = case n1 of Zero => n2 | Succ(n1') => Succ(add(n1', n2))
- add(int2nat 4, int2nat 8); val it = Succ (Succ (Succ (Succ (Succ #)))) : nat - nat2int(it); val it = 12 : int
datatype 'a list = nil | :: of 'a * 'a list
(true,3) :: (false,2) :: nil
[[1,2],[3],[]] : int list list
fun length(l) = case l of nil => 0 | h::t => 1 + length(t) (* type of h? t? *)
fun append(l1, l2) = case l1 of nil => l2 | h::t => h::append(t,l2)
val addone = fn x => x+1 (* addone: int -> int *) val addn = fn n => fn x => x + n (* addn: int -> int -> int *)
map: ('a -> 'b) -> 'a list -> 'b list
fun map f = fn l => case l of nil => nil | h::t => f(h) :: (map f t)
fun map f l = case l of nil => nil | h::t => f(h) :: (map f t)