I was playing with
winghci and I tried:
mapM id [Just 1,
Just 2, Just 3]
result: Just
[1,2,3]
I don't understand
this answer.
mapM mf xs takes a monadic function mf (having type Monad m => (a
-> m b)) and applies it to each element in list xs; the
result is a list inside a monad.
mapM :: (a -> m
b) -> [a] -> m [b]
So in this case: a =
Maybe Int (second arg in mapM id [Just1, Just 2, Just 3] and b = Int
and m = Maybe. So id is :: Maybe Int -> Maybe
Int
mapM id [Just 1, Nothing, Just 3]
result:
Nothing.
My first guess for
the result: Just [Just 1, Nothing, Just 3]
when I do: mapM id
[1,2,3] I get an error (id has wrong type, which makes sense)
Can somebody explain
what is going on here?
Kees