expander
expander を使うのがオーソドックスみたいですね。Gauche なら Common Lisp と同じく、macroexpand, macroexpand-1 などがあります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax nth-value | |
(syntax-rules () | |
((_ n values-body) | |
(call-with-values | |
(lambda () values-body) | |
(lambda vals | |
(list-ref vals n)))))) | |
(macroexpand-1 '(nth-value 0 (apply values '(a b c d e f g)))) | |
;; (#<identifier user#call-with-values> | |
;; (#0=#<identifier user#lambda> () (apply values '(a b c d e f g))) | |
;; (#0# #1=#<identifier user#vals> (#<identifier user#list-ref> #1# 0))) | |
(macroexpand '(nth-value 3 (apply values '(a b c d e f g)))) | |
;; (#<identifier user#call-with-values> | |
;; (#0=#<identifier user#lambda> () (apply values '(a b c d e f g))) | |
;; (#0# #1=#<identifier user#vals> (#<identifier user#list-ref> #1# 3))) | |
(macroexpand '(nth-value 10 (apply values '(a b c d e f g)))) | |
;; (#<identifier user#call-with-values> | |
;; (#0=#<identifier user#lambda> () (apply values '(a b c d e f g))) | |
;; (#0# #1=#<identifier user#vals> (#<identifier user#list-ref> #1# 10))) |
mac
On Lisp よろしく mac マクロを定義しておくと便利そうです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax mac | |
(syntax-rules () | |
((_ code) | |
(macroexpand-1 'code)))) | |
(mac (nth-value 0 (apply values '(a b c d e f g)))) | |
;; (#<identifier user#call-with-values> | |
;; (#0=#<identifier user#lambda> () (apply values '(a b c d e f g))) | |
;; (#0# #1=#<identifier user#vals> (#<identifier user#list-ref> #1# 0))) |
リーダーマクロ
Gacuhe #?= リーダーマクロはどうでしょう?うーん。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax nth-value | |
(syntax-rules () | |
((_ n values-body) | |
#?=(call-with-values | |
(lambda () values-body) | |
(lambda vals | |
(list-ref vals n)))))) | |
(nth-value 0 (apply values '(a b c d e f g))) | |
;; #?=(call-with-values (lambda () (apply values '(a b c d e f g))) ... | |
;; #?- a | |
;; a | |
(define-syntax nth-value | |
(syntax-rules () | |
((_ n values-body) | |
#?=(call-with-values | |
(lambda () #?=values-body) | |
(lambda vals | |
(list-ref #?=vals n)))))) | |
(nth-value 0 (apply values '(a b c d e f g))) | |
;; #?=(call-with-values (lambda () (debug-print (apply values '(a b ... | |
;; #?="(stdin)":140:(apply values '(a b c d e f g)) | |
;; #?- a | |
;; #?+ b | |
;; #?+ c | |
;; #?+ d | |
;; #?+ e | |
;; #?+ f | |
;; #?+ g | |
;; #?=vals | |
;; #?- (a b c d e f g) | |
;; #?- a | |
;; a |
quote
これは初見。任意のパターンの展開部分を quote しちゃう。これは意外と便利かも知れません。*** Debugging trick One very easy debugging trick is to wrap the template with a quote:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax nth-value | |
(syntax-rules () | |
((_ n values-body) | |
'(call-with-values | |
(lambda () values-body) | |
(lambda vals | |
(list-ref vals n)))))) | |
(nth-value 0 (apply values '(a b c d e f g))) | |
;; (call-with-values (lambda () (apply values '(a b c d e f g))) | |
;; (lambda vals (list-ref vals 0))) | |
(nth-value 3 (apply values '(a b c d e f g))) | |
;; (call-with-values (lambda () (apply values '(a b c d e f g))) | |
;; (lambda vals (list-ref vals 3))) | |
(nth-value 10 (apply values '(a b c d e f g))) | |
;; (call-with-values (lambda () (apply values '(a b c d e f g))) | |
;; (lambda vals (list-ref vals 10))) | |
複数のパターンがある場合に、特定のパターンだけ quote してみるとか・・・。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax letstar | |
(syntax-rules () | |
((_ () body ...) | |
'(let () | |
body ...)) | |
((_ ((var init)) body ...) | |
(let ((var init)) | |
body ...)) | |
((_ ((var1 init1)(var2 init2) ...) body ...) | |
(let ((var1 init1)) | |
(letstar ((var2 init2) ...) body ...))))) | |
(letstar () | |
(display 'a)) | |
;; (let () (display 'a)) | |
(letstar ((x 1)) | |
(display 'a) | |
(display x)) | |
;; a1#<undef> | |
(letstar ((x 1) | |
(y 2) | |
(z (+ x y))) | |
(print z)) | |
;; 3 | |
;; #<undef> |
マクロ初心者な私の知る限りこんなところでしょうか・・・。
0 件のコメント:
コメントを投稿