[Haskell-cafe] Spot the difference!

ajb at spamcop.net ajb at spamcop.net
Thu Sep 20 01:15:50 EDT 2007


G'day all.

Quoting PR Stanley <prstanley at ntlworld.com>:

> \_ n -> 1 + n
> \_ -> (\n -> 1 + n)
> The outcome seems to be identical. is there a substantive difference
> between the two definitions?

Certainly, GHC compiles these to the same code.  But be careful!  Consider
the following two defintions:

     test1 n _ = 1 + n
     test2 n = \_ -> 1 + n

I don't know if it's still the case, but GHC used to compile different
code for these at high optimisation levels.  The first was essentially
compiled to:

     test1 = \n _ -> 1+n

And the second to:

     test2 = \n -> let x = n+1 in \_ -> x

The difference is that test1 is faster if it's usually fully applied,
test2 is fully lazy.

Cheers,
Andrew Bromage


More information about the Haskell-Cafe mailing list