Page 1 of 1

Question on lexical/dynamic variables and symbol-value

Posted: Wed Feb 19, 2014 5:31 pm
by macrolyte
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.

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

Posted: Thu Feb 20, 2014 4:48 am
by Goheeca
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.

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

Posted: Thu Feb 20, 2014 8:00 am
by macrolyte
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.