[racket] Nested scope in D vs Racket

From: John Clements (clements at brinckerhoff.org)
Date: Mon Aug 23 12:11:52 EDT 2010

This brings up an interesting moment in PLAI that always elicits totally blank stares: the moment in lecture that matches this part of the book, on page 34 of the current online version:

"Hopefully we can agree that the value of this program is 8 (the left x in the addition evaluates to 5, the right x is given the value 3 by the inner with, so the sum is 8). The refined substitution algorithm, however, converts this expression into

{with {x 5} {+ 5 {with {x 3} 5}}}

which, when evaluated, yields 10. What went wrong here? Our substitution algorithm respected binding instances, but not their scope.
In the sample expression, the with introduces a new scope for the inner x. The scope of the outer x is shadowed or masked by the inner binding. Because substitution doesn’t recognize this possibility, it incorrectly substitutes the inner x.

Many students say to me, "but... you're not allowed to define the same name twice!"  

At a minimum, then, this discussion has an interesting association with the definition of substitution (and the teaching thereof).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4669 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20100823/3bfc552f/attachment.p7s>

Posted on the users mailing list.