guile-user
[Top][All Lists]
Advanced

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

Re: vector-fill! seems giving out the wrong result


From: Stephen Compall
Subject: Re: vector-fill! seems giving out the wrong result
Date: Tue, 04 Sep 2007 03:01:18 -0500
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4

aladdin wrote:
I had a problem when using vector-fill! to fill a dimension of 2D array. Here is my code. Please note the second section of the code. First it generate a 2x2 array with nested make-vector. Then, it called vector-fill! to fill the second row of the array with value 10. But it turns out that the whole 2x2 array is filled with 10.

Running the code under mzscheme works out the same result. Is it a bug? Or is there something wrong with my code?

Something is wrong with your code.  Consider this:

(define v '#(#(1 3) #(2 4)))
(display v)(newline)
(vector-fill! (vector-ref v 1) 10)
(display v)(newline)

(set! v (make-vector 2 (make-vector 2 1)))

Make-vector is a function.  That means the above line is equivalent to:

(set! v (let ((b (make-vector 2 1)))
          (make-vector 2 b)))

Make-vector doesn't know how to copy every object, so they assume you want *the same object* in each position. Try it out with pairs:

(let ((v (make-vector 2 (list 1))))
  (write v)
  (newline)
  (set-car! (vector-ref v 0) 42)
  (write v)
  (newline))

--
;;; Stephen Compall ** http://scompall.nocandysw.com/blog **
But you know how reluctant paranormal phenomena are to reveal
themselves when skeptics are present. --Robert Sheaffer, SkI 9/2003




reply via email to

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