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...
-
Discuss the topic "Work Stress Effect on Prenatal Development"
-
Assume that an American firm wants to import Scotch whisky from the UK. Describe how foreign exchange dealers will respond.
-
Why might management be interested in forensic accounting services?
-
Johnson Graphics Company was organized on January 1, 2017, by Cameron Johnson. At the end of the first 6 months of operations, the trial balance contained the accounts shown. Analysis reveals the...
-
#1Break-Even AnalysisThe Junior League of Yadkinville, California, collected recipesfrom members and published a cookbook entitled Food for Everyone.The book will sell for $17 per copy. The chairw 2...
-
McKinney Solvents produces a wide variety of products for the manufacturing industry. The standard mix for producing a single batch of 100 gallons of its biggest-selling product is as follows: There...
-
Lou has a landscaping company. He received a $ 1 0 , 0 0 0 payment for a landscaping job that he completed for the Rose family. How would you record this transaction?
-
Ethel Company buys Article G for P0.80 per unit. At the end of processing in Department 1, Article G split into producing D, E and F. Product D is sold at split-off with no further processing. E and...
-
Lenning Corporation uses the FIFO method in its process costing. The following data pertain to its Assembly Department for August. Work in process, August 1 Units started into production during...
-
Z Company employs a process costing system for its manufacturing operations. All direct materials are added at the beginning of the process and conversion costs are added proportionately. The...
-
You have been hired by a real estate agent to write a program that will advise them if they should or should not show an apartment to a client. The program must ask the user to enter the following...
-
Payout Ratio and Book Value per Share Divac Company has developed a statement of stockholders' equity for the year 2017 as follows: Preferred Stock Paid-In Capital- Preferred Paid-In Common Stock...
-
Hi. I need one paragraph response to this question . Why is it generally better to centrally locate receiving and shipping doors
-
Make an argument that Williams had a right to delay the closing until after August 1.
-
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.
-
Correctly apply the rules for the order of operations to accurately compute the following: \(3 \times 4^{3} \times 7+24 / 6 \times 7^{2}-9 / 3 \times 8\).
-
Determine if \(\frac{8}{14}\) and \(\frac{12}{26}\) are equivalent fractions.
-
Correctly apply the rules for the order of operations to accurately compute the following: \((8-6)^{2} \times 100-\left((48 / 6-3)^{2}-4 \times 7 ight)\).
Study smarter with the SolutionInn App