(defun transpose (m)
(cond
((null (car m)) nil)
(T (cons (mapcar 'car m) (transpose (mapcar 'cdr m))))
))
(transpose '((1 2 3) (4 5 6) (7 8 9)))
Refactorings
No refactoring yet !
Kartik Agaram
October 3, 2007, October 03, 2007 12:55, permalink
Nice! Totally beats what I had before.
mdengler
October 6, 2007, October 06, 2007 04:31, permalink
Style, and also didn't you want #'car, rather than 'car?
(defun transpose (m)
(unless (null (car m))
(cons (mapcar #'car m) (transpose (mapcar #'cdr m)))))
(transpose '((1 2 3) (4 5 6) (7 8 9)))
acjr
November 8, 2007, November 08, 2007 12:01, permalink
If the "Cond" returns a value, try this:
(defun Transpose (m) (Cond ((CAR m) (Cons (MapCAR 'CAR m) (Transpose (MapCAR 'CDR m))))))
anonymous
May 23, 2008, May 23, 2008 19:17, permalink
(defun transpose (m) (apply #'mapcar #'list m))
simpler? shorter?