I'm having a problem transforming a vector like this:
#(#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)))
Into one like this:
#(#(1 1 1 1 1) #(2 2 2 2 2) #(3 3 3 3 3))
I wrote a piece of test code but the output is wrong. I went into the debugger and I think I know which line of code cause the problem. I can't seems to find a way to make it work. Any help is greatly appreciated.
(define (test)
(let* ((table #(#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)))
(counter 5)
(size 3)
(new-table (make-vector size (make-vector counter #f))))
(let loop ((sc 0)
(cc 0))
(when (not (= cc counter))
(if (not (= sc size))
(begin (vector-set! (vector-ref new-table sc) cc (vector-ref (vector-ref table cc) sc))
(loop (+ 1 sc) cc))
(loop 0 (+ 1 cc)))))
(display new-table)))
> (test)
#(#(3 3 3 3 3) #(3 3 3 3 3) #(3 3 3 3 3))
There's a problem in this part:
Why? because you're copying the exact same vector in all off
new-table
's positions, so whenever you update one value, it'll change all of them at the same time. It's easy to see this:You have to initialize the vector at the beginning; a fixed version of your code would look like this:
However, the above solution is hard to understand. Fortunately, this seems like a good problem to use Racket's Iterations and Comprehensions, so you don't have to worry about explicitly using recursion for iteration, leading to a much clearer solution:
Either way, the output is as expected:
Note: As it is, this is a procedural programming-style solution, which modifies the new vectors in-place and has the advantage of being fast and efficient (it doesn't create more vectors or lists beyond the strictly necessary), but truth be told, this is not the usual way to solve problems in Scheme. For a functional programming-style solution, more in the spirit of Scheme, see @Ankur's answer.
You can also use
vector-map
to get the desired output: