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