CSci 170: Analysis of Programming Languages

Third ML examples

Sets

Suppose a set is given by a list of elements where each element appears exactly once. Don't assume anything about the order of the elements in the list.

Here are some functions in ML that implement the operations of

$ sml
Standard ML of New Jersey, Version 110.0.3, January 30, 1998 [CM; autoload enabled]
- fun member(x,[]) = false
= |   member(x,b::y) =
=       if x=b then true
=       else member(x,y);
val member = fn : ''a * ''a list -> bool
- member(4,[3,4,5]);
val it = true : bool
- member(4,[5,6,7]);
val it = false : bool
- member("hi",["hello","there","hi","to you, too"]);
val it = true : bool

- fun subset([],y) = true
= |   subset(a::x,y) =
=       if member(a,y) then subset(x,y)
=       else false;
val subset = fn : ''a list * ''a list -> bool
- subset([1,3,5],[2,3,4]);
val it = false : bool
- subset([1,3,5],[5,4,3,2,1]);
val it = true : bool

- fun equal(x,y) = subset(x,y) andalso subset(y,x);
val equal = fn : ''a list * ''a list -> bool
- equal([2,3,4],[4,3,2]);
val it = true : bool
- equal([2,3,4],[3,4]);
val it = false : bool

- fun union([],y) = y
= |   union(a::x,y) =
=       if member(a,y) then union(x,y)
=       else a::union(x,y);
val union = fn : ''a list * ''a list -> ''a list
- union([1,3,5],[2,3,4]);
val it = [1,5,2,3,4] : int list
- union([1,3,5],[]);
val it = [1,3,5] : int list

- fun intersection([],y) = []
= |   intersection(a::x,y) =
=       if member(a,y) then a::intersection(x,y)
=       else intersection(x,y);
val intersection = fn : ''a list * ''a list -> ''a list
- intersection([1,3,5],[2,3,4]);
val it = [3] : int list
- intersection(union([1,2],[2,3]),[1,3,5]);
val it = [1,3] : int list

- fun difference([],y) = []
= |   difference(a::x,y) =
=       if member(a,y) then difference(x,y)
=       else a::difference(x,y);
val difference = fn : ''a list * ''a list -> ''a list
- difference([1,3,5],[2,3,4]);
val it = [1,5] : int list

- fun insert(a,[]) = []
= |   insert(a,b::y) = union([a],b)::insert(a,y);
val insert = fn : ''a * ''a list list -> ''a list list
- insert(3,[[1,4],[2,5]]);
val it = [[3,1,4],[3,2,5]] : int list list


- fun power([]) = [[]]
= |   power(a::y) = union(power(y),insert(a,power(y)));
val power = fn : ''a list -> ''a list list
- power([3,5]);
val it = [[],[5],[3],[3,5]] : int list list
- power([]);
stdIn:97.1-97.10 Warning: type vars not generalized because of
   value restriction are instantiated to dummy types (X1,X2,...)
val it = [[]] : ?.X1 list list
- power([1,3,5]);
val it = [[],[5],[3],[3,5],[1],[1,5],[1,3],[1,3,5]] : int list list
< ctrl/d>
$
Note that the definition used for the function power uses an auxilliary function insert, where insert(a,x) will insert the element a into each of the sets listed in the set of sets x. Also note that power doesn't work properly when its argument is the empty set.

Mergesort

Recall that mergesort sorts a list by splitting it in two, sorting each half, then merging the two sorted halves into one sorted whole. Here are some functions that do that.
- fun merge([],y) = y
= |   merge(x,[]) = x
= |   merge(a::x,b::y) =
=       if a < b then a::merge(x,b::y)
=       else b::merge(a::x,y);
val merge = fn : int list * int list -> int list
- merge([1,4,5],[2,3,4]);
val it = [1,2,3,4,4,5] : int list

- (3,5);
val it = (3,5) : int * int
- fun fst(x,y) = x;
val fst = fn : 'a * 'b -> 'a
- fun snd(x,y) = y;
val snd = fn : 'a * 'b -> 'b

- fun split ([]) = ([],[])
= |   split (a::[]) = ([a],[])
= |   split (a::b::y) = (a::fst(split(y)),b::snd(split(y)));
val split = fn : 'a list -> 'a list * 'a list
- split([7,6,5,4,3,2,1,2,3,4,5,6,7]);
val it = ([7,5,3,1,3,5,7],[6,4,2,2,4,6]) : int list * int list

- fun mergesort([]) = []
= |   mergesort([a]) = [a]
= |   mergesort(x) =
=       merge(mergesort(fst(split(x))),mergesort(snd(split(x))));
- mergesort([4,3,2]);
val it = [2,3,4] : int list
- mergesort([1,3,6,3,7,3,8,9,2,5]);
val it = [1,2,3,3,3,5,6,7,8,9] : int list
Note how a pair is created, like (3,5), as shown above. Also, fst and snd are functions defined above to pick out the first and second coordinates of a pair.


Back to the course page.