chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Set! question


From: William Ramsay
Subject: Re: [Chicken-users] Set! question
Date: Sat, 05 May 2007 08:08:22 -0400
User-agent: Thunderbird 1.5.0.10 (X11/20070221)

Now I'm really confused. Let me describe what I'm trying to do. I have a dialog box of color buttons the user can use in my program (a text editor). The user can change the colors to try them out but can also cancel the changes and go back
to the beginning.

The colors are kept in a vector. What I tried was setting a variable for each color by referencing it's match in the vector. This failed because changing the variable changed the vector. I now use vector-copy!, which seems to work. If I change the temp vector colors and then cancel out, my original vector is still what it was. (This seems to go against what you are saying below.) Once the user has the new colors set I copy the temp vector into the original vector and the colors are changed.

Vector-copy! seems to be doing what I want.    Is something else going on?

Bill

Graham Fawcett wrote:
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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]