I have a macro like this:

(defmacro my-fun-generator (x)
  `(defun ,(intern (concat "fun-" x)) ()
     ,(concat "Print " x)
     (print ,x)))

This works (found an example online):

(dolist (str '("foo" "bar" "baz"))
  (eval `(my-fun-generator ,str)))

But this errors with (wrong-type-argument sequencep str) on the first concat:

(dolist (str '("foo" "bar" "baz"))
  (my-fun-generator str))

Is it saying the str is not of type sequence? Wouldn't str be of type string? Is a string a sequence? Just, why doesn't the second version work?

(dolist (str '("foo" "bar" "baz"))
  (my-fun-generator str))

Yes, a string is a sequence, but the symbol str is not a string - it's a symbol (which is not a sequence). What you want to pass to the macro is not str but the value of str. Instead, you're passing the symbol str to the macro.

Macros do not evaluate their arguments, by default.

That's why the example you copied substitutes the value of str for str in the sexp (list) (my-fun-generator...), and it then evaluates the resulting sexp.

After the substitution, in the first loop iteration, the sexp to be evald is the list (my-fun-generator "foo"). It's because of that eval that my-fun-generator is then used as a macro - expanded and its result evaluated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.