Question: Using Haskell, make the exec and eval functions below to work with in- and out parameters: In-parameters: Variables that must be in scope (in the
Using Haskell, make the exec and eval functions below to work with in- and out parameters:
In-parameters: Variables that must be in scope (in the environment) when the procedure is executed. These are variables that the procedure may assume are defined while inside the procedure body, since they should be in the environment from before. Out-parameters: Variables that are in the environment after the procedure body has been executed. These are the variables that the procedure will assign a value to during the execution, and can be seen as the result of the procedure.
exec :: Stmt -> Environment -> Environment exec Skip env = env exec (Assign n e) env = (n, eval e env) : env exec (Seq s1 s2) env = (exec s2 . exec s1) env exec (If e s1 s2) env = case eval e env of VBool True -> exec s1 env VBool False -> exec s2 env exec (While e s) env = exec (If e (Seq s (While e s)) Skip) env eval :: Expr -> Environment -> Val eval (IntConst i) _ = VInt i eval (BoolConst i) _ = VBool i eval (Var n) env = case lookup n env of Just v -> v _ -> error $ "unknown_variable_" ++ n eval (Unary o e) env = uop o (eval e env) eval (Binary o e1 e2) env = bop o (eval e1 env) (eval e2 env)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
