Transpose of an nxm matrix using Scheme

Scheme

Public Domain

This code takes an input matrix as a list of lists, and outputs the transpose of the matrix as a list of lists

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
(define transpose 
  (lambda (blist)
    (cond
    [(null? (car blist)) empty]
    [else
      (cons (calc car blist) (transpose (calc cdr blist)))])))

(define calc
  (lambda (operation alist)
    (cond
      [(null? alist) empty]
      [else
        (cons (operation (car alist)) (calc operation (cdr alist)))])))

(display "The output is: " )
(transpose '((1 2 3 4)(4 5 6 7)(7 8 9 0)))