<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">1. Is the following program in HtDP Beginning Student sufficient to<br>
show the difference between dynamic and static scope?<br>
-----------<br>
(define n 5)<br>
<br>
(define (f x)<br>
(+ x n))<br>
<br>
(define (g n)<br>
(f 3))<br>
-----------<br>
If I'm reasoning correctly, with static scope, f is saved with its<br>
closure, so n is forever bound to 5 and g applied to anything will<br>
always return 8. In dynamic scope, n would get its value from the<br>
argument passed into g. So,<br>
<br>
(g 7) --> 8 = static scoping<br>
(g 7) --> 10 = dynamic scoping</blockquote><div><br><br>Yes that's right. Static scope means that functions defer to their lexical context to find free variables (variables not in the function's parameter list), whereas dynamic scope means they defer to the context in which they are called.<br>
<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">2. In creating a closure, it's really only necessary to save values<br>
that the function uses, rather than the whole context</blockquote><div><br>I'd be interested to know if you can in general do static checking on closures to free up memory. I'm doing an implementation in Ruby, having only just started learning Scheme. Right now, each function call creates a symbol table that inherits from the scope in which the function is defined, and definitions retain a reference to the current scope. That way, returning a lambda from a function keeps a reference to the symbol table in use inside the outer function, but I'm not doing any clean-up on it right now as I don't know what errors that might cause further down the line.<br>
</div></div><br>