Re: A question (confusion) about closure

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

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

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

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-