[plt-scheme] Meaning of set-box!

From: John Clements (clements at brinckerhoff.org)
Date: Mon Jan 22 15:28:17 EST 2007

On Jan 22, 2007, at 12:03 PM, Woodhouse, Gregory J. wrote:

> My understanding is that set-box! is used to modify the contents of  
> a box, but neither of these seem to work
>> (define (my-set-bang x a)
>     (set-box! x a))
>> (define x 1)
>> (my-set-bang (box x) 2)
>> x
> 1
>> (set-box! (box x) 2)
>> x
> 1

What does it mean to "change the contents of a box"?  You're reading  
this as "the thing in the box changes," whereas the actuality is that  
"the box now contains a new thing."  That's what boxes do.

Put it a different way; you're putting a three into the box.  If set- 
box! meant "the thing in the box changes," then your program would mean

"change three into four"

This probably isn't what you want.

> On the other hand, I can see that the box contents does change,  
> even if the variable x does not change
>> (define x 1)
>> (define y (box x))
>> (set-box! y 2)
>> x
> 1
>> (unbox y)
> 2

Yes, you're right. The box now contains a new value.

> What I'm trying to do is define a mutator. to be specific, I want  
> to make incremental updates to an environment/symbol table. What  
> I'm doing now is something like

For mutable bindings, you want to bind variable names to boxes.

Let me strongly suggest that you go and read a PL textbook;  
"Programming Languages: Application & Interpretation" is (AFAIK) free  
and online and deals EXTENSIVELY with this very topic.

All the best,

John Clements

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

Posted on the users mailing list.