Last active
April 16, 2021 00:54
-
-
Save pedrominicz/a571d8f03726ed06022ecd745936155a to your computer and use it in GitHub Desktop.
Another list monad
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
module List where | |
data List a = Nil | Cons a (List a) deriving (Eq, Show) | |
instance Functor List where | |
fmap f Nil = Nil | |
fmap f (Cons a as) = Cons (f a) (fmap f as) | |
instance Applicative List where | |
pure a = Cons a Nil | |
(<*>) = undefined | |
append :: List a -> List a -> List a | |
append (Cons a as) bs = Cons a (append as bs) | |
append Nil bs = bs | |
join :: List (List a) -> List a | |
join Nil = Nil | |
join (Cons l ls) = append (join ls) l | |
instance Monad List where | |
a >>= f = join $ fmap f a | |
toList :: [a] -> List a | |
toList [] = Nil | |
toList (a:as) = Cons a (toList as) | |
fromList :: List a -> [a] | |
fromList Nil = [] | |
fromList (Cons a as) = a : fromList as |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment