[Haskell-cafe] mapM is supralinear?

John Lato jwlato at gmail.com
Fri Sep 9 12:19:12 CEST 2011


> From: Daniel Fischer <daniel.is.fischer at googlemail.com>
>
> On Friday 09 September 2011, 00:41:11, Roman Cheplyaka wrote:
>> * Ertugrul Soeylemez <es at ertes.de> [2011-09-07 16:20:03+0200]
>>
>> > In general it's a bad idea to use mapM over IO.
>>
>> Could you explain why?
>
> Take it with a grain of salt, there's nothing necessarily wrong with using
> mapM over IO on short lists.

Agreed.  Whenever I'd like to use mapM (or any other function for
which a *M_ is available), I've found the following rules helpful:

1.  If I can guarantee the list is short (~ n<=20), go ahead and use mapM
2.  Otherwise use mapM_, foldM_, or foldM if a real reduction is
possible (i.e. not "foldM snocM []").

Step 2 sometimes requires changing my design, but it's always been for
the better.  `mapM_` tends to require more pipeline composition, so
it's leveraging the language's strengths.

This has served me well, especially in IO, but in other monads as well.

John L.



More information about the Haskell-Cafe mailing list