鉴于我的列表可能在其结构上有所不同 - 它可能在列表中有多个列表,我如何遍历每个元素? 列表示例:(or (and (not a) (not b)) (or (and x) t)))
最佳答案
这是 maptree 函数的典型情况。
(defun maptree (fn tree)
(cond
((null tree) tree)
((atom tree) (funcall fn tree))
(t (cons
(maptree fn (first tree))
(maptree fn (rest tree))))))
所以你可以做 (maptree #'what-to-do your-list)
。
我只会打印所有元素,你可以提供任何你想要的函数,它会在你的树的每个元素上执行。
CL-USER> (let ((lis
'(or (and (not a) (not b)) (or (and x) t))))
(maptree #'print lis))
OR
AND
NOT
A
NOT
B
OR
AND
X
T
关于list - 使用 LISP 遍历列表中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34145061/