[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The Lexical Emperor Has No Close



    Date: 17 Aug 88 19:17 EST
    From: STERRITT%SDEVAX.decnet@ge-crd.arpa

    The general suggestion has been to wrap the generation of the lambda
    function in a let, binding a copy-list of the &rest args.

            But here's my question/correction/confusion:  That let-bound variable
    ISN'T closed over (or whatever the right term is), or anyway kept, in the
    lambda definition!  It's a free variable!  THE GIVEN SOLUTIONS DON'T WORK!
    I tried various variants of the solution, finally going over to making it 
    a macro, but it wound up being very ugly.

I don't understand.  The solution I sent, I tested first and it worked fine.
Here's a simpler function that tests the essence of what COMPOSE was doing.
It works.

(defun keep-&rest-list (&rest args)
  "Return a closure which, when funcalled, return a list of the
   original arguments."
  #+LISPM (setf args (copy-list args))
  #'(lambda () args))

Command: (funcall (keep-&rest-list 'FOO 'BAR 'BAZ))
(FOO BAR BAZ)
Command: (funcall (keep-&rest-list 'FOO 'BAR 'BOFFO))
(FOO BAR BOFFO)
Command: (setf x (keep-&rest-list 'THIS 'SEEMS 'TO 'WORK))
#<SYS:LEXICAL-CLOSURE (:INTERNAL KEEP-&REST-LIST 0) 40410142>
Command: (funcall x)
(THIS SEEMS TO WORK)
Command: (funcall x)
(THIS SEEMS TO WORK)
Command: (funcall x)
(THIS SEEMS TO WORK)
Command: 

Are you sure your &REST argument didn't somehow become declared SPECIAL?  That
could cause some unexpected results (since it's not a lexical variable at that
point, the lexical closure doesn't close over it).

(proclaim '(special blappo))

(defun keep-&rest-list (&rest blappo)
  "Return a closure which, when funcalled, return a list of the
   original arguments."
  #+LISPM (setf blappo (copy-list blappo))
  #'(lambda () blappo))
KEEP-&REST-LIST
Command: (setf x (keep-&rest-list 'foo 'baq))
#<SYS:LEXICAL-CLOSURE (LAMBDA NIL BLAPPO) 40431016>
Command: (funcall x)
1Trap: The variable BLAPPO is unbound.


0-- Stephen