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
Ugo Jean Mario Balducci
CS-210 Functional programming 2021
Commits
ffe0248b
Commit
ffe0248b
authored
3 years ago
by
Matt Bovel
Committed by
Olivier Blanvillain
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add exercises/solution-03.md
parent
bfeb77e0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
exercises/solution-03.md
+94
-0
94 additions, 0 deletions
exercises/solution-03.md
with
94 additions
and
0 deletions
exercises/solution-03.md
0 → 100644
+
94
−
0
View file @
ffe0248b
# Exercise Session 3, Solutions
## Question 1
```
scala
def
eq
[
T
](
a
:
T
,
b
:
T
,
leq
:
(
T
,
T
)
=>
Boolean
)
=
leq
(
a
,
b
)
&&
leq
(
b
,
a
)
def
le
[
T
](
a
:
T
,
b
:
T
,
leq
:
(
T
,
T
)
=>
Boolean
)
=
leq
(
a
,
b
)
&&
!
leq
(
b
,
a
)
```
## Question 2
Using pattern matching:
```
scala
trait
Tree
[
T
]
:
def
size
:
Int
=
this
match
case
EmptyTree
(
_
)
=>
0
case
Node
(
left
,
_
,
right
,
_
)
=>
left
.
size
+
1
+
right
.
size
case
class
EmptyTree
[
T
](
leq
:
(
T
,
T
)
=>
Boolean
)
extends
Tree
[
T
]
case
class
Node
[
T
](
left
:
Tree
[
T
],
elem
:
T
,
right
:
Tree
[
T
],
leq
:
(
T
,
T
)
=>
Boolean
)
extends
Tree
[
T
]
```
Defining a method per subclass:
```
scala
trait
Tree
[
T
]
:
def
size
:
Int
case
class
EmptyTree
[
T
](
leq
:
(
T
,
T
)
=>
Boolean
)
extends
Tree
[
T
]
:
def
size:
Int
=
0
case
class
Node
[
T
](
left
:
Tree
[
T
],
elem
:
T
,
right
:
Tree
[
T
],
leq
:
(
T
,
T
)
=>
Boolean
)
extends
Tree
[
T
]
:
def
size:
Int
=
left
.
size
+
1
+
right
.
size
```
## Question 3
```
scala
def
add
(
newElem
:
T
)
:
Tree
[
T
]
=
this
match
case
EmptyTree
(
leq
)
=>
Node
(
EmptyTree
(
leq
),
newElem
,
EmptyTree
(
leq
),
leq
)
case
Node
(
left
,
elem
,
right
,
leq
)
=>
if
leq
(
newElem
,
elem
)
then
if
leq
(
elem
,
newElem
)
then
this
else
Node
(
left
.
add
(
newElem
),
elem
,
right
,
leq
)
else
Node
(
left
,
elem
,
right
.
add
(
newElem
),
leq
)
```
## Question 4
```
scala
def
toList
:
List
[
T
]
=
def
toListAcc
(
tree
:
Tree
[
T
],
acc
:
List
[
T
])
:
List
[
T
]
=
tree
match
case
EmptyTree
(
_
)
=>
acc
case
Node
(
left
,
elem
,
right
,
_
)
=>
toListAcc
(
left
,
elem
::
toListAcc
(
right
,
acc
))
toListAcc
(
this
,
Nil
)
```
Without using an accumulator:
```
scala
def
toListNoAcc
:
List
[
T
]
=
this
match
case
EmptyTree
(
_
)
=>
Nil
case
Node
(
left
,
elem
,
right
,
_
)
=>
(
left
.
toList
:+
elem
)
++
right
.
toList
// shortand for left.toList.appended(elem).concat(right.toList)
```
## Question 5
```
scala
def
sortedList
[
T
](
leq
:
(
T
,
T
)
=>
Boolean
,
ls
:
List
[
T
])
:
List
[
T
]
=
def
buildTree
(
elems
:
List
[
T
],
acc
:
Tree
[
T
])
:
Tree
[
T
]
=
elems
match
case
Nil
=>
acc
case
elem
::
rest
=>
buildTree
(
rest
,
acc
.
add
(
elem
))
buildTree
(
ls
,
EmptyTree
(
leq
)).
toList
```
## Question 6
```
scala
enum
Tree
[
T
]
:
def
size:
Int
=
/* ... */
def
add
(
newElem
:
T
)
:
Tree
[
T
]
=
/* ... */
def
toList
:
List
[
T
]
=
/* ... */
case
EmptyTree
(
leq
:
(
T
,
T
)
=>
Boolean
)
case
Node
(
left
:
Tree
[
T
],
elem
:
T
,
right
:
Tree
[
T
],
leq
:
(
T
,
T
)
=>
Boolean
)
```
\ No newline at end of file
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