Skip to content

Instantly share code, notes, and snippets.

@ykarikos
Created September 26, 2014 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykarikos/25ebbb38e910c691a52d to your computer and use it in GitHub Desktop.
Save ykarikos/25ebbb38e910c691a52d to your computer and use it in GitHub Desktop.
Reaktor dev day Monads workshop exercise
-- #!/usr/bin/env runhaskell
import Control.Applicative
import Control.Monad
plus3 x = x + 3
data List a = Nil | Cons a (List a)
deriving (Show)
instance Functor List where
fmap _ Nil = Nil
fmap f (Cons x xs) = (Cons (f x) (fmap f xs))
--instance Monad Maybe where
-- Nothing >>= f = Nothing
-- Just a >>= f = f a
data Try a = Ok a | Error String
deriving (Show)
half x = if even x
then Ok (x `div` 2)
else Error "impossiburu"
instance Monad Try where
Error x >>= f = Error x
Ok x >>= f = f x
return x = Ok x
instance Functor Try where
fmap = liftM
instance Applicative Try where
(<*>) = ap
pure = return
-- try these out:
-- fmap (plus3) (Ok 2)
-- (+) <$> Ok 3 <*> Ok 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment