hectometrocuadrado
2021-9-11 10:46:03

What is the way for mutating nested vectors?


laurent.orseau
2021-9-11 10:54:16

I usually write a (yet another) wrapper function


laurent.orseau
2021-9-11 10:55:41

I’m sure there must be a lib that does that better though


hectometrocuadrado
2021-9-11 10:58:28

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.


laurent.orseau
2021-9-11 11:00:35

That may happen if you use make-vector as it duplicates references rather than objects. Use build-vector instead, or for/vector


hectometrocuadrado
2021-9-11 11:02:51

I see. Thanks!


alexharsanyi
2021-9-11 11:58:51

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


hectometrocuadrado
2021-9-11 12:19:01

:astonished: