Anonymous user
Arithmetic evaluation: Difference between revisions
→{{header|Haskell}}
Line 2,175:
=={{header|Haskell}}==
<lang haskell>import Text
import Text
import Text.Parsec.Combinator
import Data.Functor
data Exp = Num Int
Line 2,188 ⟶ 2,190:
table = [[op "*" (Mul) AssocLeft, op "/" (Div) AssocLeft]
,[op "+" (Add) AssocLeft, op "-" (Sub) AssocLeft]]
where op s f assoc = Infix (
factor = between
eval e = case e of
Num x -> fromIntegral x
Mul a b -> eval a * eval b
Div a b -> eval a `div` eval b
solution
solution = either (const (error "Did not parse")) eval . parse expr ""</lang>
=={{header|Icon}} and {{header|Unicon}}==
|