Last active
August 29, 2015 14:13
-
-
Save fritz0705/6cae2c1e41fd3be74f36 to your computer and use it in GitHub Desktop.
This file contains 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 | |
import Control.Monad | |
import Data.Monoid | |
data List a = Nil | (:::) a (List a) deriving (Show, Read) | |
infixr 5 ::: | |
instance Functor List where | |
fmap f (x ::: xs) = (f x) ::: (fmap f xs) | |
fmap _ Nil = Nil | |
instance Monoid (List a) where | |
mempty = Nil | |
(x ::: xs) `mappend` b = x ::: (xs `mappend` b) | |
Nil `mappend` b = b | |
instance Applicative List where | |
pure = return | |
(<*>) = ap | |
instance Monad List where | |
return x = x ::: Nil | |
Nil >>= _ = Nil | |
(x ::: xs) >>= f = f x `mappend` (xs >>= f) | |
instance MonadPlus List where | |
mzero = mempty | |
mplus = mappend | |
instance Alternative List where | |
empty = mempty | |
(<|>) = mappend |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment