[Haskell-cafe] strict mapM implementation correctness

Paolo Losi paolo.losi at gmail.com
Mon Nov 9 17:07:28 EST 2009


Hi all,

while coding recently I came across the need for
a strict mapM (that I indicidently use for m = MaybeT IO).

I implementented this with the following simple code
(it works for my use case):

mapM' :: Monad m => (a-> m b) -> [a] -> m [b]
mapM' _ []     = return []
mapM' f (x:xs) = do y  <- f x
                     ys <- y `seq` mapM' f xs
                     return (y:ys)

Now a couple of questions:

1. is it so "rare" to need such a beast? Why there isn't anything
    like that in standard libraries?
2. Is my implementation correct?
3. I've seen that mapM is implemented in base via sequence
    (which is in turn based on foldr). Is that any specific
    reason to prefer base implementation to mine?

Thanks!
Pao



More information about the Haskell-Cafe mailing list