SML programs for Lecture 11

42;
~42;
3*14;
6*21 div 3;
(9+12) div 3*(12-6);
3*61 mod 47;
1.234;
5.67E4;
0.891;
~1.01112;
1.4*3E1;
4.79*10.0-5.9;
floor 6.789;
real 456;
floor 12.3+4;
1.2*real 3+4.5;
floor (6.7+8.9);
real (floor 11.12);
"hello there";
size "hello";
"milk"^"shake";
#"a";
ord #"a";
chr 48;
true;
not false;
true orelse false andalso true;
(1,1.0,"one");
#1 (1,1.0,"one");
(("Bianca","Castafiore"),"singer");
#2 (#1 (("Cuthburt","Calculus"),"inventor"));
"banana"<>"banana";
(1,"one") = (1,"won");
(("Captain","Haddock"),"sailor")=(("Captain","Haddock"),"sailor");
3*4>5*6;
"ant"<"bee";
"anthill">"ant";
(* not 1<2; *)
not (1<2);
Back to F28PL


SML programs for Lecture 12

val toPound = 0.66;
27.45*toPound;
val employee = ("Duncan","Thaw",19000.0);
val paypounds = #3 employee*toPound;
val toPound = 0.68;
size;
not;
fun inc x = x+1;
fun plural w = w^"s";
fun sSize s = (s,size s);
fun sq (x:int) = x*x;
inc 3;
plural "banana";
sSize "fish";
sq 7;
fun sumsq x y = sq x+sq y;
sumsq 3 4;
fun isMore s n = size s > n;
isMore "hello" 6;
fun isZero 0 = true |
    isZero x = false;
isZero 0;
isZero 3;
fun next "red" = "orange" |
    next "orange"= "yellow" |
    next "yellow" = "green" |
    next "green" = "blue" |
    next "blue" = "indigo" |
    next "indigo" = "violet" |
    next s = s^": not a valid colour";
next "blue";
next "banana";
fun isZero 0 = true |
    isZero _ = false;
isZero 22;
fun AND false false = false |
    AND false true = false |
    AND true false = false |
    AND true true = true;
AND true false;
fun AND false _ = false |
    AND _ y = y;
fun sum 0 = 0 |
    sum n = n+sum (n-1);
sum 3;
fun pow2 0 = 1 |
    pow2 n = 2*pow2 (n-1);
pow2 3;
fun power _ 0 = 1 |
    power x n = x*power x (n-1);
power 3 3;
fun stops 0 = "" |
    stops n = "."^stops (n-1);
stops 3;
fun sJoin _ 0 = "" |
    sJoin s n = s^sJoin s (n-1);
sJoin "run" 3;
val pow2 = power 2;
pow2 3;
val stops = sJoin ".";
stops 3;
fun abs x =
     if x>=0
     then x
     else ~x;
fun max (x:int) y =
     if x>y
     then x
     else y;
Back to F28PL


SML programs for Lecture 13

(1,2,3);
((1,"one"),(2,"two"),(3,"three"));
(("Francis",1.7),("Frances",1.65));
((1,("even",false)),(2,("even",true)));
((1,1),(2,4));
fun tJoin (s1,s2) = s1^" "^s2;
tJoin ("hello","there");
fun swap (e1,e2) = (e2,e1);
swap (1,"two");
swap ((1,"two"),("one",2));
fun first (e1,_) = e1;
first ("hello","there");
fun second (_,e2) = e2;
second (true,42);
1::(2::(3::[]));
"Ann"::("Bill"::("Cyd"::[]));
(1,"one")::(2,"two")::(3,"three")::[];
42::[];
fun ints 0 = [] |
    ints n = n::ints (n-1);
ints 4;
fun nCopies 0 _ = [] |
    nCopies n s = s::nCopies (n-1) s;
nCopies 3 "o";
nCopies 3 9;
fun sum [] = 0 |
    sum (h::t) = h+sum t;
sum [2,4,6];
fun sJoin [] = "" |
    sJoin (h::t) = h^sJoin t;
sJoin ["a","bc","def"];
fun double [] = [] |
    double (h::t) = 2*h::double t;
double [5,3,1];
fun count0 [] = 0 |
    count0 (0::t) = 1+count0 t |
    count0 (_::t) = count0 t;
count0 [1,0,2,0,3,0];
fun count _ [] = 0 |
    count v (h::t) =
     if v=h
     then 1+count v t
     else count v t;
count "a" ["a","b","a"];
fun counts (n,z,p) [] = (n,z,p) |
    counts (n,z,p) (0::t) = counts (n,z+1,p) t |
    counts (n,z,p) (h::t) =
     if h<0
     then counts (n+1,z,p) t
     else counts (n,z,p+1) t;
counts (0,0,0) [1,~2,0,3,~4];
fun sq (x:int) = x*x;
fun squares m n =
     if m>n
     then []
     else sq m::squares (m+1) n;
squares 1 4;
let val x = 12
in x*x*x
end;
let val ((given,family),age) = (("Clark","Kent"),29)
in given
end;
exception DIVIDE_BY_ZERO;
fun divide x y =
     if y=0
     then raise DIVIDE_BY_ZERO
     else x div x;
(* divide 3 0; *)
type family = string
type given = string;
type person = family * given;
type perople = person list;
Back to F28PL


SML programs for Lecture 14

fun length [] = 0 |
    length (_::t) = 1+length t;
