[Haskell-cafe] \_ -> not equivalent to const $

Benja Fallenstein benja.fallenstein at gmail.com
Thu Jan 10 22:53:28 EST 2008

On Jan 10, 2008 11:54 PM, Luke Palmer <lrpalmer at gmail.com> wrote:
> Can someone explain what the heck is going on here?

Evaluating (const EXPR) creates a closure object with a pointer to
'const' and a pointer to the EXPR thunk. Call this closure object C.
Evaluating (C undefined) calls 'const' with the EXPR and C thunks as
its arguments, which returns the EXPR thunk. The EXPR thunk is then
forced by your code. Evaluating (C undefined) again calls 'const' with
the EXPR and C pointers as its arguments again, which again returns
the *now forced* EXPR thunk.

I.e., evaluating (const EXPR) creates a closure with a pointer to a
single EXPR thunk, and then applying the same closure to some argument
multiple times uses that same EXPR thunk every time. An unoptimized
(\_ -> EXPR) creates a new thunk each time the function is applied to
an argument.

Hope that helps with understanding what is going on?

- Benja

More information about the Haskell-Cafe mailing list