I'm trying to write three function definitions that can be used to manipulate sets in SML. As you can see we are basing the implementation on lists.
Union is the set of all elements in both Set s and Set t. (No duplicates are allowed)
Intersection is the set in which the elements is a part of both Set s and Set t.
If Set s and Set t are sets, then the relative complement of Set s in Set t, is the set of elements in Set t, but not in Set s.
Right now the code looks like this:
fun filter p [] = []
| filter p (h::t) =
if p h
then h :: (filter p t)
else (filter p t);
fun mem [] a = false
| mem (h::t) a = (a=h) orelse (mem t a);
signature SETS =
sig
type ''a set
val union : ''a set -> ''a set -> ''a set
val intersection : ''a set -> ''a set -> ''a set
val difference : ''a set -> ''a set -> ''a set
end;
structure Set : SETS =
struct
type ''a set = ''a list;
fun union s t = **(Code goes here)**
fun intersection s t = **(Code goes here)**
fun difference s t = **(Code goes here)**
end;
As you can see there is two helpfunctions to be used when needed - mem and filter.filter will go through a list and only keep those elements that satisfy some boolean function p, while mem just checks a list to see if it contains the value a.