diff --git a/src/main/scala/funsets/FunSets.scala b/src/main/scala/funsets/FunSets.scala index 3ea7c4f..111988f 100644 --- a/src/main/scala/funsets/FunSets.scala +++ b/src/main/scala/funsets/FunSets.scala @@ -18,32 +18,30 @@ trait FunSets extends FunSetsInterface { /** * Returns the set of the one given element. */ - def singletonSet(elem: Int): FunSet = ??? - + def singletonSet(elem: Int): FunSet = (e: Int) => e == elem /** * Returns the union of the two given sets, * the sets of all elements that are in either `s` or `t`. */ - def union(s: FunSet, t: FunSet): FunSet = ??? - + def union(s: FunSet, t: FunSet): FunSet = (e: Int) => s(e) || t(e) + /** * Returns the intersection of the two given sets, * the set of all elements that are both in `s` and `t`. */ - def intersect(s: FunSet, t: FunSet): FunSet = ??? + def intersect(s: FunSet, t: FunSet): FunSet = (e: Int) => s(e) && t(e) /** * Returns the difference of the two given sets, * the set of all elements of `s` that are not in `t`. */ - def diff(s: FunSet, t: FunSet): FunSet = ??? + def diff(s: FunSet, t: FunSet): FunSet = (e: Int) => s(e) && !t(e) /** * Returns the subset of `s` for which `p` holds. */ - def filter(s: FunSet, p: Int => Boolean): FunSet = ??? - + def filter(s: FunSet, p: Int => Boolean): FunSet = (e: Int) => s(e) && p(e) /** * The bounds for `forall` and `exists` are +/- 1000. @@ -55,24 +53,24 @@ trait FunSets extends FunSetsInterface { */ def forall(s: FunSet, p: Int => Boolean): Boolean = def iter(a: Int): Boolean = - if ??? then - ??? - else if ??? then - ??? + if a == bound + 1 then + true + else if !p(a) && s(a) then + false else - iter(???) - iter(???) + iter(a+1) + iter(-bound) /** * Returns whether there exists a bounded integer within `s` * that satisfies `p`. */ - def exists(s: FunSet, p: Int => Boolean): Boolean = ??? + def exists(s: FunSet, p: Int => Boolean): Boolean = !forall(s, (e: Int) => !p(e)) /** * Returns a set transformed by applying `f` to each element of `s`. */ - def map(s: FunSet, f: Int => Int): FunSet = ??? + def map(s: FunSet, f: Int => Int): FunSet = (e :Int) => exists(s, (x: Int)=> e == f(x)) /** * Displays the contents of a set