Expressions continued In this assignment we will continue with the Expr class used in…
- User
Expressions continued In this assignment we will continue with the Expr class used in the lecture notes (please take a look at the notes before starting to solve this assignment). We will expand the class with some new operations. The goal of the assignment is to practice recursive traversal of data structures. Programming Style Restrictions In this exercise we practice the use of recursion and immutable data structures. Therefore, you are not allowed to use var declarations, Arrays, or any scala.collection.mutable or java.util classes. The compiler for this exercise will not accept the use of such constructs; if your submitted file contains any such constructs, a compilation error and 0 points will occur. The VS Code environment will help to check this as well. Naturally, the use val declarations and scala.collection.immutable classes is allowed.
package expressions:
import scala.collection.immutable.Map
/**
-
The abstract base class for expressions. / abstract class Expr: / Overriding these operators enable us to construct expressions with infix notation */ def +(other: Expr) = Add(this, other) def -(other: Expr) = Subtract(this, other) def *(other: Expr) = Multiply(this, other) def unary_- = Negate(this)
/**
- Returns the set of variable names appearing in this expression */ final def vars: Set[String] = this match case Var(n) => Set(n) case Num(v) => Set() case Multiply(l, r) => l.vars ++ r.vars case Add(l, r) => l.vars ++ r.vars case Subtract(l, r) => l.vars ++ r.vars case Negate(t) => t.vars end vars
/** The exception returned by "evaluate" when a variable in the expression is not assigned a value. */ class VariableNotAssignedException(message: String) extends java.lang.RuntimeException(message)
/**
- Evaluate the expression in the point p, where p is a map
- associating each variable name in the expression into a Double value. */ final def evaluate(p: Map[String, Double]): Double = this match case Var(n) => p.get(n) match case Some(v) => v case None => throw new VariableNotAssignedException(n) case Num(v) => v case Multiply(l, r) => l.evaluate(p) * r.evaluate(p) case Add(l, r) => l.evaluate(p) + r.evaluate(p) case Subtract(l, r) => l.evaluate(p) - r.evaluate(p) case Negate(t) => -t.evaluate(p) end evaluate
/**
- Similar to evaluate except that not all the variables names must have
- an associated value in p. Therefore, instead of a Double value, a new expression
- is returned such that (i) all the variable names who are associated with a value are
- replaced with that value, while (ii) all the other variable names stay intact.
- For instance, if p maps x to 3.0 but does not mention y, then partialEvaluate(p)
- on 2.0x+yx should return an expression equivalent to 3.0*y+6.0. */ final def partialEvaluate(p: Map[String, Double]): Expr = this match case Var(n) => p.get(n).map(Num(_)).getOrElse(Var(n)) case Num(v) => Num(v) case Multiply(l, r) => Multiply(l.partialEvaluate(p), r.partialEvaluate(p)) case Add(l, r) => Add(l.partialEvaluate(p), r.partialEvaluate(p)) case Subtract(l, r) => Subtract(l.partialEvaluate(p), r.partialEvaluate(p)) case Negate(t) => Negate(t.partialEvaluate(p)) end partialEvaluate
-
- Assistant