diff --git a/week2/00-homework2.md b/week2/00-homework2.md index d478b2fa4bcdfb338517765992120c2097f1929b..3e628f85c102dee958ae9c98d2c442b09fb0b3f9 100644 --- a/week2/00-homework2.md +++ b/week2/00-homework2.md @@ -25,9 +25,9 @@ You can always refer to: We will work with sets of integers. As an example to motivate our representation, how would you represent the set of -all negative integers? You cannot list them all... one way would be so +all negative integers? You cannot list them all... one way would be to say: if you give me an integer, I can tell you whether it's in the set -or not: for `3`, I say 'no'; for `-1`, I say `yes`. +or not: for `3`, I would say `no`; for `-1`, I would say `yes`. Mathematically, we call the function which takes an integer as argument and which returns a boolean indicating whether the given @@ -39,14 +39,14 @@ Therefore, we choose to represent a set by its characteristic function and define a type alias for this representation: ```scala -type Set = Int => Boolean +type FunSet = Int => Boolean ``` Using this representation, we define a function that tests for the presence of a value in a set: ```scala -def contains(s: Set, elem: Int): Boolean = s(elem) +def contains(s: FunSet, elem: Int): Boolean = s(elem) ``` ## 2.1 Basic Functions on Sets @@ -58,7 +58,7 @@ Let's start by implementing basic functions on sets. signature is as follows: ```scala - def singletonSet(elem: Int): Set + def singletonSet(elem: Int): FunSet ``` Now that we have a way to create singleton sets, we want to define @@ -71,9 +71,9 @@ Let's start by implementing basic functions on sets. functions have the following signatures: ```scala - def union(s: Set, t: Set): Set - def intersect(s: Set, t: Set): Set - def diff(s: Set, t: Set): Set + def union(s: FunSet, t: FunSet): FunSet + def intersect(s: FunSet, t: FunSet): FunSet + def diff(s: FunSet, t: FunSet): FunSet ``` 3. Define the function `filter` which selects only the elements of a @@ -82,7 +82,7 @@ Let's start by implementing basic functions on sets. follows: ```scala - def filter(s: Set, p: Int => Boolean): Set + def filter(s: FunSet, p: Int => Boolean): FunSet ``` ## 2.2 Queries and Transformations on Sets @@ -93,7 +93,7 @@ is true for all elements of the set. This `forall` function has the following signature: ```scala -def forall(s: Set, p: Int => Boolean): Boolean +def forall(s: FunSet, p: Int => Boolean): Boolean ``` Note that there is no direct way to find which elements are in a @@ -109,14 +109,15 @@ in order to limit the search space. the `???`): ```scala - def forall(s: Set, p: Int => Boolean): Boolean = { - def iter(a: Int): Boolean = { - if (???) ??? - else if (???) ??? - else iter(???) - } - iter(???) - } + def forall(s: FunSet, p: Int => Boolean): Boolean = + def iter(a: Int): Boolean = + if ??? then + ??? + else if ??? then + ??? + else + iter(???) + iter(???) ``` 2. Using `forall`, implement a function `exists` which tests whether a @@ -125,7 +126,7 @@ in order to limit the search space. universal and existential quantifiers of first-order logic. ```scala - def exists(s: Set, p: Int => Boolean): Boolean + def exists(s: FunSet, p: Int => Boolean): Boolean ``` 3. Finally, write a function `map` which transforms a given set into @@ -133,7 +134,7 @@ in order to limit the search space. function. `map` has the following signature: ```scala - def map(s: Set, f: Int => Int): Set + def map(s: FunSet, f: Int => Int): FunSet ``` ## Extra Hints