[Haskell-cafe] Re: Tutorial uploaded

Sebastian Sylvan sebastian.sylvan at gmail.com
Thu Dec 22 13:52:31 EST 2005

On 12/22/05, Daniel Carrera <daniel.carrera at zmsl.com> wrote:
> Paul Moore wrote:
> > As I say, I'm not trying to criticize anyone here, but it seems to be
> > quite hard to get across to people who have understood and assimilated
> > this sort of stuff, just how hard it feels to newcomers. We understand
> > the explanations (we do! really! :-)) but even understanding them, we
> > are still left with a lack of confidence. It's like being shown a full
> > set of carpentry tools, having every one explained, but still reaching
> > for the hammer every time and banging something no matter what we're
> > trying to do :-)
> I had never heard of mapM, or other -M functions. I can't imagine why
> those would be needed. It seems like pointless duplication.

mapM is like map except you map an IO Action over a list instead of a
function of a list.

For instance
sizes <- mapM getFileSize myListOfFileNames

If you used "map" here you'd end up with a list of IO actions, and not
a list of file sizes. You'd then have to go through this list of IO
actions and using (<-) on each element to get the file sizes. This
can, incidentally, be done using the function 'sequence'.

liftM lifts a function so that you can use a regular function on an IO
Action instead of first having to extract the value of the IO action
using (<-). It's just shorthand, so you could do:

x <- liftM length (readFile "afile")

Instead of having to do

f <- readFile "afile"
let x = length f

The M functions really are useful, get to know them!


Sebastian Sylvan
UIN: 44640862

More information about the Haskell-Cafe mailing list