(*checks if 1 = 0 (it doesn't), if it does (it doesn't) then it returns 4, otherwise it returns 5 (it does)*) if 1=0 then 4 else 5; (*similar to the previous example however it checks if x and y are equal and returns true if this is the case*) fun myeqtest x y = if x=y then true else false (*a much shorter version of the previous example*) fun myeqtest x y = (x=y); (*a use case of the myeqtest function*) myeqtest 4 4; (*myeqtest can be shortened further*) op =; (*sum of an int list*) fun sumlist1 [] = 0 | sumlist1 (h::t) = h+(sumlist1 t); (*same as the previous example, however this uses plain recursion instead of tail recursion*) fun sumlist2b x [] = x | sumlist2b x (h::t) = sumlist2b (x+h) t; (*this function allows the previous example to be used without passing in the x value *) fun sumlist2 l = sumlist2b 0 l; (*factorial function*) fun fact 0 = 1 | fact n = n*(fact (n-1)); (*duplication function*) fun nduplicate 0 x = [] | nduplicate n x = x::(nduplicate (n-1) x); (*creates a duplicated list of factorial x factorial x divided 2 times*) fun longlist x = nduplicate (fact x) (fact (x div 2)); (*using sumlist with longlist 10*) sumlist2 (longlist 10); (*this function returns the length of a list*) fun listlength [] = 0 | listlength (h::t) = 1 + (listlength t); (*making the datatype binarytree*) datatype binarytree = Node of unit | Pair of binarytree*binarytree; (*deconstruction of the Node type*) Node; (*making a pair of Nodes*) Pair(Node,Node); (*finding the list length of the previous example stored in a list (this returns 1)*) listlength [Pair(Node,Node)]; (*making the natural numbers datatype*) datatype mynat = Zero | Succ of mynat; (*this function converts integers to mynats*) fun int2mynat 0 = Zero | int2mynat n = Succ(int2mynat(n-1)); (*this converts mynats to integers*) fun mynat2int Zero = 0 | mynat2int (Succ x) = 1+(mynat2int x); (*converts fact 10 to and from a mynat*) mynat2int (int2mynat (fact 10)); (*adds two mynats together*) fun cheatingadd x y = int2mynat((mynat2int x) + (mynat2int y)); (*an example of how the previous example works*) cheatingadd (int2mynat 1000) (int2mynat 1000); (*a way of adding mynats without converting integers first*) fun honestadd Zero y = y | honestadd (Succ x) y = Succ(honestadd x y); (*a test of the previous function*) mynat2int (honestadd (int2mynat 1000) (int2mynat 1000)); (*showing an example of an exception, raises a div exception since 0 is being used in a division. this is a runtime error*) 5 div 0; (*this is a type error*) 5 div 5.5; (*returns the top 2 elements in a list, note that if this program takes in a list with only one element we get an error*) fun twotop (h1::h2::t) = (h1,h2); (*defines the exception 'listodd'*) exception listodd; (*this will not get an error, it will raise an exception*) fun twotop (h1::h2::t) = (h1,h2) | twotop _ = raise listodd; (*using the twotop function and handling the error to return the tupple (0,0) instead of raising the exception*) twotop [2] handle listodd => (0,0); (*making exception boo and hoo*) exception boo; exception hoo; (*which exception will win?*) (raise boo,raise hoo); (*now which?*) (raise boo) andalso (raise hoo);