Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme / Racket Vector in Vector transformation

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))
like image 786
Timbo925 Avatar asked Feb 10 '26 12:02

Timbo925


1 Answers

You can also use vector-map to get the desired output:

(define table #(#(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3)))

(apply vector-map vector (vector->list table))
like image 178
Ankur Avatar answered Feb 17 '26 12:02

Ankur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!