2010-03-26 35 views
6

我一直在编写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)) 

我不知道我怎么会用一个需要它的参数的宏写成一个向量:

(maC#(1 2 3 4)) 
=> 
1 
4 

是否有某种技术使用这些模式?

谢谢!

回答

1

宏可能不需要将其参数写成矢量,但在它们出现时提供有用的行为。最值得注意的例子很可能是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) 

作为另一个例子,见this pattern matching package其允许对向量匹配和因此使用矢量图形在其宏定义(包括在链接到的页面与包元数据一起) 。

+0

谢谢!现在它变得更有意义了! :-) – Jay 2010-04-01 04:12:07