Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CS-210 Functional programming
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
lamp
CS-210 Functional programming
Commits
b8af1bee
Commit
b8af1bee
authored
3 years ago
by
Matt Bovel
Browse files
Options
Downloads
Patches
Plain Diff
Update solutions 9 and 10
parent
acfe913a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
exercises/solution-09.md
+14
-30
14 additions, 30 deletions
exercises/solution-09.md
exercises/solution-10.md
+32
-0
32 additions, 0 deletions
exercises/solution-10.md
with
46 additions
and
30 deletions
exercises/solution-09.md
+
14
−
30
View file @
b8af1bee
...
...
@@ -2,18 +2,18 @@
## Question 1
```
scala
mdoc
```
scala
def
groupBy
[
T
,
S
](
f
:
T
=>
S
)(
xs
:
List
[
T
])
:
Map
[
S
,
List
[
T
]]
=
var
result
= Map[S, List[T]]()
var
acc
=
Map
[
S
,
List
[
T
]]()
xs
.
reverse
.
foreach
(
el
=>
val
key
=
f
(
el
)
val prevValue =
result
.getOrElse(key, List())
result = result
.updated(key, el :: prevValue)
val
prevValue
=
acc
.
getOrElse
(
key
,
List
())
acc
=
acc
.
updated
(
key
,
el
::
prevValue
)
)
result
acc
```
```
scala
mdoc
```
scala
def
groupBy2
[
T
,
S
](
f
:
T
=>
S
)(
xs
:
List
[
T
])
:
Map
[
S
,
List
[
T
]]
=
xs
.
foldRight
(
Map
[
S
,
List
[
T
]]())((
el
:
T
,
acc
:
Map
[
S
,
List
[
T
]])
=>
val
key
=
f
(
el
)
...
...
@@ -24,20 +24,21 @@ def groupBy2[T, S](f: T => S)(xs: List[T]): Map[S, List[T]] =
Example run:
```
scala
mdoc
```
scala
groupBy
[
Int
,
Int
](
_
%
3
)((
0
to
10
).
toList
)
// res0: Map[Int, List[Int]] = Map(
// 1 -> List(1, 4, 7, 10),
// 0 -> List(0, 3, 6, 9),
// 2 -> List(2, 5, 8)
// )
```
## Question 2
### Question 2.1
```
scala mdoc:invisible
trait Logger:
def log(message: String): Unit
```
```
scala
mdoc
```
scala
class
LoggerBuffered
extends
Logger
:
private
var
output:
String
=
""
def
log
(
message
:
String
)
:
Unit
=
output
=
output
+
message
+
"\n"
...
...
@@ -46,25 +47,8 @@ class LoggerBuffered extends Logger:
### Question 2.2
```
scala mdoc:invisible
enum Expr:
case Var(name: String)
case Op(name: String, args: List[Expr])
import Expr._
final case class UnknownVarException(name: String) extends Exception
final case class UnknownOpException(name: String) extends Exception
def eval(e: Expr, context: Map[Var, Int]): Int = e match
case sym: Var if context.contains(sym) => context(sym)
case sym: Var => throw UnknownVarException(sym.name)
case Op("*", args) => args.map(eval(_, context)).foldLeft(1)(_ * _)
case Op("+", args) => args.map(eval(_, context)).foldLeft(0)(_ + _)
case Op(name, _) => throw UnknownOpException(name)
```
```
scala
mdoc
```
scala
def
evalTracing
(
e
:
Expr
,
context
:
Map
[
Var
,
Int
])(
using
logger
:
Logger
)
:
Int
=
e
match
case
sym
:
Var
if
context.contains
(
sym
)
=>
val
value
=
context
(
sym
)
...
...
This diff is collapsed.
Click to expand it.
exercises/solution-10.md
0 → 100644
+
32
−
0
View file @
b8af1bee
# Exercise Session 9, Solutions
## Question 1
```
scala
"gcd"
->
Name
(
"Not provided (part of the lab)"
)
```
## Question 2
```
scala
"map"
->
Fun
(
"ls"
,
Fun
(
"f"
,
Match
(
Name
(
"ls"
),
EmptyList
,
"x"
,
"xs"
,
Cons
(
Call
(
Name
(
"f"
),
Name
(
"x"
)),
Call
(
Call
(
Name
(
"map"
),
Name
(
"xs"
)),
Name
(
"f"
))))))
```
```
scala
"foldLeft"
->
Name
(
"Not provided (part of the lab)"
)
```
## Question 3
```
scala
"CAS"
->
Fun
(
"idx"
,
Fun
(
"old"
,
Fun
(
"new"
,
IfNonzero
(
minus
(
Read
(
Name
(
"idx"
)),
Name
(
"old"
)),
Constant
(
0
),
Write
(
Name
(
"idx"
),
Name
(
"new"
),
Constant
(
1
)))))),
```
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