Re problems in multithreaded programs:<br><br>     The dynamic environment is composed of two parts: the "local dynamic environment" and the "global dynamic environment".  There is a single global dynamic environment, and it is used to lookup parameter objects that can't be found in the local dynamic environment.<br>
<br>is that the main reason? that with the parameter method, i'm changing the 'local env' (which each thread has it's own) whereas with the dynamic-wind method, I'm setting a var in the global env (which all the threads share)<br>
<br><div class="gmail_quote">On Sat, Jan 31, 2009 at 9:05 PM, Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d"><br>
On 31-Jan-09, at 11:44 PM, lowly coder wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Great, the following works:<br>
<br>
cat test.scm<br>
(define (foo x) (* x 2))<br>
<br>
(define (g x) (foo x))<br>
<br>
<br>
(pp (g 2))<br>
(pp<br>
 (let ((old-func '()))<br>
   (dynamic-wind<br>
     (lambda ()<br>
       (set! old-func foo)<br>
       (set! foo (lambda (x) (* x 3))))<br>
     (lambda () (g 2))<br>
     (lambda () (set! foo old-func)))))<br>
<br>
(pp (g 2))<br>
<br>
4<br>
6<br>
4<br>
<br>
two questions:<br>
1) can anything go wrong with variable capture / aliasing? [intuitively, I believe no, so long as I don't use 'old-func' in my thunk<br>
2) is there a more elegant/idiomatic way to do this?<br>
</blockquote>
<br></div>
In terms of elegance I prefer this (which eliminates all variable capture problems):<br>
<br>
 (let ((thunk (lambda () (g 2)))<br>
       (new-foo (lambda (x) (* x 3)))<br>
       (old-foo foo))<br>
   (dynamic-wind<br>
     (lambda () (set! foo new-foo))<br>
     thunk<br>
     (lambda () (set! foo old-foo))))<br>
<br>
But once again this will not work right if multiple threads are dynamically scoping foo simultaneously, but the parameterize based approach will work fine.<br><font color="#888888">
<br>
Marc<br>
<br>
</font></blockquote></div><br>