In class we saw how to evaluate an expression given a map from identifiers to their...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
In class we saw how to evaluate an expression given a map from identifiers to their values. def evalExpr (e: Expr, env: Hap[String, Double]): Double = ... In this assignment, you are asked to handle assignment statement. The Idea here is that the assignment statement takes an environment and modifies it. def evalstatement(s: Statement, env: Rap[String, Double]): Hap[String, Double] =... As an example consider the map env Hap("x"-> 20, "y" -> 35) The assignment statement z:-x+y Assignment("z", Plus (var("x"), Var("Y"))) Should return a map Hap("x"-> 20, "y" -> 35, "2" -> 55) The strategy should be as follows: (a) evaluate the expression in the RHS of the assignment statement, (D) add the entry that maps the declared variable to the new value and (c) return this map. https://docs.scala-lang.org/overviews/collections-2.13/maps.html In []: def evalExpr(e: Expr, env: Rap[String, Double]): Double = { def binFun(el: Expir, e2: Expr, op: (Double, Double) => Double): Double = { val vi evalExpr(el, env) } e match { case Plus (el, e2) => binFun(el, ez, _ + _ ), case Minus(el, e2) => binFun(el, e2, _) } val v2 = evalExpr(e2, env) op (v1, v2) } } case Star (el, e2) => binFun(el, e2, __) case Ver(v) => { if (env.contains(v)){ } else { throw new IllegalArgumentException(s"$v undefined variable.") } env(v) } case Const(f) => f def evalstatement(s: Statement, env: Rap[String, Double]): Nap[String, Double] =smatch { case Assignment(varliame, rhsExpr) => { // YOUR CODE HERE 222 In []: //BEGIN TEST def testlap (m1: Hap[String, Double], varname: String, testval: Double) = { m1.contains (varnamne) && m1(varname) == testval } val ml = Hap[String, Double]() val v1 = Assignment(x", Const(2.0)) val m2 = evalstatement(v1, m1) assert (testlap (m2, "x", 2.0), "TEST 1 PASSED!") passed (10) //END TEST val v2 = Assignment("y", Plus (Var("x"). Const(4.0))) val m3 = evelstatenment(v2, m2) assert (testisp(m3, "x", 2.0), "TEST 2.1 PASSED!") assert(testisp(m3, "y", 6.0), "TEST 2.2 PASSED!") val v3 = Assignment("z", Star(var(""), Var("x"))) val m4 = evalstatement(v3, m3) assert (testhop (n4, "x", 2.0), "TEST 3.1 PASSED!") assert(test ap(m4, "y", 6.0), "TEST 3.2 PASSED!") assert (testop(n4, "z", 12.0), "TEST 3.3 PASSED!") In class we saw how to evaluate an expression given a map from identifiers to their values. def evalExpr (e: Expr, env: Hap[String, Double]): Double = ... In this assignment, you are asked to handle assignment statement. The Idea here is that the assignment statement takes an environment and modifies it. def evalstatement(s: Statement, env: Rap[String, Double]): Hap[String, Double] =... As an example consider the map env Hap("x"-> 20, "y" -> 35) The assignment statement z:-x+y Assignment("z", Plus (var("x"), Var("Y"))) Should return a map Hap("x"-> 20, "y" -> 35, "2" -> 55) The strategy should be as follows: (a) evaluate the expression in the RHS of the assignment statement, (D) add the entry that maps the declared variable to the new value and (c) return this map. https://docs.scala-lang.org/overviews/collections-2.13/maps.html In []: def evalExpr(e: Expr, env: Rap[String, Double]): Double = { def binFun(el: Expir, e2: Expr, op: (Double, Double) => Double): Double = { val vi evalExpr(el, env) } e match { case Plus (el, e2) => binFun(el, ez, _ + _ ), case Minus(el, e2) => binFun(el, e2, _) } val v2 = evalExpr(e2, env) op (v1, v2) } } case Star (el, e2) => binFun(el, e2, __) case Ver(v) => { if (env.contains(v)){ } else { throw new IllegalArgumentException(s"$v undefined variable.") } env(v) } case Const(f) => f def evalstatement(s: Statement, env: Rap[String, Double]): Nap[String, Double] =smatch { case Assignment(varliame, rhsExpr) => { // YOUR CODE HERE 222 In []: //BEGIN TEST def testlap (m1: Hap[String, Double], varname: String, testval: Double) = { m1.contains (varnamne) && m1(varname) == testval } val ml = Hap[String, Double]() val v1 = Assignment(x", Const(2.0)) val m2 = evalstatement(v1, m1) assert (testlap (m2, "x", 2.0), "TEST 1 PASSED!") passed (10) //END TEST val v2 = Assignment("y", Plus (Var("x"). Const(4.0))) val m3 = evelstatenment(v2, m2) assert (testisp(m3, "x", 2.0), "TEST 2.1 PASSED!") assert(testisp(m3, "y", 6.0), "TEST 2.2 PASSED!") val v3 = Assignment("z", Star(var(""), Var("x"))) val m4 = evalstatement(v3, m3) assert (testhop (n4, "x", 2.0), "TEST 3.1 PASSED!") assert(test ap(m4, "y", 6.0), "TEST 3.2 PASSED!") assert (testop(n4, "z", 12.0), "TEST 3.3 PASSED!")
Expert Answer:
Answer rating: 100% (QA)
Heres the implementation for the evalstatement function scala def evalstatemen... View the full answer
Related Book For
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch
Posted Date:
Students also viewed these programming questions
-
Freshly painted aluminum rods, 0.02 m in diameter, are withdrawn from a drying oven at 150 C and cooled in a 3 m/s cross flow of air at 23 C. How long will it take to cool them to 40 C so they can be...
-
KYC's stock price can go up by 15 percent every year, or down by 10 percent. Both outcomes are equally likely. The risk free rate is 5 percent, and the current stock price of KYC is 100. (a) Price a...
-
In each part you are given the consequence of a SQL inquiry along with a potentially wrong end drawn from this outcome. For each situation your errand is to contend possibly in support of the end....
-
The stator of a 220-V, 60-Hz induction motor has 12-turn coils. You wish to re-wound it for working at 380-V, 50-Hz. While maintaining the flux same as of original motor, calculate the number of...
-
A man of mass m1 stands on the edge of a horizontal uniform disc of mass m2 and radius R which is capable of rotating freely about a stationary vertical axis passing through its centre. At a certain...
-
If the future value of an ordinary, 6-year annuity is $9,100 and interest rates are 9.5 percent, what's the future value of the same annuity due? (Round your answer to 2 decimal places.)
-
Table B. 11 presents data on the quality of Pinot Noir wine. a. Build a regression model relating quality \(y\) to flavor \(x_{4}\) that incorporates the region information given in the last column....
-
Glass, Inc., began 2012 with $83,000 in cash. During 2012, Glass earned net income of $430,000, and adjustments to reconcile net income to net cash provided by operations totaled $75,000, a positive...
-
Heather Smith Cosmetics (HSC) manufactures a variety of products and is organized into three divisions (investment centers): soap products, skin lotions, and hair products. Information about the most...
-
The G. Saussy Manufacturing company is putting out four new electronic components. Each of Saussy's four plants has the capacity to add one more product to its current line of electronic parts. The...
-
30 3. Lab 1: Physical and Chemical Changes Name Lab Partner's Name Purpose: Interpretation of Results: Identify by number which experiments resulted in physical change and which experiments resulted...
-
What essential role does the cost-coding system play?
-
Select a specialty discipline of interest to you for the building example. Develop a general precedence-based plan for that discipline.
-
What are the two important objectives of the project cost-control system?
-
What critical elements are important to support the planning process?
-
What are impediments to planning, both at the beginning of a project and throughout the project?
-
describes the ISATAP, 6to4 and Teredo transition technologiesfor IPv6. In your own words, define one (in a way thatdifferentiates it from the others). Then,do one of the following: Describe a...
-
2. In the circuit given in Figure 2, i,(t) = 5.67cos(5t)A and v (t) = 70.71 cos(5t 60) V a) Find the equivalent load impedance. State whether the load is inductive or capacitive. b) Calculate the...
-
Consider a class Characteristic that will be used in an online dating service to assess how compatible two people are. Its attributes are descriptiona string that identifies the characteristic...
-
Write a program that reads a 4-bit binary number from the keyboard as a string and then converts it into decimal. For example, if the input is 1100, the output should be 12. Break the string into...
-
Write an algorithm that finds the maximum value in a list of values.
-
OBB Personenverkehr (OBB), the Austrian state-owned railway, carries about 235 million passengers a year on routes within wholly owned by OBB Holding Group, which in turn, is wholly owned by the...
-
Jose Medellin participated in the rape and murder of two teenage girls in Houston, Texas. He was arrested and read his Miranda rights, and he confessed. At the time, he was not informed that he could...
-
The United States brought a lawsuit requesting forfeiture of a \($38.5\) million jet purchased by Teodoro Nguema Obiang Mangue (Nguema) because the government believed the jet had been purchased with...
Study smarter with the SolutionInn App