3

I've written a piece of code, which creates a vector 'scoreboard' that contains 3 seperate vectors of size 3, all containing the symbol ? at all indices 0-2. When i now execute a 'vector-set!' on the first vector of scoreboard, to change its first element to a 'X, vectors 2 and 3 will change too. How does this occur?

(define scoreboard (make-vector 3 (make-vector 3 '?)))
(define (display-scoreboard)
(display (vector-ref scoreboard 0))
(newline)
(display (vector-ref scoreboard 1))
(newline)
(display (vector-ref scoreboard 2))
(newline))

(define (X! pos)
(cond
((>= 3 pos) (vector-set! (vector-ref scoreboard 0) (- pos 1) 'X))
))

(display-scoreboard)
(X! 1)
(newline)
(display-scoreboard)

output:

#(? ? ?)
#(? ? ?)
#(? ? ?)

#(X ? ?)
#(X ? ?)
#(X ? ?)

Desired output:

#(? ? ?)
#(? ? ?)
#(? ? ?)

#(X ? ?)
#(? ? ?)
#(? ? ?)
TomatoFarmer
  • 463
  • 3
  • 13

2 Answers2

5

enter image description here

The image shows that (make-vector 3 (make-vector 3 '())) creates a vector that has the same row (vector) in all three slots.

Instead, write (vector (vector '() '() '()) (vector '() '() '()) (vector '() '() '())). Or make a little helper function:

(define (make-row) (vector '() '() '()))
(vector (make-row) (make-row) (make-row))
soegaard
  • 30,661
  • 4
  • 57
  • 106
4

In addition to @soegaard's excellent answer, I want to point out that the idiomatic way to create a non-shared vector of vectors is using build-vector.

;; ignore i and j since we want all elements to be the same
> (define table (build-vector 3 (lambda (i) (build-vector 3 (lambda (j) '?)))))
> table
'#(#(? ? ?) #(? ? ?) #(? ? ?))
;; this will mutate only one row
> (vector-set! (vector-ref table 0) 0 42)
> table
'#(#(42 ? ?) #(? ? ?) #(? ? ?))
;; we can use i and j to make different elements for each index
> (build-vector 3 (lambda (i) (build-vector 3 (lambda (j) (* (add1 i) (add1 j))))))
'#(#(1 2 3) #(2 4 6) #(3 6 9))
Sorawee Porncharoenwase
  • 6,305
  • 1
  • 14
  • 28