How are these nested vectors connected?

Turgut Kursun

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

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to find indexes in deeply nested data structure(vectors and lists) in Clojure?

分類Dev

How to check if MQ is connected

分類Dev

How to view that Advertiser is connected

分類Dev

How are n dimensional vectors state vectors represented in Q Learning?

分類Dev

How to extend `==` behavior to vectors that include NAs?

分類Dev

How to quickly multiply a list of matrices by a list of vectors?

分類Dev

How to interface Prolog CLP(R) with real vectors?

分類Dev

How to cbind vectors of different length in R?

分類Dev

How to deal with indexing involving three vectors?

分類Dev

How to assert if two vectors are equal in CppUnitTest framework

分類Dev

How to create vectors dynamically in C++

分類Dev

How to copy data between two vectors faster?

分類Dev

How to create a list of vectors with specific format?

分類Dev

how to get azimuth and elevation from enu vectors

分類Dev

How to find elements common in at least 2 vectors?

分類Dev

How to calculate the Euclidean distance between vectors?

分類Dev

TightVNC: How to list all connected users

分類Dev

Regex: How to ignore dots in connected words

分類Dev

How does an AWS IoT device show as "connected"

分類Dev

How is async interface connected to GWT.create?

分類Dev

How to drop a Redshfit database with connected users

分類Dev

How to detect if Chromecast is already connected on Android Sender?

分類Dev

How detect the devices connected to wif network

分類Dev

mvc signalr how to display all connected users

分類Dev

How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

分類Dev

How to read nested directive?

分類Dev

How to write nested if in makefile?

分類Dev

How to specify nested model

分類Dev

How to resolve nested promises?

Related 関連記事

  1. 1

    How to find indexes in deeply nested data structure(vectors and lists) in Clojure?

  2. 2

    How to check if MQ is connected

  3. 3

    How to view that Advertiser is connected

  4. 4

    How are n dimensional vectors state vectors represented in Q Learning?

  5. 5

    How to extend `==` behavior to vectors that include NAs?

  6. 6

    How to quickly multiply a list of matrices by a list of vectors?

  7. 7

    How to interface Prolog CLP(R) with real vectors?

  8. 8

    How to cbind vectors of different length in R?

  9. 9

    How to deal with indexing involving three vectors?

  10. 10

    How to assert if two vectors are equal in CppUnitTest framework

  11. 11

    How to create vectors dynamically in C++

  12. 12

    How to copy data between two vectors faster?

  13. 13

    How to create a list of vectors with specific format?

  14. 14

    how to get azimuth and elevation from enu vectors

  15. 15

    How to find elements common in at least 2 vectors?

  16. 16

    How to calculate the Euclidean distance between vectors?

  17. 17

    TightVNC: How to list all connected users

  18. 18

    Regex: How to ignore dots in connected words

  19. 19

    How does an AWS IoT device show as "connected"

  20. 20

    How is async interface connected to GWT.create?

  21. 21

    How to drop a Redshfit database with connected users

  22. 22

    How to detect if Chromecast is already connected on Android Sender?

  23. 23

    How detect the devices connected to wif network

  24. 24

    mvc signalr how to display all connected users

  25. 25

    How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

  26. 26

    How to read nested directive?

  27. 27

    How to write nested if in makefile?

  28. 28

    How to specify nested model

  29. 29

    How to resolve nested promises?

ホットタグ

アーカイブ