Jay*_*Jay 6 lisp macros scheme syntax-rules define-syntax
我一直在编写Common Lisp宏,所以Scheme的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))
Run Code Online (Sandbox Code Playgroud)
我不知道我是如何使用一个需要将其参数写成向量的宏:
(mac #(1 2 3 4))
=>
1
4
Run Code Online (Sandbox Code Playgroud)
是否有某种技术使用这些模式?
谢谢!
宏可能不需要将其参数编写为向量,但在它们需要时提供有用的行为。最值得注意的例子可能是准引用:
;; 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)
Run Code Online (Sandbox Code Playgroud)
作为另一个示例,请参阅此模式匹配包,它允许对向量进行匹配,从而在其宏定义中使用向量模式(与包元数据一起包含在链接到的页面上)。