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 evalIdExpr (Sub (Leaf 33) (Leaf 22)) [] == 11 quickCheck evalIdExpr (Mul (Leaf 31) (Leaf 4)) [] == 124 quickCheck evalIdExpr (Uminus (Leaf 33)) [] == (-33) quickCheck evalIdExpr (Mul (Leaf 4) (Sub (Add (Leaf 3) (Uminus (Leaf 33))) (Leaf 20))) [] == (-200)
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.