Toggle Variables in Lisp

I wrote a function

(defun test ()
  (let ((str1 "foo") (str2 "bar"))
    (loop for s in '(str1 str2) do (message s))))

but that will not work. Elisp Backtrace Post:

Debugger introduced - Lisp error: (invalid argument type stringp str1)

How can I make it work?

PS: the next modified version works fine, but I need the original version

(defun test1 ()
  (loop for s in '("asdf" "fdsa") do (message s)))
+5
source share
3 answers

The operator quote(for which the apostrophe is syntactic sugar) means that its arguments are not evaluated, i.e. (quote (str1 str2))returns a list of two characters. Use list: instead (list str1 str2).

+16
source

Create a list of values:

(defun test ()
  (let ((str1 "foo") (str2 "bar"))
    (loop for s in (list str1 str2) do (message s))))
+6
source

to try:

`(,str1 ,str2)
+3
source

All Articles