2010/06/02

syntax-rules: nth-value


多値を返す手続きから受け取った多値の n 番目を返すマクロ。
;; nth-value
;; http://web.archive.org/web/20060616054033/home.comcast.net/~prunesquallor/macro.txt
(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)))
; -> a
(nth-value 3 (apply values '(a b c d e f g)))
; -> d
(nth-value 10 (apply values '(a b c d e f g)))
; -> *** ERROR: argument out of range: 10
view raw nth-value.scm hosted with ❤ by GitHub


The Scheme Programming Language, 4th Edition

0 件のコメント:

コメントを投稿