2010-03-26 36 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