2010/05/26

syntax-rules: try

macroの方のPDFを一通り読み終えたので、continuationの方のPDFを読み始めました。
英語の方は、ほとんどわかりません。The Seasoned SchemerThe Little Schemer, 4th Editionは雰囲気と勢いで読みました。
The Seasoned Schemerに出てくる try のことを思い出しました。

;; try
(define-syntax try
(syntax-rules ()
((_ var a . b)
(let/cc success
(let/cc var
(success a)) . b))))
(define (accept-odd-only ls throw)
(let ((ret (every odd? ls)))
(if ret
ls
(throw ret))))
(use srfi-1)
(try throw
(accept-odd-only '(1 2 3 4 5) throw)
(print "oops")
(print "error!"))
;; oops
;; error!
;; #<undef>
(try throw
(accept-odd-only '(1 3 5 7 9) throw)
(print "oops")
(print "error!"))
;; (1 3 5 7 9)
view raw try.scm hosted with ❤ by GitHub




The Little Schemer, 4th EditionThe Seasoned Schemer

0 件のコメント:

コメントを投稿