这是我到目前为止所拥有的:
(defun append-all(x L)
(if (null L)
L
(cons (append (car L) x) (append-all x (cdr L))))
)
)
输出:
(append-all '3 '((1) (2 1) (2)))
((1 . 3) (2 1 . 3) (2 . 3))
想要:
((1 3) (2 1 3) (2 3))
这是一个辅助函数,因此它是一个链表这一事实似乎给我带来了问题。
谢谢
编辑:修复递归调用
最佳答案
在您的代码中,更改这部分:
(append (car L) x)
对此:
(append (car L) (list x))
它之前不起作用,因为 append
应该接收两个列表作为参数,而不是一个列表和一个元素。
关于list - 在 Lisp 中,如何将给定元素添加到给定列表中的每个列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468925/