> From: "xah...@[EMAIL PROTECTED]
" <xah...@[EMAIL PROTECTED]
>
> Basically, if your language can define a function, and mai[n]tain a
> permanent internal variable inside, it's closure.
It's actually more than that. Simply having a variable that is
private to a single block but not re-initialized every time the
block is entered, which retains its value from one entry to the
next, was called an "OWN" variable in Algol 60:
<http://www.masswerk.at/algol60/modified_report.htm#5>
but is now called a "Static local variable" in WikiPedia:
<http://en.wikipedia.org/wiki/Local_variable#Static_local_variables>
But in a closure, the *same* private variable ("binding" in Lisp
jargon) can be *shared* between two or more blocks/functions. Thus
a closure allows *shared* own/static variables, so that the
*shared* latest-value is preserved across entries to *either* block
and accessible equally from each:
(let ((x 42))
(defun setx (newx) (setq x newx))
(defun getx () x)
"Done: Defined SETX with one arg and GETX with no args")
It works with first-class anonymous functions just the same:
(let ((x 42))
(values (function (lambda (newx) (setq x newx)))
(function (lambda () x))))
And of course you can mix the two if you want.
(let ((x 42))
(defun setx (newx) (setq x newx))
(function (lambda () x)))
Note that technically the name is "lexical closure" for the
mechanism used in Lisp, where we use a lexical binding such as LET
or LAMBDA or even PROG to temporarily create an environment with
one or more lexical variables temporarily created, in which several
functions can be created/defined, each sharing that temporary
environment, hence all of them sharing those particular lexical
bindings, except that because of the functions closing over
variables in that lexical environment, the bindings become
permanent in the sense that the lexical-binding environment is
preserved indefinitely, to be used by those defined/created
functions as long as those functions continue to exist. (Or just
define/create ONE function in that lexical environment, which
reduces to OWN/STATIC variables from Algol/WikiPedia.)
For a koan-like explanation: A set of one or more "lexical
closure(s)" is/are a set of functions defined inside of a lexical
context of bindings, thereby achieving the effect of those bindings
becoming privately shared own/static variables.
> Note: The terminology =E2=80=9CClosure=E2=80=9D is actually a very
> bad terminology. It spreads endless confusion and non-understanding.
I agree. The correct Lexical Closure also leads to some confusion
among people who haven't read a decent explanation yet. I hope my
explanations above are close to what would be needed to un-confuse
newbies. Maybe somebody can re-word it to flow easier? My wording
is somewhat clumsy but maybe Kent can clean it up to be *perfect*?
One really neat thing about Common Lisp's lexical closures,
compared to C++'s and Java's classes with static variables, is that
in CL our lexical environment doesn't require a name (the name of
the C++ or Java *class*) and doesn't need to be defined in a
separate file whose name matches the name of the *class*.


|