CS计算机代考程序代写 cache interpreter 6/1/2021 Q3.1 – CS 421 | PrairieLearn

6/1/2021 Q3.1 – CS 421 | PrairieLearn
Quiz 3
Assessment overview
Total points: 10/30
Score: 33%
Q3.1. Evaluating Where Expressions
Problem
We’re going to add a new type of expression to our interpreter. The WhereExp data constructor takes an Exp, and a list of (String, Exp) pairs. Evaluating these involve evaluating the first expression given some symbols that are equal to the expressions they are paired with. You may assume that the expressions bound to variables in the where clause do not cascade i.e. they only depend on other values in the environment that exists when the WhereExp is evaluated.
Given the following code:
Question
Value:
10
data Exp = IntExp Integer
| IntOpExp String Exp Exp
| WhereExp Exp [(String, Exp)]
| VarExp String
deriving(Show,Eq)
type Env = [(String, Val)]
data Val = IntVal Integer
| ExnVal String
deriving(Show, Eq)
intOps :: [(String, Integer -> Integer -> Integer)]
intOps = [(“+”,(+)), (“-“, (-)), (“*”, (*)), (“/”, (div))]
liftIntOp :: (Integer -> Integer -> Integer) -> Val -> Val -> Val
liftIntOp f (IntVal i1) (IntVal i2) = IntVal (f i1 i2)
liftIntOp _ _ _ = ExnVal “not and IntVal!”
insert :: String -> Val -> Env -> Env
insert s v env = (s,v):env
eval :: Exp -> Env -> Val
eval (IntExp i) _ = IntVal i
eval (IntOpExp op e1 e2) env =
let v1 = eval e1 env
v2 = eval e2 env
Just f = lookup op intOps
in liftIntOp f v1 v2
eval (VarExp var) env =
case lookup var env of
Just val -> val
Nothing -> IntVal 0
History:
Add the clause for eval to handle WhereExp. Examples
Your Solution Starter Code
Awarded points:
Previous question Next question
0/10
Report an error in this question
Attached files
No attached files
Attach a file
Attach text
ghci> eval (WhereExp (VarExp “x”) [(“x”,(IntExp 0))]) []
(IntVal 0)
ghci> eval (IntOpExp “*” (VarExp “z”) (WhereExp (IntOpExp “-” (VarExp “x”) (VarExp
“y”)) [(“y”,(IntExp 5))])) [(“x”, (IntVal 12)), (“z”, (IntVal (-4)))]
(IntVal (-28))
Lib.hs
1 eval (WhereExp v e1) env = insert e1 env 2
3
https://www.prairielearn.org/pl/course_instance/128628/instance_question/143014717/?variant_id=38632240 1/3

6/1/2021 Q3.1 – CS 421 | PrairieLearn
Restore original file
Save & Grade Unlimited attempts
Submitted answer 30 incorrect: 0% Submitted at 2021-06-01 23:16:49 (CDT)
Save only
hide
Files
Lib.hs
uploaded
Download
Show preview
Score: 0%
Message
Compilation failed; ‘stack test’ exited with error code 1. Check the code for
infinite recursion or syntax errors.
Output
coding> configure (lib + test)
Configuring coding-0.1.0.0…
coding> build (lib + test)
Preprocessing test suite ‘spec’ for coding-0.1.0.0..
Building test suite ‘spec’ for coding-0.1.0.0..
[1 of 3] Compiling Lib
/grade/run/src/Lib.hs:39:28: error:
* Couldn’t match expected type `Val’ with actual type `Env -> Env’
* Probable cause: `insert’ is applied to too few arguments
In the expression: insert e1 env
In an equation for `eval’: eval (WhereExp v e1) env = insert e1 env
|
39 | eval (WhereExp v e1) env = insert e1 env
| ^^^^^^^^^^^^^
/grade/run/src/Lib.hs:39:35: error:
* Couldn’t match type `(String, Exp)’ with `Char’
Expected type: String
Actual type: [(String, Exp)]
* In the first argument of `insert’, namely `e1′
In the expression: insert e1 env
In an equation for `eval’: eval (WhereExp v e1) env = insert e1 env
|
39 | eval (WhereExp v e1) env = insert e1 env
https://www.prairielearn.org/pl/course_instance/128628/instance_question/143014717/?variant_id=38632240 2/3

6/1/2021 Q3.1 – CS 421 | PrairieLearn
Submitted answer 29 incorrect: 0% Submitted at 2021-06-01 23:15:36 (CDT)
show
Submitted answer 28 incorrect: 0% Submitted at 2021-06-01 23:15:14 (CDT)
show
Show/hide older submissions
| ^^
/grade/run/src/Lib.hs:39:38: error:
* Couldn’t match type `[(String, Val)]’ with `Val’
Expected type: Val
Actual type: Env
* In the second argument of `insert’, namely `env’
In the expression: insert e1 env
In an equation for `eval’: eval (WhereExp v e1) env = insert e1 env
|
39 | eval (WhereExp v e1) env = insert e1 env
| ^^^
— While building package coding-0.1.0.0 (scroll up to its section to see the
error) using:
/home/ag/.stack/setup-exe-cache/x86_64-linux/Cabal-
simple_mPHDZzAJ_3.2.1.0_ghc-8.10.4 –builddir=.stack-work/dist/x86_64-
linux/Cabal-3.2.1.0 build lib:coding test:spec –ghc-options “”
Process exited with code: ExitFailure 1
https://www.prairielearn.org/pl/course_instance/128628/instance_question/143014717/?variant_id=38632240 3/3