Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created May 18, 2015 11:40
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 edvakf/6a8cce52f8112052d16e to your computer and use it in GitHub Desktop.
Save edvakf/6a8cce52f8112052d16e to your computer and use it in GitHub Desktop.
mylist.hs
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