2010/05/26

syntax-rules: shadow shadows

おー。最初見たときは、驚きました。
;; shadow
(define-syntax shadow
(syntax-rules ()
((_ used-arg val body)
(let ((used-arg val))
body))))
(define a 1)
a
; -> 1
(shadow a 10
a)
; -> 10
a
; -> 1
view raw shadow.scm hosted with ❤ by GitHub

では、こうしたら。
(define-syntax shadows
(syntax-rules ()
((_ ((used-arg val) ...) body ...)
(let ((used-arg val) ...)
body ...))))
(define b 100)
b
; -> 100
(define c "Hello")
c
; -> "Hello"
(define d 'dead)
d
; -> dead
(shadows ((a b)
(b 5)
(c "world")
(d 'live))
(values a b c d))
;; 100
;; 5
;; "world"
;; live
view raw shadows.scm hosted with ❤ by GitHub

おー。便利。
待てよ、これなんてlet?

展開。
(let ((- 'minus))
(macroexpand
'(dotimes 5 (print "hello"))))
;; (#<identifier user#let> #0=#<identifier user#loop>
;; ((#1=#<identifier user#i> 5))
;; (#<identifier user#when>
;; (#<identifier user#<> 0 #1#)
;; (print "hello")
;; (#0# (#<identifier user#-> #1# 1))))


プログラミングGaucheThe Reasoned Schemer

0 件のコメント:

コメントを投稿