I was reading about list monads and encountered:
[1,2] >>= \n -> ['a','b'] >>= \ch -> return (n,ch)
it produces
[(1,'a'),(1,'b'),(2,'a'),(2,'b')]
Here's how I understand it:
Implicit parentheses are:
([1,2] >>= \n -> ['a','b']) >>= (\ch -> return (n,ch))
([1,2] >>= \n -> ['a','b'])
should give [('a',1),('b',1),('a',2),('b',2)]
because
instance Monad [] where
return x = [x]
xs >>= f = concat (map f xs) -- this line
fail _ = []
so concat (map f xs)
is concat (map (\n -> ['a','b']) [1,2])
which should produce [('a',1),('b',1),('a',2),('b',2)]
- quite the opposite of the actual output.
Then I don't understand >>= (\ch -> return (n,ch))
part - I think that n
here has no sense. That specific reasoning is flawed, could you please explain how that expression([1,2] >>= \n -> ['a','b'] >>= \ch -> return (n,ch)
) is computed step-by-step?