Common lisp 在lisp中转换列表列表的安全方法?
转换列表列表很容易,但附带一个警告,即不能超过Common lisp 在lisp中转换列表列表的安全方法?,common-lisp,Common Lisp,转换列表列表很容易,但附带一个警告,即不能超过调用参数限制 当列表长度可能超过调用参数限制时,编写转置的安全方法是什么 例如,答案不是这个: 这是一个非递归版本,没有任何阻碍 等人: 测试: 请注意,我在每次迭代中扫描列表的两次,一次在some中,一次在pop all(与中相同) 我们可以通过一些额外的工作来避免: (defun pop-all (list-of-lists) "Pop each list in the list, return list of pop results an
调用参数限制
当列表长度可能超过调用参数限制时,编写转置的安全方法是什么
例如,答案不是这个:
这是一个非递归版本,没有任何阻碍 等人: 测试: 请注意,我在每次迭代中扫描列表的
两次,一次在some
中,一次在pop all
(与中相同)
我们可以通过一些额外的工作来避免:
(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))
那怎么办
(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
(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))