For reference, here is an example of a working monad NestedList. It is easy to verify that this instance satisfies the laws of the monad.
import Control.Monad
data NestedList a = Elem a | List [NestedList a] deriving (Show)
instance Monad NestedList where
return x = Elem x
(Elem x) >>= f = f x
(List xs) >>= f = List $ map step xs
where step (Elem a) = f a
step lst = lst >>= f
Here is the test program:
import Control.Monad
data NestedList a = Elem a | List [NestedList a] deriving (Show)
instance Monad NestedList where
return x = Elem x
(Elem x) >>= f = f x
(List xs) >>= f = List $ map step xs
where step (Elem a) = f a
step lst = lst >>= f
double :: a -> NestedList a
double a = List ([Elem a] ++ [Elem a])
add :: (Num a) => a -> a -> NestedList a
add a e = Elem $ a + e
main = do let a = Elem 1 >>= double
let b = List [Elem 1, Elem 2] >>= double
let c = List [Elem 2,List [Elem 3,Elem 4],Elem 5] >>= add 1
print a
print b
print c
And his conclusion:
$ runhaskell t.hs
List [Elem 1,Elem 1]
List [List [Elem 1,Elem 1],List [Elem 2,Elem 2]]
List [Elem 3,List [Elem 4,Elem 5],Elem 6]