Carp/docs/Quasiquotation.md
Veit Heller 2023c93d62
feat: Quasiquotation (#1129)
* feat: quasiquotation

* test: add tests for quasiquotation

* fix: fix typo in call to doc

* fix: do not evaluate quasiquote too eagerly

* test: pull quasiquote test into macro

* docs: fix unquote example with better constant

* feat: add quasiquote literals

* refactor: simplify reader macros
2021-01-15 10:50:04 +01:00

1.2 KiB
Raw Blame History

Quasiquotation

Quasiquotation is a way to quote parts of a list while evaluating others. Its only available in the dynamic parts of the program.

Quasiquotation enables the user to intersperse evaluated or “unquoted” portions into a unevaluated or “quoted” list.

(defdynamic x 2)

(quasiquote (+ (unquote x) 1)) ; => (+ 2 1)

; also available as literals, quasiquote becomes `
; and unquote becomes %
`(+ %x 1) ; => (+ 2 1)

Note that unquoting only makes sense inside quasiquote forms and using it outside will lead to errors at macro expansion time.

Since quasiquotation primarily deals with lists, the user might sometimes want to intersperse another list of values flatly, “splicing” them in. For this case Carp provides unquote-splicing.

(defdynamic x '(1 2))

(quasiquote (+ (unquote-splicing x))) ; => (+ 1 2)

; the literal for unquote-splicing is %@
`(+ %@x) ; => (+ 1 2)

Please note that while the code examples above only use variables, any expression can be used inside the unquote variants.

(quasiquote (+ (unquote-splicing (map inc [1 2])))) ; => (+ 2 3)
; or
`(+ %@(map inc [1 2])) ; => (+ 2 3)