Hi!
I've just committed a new implementation of TRANSPOSE-LISTS (see below) because the old one (although it was cute) had problems with CALL-ARGUMENTS-LIMIT. Light testing reveals no problems with the Lisps I have on my hard drive but maybe some LOOP black belt detects a violation of the spec that I don't see.
Cheers, Edi.
(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))))
* 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))))))
[Look at what Gnus made out of your email address. Strange...]
On Tue, 09 Aug 2005 17:18:52 +0200, Helmut@agharta.de, Eller@agharta.de, heller@common-lisp.net wrote:
Uugh, how ugly :-)
How about?
(defun transpose-lists (lists) (cond ((some #'null lists) '()) (t (cons (mapcar #'car lists) (transpose-lists (mapcar #'cdr lists))))))
Ugh, how nice... :)
Damn, I knew there was a recursive solution lurking in there.
My only excuse is that my version seems to be about twice as fast on CMUCL and LispWorks. Call it premature optimization... :)
Cheers, Edi.
Helmut@common-lisp.net, Eller@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]>
On Tue, Aug 09, 2005 at 09:49:30PM +0200, Pascal Bourguignon wrote:
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]>
Edi originally wrote:
I've just committed a new implementation of TRANSPOSE-LISTS (see below) because the old one (although it was cute) had problems with CALL-ARGUMENTS-LIMIT.
I think you've re-invented the old implementation. :)
-bcd
On Tue, 9 Aug 2005 21:49:30 +0200, Pascal Bourguignon pjb@informatimago.com wrote:
Is it the sun?
It has been raining here for the last week or so.