(quasiquote hqq templatei) syntax
`qq templatei syntax
"Backquote" or "quasiquote" expressions are useful for constructing a list or vector
structure when most but not all of the desired structure is known in advance. If no
commas appear within the hqq templatei, the result of evaluating `qq templatei is equivalent
to the result of evaluating 'qq templatei. If a comma appears within the qq template,
however, the expression following the comma is evaluated ("unquoted") and its result is
inserted into the structure instead of the comma and the expression. If a comma appears
followed immediately by an atsign(@), then the following expression must evaluate to a list;
the opening and closing parentheses of the list are then "stripped away" and the elements
of the list are inserted in place of the comma at-sign expression sequence. A comma at-sign
should only appear within a list or vector `qq template.
`(list ,(+ 1 2) 4) =) (list 3 4)
(let ((name 'a)) `(list ,name ',name))
=> (list a (quote a))
`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)
=> (a 3 4 5 6 b)
`(( foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))
=> ((foo 7) . cons)
`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8)
=> #(10 5 2 4 3 8)
Quasiquote forms may be nested. Substitutions are made only for unquoted components appearing
at the same nesting level as the outermost backquote. The nesting level increases by one inside
each successive quasiquotation, and decreases by one inside each unquotation.
`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
=> (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
(let ((name1 'x)
(name2 'y))
`(a `(b ,,name1 ,',name2 d) e))
=> (a `(b ,x ,'y d) e)
The two notations `qq templatei and (quasiquote `qq templatei) are identical in all respects.
,`expressioni is identical to (unquote hexpressioni), and ,@hexpressioni is identical to
(unquote-splicing hexpressioni). The external syntax generated by write for two-element lists
whose car is one of these symbols may vary between implementations.
(quasiquote (list (unquote (+ 1 2)) 4))
=> (list 3 4)
'(quasiquote (list (unquote (+ 1 2)) 4))
=> `(list ,(+ 1 2) 4)
i.e., (quasiquote (list (unquote (+ 1 2)) 4))
Unpredictable behavior can result if any of the symbols quasiquote, unquote, or unquote-splicing
appear in positions within a hqq templatei otherwise than as described above.
`qq templatei syntax
"Backquote" or "quasiquote" expressions are useful for constructing a list or vector
structure when most but not all of the desired structure is known in advance. If no
commas appear within the hqq templatei, the result of evaluating `qq templatei is equivalent
to the result of evaluating 'qq templatei. If a comma appears within the qq template,
however, the expression following the comma is evaluated ("unquoted") and its result is
inserted into the structure instead of the comma and the expression. If a comma appears
followed immediately by an atsign(@), then the following expression must evaluate to a list;
the opening and closing parentheses of the list are then "stripped away" and the elements
of the list are inserted in place of the comma at-sign expression sequence. A comma at-sign
should only appear within a list or vector `qq template.
`(list ,(+ 1 2) 4) =) (list 3 4)
(let ((name 'a)) `(list ,name ',name))
=> (list a (quote a))
`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)
=> (a 3 4 5 6 b)
`(( foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))
=> ((foo 7) . cons)
`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8)
=> #(10 5 2 4 3 8)
Quasiquote forms may be nested. Substitutions are made only for unquoted components appearing
at the same nesting level as the outermost backquote. The nesting level increases by one inside
each successive quasiquotation, and decreases by one inside each unquotation.
`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
=> (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
(let ((name1 'x)
(name2 'y))
`(a `(b ,,name1 ,',name2 d) e))
=> (a `(b ,x ,'y d) e)
The two notations `qq templatei and (quasiquote `qq templatei) are identical in all respects.
,`expressioni is identical to (unquote hexpressioni), and ,@hexpressioni is identical to
(unquote-splicing hexpressioni). The external syntax generated by write for two-element lists
whose car is one of these symbols may vary between implementations.
(quasiquote (list (unquote (+ 1 2)) 4))
=> (list 3 4)
'(quasiquote (list (unquote (+ 1 2)) 4))
=> `(list ,(+ 1 2) 4)
i.e., (quasiquote (list (unquote (+ 1 2)) 4))
Unpredictable behavior can result if any of the symbols quasiquote, unquote, or unquote-splicing
appear in positions within a hqq templatei otherwise than as described above.
本文介绍回引号(quasiquote)和准引号(quasiquote)的概念及用法,这两种表达方式用于构造列表或向量结构,当大部分结构已知而部分需要动态填充时特别有用。文章通过多个示例展示了如何使用逗号和逗号加@符号进行未引用表达式的插入。
83

被折叠的 条评论
为什么被折叠?



