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

progv and dynamic variables

    What should the following do:

     (defun foo (a s v)
      (progv s v (print a)))


     (foo 3 '(a) '(4))

     The actual result is 3 but from the book description of PROGV
     on p112 it appears that it should be 4. I have tried this on
     3 different common lisps with the same result.

The `a' in (print a) refers to the lexical variable `a' named in
foo's parameter list. The progv binds the dynamic variable `a'.
You can get the answer to be 4 a number of ways:

 (defvar a)

 (defun foo (a s v)
  (progv s v (print a)))

This makes all references to `a' pervasively dynamic so even
the call to foo binds the dynamic `a'.

 (defun foo (a s v)
  (declare (special a))
  (progv s v (print a)))

Has the same effect on foo, but other functions can use a lexically
scoped `a'.

 (defun foo (a s v)
  (progv s v
    (declare (special a))
    (print a)))

The binding of `a' during the call to foo is lexical, but the
reference in (print a) is dynamic.