[plt-scheme] boxes?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Jan 24 08:30:32 EST 2007

On Jan 23, 2007, at 10:49 PM, John Clements wrote:

> On Jan 23, 2007, at 7:17 PM, Gregory Woodhouse wrote:
>> On Jan 23, 2007, at 5:42 PM, Carl Eastlund wrote:
>>> The problem here is not that numbers and lists work differently, but
>>> that lists and *variables* work differently.  The (set! ...) form
>>> works on variables, not values; the set-car! form works on  
>>> values.  It
>>> would be possible to implement integers that had a "set-integer!"
>>> procedure, so you could modify an integer as it was passed  
>>> around, but
>>> that's not the same as set!.
>> Fair enough, but my point (rather a trivial one, perhaps) was that  
>> the similarities in syntax can be confusing.
>> My understanding (right or wrong) is that (set! x 4) changes the  
>> binding of the nearest enclosing x to 4. At least that's how I  
>> implemented it in my evaluator.
> No, you're exactly right: set! changes a _binding_.

1. Careful! Denotationally the binding of a variable is determined by  
its environment, and in this sense, set! doesn't change the binding  
at all. A variable is always associate with one and the same  
location. What does change is the association between the location  
and the value.

2. Also: I used to teach that a set! must cross a lambda boundary to  
be useful. If you follow this advice, i.e.,

    (lambda (x y) (set! x 10) ...) is bad
    (lambda (x) (lambda (y) (set! x 10) ...)) is good,

then set! is also guaranteed to change a value, namely, the closure  
in which it lives.

3. If you think Scheme is bad, imagine the confusion that Javars and  
their ilk must have:

  x = x + 1;

could be (a) a variable assignment, pretty much like the bad set! or  
(b) a variable assignment that is kind of okay (because it crosses  
the lambda binding in a for/blah) or (c) a field assignment in the  
spirit of

  this.x = this.x + 1;

Of course, if you always insist on writing object-ref.field-name,  
then you won't be confused easily. But the // = // syntax of evil- 
syntax languages is far more burdensome than set! vs set-car!.

-- Matthias

Posted on the users mailing list.