"Unspecial" Global Variables?
Posted: Mon Mar 22, 2010 12:19 am
This is from Touretzky's book:
"...it is entirely legal to have a lexically scoped (unspecial) global variable."
This issue has been bothering me: The use of setf for global variables. I have seen this even in Peter Norvig's PAIP. I know you should not use setf like defvar, but it seems that is the case with these books. I have looked at CLtL 2nd edition and it makes no mention of using setf this way, so I am assuming using setf this way is something even older. Here is a typical example from the Touretzky's book that I want to explore further:
I have tested this and I got the same result from the book, so it would seem that Touretzky is right. I can use unspecial or "non-dynamic" global variables; although I get warned every time I use setf like that. Touretzky even says I can even ignore the warnings. It appears the result of setf is quite distinct from defconstant and defvar. If I used defvar instead of setf, the result would be ((GUPPY MINNOW) (GUPPY MINNOW)). If I replaced setf with defconstant, I would get an error message. My question is this: What in the world would "unspecial" global variables be useful for? If it is useful, would there be a better way to do this instead of using setf?
"...it is entirely legal to have a lexically scoped (unspecial) global variable."
This issue has been bothering me: The use of setf for global variables. I have seen this even in Peter Norvig's PAIP. I know you should not use setf like defvar, but it seems that is the case with these books. I have looked at CLtL 2nd edition and it makes no mention of using setf this way, so I am assuming using setf this way is something even older. Here is a typical example from the Touretzky's book that I want to explore further:
Code: Select all
(setf fish '(salmon tuna))
(defun ref-fish () fish)
(defun test-lexical (fish)
(list fish (ref-fish)))
(test-lexical '(guppy minnow))
((GUPPY MINNOW) (SALMON TUNA))