length ["a","b","c"];
fun append [] l2 = l2 |
    append (h1::t1) l2 =
     h1::append t1 l2;
append ["a","b","c"] ["d","e","f"];
[1,2,3]@[4,5,6];
fun member _ [] = false |
    member e1 (e2::t) = e1=e2 orelse member e1 t;
member 7 [1,9,7,4];
fun add e [] = [e] |
    add e1 (e2::t) =
     if e1=e2
     then e2::t
     else e2::add e1 t;
add 1 [2,5,4];
add 4 [2,5,4,1];
fun delete _ [] = [] |
    delete e1 (e2::t) =
     if e1=e2
     then t
     else e2::delete e1 t;
delete "c" ["a","b","c","d"];
fun filter _ [] = [] |
    filter p (h::t) =
     if p h
     then h::filter p t
     else filter p t;
fun isPos x = x>0;
filter isPos [~2,1,0,2];
fun map _ [] = [] |
    map f (h::t) = f h::map f t;
map size ["a","bc","def"];
fun sq (x:int) = x*x;
fun cube x = x*sq x;
fun powers x = (x,sq x,cube x);
map powers [1,2,3];
fun insert (i:int) [] = [i] |
    insert i1 (i2::t) =
     if i1<i2
     then i1::i2::t
     else i2::insert i1 t;
insert 7 [5,9];
fun sort [] = [] |
    sort (h::t) = insert h (sort t);
sort [7,9,5];
fun foldr f b [] = b |
    foldr f b (h::t) = f h (foldr f b t);
fun sJoin s1 s2 = s1^s2;
foldr sJoin "" ["a","bc","def"];
fun add (x:int) y = x+y;
val sum = foldr add 0;
sum [1,2,3];
val sort = foldr insert [];
sort [3,2,1];
fun gInsert p v [] = [v] |
    gInsert p v (h::t) =
     if p v h
     then v::h::t
     else h::gInsert p v t;
fun iLess (x:int) y = x<y;
val insert = gInsert iLess;
fun gSort p [] = [] |
    gSort p (h::t) = gInsert p h (gSort p t);
val sort = gSort iLess;
sort [3,2,1];
fun gSort p = foldr (gInsert p) [];
val sort = gSort iLess;
sort [3,2,1];
Back to F28PL


SML programs for Lecture 15

explode "abc";
implode [#"a",#"b",#"c"];
fun toInt1 v [] = v |
    toInt1 v (h::t) = toInt1 (10*v+ord h-ord #"0") t;
fun toInt s = toInt1 0 (explode s);
toInt "4321";
fun toChar v = chr (v+ord #"0");
fun getDigits1 0 = []  |
    getDigits1 i = getDigits1 (i div 10)@[i mod 10];
fun getDigits 0 = [0] |
    getDigits i = getDigits1 i;
getDigits 4321;
fun toChar v = chr (v+ord #"0");
fun fromInt i = implode (map toChar (getDigits i));
fromInt 4321;
datatype STATE = ON | OFF;
ON;
fun switch ON = OFF |
    switch OFF = ON;
switch OFF;
datatype NUMB = INT of int | REAL of real;
INT 33;
REAL 4.56;
fun ADD (INT i1) (INT i2) = INT (i1+i2) |
    ADD (INT i) (REAL r) = REAL (real i+r) |
    ADD (REAL r) (INT i) = REAL (r+real i) |
    ADD (REAL r1) (REAL r2) = REAL (r1+r2);
ADD (REAL 3.4) (INT 5);
datatype ILIST = INULL | ICONS of int * ILIST;
val i = ICONS (1,ICONS (2,ICONS (3,INULL)));
fun iSum INULL = 0 |
    iSum (ICONS (value,next)) = value+iSum next;
iSum i;
datatype STREE = SNULL | SNODE of string * STREE * STREE;
val s =
SNODE ("m",SNODE ("f",SNODE ("c",SNULL,SNULL),SNODE("h",SNULL,SNULL)),
           SNODE ("s",SNODE ("p",SNULL,SNULL),SNODE("w",SNULL,SNULL)));
fun toList SNULL = [] |
    toList (SNODE(s,left,right)) = toList left@(s::toList right);
toList (SNODE ("m",SNODE ("f",SNODE ("c",SNULL,SNULL),SNODE("h",SNULL,SNULL)),
           SNODE ("s",SNODE ("p",SNULL,SNULL),SNODE("w",SNULL,SNULL))));
datatype EXP = INT of int |
               ADD of EXP * EXP |
               SUB of EXP * EXP |
               MULT of EXP * EXP |
               DIV of EXP * EXP;
val t = MULT (ADD (INT 1,INT 2),SUB (INT 3,INT 4));
fun show (INT i) = fromInt i |
    show (ADD(e1,e2)) = "("^show e1^"+"^show e2^")" |
    show (SUB(e1,e2)) = "("^show e1^"-"^show e2^")" |
    show (MULT(e1,e2)) = "("^show e1^"*"^show e2^")" |
    show (DIV(e1,e2)) = "("^show e1^"/"^show e2^")" ;
show t;
fun eval (INT i) = i |
    eval (ADD(e1,e2)) = eval e1+eval e2 |
    eval (SUB(e1,e2)) = eval e1-eval e2 |
    eval (MULT(e1,e2)) = eval e1*eval e2 |
    eval (DIV(e1,e2)) = eval e1 div eval e2;
eval t;
datatype 'a LIST = NULL | CONS of 'a * 'a LIST;
Back to F28PL