From: Cory Spencer
Subject: Quickie Closure (?) Question...
Date: 
Message-ID: <a44icg$32i$1@nntp.itservices.ubc.ca>
Just a quick question - is the following by itself an example of a
closure?

  (let ((foo 1))
    (defun bar ()
      (incf foo)))

(In all the closure examples I've seen, the closure itself has always been
a lambda form being returned from some expression.)

Thanks!

Cory

From: Pierre R. Mai
Subject: Re: Quickie Closure (?) Question...
Date: 
Message-ID: <87k7tm2jm4.fsf@orion.bln.pmsf.de>
Cory Spencer <········@interchange.ubc.ca> writes:

> Just a quick question - is the following by itself an example of a
> closure?
> 
>   (let ((foo 1))
>     (defun bar ()
>       (incf foo)))
> 
> (In all the closure examples I've seen, the closure itself has always been
> a lambda form being returned from some expression.)

Yes, it is an example of a closure.  You might want to try
macroexpanding the defun form to see, that behind the scenes lambda is
also in play, e.g. in CMU CL:

 (macroexpand '(defun bar () (incf foo)))

=>

(C::%DEFUN 'BAR
           #'(LAMBDA () (BLOCK BAR (INCF FOO)))
           NIL
           '(DEFUN BAR () (INCF FOO)))

So that your code is -- in CMU CL -- equivalent to:

(LET ((FOO 1))
  (C::%DEFUN 'BAR
             #'(LAMBDA () (BLOCK BAR (INCF FOO)))
             NIL
             '(DEFUN BAR () (INCF FOO))))

So the (function) lambda form in that expansion produces a function
that closes over foo (i.e. produces a closure), which is then passed
onto C::%DEFUN, which installs that function in the function slot of
the symbol BAR (and does other bookkeeping tasks).

Regs, Pierre.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein
From: Jochen Schmidt
Subject: Re: Quickie Closure (?) Question...
Date: 
Message-ID: <a44l62$58l$1@rznews2.rrze.uni-erlangen.de>
Cory Spencer wrote:

> Just a quick question - is the following by itself an example of a
> closure?
> 
>   (let ((foo 1))
>     (defun bar ()
>       (incf foo)))
> 
> (In all the closure examples I've seen, the closure itself has always been
> a lambda form being returned from some expression.)

It really pays to lookup such notions in the HyperSpec Glossary.

From the HyperSpec Glossary:

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.


So you can see that "anonymity" is not what makes a closure a closure.
You could say a closure is simply "A function closed over it's environment"
Therefore BAR in your above example is definitely a closure.

ciao,
Jochen

--
http://www.dataheaven.de