Skip to content
Snippets Groups Projects
Commit eda043a4 authored by Guillaume Martres's avatar Guillaume Martres
Browse files

homework2: small fixes

parent 46d06504
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment