動的環境を指定してthunkを呼ぶ

shift/reset と動的環境の関係についていろいろ考えていた時に出てきた副産物.

(use util.match)

(define (capture/de proc)
  (match (let/cc de
           (list (proc de)))
    ((r) r)
    (thunk
     (thunk))))

(define (with/de de thunk)
  (let/cc cont
    (de (.$ cont thunk))))

(define (de-test thunk)
  (dynamic-wind
    (^() (print "before thunk"))
    thunk
    (^() (print "after thunk"))))
gosh> (capture/de (^k (de-test (^() (with/de k (^() (print "hoge") 42))))))
before thunk
after thunk
hoge
before thunk
after thunk
42
gosh> (regexp-replace-all #/./ "aa" (^m (print "hoge") "b"))
"hoge\nbhoge\nb"
gosh> (capture/de (^k (regexp-replace-all #/./ "aa" (^m (with/de k (^() (print "hoge"))) "b"))))
hoge
hoge
"bb"

深く考えてないけど動いてそう.