Skip to content
Snippets Groups Projects
Commit 281e5348 authored by Timothée Floure's avatar Timothée Floure
Browse files

Fix scala syntax highlighting in homework2 instructions

parent d731fc41
No related branches found
No related tags found
No related merge requests found
......@@ -38,12 +38,16 @@ the characteristic function `(x: Int) => x < 0`.
Therefore, we choose to represent a set by its characteristic function
and define a type alias for this representation:
type Set = Int => Boolean
```scala
type Set = Int => Boolean
```
Using this representation, we define a function that tests for the
presence of a value in a set:
def contains(s: Set, elem: Int): Boolean = s(elem)
```scala
def contains(s: Set, elem: Int): Boolean = s(elem)
```
## 2.1 Basic Functions on Sets
......@@ -53,7 +57,9 @@ Let's start by implementing basic functions on sets.
value: the set represents the set of the one given element. Its
signature is as follows:
def singletonSet(elem: Int): Set
```scala
def singletonSet(elem: Int): Set
```
Now that we have a way to create singleton sets, we want to define
a function that allow us to build bigger sets from smaller ones.
......@@ -64,16 +70,20 @@ Let's start by implementing basic functions on sets.
elements of the set `s` that are not in the set `t`. These
functions have the following signatures:
def union(s: Set, t: Set): Set
def intersect(s: Set, t: Set): Set
def diff(s: Set, t: Set): Set
```scala
def union(s: Set, t: Set): Set
def intersect(s: Set, t: Set): Set
def diff(s: Set, t: Set): Set
```
3. Define the function `filter` which selects only the elements of a
set that are accepted by a given predicate `p`. The filtered
elements are returned as a new set. The signature of `filter` is as
follows:
def filter(s: Set, p: Int => Boolean): Set
```scala
def filter(s: Set, p: Int => Boolean): Set
```
## 2.2 Queries and Transformations on Sets
......@@ -82,7 +92,9 @@ elements of a set. The first function tests whether a given predicate
is true for all elements of the set. This `forall` function has the
following signature:
def forall(s: Set, p: Int => Boolean): Boolean
```scala
def forall(s: Set, p: Int => Boolean): Boolean
```
Note that there is no direct way to find which elements are in a
set. `contains` only allows to know whether a given element is
......@@ -96,27 +108,33 @@ in order to limit the search space.
function nested in `forall`. Its structure is as follows (replace
the `???`):
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (???) ???
```scala
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (???) ???
else if (???) ???
else iter(???)
}
iter(???)
}
iter(???)
}
```
2. Using `forall`, implement a function `exists` which tests whether a
set contains at least one element for which the given predicate is
true. Note that the functions `forall` and `exists` behave like the
universal and existential quantifiers of first-order logic.
def exists(s: Set, p: Int => Boolean): Boolean
```scala
def exists(s: Set, p: Int => Boolean): Boolean
```
3. Finally, write a function `map` which transforms a given set into
another one by applying to each of its elements the given
function. `map` has the following signature:
def map(s: Set, f: Int => Int): Set
```scala
def map(s: Set, f: Int => Int): Set
```
## 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