Last active
April 26, 2018 15:00
-
-
Save ajkavanagh/7de8ac1434ebf83578ff33776ec011ff 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
import Control.Applicative (Applicative(..)) | |
import Control.Monad (liftM, ap) | |
-- MyMaybe is to play with Maybes | |
data MyMaybe x = MyJust x | MyNothing | |
deriving Show | |
-- Monad MyMabye | |
instance Functor MyMaybe where | |
fmap = liftM | |
instance Applicative MyMaybe where | |
pure x = MyJust x | |
(<*>) = ap | |
instance Monad MyMaybe where | |
(>>=) (MyJust x) f = f x | |
(>>=) MyNothing f = MyNothing | |
data Term = Con Int | Div Term Term | |
eval :: Term -> MyMaybe Int | |
eval (Con a) = return a | |
eval (Div t1 t2) = eval t1 >>= (\x -> eval t2 >>= (\y -> return (x `div` y))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment