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

Andrew Coppin andrewcoppin at btinternet.com
Mon May 16 20:03:12 CEST 2011


On 16/05/2011 10:07 AM, 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
> (correct me if I'm wrong), so I was wondering why this definition was
> chosen instead of the other one. Does anybody know?

Any time you see a more convoluted definition which ought to be 
equivilent to a simpler one, the answer is usually "because this way 
makes some important compiler optimisation fire". It's even possible 
that the optimisation in question would fire anyway now, but way back 
when the code was written, the compiler wasn't as smart.



More information about the Haskell-Cafe mailing list