[email protected]
[Top] [All Lists]

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

Subject: [Haskell-cafe] Request to review my attempt at understanding Monads
From: CK Kashyap
Date: Mon, 28 Dec 2009 22:58:32 -0800 PST
Hi,
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?

Regards,
Kashyap

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


      

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

<Prev in Thread] Current Thread [Next in Thread>