Unbound variable in Allegro

241 Views Asked by At

In example code in a book I'm reading, there's a line for a macro that provides shorthand for getting the global value of a symbol:

(defmacro sv (v) '(symbol-value `,v))

However, Allegro sees V as an unbound variable. I don't know how to change this macro to return the correct result, because I've never encountered a problem like this before. How would I get the global value of any symbol? Thanks for your help!

1

There are 1 best solutions below

5
scooter me fecit On BEST ANSWER

You need a backquote, not a single quote, in front of the whole s-exp:

(defmacro sv (v) `(symbol-value ,v))

(I know, it's hard to see: Just move the backquote in your original s-exp from in front of ,v and replace the single quote at the beginning of '(symbol-value ...)).

Backquote operates on expressions (lists), not individual atoms, per se. If what you typed was actually in the book, it's likely an erratum.

A slightly better version of sv:

(defmacro sv (v) `(symbol-value (quote ,v)))

or

(defmacro sv (v) `(symbol-value ',v))

In the last version, interchange the backquote and single quote in your original code. The penultimate version just makes it easier to read.

However, symbol-value only queries the current value bound to the variable. For example, this code evaluates to 'something-else:

(defparameter *wombat* 123)
(let ((*wombat* 'something-else)) (sv *wombat*))

The *wombat* in the let form is a lexical variable, whereas the *wombat* in the defparameter form is dynamic. The let form temporarily hides the visibility of the defparameter.