<div dir="ltr"><div>Thank you very much for your reply Marc!</div><div><br></div><div>>This implementation technique has problems:</div><div>></div><div>>- the cost of call/cc will be proportional to the number of parameters</div>
<div>>  (because at the time of creating a continuation you have to take a</div><div>>  snapshot of all those parameters, and when the continuation is</div><div>>  invoked you have to copy the snapshot back to the parameters)</div>
<div><br></div><div>The cost of call/cc would be proportional to the number of</div><div>parameterize (and hence dynamic-wind) calls you would be currently</div><div>nested in right? Other parts of the program, that are not as deeply</div>
<div>nested in parameterize, would not be affected. Is my understanding correct?</div><div><br></div><div>>- it doesn't handle mutation (you would have to introduce cells for</div><div>>  that, and dereference these cells when accessing the parameter)</div>
<div><br></div><div>I don't quite understand this aspect. </div><div><br></div><div>I expect:</div><div>   (parameterize [(a 3)]</div><div>      ... body ...)   </div><div>to be roughly equivalent to:</div><div>   (let [(old-a a)]</div>
<div>      (dynamic-wind</div><div>         (lambda () (set! a 3))</div><div>         (lambda () ... body ...)</div><div>         (lambda () (set! a old-a))))</div><div>So I would expect to be able to use (set! a 10) from within the body</div>
<div>just like normal. </div><div>      </div><div>>- it doesn't work in a multithreaded environment (the content of the</div><div>>  parameters will be clobbered by the different threads operating on</div><div>
>  those parameters)</div><div><br></div><div>Yes. Threading & Dynamic bindings together seem subtle. </div><div><br></div><div>>> 2) Is there a way to get a closure to close over the dynamic</div><div>>> environment? The use case is that I want to use dynamic variables</div>
<div>>> *purely* to avoid having to pass commonly used arguments</div><div>>> explicitly. Thus I am using the dynamic variable as an "implicitly"</div><div>>> passed argument. And in such a case, I would like closures to refer</div>
<div>>> to the value of the dynamic variable at the time of closure</div><div>>> creation.</div><div>></div><div>>Closures capture the lexical variables.  Parameter objects can't be</div><div>>automatically captured by closures because that would mean that a</div>
<div>>calling function can't use parameter objects to pass implicit</div><div>>parameters to the called function.  So if you want to capture the</div><div>>value of some parameter objects you will have to do it manually,</div>
<div>>i.e. something like:</div><div>></div><div>>(define p (make-parameter 42))</div><div>></div><div>>(define (make-adder) ;; creates a function which adds the value of</div><div>>  parameter p (let ((captured-p (p))) (lambda (x) (parameterize ((p</div>
<div>>  captured-p)) (+ x (p))))))</div><div>></div><div>>(define a (make-adder))</div><div>></div><div>>(p 1000) ;; change value of parameter p</div><div>></div><div>>(pp (a 10)) => 52</div><div>></div>
<div>>(p) => 1000</div><div><br></div><div>I understand the example that you gave on how to manually capture the</div><div>parameter object. Could you clarify what you mean by "a</div><div>calling function can't use parameter objects to pass implicit</div>
<div>parameters to the called function"?</div><div><br></div><div><br></div><div>Thanks very much for your help.</div><div>  -Patrick</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Nov 3, 2013 at 6:03 AM, Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On Nov 3, 2013, at 12:27 AM, Patrick Li <<a href="mailto:patrickli.2001@gmail.com">patrickli.2001@gmail.com</a>> wrote:<br>
<br>
> Hello!<br>
><br>
> I am interested in the design choices surrounding SRFI-39: the<br>
> make-parameter function and parameterize special form. There are<br>
> specifically 2 issues that I don't understand.<br>
><br>
> 1) The parameter is currently a function that yields the value its<br>
> holding when it is called. What is the purpose of forcing the user to<br>
> "dereference" the parameter in this way? This is constrast to<br>
> implementing dynamic variables using a mutable global variable, and<br>
> having parameterize directly mutate the global variable within a<br>
> dynamic-wind context.<br>
<br>
</div>This implementation technique has problems:<br>
<br>
- the cost of call/cc will be proportional to the number of parameters (because at the time of creating a continuation you have to take a snapshot of all those parameters, and when the continuation is invoked you have to copy the snapshot back to the parameters)<br>

<br>
- it doesn't handle mutation (you would have to introduce cells for that, and dereference these cells when accessing the parameter)<br>
<br>
- it doesn't work in a multithreaded environment (the content of the parameters will be clobbered by the different threads operating on those parameters)<br>
<div class="im"><br>
> 2) Is there a way to get a closure to close over the dynamic<br>
> environment? The use case is that I want to use dynamic variables<br>
> *purely* to avoid having to pass commonly used arguments<br>
> explicitly. Thus I am using the dynamic variable as an "implicitly"<br>
> passed argument. And in such a case, I would like closures to refer to<br>
> the value of the dynamic variable at the time of closure creation.<br>
<br>
</div>Closures capture the lexical variables.  Parameter objects can't be automatically captured by closures because that would mean that a calling function can't use parameter objects to pass implicit parameters to the called function.  So if you want to capture the value of some parameter objects you will have to do it manually, i.e. something like:<br>

<br>
(define p (make-parameter 42))<br>
<br>
(define (make-adder) ;; creates a function which adds the value of parameter p<br>
  (let ((captured-p (p)))<br>
    (lambda (x)<br>
      (parameterize ((p captured-p))<br>
        (+ x (p))))))<br>
<br>
(define a (make-adder))<br>
<br>
(p 1000) ;; change value of parameter p<br>
<br>
(pp (a 10)) => 52<br>
<br>
(p) => 1000<br>
<span class="HOEnZb"><font color="#888888"><br>
Marc<br>
<br>
</font></span></blockquote></div><br></div>