[slime-devel] transpose-lists

Pascal Bourguignon pjb at informatimago.com
Tue Aug 9 19:49:30 UTC 2005


Helmut at common-lisp.net, Eller at common-lisp.net, writes:
> * Edi Weitz [2005-08-09 16:00+0200] writes:
> 
> > (defun transpose-lists (lists)
> >   "Turn a list-of-lists on its side.
> > If the rows are of unequal length, truncate uniformly to the shortest.
> >
> > For example:
> > \(transpose-lists '((ONE TWO THREE) (1 2)))
> >   => ((ONE 1) (TWO 2))"
> >   (catch 'done
> >     (loop with result
> >           with collectors = (loop for list in lists
> >                                   collect (let ((list list))
> >                                             (lambda ()
> >                                               (cond ((null list)
> >                                                      (throw 'done result))
> >                                                     (t (pop list))))))
> >           collect (loop for collector in collectors
> >                         collect (funcall collector)) into temp-result
> >           do (setq result temp-result))))
> 
> Uugh, how ugly :-)
> 
> How about?
> 
> (defun transpose-lists (lists)
>   (cond ((some #'null lists) '())
> 	(t (cons (mapcar #'car lists)
> 		 (transpose-lists (mapcar #'cdr lists))))))

Is it the sun? Are we becoming silly?

(defun transpose-lists (lists) (apply (function mapcar) (function list) lists))


[4]> (transpose-lists '((1 2 ) (one two three) ()))
NIL
[5]> (transpose-lists '((1 2 ) (one two three)))
((1 ONE) (2 TWO))
[6]> 


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Wanna go outside.
Oh, no! Help! I got outside!
Let me back inside!



More information about the slime-devel mailing list