[plt-scheme] boxes?
Greg-
On Jan 23, 2007, at 7:54 PM, gregory.woodhouse at sbcglobal.net wrote:
> The genesis of my original question was a combination of a
> misreading of a manual, and the observation that numbers and pairs
> just don't behave the same way:
>
>> (define (test1 x)
> (set! x 4))
>> (define x 1)
>> (test1 x)
>> x
> 1
>> (define (test2 x)
> (set-car! x 4))
>> (define x '(1 1))
>> (test2 x)
>> x
> (4 1)
>>
There is a lot to observe about the above code. But that code alone
does *not* illustrate any differences between numbers and pairs.
(Joe Marshall posted some example code that does illustrate such
differences.)
I do not think one can fully understand the difference between set!
and set-car! solely from the code you wrote above. Attempting to do
so might lead one to think that set! treats numbers differently from
pairs, when that is not what is going on at all.
Rather than try to jump straight into understanding the many
differences between set! and set-car!, you should see if you can
understand set! on its own.
Here is some pair-free code for you to ponder over; why do you think
we see the results below?
> (define (test1 x)
(set! x 4))
> (define (test2 y)
(set! y 5))
> (define (test3 y)
(set! x 6))
> (define (test4 x)
(set! y 7))
> (define x 1)
> x
1
> (test1 x)
> x
1
> (test2 x)
> x
1
> (test3 x)
> x
6
> (test4 x)
error set!: cannot set undefined identifier: y
> (define y 8)
> y
8
> (test4 x)
> y
7
>
Hint: what are the lexical scopes for the various occurrences of x
and y in the code?
set! is a very different beast from set-car!, set-cdr!, vector-set!,
etc. . .
-Felix