Implemented the functions of the exercise

This commit is contained in:
beauvill 2019-09-26 23:37:12 +02:00
parent 4644cfb059
commit c3413d7418

View File

@ -18,32 +18,30 @@ trait FunSets extends FunSetsInterface {
/** /**
* Returns the set of the one given element. * 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, * Returns the union of the two given sets,
* the sets of all elements that are in either `s` or `t`. * 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, * Returns the intersection of the two given sets,
* the set of all elements that are both in `s` and `t`. * 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, * Returns the difference of the two given sets,
* the set of all elements of `s` that are not in `t`. * 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. * 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. * 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 forall(s: FunSet, p: Int => Boolean): Boolean =
def iter(a: Int): Boolean = def iter(a: Int): Boolean =
if ??? then if a == bound + 1 then
??? true
else if ??? then else if !p(a) && s(a) then
??? false
else else
iter(???) iter(a+1)
iter(???) iter(-bound)
/** /**
* Returns whether there exists a bounded integer within `s` * Returns whether there exists a bounded integer within `s`
* that satisfies `p`. * 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`. * 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 * Displays the contents of a set