[plt-scheme] vectors by value
Robby, Bill
Thanks both for your replies. Some things are clearer, and some are
murkier...
I realize now that by asking about passing arguments by reference or by
value I was looking at the wrong issue; the issue is that the set! and
vector-set! functions work differently, set! changing the association of
the variable and the value, while vector-set changes the values
themselves.
The murky parts, though, will have to wait for tomorrow; I see if I can
work through the documentation and your suggestions then.
Thanks again,
John
John T. Murphy
University of Arizona
Department of Anthropology
jtmurphy at email.arizona.edu
-----Original Message-----
From: Bill Richter [mailto:richter at math.northwestern.edu]
Sent: Friday, February 07, 2003 7:58 PM
To: jtm at perfectknowledgedb.com
Cc: plt-scheme at qua.cs.brown.edu
Subject: Re: [plt-scheme] vectors by value
John, let me add to Robby's expert analysis:
If this doesn't help, please check out http://www.htdp.org/ and
read part vii (changing the state of variables) and viii (changing
compound values).
You didn't raise a newbie question, because your code won't run in
Advanced Student:
(define mylist '(hi there))
(define (changemylist x)
(begin
(set! x (append x '(this is new)))
x))
Welcome to DrScheme, version 203.
Language: Advanced Student.
set!: expected a defined name after `set!', but found a function
argument name
So 38.4 The Meaning of Advanced Scheme of HtDP will not explain your
code. That's of course a big advantage of Advanced Scheme, the
simplified semantics, which mostly lets you use the substitution rule.
What you need to read is Section 40 (in viii as Robby said), which is
about constructors & mutators. Advanced Student has a define-struct
mechanism that lets you perform the illegal code above. In particular:
Mutable Structures
Mixing set! and Structure Mutators: When a program uses both
set!-expressions and structure mutators, our evaluation rules
fail for some cases. Specifically, they don't explain sharing
properly. Consider this program fragment:
(define the-point (make-posn 3 4))
(define another-point the-point)
(begin
(set! the-point 17)
(= (posn-x another-point) 3))
According to our rules, the two definitions refer to the same
structure. The second one does so by indirection. The
set!-expression changes what the-point stands for, but it
shouldn't affect the second definition. In particular, the
program should produce true. If we were to use our rules in a
naive manner, we would not be able to validate this point.