
On Sat, Jan 23, 2010 at 6:40 PM, John Moore
Hi I'm trying to get a if statement to work but not sure if this is the right approach, what I want to do in simple english is to evaluate one expression at a time for example. (3+2)+(4+5) I want it to return 5 + (4+5) then 5+9.
data Expression = Val Integer | Add Expression Expression | Subtract Expression Expression | Multiply Expression Expression | Divide Expression Expression deriving Show evalStep :: Expression -> Expression evalStep (Val x)= (Val x) evalStep (Add x y) = do
if x = isDigit then if y isDigit else evalStep x,y
I thinking about using recursion down a tree left then right but that seems very complicated. Any thoughts welcome! John
I renamed your Expression data type to simply E. Saves some typing :-) data E = Val Integer | Add E E | Sub E E | Mul E E | Div E E deriving Show evalStep :: E -> E evalStep (Val x) = (Val x) -- Note that with a pattern match you can look deeply into a structure. -- in this case we match on the addition of two values. evalStep (Add (Val a) (Val b)) = Val (a + b) evalStep (Add x@(Val _) y) = Add x (evalStep y) evalStep (Add x y@(Val _)) = Add (evalStep x) y You can copy the equations with "Add" and substitute with Sub, Mul and Div and their related functions (-), (*) and div to get the complete evalStep function. Regards, Roel