Question on lexical/dynamic variables and symbol-value

Discussion of Common Lisp
Post Reply
macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Question on lexical/dynamic variables and symbol-value

Post by macrolyte » Wed Feb 19, 2014 5:31 pm

Hello All,
I've been working on a side project, but I'm back... In my search to close the holes in my cognition of Lisp, I came across this paper. The code:

Code: Select all


     (defvar z 1) ;; binding persists unless mutated destructively
     (defun koo() z) ;; returns the value binding of 'z
     (defun laz()
        (let ((z 2)) ;; value binding of 'z is within code of the 'let body form
            (koo))) ;; 'koo is outside the code for 'laz
     (laz) ;; returns (2) for the lexically scoped value binding of 'z.

Which makes sense; If the assumptions made in the comments are in error, please feel free to correct them, however, I believe I have this. But the following:

Code: Select all


    (defun maz()
        (let ((z 2))
            (list z (symbol-value 'z)))) ;; list the value binding of 'z and its symbol-value
    (maz) ;; returns the list (2 2)

Which is okay, except the HyperSpec states that 'symbol-value can't see lexical variables! Again, I have:

Code: Select all


    (defun naz()
        (let ((x 2)) ;; 'x has no binding, period.
            (list x (symbol-value 'x)))) ;; 'symbol-value doesn't see lexical variables(?)
    (naz) ;; when evaluated will return an error since 'x is a lexical variable: behavior is as expected. Yea!!!

    (defun loo()
        (let ((x 1))
            (declare (special x)) ;; 'x is now a dynamic variable within the 'let body form
            (naz))) ;; 'naz is called with the dynamic binding over 'x
    (loo)  ;; returns the list(2 1). 'symbol-value returns (1) for the value of 'x: again, behavior as expected

Which is as it should be. So did I miss something in the paper, or was something not addressed about local special variables vs global special variables? Thanks.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Question on lexical/dynamic variables and symbol-value

Post by Goheeca » Thu Feb 20, 2014 4:48 am

From CLHS defvar has a side effect:
If a defvar or defparameter form appears as a top level form, the compiler must recognize that the name has been proclaimed special.
So your z is already proclaimed special.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Question on lexical/dynamic variables and symbol-value

Post by macrolyte » Thu Feb 20, 2014 8:00 am

Goheeca wrote:From CLHS defvar has a side effect:
If a defvar or defparameter form appears as a top level form, the compiler must recognize that the name has been proclaimed special.
So your z is already proclaimed special.
I knew it had to be something I missed! Muchas Gracias, Gohecca.

Post Reply