Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hi mate, working in intelli J following a pdf tute on case classes, just can't work out why the compiler is complaining about where I've done my type definition?
- /**
- * Created by jim on 31/10/2014.
- */
- 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 derive(t: Tree, v: String): Tree = t match {
- case Sum(l, r) => Sum(derive(l, v), derive(r,v))
- case Var(n) if (v == n) => Const(1)
- case _ => Const (0)
- }
- def main(args: Array[String]): Unit = {
- val exp: Tree = Sum(Sum(Var("x"), Var("x")), Sum(Const(7), Var("y")))
- val env: Environment = { case "x" => 5 case "y" => 7 }
- println("Expression: " + exp)
- println("Evaluation with x=5, y=7: " + eval(exp, env))
- println("Derivative relative to x:\n " + derive(exp, "x"))
- println("Derivative relative to y:\n" + derive(exp, "y"))
- }
- Compiler errors are:
- Error:(8, 1) expected class or object definition
- type Environment = String => Int
- ^
- Error:(10, 1) expected class or object definition
- def eval(t: Tree, env: Environment): Int = t match {
- ^
- Error:(16, 1) expected class or object definition
- def derive(t: Tree, v: String): Tree = t match {
- ^
- Error:(22, 1) expected class or object definition
- def main(args: Array[String]): Unit = {
- ^
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement