[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)))

     with:

     (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:

1.
 (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'.

2.
 (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'.

3.
 (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.