Skip to content
Snippets Groups Projects
Commit b8af1bee authored by Matt Bovel's avatar Matt Bovel
Browse files

Update solutions 9 and 10

parent acfe913a
Branches
No related tags found
No related merge requests found
......@@ -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)
......
# 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)))))),
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment