[Haskell-beginners] Re: Closure
Daniel Bastos
dbastos+0 at toledo.com
Sat Aug 15 12:07:08 EDT 2009
In article <h65p0h$rjl$1 at ger.gmane.org>,
Heinrich Apfelmus wrote:
> The simplest example of a closure is indeed
>
> foo = add 3
>
> where
>
> add = \x y -> x + y
Question. This is actually equal to
add x y = x + y
But you wrote in terms of \. Why such preference?
> 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 .
I see.
> Note that closures are an implementation detail. From a semantic point
> of view, add 3 can readily be understood as an ordinary function.
This makes sense. Because, even in a language like C, a similar effect
can be achieved, no? For example
int plus(int x, int y) { return x + y; }
int plus3(int y) { plus(3, y); }
So, what I can't do in C, besides almost everything I can't do, is to
do this nicely like I do in Haskell. But we don't call this a
closure. In fact, we say C does not allow for closures. So what am I
missing?
More information about the Beginners
mailing list