a faster, accumulating mapM (was Re: [Haskell-cafe] mapM vs mapM_ performance)

Don Stewart dons at galois.com
Thu Apr 24 20:44:43 EDT 2008


niklas.broberg:
> >  >  2) Is there a reason to not use mapM3 above?
> >
> > Yes, there certainly is.  mapM3 is not equivalent to mapM; it is too strict:
> >
> >  *Main> take 3 $ head $ mapM return [1,2,3,4,undefined]
> >  [1,2,3]
> >  *Main> take 3 $ head $ mapM3 return [1,2,3,4,undefined]
> >  [*** Exception: Prelude.undefined
> >
> >  So, like foldl', mapM3 seems a viable alternative for mapM, but not a
> >  replacement.
> 
> Wow. A 10x slowdown for a very commonly used function that in 99.8% of
> all use cases has no need for the extra laziness at all. No wonder
> some people say Haskell is a toy language...
> 

mapM_ is far more common, and optimised specially.

-- Don


More information about the Haskell-Cafe mailing list