All pastes #206842 Raw Edit

Miscellany

public text v1 · immutable
#206842 ·published 2006-10-17 16:32 UTC
rendered paste body
;; question 7 (bonus)

;; number of rows
(define (numRow lst) (- (length lst) 1))

;; number of columns 
(define (numCol lst) (- (length (first lst)) 1))

;; createRow : number number list -> list
(define (createRow rows columns lst)
  (cond
    [(= rows -1) empty]
    [else (cons (list-ref (list-ref lst rows) columns)
                (createRow (sub1 rows) columns lst))]))

;; loop : number number list -> list
(define (loop rows columns lst)
  (cond
    [(= columns -1) empty]
    [else (cons (reverse (createRow rows columns lst)) 
                (loop rows (sub1 columns) lst))]))

;; transpose : list -> list
(define (transpose mat)
  (cond
    [(empty? mat) empty]
    [else (reverse (loop (numRow mat) (numCol mat) mat))]))

;; tests
(define testCase (list (list 1 4 3 2)
                       (list 0 1 0 4)
                       (list 2 0 1 3))) 

(define expectedOutcome (list (list 1 0 2)
                              (list 4 1 0)
                              (list 3 0 1)
                              (list 2 4 3)))

(equal? (transpose testCase) expectedOutcome)
(equal? (transpose expectedOutcome) testCase)