Re: A question (confusion) about closure



On Fri, 2 May 2008 18:07:31 -0700 (PDT), "xahlee@xxxxxxxxx"
<xahlee@xxxxxxxxx> wrote:

Dear joswig,

i don't quite understand what you were saying, perhaps largely due to
your use of Common Lisp and jargons specific to it.

Rainer could have explained the concept without resorting to Lisp, but
apart from the lame attempt to relate closures to state, your
explanation was completely useless.

If you would, defend your thesis using plain English with good effort
at technical writing, or perhaps with emacs lisp code, then we can
start a debate.

if you think hard about closures, as if turning the concept into a
mathematical definition (aka formalization or codification), and
further, if you like, take a survey of say the top 10 major langs,
about their concept of closure (if any), further, then i think you'll
agree, that what i said about closures is perfect. Note the word
_perfect_ here. Its not _approximately right_, nor _helpful analogy_,
nor _insightful_. Perfect.

Your explanation was so off the mark as to be totally meaningless ...
not even just confusing ... totally meaningless.

I am perfect.

A perfect imbecile maybe.

Xah

George


xah@xxxxxxxxxx
? http://xahlee.org/

?

On May 2, 4:11 pm, "jos...@xxxxxxxxxxxxxxxxxxxxxxx" <jos...@corporate-
world.lisp.de> wrote:
On May 2, 11:02 am, "xah...@xxxxxxxxx" <xah...@xxxxxxxxx> wrote:

There is no universal definition of the concept of closure in the
context of programing languages.

Fortunately there is. You just have to look it up.

Basically, if your language can define a function, and maitain a
permanent internal variable inside, it's closure.

For example, if you define a global var, and your function use access
and or set that global var, your function will be able to maintain a
state.

Closures have nothing to do with global vars. Your attempt
to explain it does not help.

We can have different closures with the same function
and each has different variable bindings. Your global-variable-model
does not provide that.

(defun make-thing (name)
(flet ((local-function (message &rest args)
(case message
(:print (format t "Hi, I am ~a" name))
(:rename (setf name (first args))))))
#'local-function))

(make-thing "Red Chair") returns a closure.
(make-thing "Blue Chair") returns another closure.

Let's look inside:

? (describe (make-thing "Red Chair"))
#<COMPILED-LEXICAL-CLOSURE LOCAL-FUNCTION #x2AA04BE>
Name: LOCAL-FUNCTION
Inner lfun: #<Compiled-function LOCAL-FUNCTION (Non-Global)
#x2A9AFD6>
Closed over values
(0): "Red Chair"

? (describe (make-thing "Blue Chair"))
#<COMPILED-LEXICAL-CLOSURE LOCAL-FUNCTION #x2AA3C6E>
Name: LOCAL-FUNCTION
Inner lfun: #<Compiled-function LOCAL-FUNCTION (Non-Global)
#x2A9AFD6>
Closed over values
(0): "Blue Chair"

From the identifier you can see that both closures use the same 'Inner
lfun' #x2A9AFD6.
From the identifier you can see that both closures are different:
#x2AA04BE and #x2AA3C6E
The closures list different 'closed over values: "Red Chair" and "Blue
Chair".

Both closures have the same inner function (it is just one function)
but different variable bindings.
So, we have two closures, but only one inner function and two
different bindings.

Do you see the difference to your 'explanation'? It is not sufficient
that a function
has its own (uniquely named) global variables. You would need new
variables
every time a closure gets created.

Let's try your version:

(defparameter __name nil)

(defun make-thing (name)
(setf __name name)
(flet ((local-function (message &rest args)
(case message
(:print (format t "Hi, I am ~a" __name))
(:rename (setf __name (first args))))))
#'local-function))

(let ((a (make-thing :a))
(b (make-thing :b)))
(funcall a :print)
(terpri)
(funcall b :print))

Hi, I am B
Hi, I am B

Too, bad. Doesn't work. No closures.

? (describe (make-thing :A))
#<Compiled-function LOCAL-FUNCTION (Non-Global) #x2ADD4D6>
Name: LOCAL-FUNCTION
Arglist (analysis): (CCL::ARG-0 &REST CCL::THE-REST)

Again, no closure.

Above is btw. Common Lisp, which uses lexical binding by default
(different from Emacs Lisp)
and thus supports closures directly.

--------------

Note: The terminology ?Closure? is actually a very bad terminology. It
spreads endless confusion and non-understanding. See:

Your usual FUD. If you study the above example, you can end your
confusion.

...

--
for email reply remove "/" from address
.



Relevant Pages

  • Re: Java or C++?
    ... >>> I don't see how scope comes into it, scope in lisp isn't that much ... > Do you mean that lisp is different in the presence of closures. ... In the presence of an generational GC like OCaml's, a pushed stack element ...
    (comp.programming)
  • Re: History of lexical scoping in Scheme and other Lisps?
    ... > Lisp standard) that Scheme introduced lexical scoping and lexical ... Does that mean that all Lisp dialects prior to Scheme used ... The first example of lexical scoping and lexical closures can be found in ...
    (comp.lang.lisp)
  • Matthew Danish must never say "stagger" again [was Re: "Staggering power of closures.
    ... I learned a whole lot about Lisp optimization that I never would ... >>example of this is closures. ... >>them to comprehend at first the staggering power of such a simple idea. ... > have I just grasped the idea so well before that I'm missing it now? ...
    (comp.lang.lisp)
  • Re: The LOOP macro
    ... In Lisp, you can build your own pattern matching layer on top of what's ... language features from other languages and reimplement them on top of, ... everyone would ask him to use closures instead... ... make you think about what abstractions you _actually_ need. ...
    (comp.lang.lisp)
  • Re: A question (confusion) about closure
    ... that what i said about closures is perfect. ... and each has different variable bindings. ... (defun make-thing (name) ... Above is btw. Common Lisp, which uses lexical binding by default ...
    (comp.lang.lisp)