Subject: Re: Closures: I'm not getting it
From: rpw3@rpw3.org (Rob Warnock)
Date: Sun, 16 May 2004 20:44:04 -0500
Newsgroups: comp.lang.lisp
Message-ID: <AfmdnZ2oGb55hTXd4p2dnA@speakeasy.net>
Nils G�sche <ngo@cartan.de> wrote:
+---------------
| Barry Margolin <barmar@alum.mit.edu> writes:
| > Did you try (compile 'closure-1) first?  The REPL might not
| > automatically compile nested function definitions.
| 
| For some reason, LispWorks doesn't want to COMPILE interpreted
| closures.  Apparently, it doesn't have to, either:
| 
| # The consequences are undefined if the lexical environment
| # surrounding the function to be compiled contains any bindings other
| # than those for macros, symbol macros, or declarations.
+---------------

True, but you can fake that out if you really, really want a "naked"
compiled closure: simply wrap the lexical environment setting and
the target closure inside *another* closure, compile *that*, and
then FUNCALL it:

    > (funcall
	(compile nil
		 (lambda()
		   (let ((x 1))
		     (lambda () (incf x))))))
    ; Compiling LAMBDA NIL: 
    ; Compiling Top-Level Form: 

    #<Closure Over Function "LAMBDA NIL" {484EEE91}>
    > (funcall *)

    2
    > (funcall **)

    3
    > (funcall ***)

    4
    >


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607