Re: A question (confusion) about closure



On May 2, 1:31 am, Samik <sam...@xxxxxxxxx> wrote:
Hi,

I have a confusion regarding closure and its definition. As per Paul
Graham:

When a function refers to a variable defined outside it, it’s called a
free variable. A function that refers to free lexical variable is
called a closure.

However as per CLHS a closure or lexical closure is:

lexical closure n. a function that, when invoked on arguments,
executes the body of a lambda
expression in the lexical environment that was captured at the time of
the creation of the lexical closure, augmented by bindings of the
function's parameters to the corresponding arguments.

lexical environment n. that part of the environment that contains
bindings whose names have lexical scope. A lexical environment
contains, among other things: ordinary bindings of variable names to
values, lexically established bindings of function names to functions,
macros, symbol macros, blocks, tags, and local declarations (see
declare).

Hi Samik,

I'm no pro, but Graham's explanation does not square with everything
else I've been learning about closures. The definitions you provided
are correct, but they are difficult to understand for people who are
unfamiliar with the terms they use. The big "aha" moment for me
occurred when I realized the "lexical environment" was a THING. It's
essentially something like an association table that matches symbols
to values.

Think of it this way: in a Lisp without closures, a procedure is
just a procedure. In a lisp with closures, a procedure is data-
structure that has two parts: 1) code that does something, and 2)
something like an association table that captures the state of all the
variables when the function is called. So every time a function is
run in a Lisp with closures, a new lexical environment is
manufactured to go along with it.

When Lispers talk about how closures can be used to implement objects
and object-oriented programming, they are talking about using
techniques to coax the procedure part of the procedure-data-structure
into manipulating the lexical environment part of the procedure-data-
structure.

Edward
.



Relevant Pages

  • Re: setq givs a warning i SBCL, Not CLISP
    ... Allegro 8 says that the function returned is a closure. ... (because a closure with a null lexical environment is perfectly legal, ... it implies that specials are dynamic and not lexical. ... declaration to the contrary (which implies that the special binding is ...
    (comp.lang.lisp)
  • A question (confusion) about closure
    ... I have a confusion regarding closure and its definition. ... When a function refers to a variable defined outside it, ... lexical environment n. ... bindings whose names have lexical scope. ...
    (comp.lang.lisp)
  • Re: Making a function "forget" a variable
    ... defined in some lexical environment which contains the ... Very rarely and then usually because I did get lazy and name things too closely, such as elephant-x and elephant-y, so I do not look for language tricks just because I am lazy because if I am lazy I sure do not have the energy to write a closure and then studiously create just the right ). ... Even if I were more energetic, I would not incur this cost on the authoring of every closure I ever authored just for the one time I will reference the wrong closed-over var. ...
    (comp.lang.lisp)
  • Re: Confusion about closures
    ... var firstNameValue = { ... Via saved lexical environment a function finds its free variables. ... So answering your question, yes, this anonymous immediately invoked functional expression is a closure. ...
    (comp.lang.javascript)
  • Re: A question (confusion) about closure
    ... I have a confusion regarding closure and its definition. ... When a function refers to a variable defined outside it, ... lexical environment n. ... bindings whose names have lexical scope. ...
    (comp.lang.lisp)