Suppose I have a function
CL-USER> (defun trimmer (seq) "This trims seq and returns a list"
(cdr
(butlast seq)))
TRIMMER
CL-USER> (trimmer '(1 2 3 VAR1 VAR2))
(2 3 VAR1)
CL-USER>
Notice how, due to QUOTE, VAR1 and VAR2 are not resolved. Suppose I want to resolve the symbols VAR1 and VAR2 to their values - is there a standard function to do this?
Do not use quote
to create a list with variables; use list
instead:
CL-USER> (trimmer (list 1 2 3 var1 var2))
(2 3 value-of-var1)
(where value-of-var1
is the value of var1
).
Quote
only prevents evaluation of whatever its argument is. If its argument happens to be a list literal, then that is returned. However, to create lists that are not just literals, use list
. You can use backquote syntax, but that is rather obfuscation in such a case.
Backquote is the usual way to interpolate values into a quoted list:
> (setq var1 4 var2 5)
5
> `(1 2 3 ,var1 ,var2)
(1 2 3 4 5)
Edited to add: if you want to process a list so that symbols are replaced with their symbol-value
, then you need a function something like this:
(defun interpolate-symbol-values (list)
"Return a copy of LIST with symbols replaced by their symbol-value."
(loop for x in list
collect (if (symbolp x) (symbol-value x) x)))
> (interpolate-variables '(1 2 3 var1 var2))
(1 2 3 4 5)
This seems like a strange thing to want to do, however. Can you say more about what you are trying to achieve? Almost certainly there's a better way to do it than this.