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...
-
It is desired to meter a flow of 20C gasoline in a 12-cm-diameter pipe, using a modern venturi nozzle. In order for international standards to be valid (Fig. 6.40), what is the permissible range of...
-
When an order is placed with a company, there is a probability of 0.2 that it is an express order. Estimate the probability that 90 or more of the next 400 orders will be express orders.
-
What is the primary reason power must be added in a turn to maintain airspeed?
-
NorthAm Trucking is a long-haul trucking company serving customers all across the continental United States and parts of Canada and Mexico. At present, all billing activities from preparation to...
-
Discuss the role of process simulation in the design and evaluation of sustainable processes. How can Life Cycle Assessment (LCA) and simulation be combined to optimize environmental impacts and...
-
You have just been hired as a brand manager at Kelsey-White, an American multinational consumer goods company. Recently the firm invested in the development of K-W Vision, a series of systems and...
-
Sarah Brown's Cosmetic Company is concerned with the stock of three type of perfume with the following data: Perfume I Annual demand 150 Product specific order 700 cost per order (Rs.) Unit Cost...
-
Question 1 You are required to complete the given code segment. a) Given a Java class named Validate Bean.java with data members and methods. Complete the given code segment for getter method and...
-
Hafnaoui Company reported pretax net income from continuing operations of $903,500 and taxable income of $712,500. The book-tax difference of $191,000 was due to a $242,000 favorable temporary...
-
How can organizational leaders implement sophisticated empowerment frameworks that transcend traditional hierarchical structures, fostering a sense of autonomy and accountability among employees ?
-
If only trust were easy to gain and keep. Companies today are struggling with the relationship between them and their employees. Why? Talk about trust in Boeing Defense organization and what you...
-
What is reputation management? Use an example to illustrate your answer 2- What information do you need to include in a media buying brief? Use an example that include Radio, Outdoors and Social...
-
if rr = 2000, c = 2000, ER = 6000, DD = 200000, y = 80000, price = 1.5, rr or h = 0.10, by use M1, the Md in Keynes and value of money in pigou are equal (2 Points) a. 1.5.34552 b. 126671 70001 d....
-
On July 1, 2011, Flashlight Corporation sold equipment it had recently purchased to an unaffiliated company for $480,000. The equipment had a book value on Flashlights books of $390,000 and a...
-
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.
-
Colors of fabrics at a clothing store. Determine whether the data are qualitative or quantitative. Explain your reasoning.
-
Weights of bricks at a construction site. Determine whether the data are qualitative or quantitative. Explain your reasoning.
-
Marital statuses mentioned in an employment form. Determine whether the data are qualitative or quantitative. Explain your reasoning.
Study smarter with the SolutionInn App