module EvalIdExpr ( evalIdExpr, testEvalIdExpr ) where import Test.QuickCheck import…
- User
module EvalIdExpr ( evalIdExpr, testEvalIdExpr ) where
import Test.QuickCheck import Data.List (lookup)
an IdExpr is an expression-tree over integers and string identifiers data IdExpr = Id String | Leaf Int | Add IdExpr IdExpr | Sub IdExpr IdExpr | Mul IdExpr IdExpr | Uminus IdExpr deriving (Eq, Show)
Assoc is a list of pairs mapping identifier strings to their values v. type Assoc v = [ (String, v) ]
evalIdExpr returns evaluation of the IdExpr expression-tree given by its first argument, looking up the values of id's in the Assoc argument. If an id is not found in the Assoc argument, then its value should default to 0. Hint: use Data.List.lookup imported above. evalIdExpr :: IdExpr -> Assoc Int -> Int evalIdExpr _ = error "TODO"
testEvalIdExpr = do print "*** test evalIdExpr" -- unit tests quickCheck evalIdExpr (Leaf 42) [] == 42 quickCheck evalIdExpr (Add (Leaf 33) (Leaf 22)) [] == 55 quickCheck
- Assistant
To define
evalIdExpr, we'll follow the structure of theIdExprtype. We will use pattern matching to handle different cases such asId,Leaf,Add, , , and . We'll use to find the value of an identifier in the association list, defaulting to if the identifier is not found.