Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CS-210 Functional programming 2021
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
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
Chau Ying Kot
CS-210 Functional programming 2021
Commits
d0e2bd48
Commit
d0e2bd48
authored
3 years ago
by
Matt Bovel
Committed by
Olivier Blanvillain
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add exercises/solution-02.md
parent
816cf643
Branches
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
exercises/solution-02.md
+100
-0
100 additions, 0 deletions
exercises/solution-02.md
with
100 additions
and
0 deletions
exercises/solution-02.md
0 → 100644
+
100
−
0
View file @
d0e2bd48
# Exercise Session 2, Solutions
## Question 1
```
scala
def
flip
(
f
:
(
Double
,
Int
)
=>
Boolean
)
:
(
Int
,
Double
)
=>
Boolean
=
(
x1
:
Int
,
x2
:
Double
)
=>
f
(
x2
,
x1
)
```
## Question 2
### Question 2.1
```
scala
val
id
:
Int
=>
Int
=
(
x
:
Int
)
=>
x
```
### Question 2.2
```
scala
def
compose
(
f
:
Int
=>
Int
,
g
:
Int
=>
Int
)
:
Int
=>
Int
=
(
x
:
Int
)
=>
f
(
g
(
x
))
```
`compose(id, f)(k)`
evaluates to
`f(k)`
, as does
`compose(f, id)(k)`
.
More generally (see
[
"Identity Function" on Wikipedia
](
https://en.wikipedia.org/wiki/Identity_function#Algebraic_properties
)
):
> If f: M → N is any function, then we have f ∘ id<sub>M</sub> = f = id<sub>N</sub> ∘ f.
### Question 2.3
```
scala
def
repeated
(
f
:
Int
=>
Int
,
n
:
Int
)
:
Int
=>
Int
=
if
n
==
0
then
identity
else
compose
(
f
,
repeated
(
f
,
n
-
1
))
```
## Question 3
### Question 3.1
```
scala
def
curry2
(
f
:
(
Double
,
Int
)
=>
Boolean
)
:
Double
=>
(
Int
=>
Boolean
)
=
(
x1
:
Double
)
=>
(
x2
:
Int
)
=>
f
(
x1
,
x2
)
```
### Question 3.2
```
scala
def
uncurry2
(
f
:
Double
=>
Int
=>
Boolean
)
:
(
Double
,
Int
)
=>
Boolean
=
(
x1
:
Double
,
x2
:
Int
)
=>
f
(
x1
)(
x2
)
```
## Question 4
```
scala
import
scala.annotation.tailrec
def
fixedPoint
(
f
:
Int
=>
Int
)
:
Int
=>
Int
=
@tailrec
def
rec
(
x
:
Int
)
:
Int
=
val
y
=
f
(
x
)
if
x
==
y
then
x
else
rec
(
y
)
rec
```
-
`fixedPoint(x => x/2)(4)`
returns
`0`
.
-
`fixedPoint(id)(123456)`
returns
`123456`
.
-
`fixedPoint(x => x + 1)(0)`
does not terminate.
-
`fixedPoint(x => if (x % 10 == 0) x else x + 1)(35)`
returns
`40`
.
-
`fixedPoint(x => x / 2 + 5)(20)`
returns
`10`
.
## Question 5
### Question 5.1
```
scala
def
sum
(
a
:
Int
,
b
:
Int
)(
f
:
Int
=>
Int
)
:
Int
=
@tailrec
def
loop
(
i
:
Int
,
acc
:
Int
)
:
Int
=
if
i
>
b
then
acc
else
loop
(
i
+
1
,
acc
+
f
(
i
))
loop
(
a
,
0
)
```
### Question 5.2
```
scala
def
quadratic
(
c
:
Int
)
:
Int
=>
Int
=
(
x
:
Int
)
=>
(
x
-
c
)
*
(
x
-
c
)
```
### Question 5.3
```
scala
def
quad3Integrate
(
a
:
Int
,
b
:
Int
)
:
Int
=
sum
(
a
,
b
-
1
)(
quadratic
(
3
))
```
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