Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS-210 Functional Programming 2019
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taha Zakariya
CS-210 Functional Programming 2019
Commits
281e5348
Commit
281e5348
authored
5 years ago
by
Timothée Floure
Browse files
Options
Downloads
Patches
Plain Diff
Fix scala syntax highlighting in homework2 instructions
parent
d731fc41
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
week2/00-homework2.md
+33
-15
33 additions, 15 deletions
week2/00-homework2.md
with
33 additions
and
15 deletions
week2/00-homework2.md
+
33
−
15
View file @
281e5348
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment