Programming In Scheme: Transpose

Below is a simple program developed in the Scheme programming language to illustrate how functional languages operate. This particular program will transpose a matrix. See if you can understand the functional logic behind the program.


(define transpose
  (lambda (lis)
    (cond
      ((null? lis) ())
      ((null? (car lis)) ())
      (else
        (cons (append (map car lis)) (transpose (map cdr lis)))
      )
    )
  )
)

(transpose '( (1 2 3 4) (4 5 6 7) (7 8 9 0) ))

When run, the output is:

((1 4 7) (2 5 8) (3 6 9) (4 7 0))
>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>