Skip to content

Instantly share code, notes, and snippets.

@nattybear
Created July 24, 2022 00:51
Show Gist options
  • Save nattybear/54f0d654bf75ad8b50aedce981fb6731 to your computer and use it in GitHub Desktop.
Save nattybear/54f0d654bf75ad8b50aedce981fb6731 to your computer and use it in GitHub Desktop.
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