On 5/4/07, William Ramsay <address@hidden> wrote:
You confirmed what I originally said (although I probably said it
poorly). If you set B to (vector-ref A 2)
as in line 4 of your example, both B and A-2 equal the same thing.
Yes.
The question actually is "how do you
get the value of A-2 in such a way that you can work with it?" I seem
to recall that Common Lisp has a
dup procedure that makes a copy of an object. Other than using
vector-copy! and copying the whole thing,
is there a way to copy a single item?
Vector-copy! will not do what you think it will. If I have a sequence
of buildings in my vector, and you call:
(vector-copy! graham-v bill-v)
(vector-for-each (lambda (i bldg) (fire-rocket-at bldg)) bill-v)
then
(vector-every destroyed? graham-v) ==> #t
My copy of the vector now refers to a sequence of rubble-filled lots.
Our sequences are just sequences of *references* to buildings;
vector-copy! is not a "deep copy" or "recursive copy", so your actual
elements are identical to mine.
If you want deep (recursive) copies of a structure, you could try
(object-copy), in the lolevel library. Note that it is not a RnRS or
SRFI procedure. Here's an example of copying one element of a vector:
#;4> (use lolevel)
#;5> (define zero "zero")
#;6> (define A (vector zero "one" "two"))
#;7> (eq? zero (vector-ref A 0))
#t ;; zero and A[0] are the same object.
#;8> (define B (object-copy (vector-ref A 0)))
#;9> (eq? B (vector-ref A 0))
#f ;; but B is a different object.
#;10> (eq? B zero)
#f
#;11> (string=? B zero)
#t ;; comparison by value still works.
#;12> (string-set! B 0 #\Z)
#;13> (list B Z (vector-ref A 0))
("Zero" "zero" "zero")
But this isn't typically used: there are reasons why object-copy is
squirreled away in the lolevel library. Are you certain that you need
a copy of this object in order to "use" it in your program?
Graham