Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created September 8, 2010 05:17
scheme, gauche, dlambda, let over lambda, LOL
;; 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