How are these nested vectors connected?

自作多情 提交于 2021-02-09 17:47:17

问题


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 ? ?)
#(? ? ?)
#(? ? ?)

回答1:


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))



回答2:


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))


来源:https://stackoverflow.com/questions/56824929/how-are-these-nested-vectors-connected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!