More‎ > ‎

Quasiquotation

Quasiquotation is a convenient syntax for creating lists that contain a mix of literal values and values that are "pasted in" from a variable or other expression:

arc> (with (x 2 y '(4 5 6))
       `(1 ,x 3 ,@y 7))
(1 2 3 4 5 6 7)

A plain comma like , inserts its argument as-is, while ,@ splices into the resulting list.  If the above expression used a plain , for y instead of ,@ we'd see the list appearing itself, rather than its contents:

arc> (with (x 2 y '(4 5 6))
       `(1 ,x 3 ,y 7))
(1 2 3 (4 5 6) 7)

You never have to use quasiquotation since a list can always be constructed "by hand" using listconsjoin, and so on.  However it's often convenient.  It's especially nice in macros, because it makes the definition of the macro look like the code it expands into.

(mac when (test . body)
  `(if ,test (do ,@body)))


Comments