这里是一个非递归版本支配 通过call-arguments-limit
等:
(defun pop-all (list-of-lists)
"Pop each list in the list, return list of pop results
and an indicator if some list has been exhausted."
(loop for tail on list-of-lists collect (pop (car tail))))
(defun transpose (list-of-lists)
"Transpose the matrix."
(loop with tails = (copy-list list-of-lists)
while (some #'consp tails) ; or any?
collect (pop-all tails)))
测试:
(defparameter ll '((1 4 7) (2 5 8) (3 6 9)))
(transpose ll)
==> ((1 2 3) (4 5 6) (7 8 9))
ll
==> ((1 4 7) (2 5 8) (3 6 9))
(equal ll (transpose (transpose ll)))
==> T
请注意,我扫描list-of-lists
两次每次迭代 - 一次在some
和一次在pop-all
(与similar answer相同)。
我们可以用一些额外的工作避免:
(defun pop-all (list-of-lists)
"Pop each list in the list, return list of pop results
and an indicator if some list has been exhausted."
(loop for tail on list-of-lists
for more = (consp (car tail)) then (and more (consp (car tail)))
collect (pop (car tail)) into card
finally (return (values cars more))))
(defun transpose (list-of-lists)
"Transpose the matrix."
(loop with tails = (copy-list list-of-lists) and more and cars
do (setf (values cars more) (pop-all tails))
while more collect cars))
来源
2017-02-15 14:45:15
sds
http://stackoverflow.com/questions/39943232/matrix-transpose-common-lisp关于什么 – anquegi