define-syntax
ラケットを使用して、またはdefine-syntax-rule
ラケットでキャプチャ マクロを定義する最も簡単な方法は何ですか?
具体的な例として、aif
CL スタイルのマクロ システムの簡単な例を次に示します。
(defmacro aif (test if-true &optional if-false)
`(let ((it ,test))
(if it ,if-true ,if-false)))
アイデアは、and句it
の結果にバインドされるということです。素朴な音訳(オプションの代替を差し引いたもの)はtest
if-true
if-false
(define-syntax-rule (aif test if-true if-false)
(let ((it test))
(if it if-true if-false)))
これは問題なく評価されますit
が、句で使用しようとするとエラーになります。
> (aif "Something" (displayln it) (displayln "Nope")))
reference to undefined identifier: it
anaphora
卵は次のように実装さaif
れます
(define-syntax aif
(ir-macro-transformer
(lambda (form inject compare?)
(let ((it (inject 'it)))
(let ((test (cadr form))
(consequent (caddr form))
(alternative (cdddr form)))
(if (null? alternative)
`(let ((,it ,test))
(if ,it ,consequent))
`(let ((,it ,test))
(if ,it ,consequent ,(car alternative)))))))))
ir-macro-transformer
しかし、Racket は定義も文書化もしていないようです。