Skip to content

Instantly share code, notes, and snippets.

@ajkavanagh
Last active April 26, 2018 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajkavanagh/7de8ac1434ebf83578ff33776ec011ff to your computer and use it in GitHub Desktop.
Save ajkavanagh/7de8ac1434ebf83578ff33776ec011ff to your computer and use it in GitHub Desktop.
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