Scheme / Racket Vector in Vector transformation -


i'm having problem transforming vector this:

#(#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3))) 

into 1 this:

#(#(1 1 1 1 1) #(2 2 2 2 2) #(3 3 3 3 3)) 

i wrote piece of test code output wrong. went debugger , think know line of code cause problem. can't seems find way make work. 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 problem in part:

(make-vector size (make-vector counter #f)) 

why? because you're copying exact same vector in off new-table's positions, whenever update 1 value, it'll change of them @ same time. it's easy see this:

(define new-table (make-vector 3 (make-vector 3 #f))) (vector-set! (vector-ref new-table 0) 0 42) ; modify single position ... new-table => '#(#(42 #f #f) #(42 #f #f) #(42 #f #f))  ; ... of them changed! 

you have initialize vector @ beginning; fixed version of code this:

(let* ((table '#(#(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3)))        (counter (vector-length table))        (size (vector-length (vector-ref table 0)))        (new-table (make-vector size)))   ; initialization   (let loop ((i 0))     (when (< size)       (vector-set! new-table (make-vector counter))       (loop (+ 1))))     (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))))     new-table)) 

however, above solution hard understand. fortunately, seems problem use racket's iterations , comprehensions, don't have worry explicitly using recursion iteration, leading clearer solution:

(let* ((table '#(#(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3)))        (counter (vector-length table))        (size (vector-length (vector-ref table 0)))        (new-table (make-vector size)))   (for ([sc (in-range size)])     (vector-set! new-table sc (make-vector counter)) ; initialization     (for ([cc (in-range counter)])       (vector-set! (vector-ref new-table sc) cc                    (vector-ref (vector-ref table cc) sc))))   new-table) 

either way, output expected:

=> '#(#(1 1 1 1 1) #(2 2 2 2 2) #(3 3 3 3 3)) 

note: is, procedural programming-style solution, modifies new vectors in-place , has advantage of being fast , efficient (it doesn't create more vectors or lists beyond strictly necessary), truth told, not usual way solve problems in scheme. functional programming-style solution, more in spirit of scheme, see @ankur's answer.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -