-
-
Save nattybear/54f0d654bf75ad8b50aedce981fb6731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Expr = I Int | |
| B Bool | |
| Add Expr Expr | |
| Mul Expr Expr | |
| Eq Expr Expr | |
deriving Show | |
eval :: Expr -> Maybe (Either Int Bool) | |
eval (I n) = Just (Left n) | |
eval (B b) = Just (Right b) | |
eval (Add e1 e2) = case (eval e1, eval e2) of | |
(Just (Left x), Just (Left y)) -> Just (Left (x + y)) | |
_ -> Nothing | |
eval (Mul e1 e2) = case (eval e1, eval e2) of | |
(Just (Left x), Just (Left y)) -> Just (Left (x * y)) | |
_ -> Nothing | |
eval (Eq e1 e2) = case (eval e1, eval e2) of | |
(Just (Right x), Just (Right y)) -> Just (Right (x == y)) | |
(Just (Left x), Just (Left y)) -> Just (Right (x == y)) | |
_ -> Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment