[plt-scheme] vectors by value
John, look at this mod of your code:
(define mylist '(hi-there))
(define (changemylist x)
(begin
(set! x '(yall))
x))
(define myvector #(hi-there))
(define changemyvector
(lambda (x)
(vector-set! x 0 'yall)
x))
mylist
(changemylist mylist)
mylist
myvector
(changemyvector myvector)
myvector
(define mylist '(whatcha-doin?))
mylist
(set! mylist '(nuthin))
mylist
Welcome to DrScheme, version 203.
Language: Pretty Big (includes MrEd and Advanced) custom.
(hi-there)
(yall)
(hi-there)
#1(hi-there)
#1(yall)
#1(yall)
(whatcha-doin?)
(nuthin)
>
The 3rd `whatcha-doin?/nuthin' exchange shouldn't surprise you.
What's suprising is the first one, that `changemylist' didn't change
`mylist'! If you a standard Scheme source, e.g. R5RS or SICP, you'll
see that the usual explanation of this is in terms of environments:
that `changemylist' defines an environment and binds `x' to a
location, and stores first `hi-there' in this location, and then
stores `yall' in this location. but `changemylist' does nothing to the
global value of `mylist'. Pretty mind-boggling stuff, and HtDP offers
a simplified semantics.