[Haskell-beginners] Re: Closure
apfelmus at quantentunnel.de
Sat Aug 15 03:44:16 EDT 2009
Daniel Bastos wrote:
>> So it sounds correct to say that a closure is a function that brings
>> an environment with it, such as variables defined outside of it.
>> With this ability, we can construct functions on the fly because a
>> function can return a closure which is amended and, say, returned
>> again another closure more fully specified.
> Hello. This was actually a request for comments. Though I didn't say
> it. Does that sound correct? Any comments? Thanks much.
Yes, that's pretty much correct.
The simplest example of a closure is indeed
foo = add 3
add = \x y -> x + y
Reduction to weak head normal form yields
foo = let x = 3 in \y -> x + y
which means that foo is a function \y -> x + y paired with the value
of the free variable x .
Note that closures are an implementation detail. From a semantic point
of view, add 3 can readily be understood as an ordinary function.
More information about the Beginners