What is the way for mutating nested vectors?
I usually write a (yet another) wrapper function
I’m sure there must be a lib that does that better though
I have this function that accepts a vector of vectors: (define (2vector-set! vec i j v)
(vector-set! (vector-ref vec i) j v))
The problem is that sometimes each slot of a vector is the same object. Then, mutating a slot means mutating multiple slots.
That may happen if you use make-vector
as it duplicates references rather than objects. Use build-vector
instead, or for/vector
I see. Thanks!
This may help understand the difference between make-vector
and build-vector
:
#lang racket
(define v (make-vector 5 (vector 1 2 3 4 5)))
(define u (build-vector 5 (lambda (i) (vector 1 2 3 4 5))))
(printf "apparent value of v = ~a~%" v)
(parameterize ([print-graph #t])
(printf "actual value of v = ~a~%" v))
(printf "apparent value of u = ~a~%" u)
(parameterize ([print-graph #t])
(printf "actual value of u = ~a~%" u))
Prints out:
apparent value of v = #(#(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5))
actual value of v = #(#0=#(1 2 3 4 5) #0# #0# #0# #0#)
apparent value of u = #(#(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5))
actual value of u = #(#(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5) #(1 2 3 4 5))
:astonished: