object Sets { type Set = Int => Boolean val bound = 1000 // returns true if elem is in s, false otherwise def contains(s: Set, elem: Int): Boolean = // returns a set consisting of a single element, elem def singletonSet(elem: Int): Set = // returns an empty set def emptySet: Set = // inserts element x into set s def add(s: Set, x: Int): Set = // returns the union of s and t def union(s: Set, t: Set): Set = // returns the intersection of s and t def intersect(s: Set, t: Set): Set = // returns the difference of s and t, s-t def difference(s: Set, t: Set): Set = // returns true is s is a subset of t, false otherwise def subset(s: Set, t: Set): Boolean = // returns true is s is equal to t, false otherwise def equal(s: Set, t: Set): Boolean = // returns the number of elements in s def size(s: Set): Int = // returns a subset of s of elements of s that satisfy the predicate p def filter(s: Set, p: Int => Boolean): Set = // returns true if all elements of s satisfy predicate p def forall(s: Set, p: Int => Boolean): Boolean = // returns true if at least one element of s satisfies predicate p def exists(s: Set, p: Int => Boolean): Boolean = // returns a new set obtained by applying f to each element of s // and forming a set of the results // e.g. if s = {1, 2, 3} and f = (x>x*x) the map(s,f) = {1, 4, 9} def map(s: Set, f: Int => Int): Set = def toString(s: Set): String = { val xs = for (i <- -bound to bound if contains(s, i)) yield i xs.mkString("{", ",", "}") } def printSet(s: Set) { println(toString(s)) } }