Subject: Re: Lexical scope question
From: rpw3@rpw3.org (Rob Warnock)
Date: Sat, 14 Jul 2007 03:10:57 -0500
Newsgroups: comp.lang.lisp
Message-ID: <IPGdnZMzNqCMGAXbnZ2dnUVZ_judnZ2d@speakeasy.net>
Thomas A. Russ <tar@sevak.isi.edu> wrote:
+---------------
| Osei <osei.poku@gmail.com> writes:
| > Is there any way to call the lambda function outside outer2 and still
| > get the binding it had inside or do I have to define INNER using FLET
| > inside OUTER2.
| 
| That would be one method.  But then you would be looking to get lexical
| scope instead of dynamic scope.  Perhaps the following code will help
| make this distinction clear?
| 
| (defun scope-test ()
|   (let ((lex "inner-lexical")
|         (dyn "inner-dynamic"))
|     (declare (special dyn))
|     (lambda () (format t "Lex = ~A    Dyn = ~A" lex dyn))))
| 
| (let ((lex "top-lexical")
|       (dyn "top-dynamic"))
|   (declare (special dyn))
|   (funcall (scope-test)))
+---------------

Sneaky! I was confused for a moment by the output
of that one until I actually read it correctly!  ;-}

The OP might also find the following version amusing:

  (defun scope-test ()
    (let ((lex "inner-lexical")
          (dyn "inner-dynamic"))
      (declare (special dyn))
      (flet ((do-print (n)
	       (format t "~d: Lex = ~A    Dyn = ~A~%" n lex dyn)))
	(do-print 1)
        #'do-print)))
 
  (let ((lex "top-lexical")
        (dyn "top-dynamic"))
    (declare (ignorable lex)		; muffle warning
	     (special dyn))
    (funcall (scope-test) 2))


-Rob

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