如何在语法规则中使用矢量模式?
-
22-09-2019 - |
题
我一直在写Common Lisp的宏,所以方案的R5RS宏是有点不自然给我。我的认为的我有这个想法,但我不明白怎么一会用在语法规则向量模式:
(define-syntax mac
(syntax-rules ()
((mac #(a b c d))
(let ()
(display a)
(newline)
(display d)
(newline)))))
(expand '(mac #(1 2 3 4))) ;; Chicken's expand-full extension shows macroexpansion
=> (let746 () (display747 1) (newline748) (display747 4) (newline748))
我不明白我怎么会用一个需要它的参数写为载体的宏:
(mac #(1 2 3 4))
=>
1
4
是否有某种技术的使用这些模式?
谢谢!
解决方案
一个宏可能不需要它的参数被写入作为载体,还提供一种用于当它们是有用的行为。最值得注意的例子很可能是quasiquote:
;; a couple of test variables
(define foo 1)
(define bar 2)
;; vector literals in Scheme are implicitly quoted
#(foo bar) ; returns #(foo bar), i.e. a vector of two symbols
;; however quasiquote / unquote can reach inside them
`#(,foo ,bar) ; returns #(1 2)
作为另一实例,请参见此其允许在矢量匹配模式匹配包因此使用矢量图形在其宏定义(包括在链接到的页面与包元数据一起)。
不隶属于 StackOverflow