case class ClassName(field1 : Type1, field2 : Type2, ...) extends Superclass
class SimpleTree case class Leaf(value : Int) extends SimpleTree case class Node(left : SimpleTree, right : SimpleTree) extends SimpleTree
ClassName(arg1, arg2, ...)
Node(Node(Leaf(3), Leaf(2)), Node(Leaf(7), Node(Leaf(6), Leaf(8))))

selectorExpr match {
case pattern => expr
...
case pattern => expr
}
tree match {
case Node(l, r) => expr1
case Leaf(v) => expr2
}
l, r, v are bound to the
values in the case class instances; you can use them in the expressions.
def sum(t : SimpleTree) : Int = t match {
case Node(l, r) => sum(l) + sum(r)
case Leaf(v) => v
}
case _
Some, NoneList.find returns Option[A].
val result = lst.find(_ % 2 == 0)
result match {
case Some(x) => println(x)
case None => println("No match")
}
null if there is no match? null might be a valid value in the collectionInt can't be null3 + 4 * x

class ExprTree case class Number(value : Int) extends ExprTree case class Variable(name : String) extends ExprTree case class Operator(left : ExprTree, right : ExprTree, f: (Int, Int) => Int) extends ExprTree
Operator(Number(3), Operator(Number(4), Variable("x"), _ * _), _ + _)
3 + 4 * x into Scala
valueMap[String, Int]Map(key1 -> value1, key2 -> value2, ...) yields
map with given key/value pairsmap + (key -> value) yields map with new key/value
pairmap(key) yields value of key (must exist)map.get(key) yields Option, either
Some(value)or Nonedef eval(expr : ExprTree, symbols : Map[String, Int]) : Int =
expr match {
case Number(num) => num
case Variable(name) => symbols(name)
case Operator(left, right, f) => f(eval(left, symbols), eval(right, symbols))
}
Lab9)lab9)Main)main method
package lab9
object Main {
def main(args: Array[String]) {
println("Hello") }
}
object = class with only one instance, similar to
static in Java
Add the classes ExprTree, Number,
Variable, Operator to the Main.scala file. Add
the eval method to the Main object. In the
main method, construct the tree

and evaluate it with a symbol table in which x is 5. What is
the code of your Main.scala file?
If you didn't install Eclipse and the Scala plugin before class, then write
the file with a text editor (in a lab9 subdirectory to match the
package name) and compile/run with
scalac lab9/Main.scala scala lab9.Main
Now we want to add variable definitions (which we will write as val x
= expr in the next lecture). For now, we will assume that they are
already parsed into instances of
case class Definition(name : String, expr : ExprTree)
For example, a definition val x = 2 would be a
Definition("x", Number(2)). A definition changes the symbol table.
In a functional setting, that means we need to return a new table that contains
all bindings in the old table and the new binding. For example,
val def1 = Definition("x", Number(2))
val def2 = Definition("y", Variable("x"))
val sym0 = Map[String, Int]()
val sym1 = eval(def1, sym0) // "x" -> 2
val sym2 = eval(def2, sym1) // "x" -> 2, "y" -> 2
Implement this eval method . What is the code of
your method? (You can get the fields of a definition as defi.name,
defi.expr.)