Created
May 18, 2015 11:40
-
-
Save edvakf/6a8cce52f8112052d16e to your computer and use it in GitHub Desktop.
mylist.hs
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
data MyList a = MyEmpty | MyCons a (MyList a) deriving (Show) | |
instance Functor MyList where | |
fmap f MyEmpty = MyEmpty | |
fmap f (MyCons x xs) = MyCons (f x) (fmap f xs) | |
instance Applicative MyList where | |
pure x = MyCons x MyEmpty | |
MyEmpty <*> _ = MyEmpty | |
_ <*> MyEmpty = MyEmpty | |
MyCons f fs <*> MyCons x xs = MyCons (f x) (fs <*> xs) | |
instance Monad MyList where | |
return = pure | |
MyEmpty >>= _ = MyEmpty | |
MyCons x xs >>= f = myconcat (f x) (xs >>= f) | |
where | |
myconcat MyEmpty xs = xs | |
myconcat (MyCons x xs) ys = MyCons x (myconcat xs ys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment