Oleg Pavliv
Oleg Pavliv

Reputation: 21152

Loop over variables in Lisp

I wrote the function

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

but it does not work. The Elisp Backtrace message is:

Debugger entered--Lisp error: (wrong-type-argument stringp str1)

How can I make it work?

P.S.: the following modified version works perfectly, but I need the original version

(defun test1 ()
  (loop for s in '("asdf" "fdsa") do (message s)))

Upvotes: 5

Views: 1443

Answers (3)

defstruct
defstruct

Reputation:

Build a list of the values:

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

Upvotes: 6

Svante
Svante

Reputation: 51501

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

Upvotes: 16

dsm
dsm

Reputation: 10393

try:

`(,str1 ,str2)

Upvotes: 3

Related Questions