2010/03/29

defmacro: when, unless

実は、マクロについては「プログラミングGauche」をサラッと眺めただけだったりします。
;; when, unless, macroexpand
;; http://www.sampou.org/scheme/t-y-scheme/t-y-scheme-Z-H-10.html#node_chap_8
;; when
(define-macro my-when
(lambda (test . branch)
(list 'if test
(cons 'begin branch))))
;; quasiquote, unquote, unquote-splicing
(define-macro my-when
(lambda (test . branch)
`(if ,test
(begin ,@branch))))
((lambda (x)
(my-when (> x 5)
(display "ok")
(newline)
(display x)
(newline))) 6)
;; ok
;; 6
;; #<undef>
(macroexpand '((lambda (x)
(my-when (> x 5)
(display "ok")
(newline)
(display x)
(newline))) 6))
; -> ((lambda (x) (my-when (> x 5) (display "ok") (newline) (display x) (newline))) 6)
;; unless
(define-macro my-unless
(lambda (test . branch)
(list 'if (list 'not test)
(cons 'begin branch))))
;; quasiquote, unquote, unquote-splicing
(define-macro my-unless
(lambda (test . branch)
`(if (not ,test)
(begin ,@branch))))
((lambda (x)
(my-unless (> x 5)
(display "ok")
(newline)
(display x)
(newline))) 3)
;; ok
;; 3
;; #<undef>
(macroexpand '((lambda (x)
(my-unless (> x 5)
(display "ok")
(newline)
(display x)
(newline))) 3))
; -> ((lambda (x) (my-unless (> x 5) (display "ok") (newline) (display x) (newline))) 3)
view raw when-unless.scm hosted with ❤ by GitHub

プログラミングGauche

0 件のコメント:

コメントを投稿