module EvalIntExpr ( evalIntExpr, testEvalIntExpr, IntExpr(..) ) where
import Test.QuickCheck
-- an IntExpr is an expression-tree over integers
data IntExpr =
Leaf Int |
Add IntExpr IntExpr |
Sub IntExpr IntExpr |
Mul IntExpr IntExpr |
Uminus IntExpr
deriving (Eq, Show)
-- evalIntExpr returns the Int result of evaluating the IntExpr
-- expression-tree given by its argument.
evalIntExpr :: IntExpr -> Int
evalIntExpr _ = error "TODO"
testEvalIntExpr = do
print "******* test evalIntExpr"
To complete the EvalIntExpr module, you need to implement the evalIntExpr function that evaluates an IntExpr expression tree. Let's implement this function according to the types of expressions it could encounter (i.e., Leaf, Add, Sub, Mul, and Uminus).
Here is a possible implementation for the evalIntExpr function:
This provides a complete implementation for the EvalIntExpr module, addressing both the explicit testing for distinct IntExpr instances and property-based testing covering essential algebraic properties like commutativity and associativity of addition and multiplication, as well as the distribute law.