Created
September 8, 2010 05:17
scheme, gauche, dlambda, let over lambda, LOL
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
;; http://letoverlambda.com/index.cl/guest/chap5.html#sec_7 | |
(define-syntax dlambda | |
(syntax-rules (else) | |
((_ (msg1 (darg1 ...) dbody1 ...)(msg2 (darg2 ...) dbody2 ...) ...) | |
(lambda (key . args) | |
(case key | |
((msg1)(apply (lambda (darg1 ...) | |
dbody1 ...) args)) | |
((msg2)(apply (lambda (darg2 ...) | |
dbody2 ...) args)) | |
... | |
(else key)) | |
)))) | |
(define count-test | |
(let ((count 0)) | |
(dlambda | |
(:reset ()(set! count 0)) | |
(:inc (n)(inc! count n)) | |
(:dec (n)(dec! count n)) | |
(:bound (lo hi) | |
(set! count (min hi (max lo count))))))) | |
(count-test :inc 1) | |
;; 1 | |
(count-test :inc 1) | |
;; 2 | |
(count-test :hoge) | |
;; :hoge | |
(count-test :dec 1) | |
;; 1 | |
(count-test :dec 1) | |
;; 0 | |
(count-test :dec 100) | |
;; -100 | |
(count-test :bound -10 10) | |
;; -10 | |
(count-test :reset) | |
;; 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment