[gambit-list] dynamic scope

Taylor R Campbell campbell at mumble.net
Sun Feb 1 15:20:07 EST 2009


   Date: Sun, 1 Feb 2009 00:05:49 -0500
   From: Marc Feeley <feeley at iro.umontreal.ca>

     (let ((thunk (lambda () (g 2)))
           (new-foo (lambda (x) (* x 3)))
           (old-foo foo))
       (dynamic-wind
         (lambda () (set! foo new-foo))
         thunk
         (lambda () (set! foo old-foo))))

This idiom doesn't respect assignments to FOO that happen outside the
dynamic extent of the call to THUNK.  If control exits the extent, FOO
is set to another value outside the extent, and control re-enters the
extent, then when control exits again, FOO will have its value
reverted to whatever it was when control first entered the extent,
forgetting its current value outside the extent.  Thus the dynamic
binding is not transparent to the enclosing extent.  To make it
transparent, write:

(let ((new-foo ...) (old-foo #f))
  (dynamic-wind
   (lambda ()
     (set! old-foo foo)
     (set! foo new-foo))
   thunk
   (lambda ()
     (set! foo old-foo)
     (set! old-foo #f))))



More information about the Gambit-list mailing list