Skip to content

Instantly share code, notes, and snippets.

@suminb
Created April 2, 2016 17:41
Show Gist options
  • Save suminb/8b6d14293a1c652ef08ef0e1a4a36dac to your computer and use it in GitHub Desktop.
Save suminb/8b6d14293a1c652ef08ef0e1a4a36dac to your computer and use it in GitHub Desktop.
My first Scala program
object Main {
abstract class Tree
case class Sum(l: Tree, r: Tree) extends Tree
case class Var(n: String) extends Tree
case class Const(v: Int) extends Tree
type Environment = String => Int
def eval(t: Tree, env: Environment): Int = t match {
case Sum(l, r) => eval(l, env) + eval(r, env)
case Var(n) => env(n)
case Const(v) => v
}
def main(args: Array[String]) {
val exp: Tree = Sum(Sum(Var("x"), Var("x")), Sum(Const(4), Var("y")))
val env: Environment = {
case "x" => 4
case "y" => 7
}
println(eval(exp, env))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment