Second round: Implement an expression evaluator for arithmetic expressions. Followed up by, how would I add operator precedence and variables.
Anonymous
data Expr = Const Int | Add Expr Expr | Sub Expr Expr | Mul Expr Expr eval :: Expr -> Int eval[Const x) = x eval[Add x y) = eval x + eval y eval[Sub x y) = eval x - eval y eval[Mul x y) = eval x * eval y -- variables would involve adding some sort of "let" definition... -- "precedence" is explicit in this definition, because even if you do something like -- eval $ (Const 6) `Add` (Const 5) `Mul` (Const 3) -- haskell will use its default (left) fixity, and so some sort of inherent Mul precedence would be negated by its straight up definition: -- eval $ Mul (Const 3) $ Add (Const 6) (Const 5)
Check out your Company Bowl for anonymous work chats.