[Haskell-cafe] >>= definition for list monad in ghc

Daniel Fischer daniel.is.fischer at googlemail.com
Mon May 16 11:18:11 CEST 2011


On Monday 16 May 2011 11:07:15, Michael Vanier wrote:
> Usually in monad tutorials, the >>= operator for the list monad is
> defined as:
> 
> m >>= k = concat (map k m)  -- or concatMap k m
> 
> but in the GHC sources it's defined as:
> 
> m  >>=  k  =  foldr  ((++)  .  k)  []  m
> 
> As far as I can tell, this definition is equivalent to the previous one

It is indeed, otherwise at least one of them would be wrong.

> (correct me if I'm wrong), so I was wondering why this definition was
> chosen instead of the other one.  Does anybody know?

I don't *know*, but I suspect it's for efficiency, writing

concat (map k m)

might not be unfolded enough to make foldr/build fusion fire in cases where 
it applies.
I'm just guessing, though.

> 
> Thanks in advance,
> 
> Mike



More information about the Haskell-Cafe mailing list