Created
September 26, 2014 09:14
-
-
Save ykarikos/25ebbb38e910c691a52d to your computer and use it in GitHub Desktop.
Reaktor dev day Monads workshop exercise
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
-- #!/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