Skip to content

Instantly share code, notes, and snippets.

@edvakf edvakf/mylist.hs
Created May 18, 2015

What would you like to do?
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)
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
You can’t perform that action at this time.