Hi André,

Am 28.11.2008 um 22:56 schrieb André Thieme:
Maybe I have a wrong understanding of "closure", but
as far as I understand, every function definition in
Clojure - be it defn or #() - finally translates to (fn ...).
And fn creates a closure.

I tooled myself up and looked up the definition of "closure"
on Wikipedia. It seems that the definition says "one or more
bound variables". I still don't see why the "empty" environment
should that much of a special case. Why not just say:
"a closure keeps its environment". Why should it matter
whether its empty or not?

Well, from my experience I can say that a lot of people
think that “anon function = closure”, which would be
fn in Clojure or lambda in Common Lisp.
This however is not generally correct. In my previous
posting I gave an example of how a named function
became a closure.

There is no thing like a named function. defn expands
to (def ... (fn ...)). So an anonymous function is assigned
to a Var which has a name.

anon function != closure. This happens to be a feature
of Clojure, that it turns functions into closures.

Consider the GCC C extension for inner functions:

struct some_thing
do_something(int x)
{
    int
    do_more()
    {
        return x + 1;
    }

    return call_with_fun_pointer(do_more);
}

do_more could be considered an "anonymous"
function since it is only available inside do_something.
But it is not a closure. Suppose call_with_fun_pointer
stores away the pointer and returns.

Calling do_more later on is invalid, because the
stack was unwound and x doesn't exist anymore.
This is exactly, what a closure prevents.

But the whole issue is only a misunderstanding on
my side about what a closure is, and some philosophical
point, whether the empty environment is a special
case or not...

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to