Created
April 2, 2016 17:41
-
-
Save suminb/8b6d14293a1c652ef08ef0e1a4a36dac to your computer and use it in GitHub Desktop.
My first Scala program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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