[Haskell-cafe] Request to review my attempt at understanding Monads

CK Kashyap ck_kashyap at yahoo.com
Tue Dec 29 01:58:32 EST 2009

I've been reading the papers titled "Comprehending Monads" and "Monadic Parser Combinator" to understand Monads and I think I am beginning to
understand it. In my attempt to validate my understanding, I've written my version of List data structure with Monadic behaviour -
I'd appreciate answers to the following queries - 
1. Comments about the functions I've written
2. I've used the do notation at the bottom which is a result of my List being a Monad - are there any other benefits that comes in because of List being a Monad? What would MonadPlus provide me?
3. The comprehension syntax for Lists in Haskell - can that be used in anyway for other Monads?


import Monad ( MonadPlus(..) )

data List a = Cons a (List a) | Empty
deriving Show

--myMap :: (t -> a) -> List t -> List a
myMap :: (t -> a) -> List t -> List a
myMap f Empty = Empty
myMap f (Cons a rest) = Cons (f a) (myMap f rest)

--myAppend :: List a -> List a -> List a
myAppend :: List a -> List a -> List a
myAppend Empty l = l
myAppend l Empty = l
myAppend (Cons a rest) l = Cons a (myAppend rest l)

--myConcat :: List (List a) -> List a
myConcat :: List (List a) -> List a
myConcat Empty= Empty
myConcat (Cons Empty rest)= myConcat rest
myConcat (Cons list rest)= myAppend list (myConcat rest)

instance Monad List where
return a = Cons a Empty
Empty >>= f = Empty
l >>= f = myConcat (myMap f l)

instance MonadPlus List where
p `mplus` q = myAppend p q
mzero= Empty

list2myList :: [a] -> List a
list2myList [] = Empty
list2myList (x:xs) = Cons x (list2myList xs)

l1 =  list2myList [1..10]
l2 = do
x <- l1
y <- Cons (2*x) Empty
return y


More information about the Haskell-Cafe mailing list