Search code examples
common-lispmacrodef

Quoting programmatically (i.e., for macros)


I would like to be able to do this:

(mapcar #'quote '(1 2 3 4))

And get this

('1 '2 '3 '4)

However, since QUOTE is a special form, it can not be funcalled.

I've tried to macro it up:

(defmacro quoter (&rest args)
  `(loop for arg in ,@args collect (quote arg)))

(quoter '(1 2 3 4 ))

But that gets me, as I might expect....

(LOOP FOR ARG IN '(1 2 3 4)
      COLLECT 'ARG)

Converting the input into a string and then into a symbol from that won't work - I might have incoming forms, not just atoms.

I'm sure I'm missing something here. :-)


Solution

  • This will produce an expansion you want:

    (defmacro quoter (&rest args)
       (loop for arg in args collect `(quote ,arg)))
    

    ...but it's unlikely that such a macro is really what you want at a higher level (unless you want just to play with macros). The expansion is not a valid list form, so the only way to use it is to call MACROEXPAND yourself. And if you're going to call MACROEXPAND, why not make it a function and call that function?