我有一个难倒我的家庭作业!我必须创建一个函数 goo(A L) 来删除 L 中的每个 A,并且它还必须适用于嵌套列表。
这是我到目前为止所得到的
(defun goo(A L)
(cond ((null L) nil) ; if list is null, return nil
(T ; else list is not null
(cond ((atom (car L))) ;if car L is an atom
((cond ((equal A (car L)) (goo A (cdr L))) ;if car L = A, call goo A cdr L
(T (cons (car L) (goo A (cdr L)))))) ;if car L != A,
(T (cons (goo A (car L)) (goo A (cdr L)))))) ;else car L is not atom, call goo on car L and call goo on cdr L
))
无论我给它什么,这个函数都会返回 True。
最佳答案
你 parent 搞砸了。移动 (atom (car L))
周围的最后一个括号以包含下一个 cond
表达式。我建议使用显示匹配括号的 IDE。
至于样式,如果你不知道,cond
can accept multiple clauses .这样你就不需要再有 t
和 cond
了。您也可以use 'if'如果您仅测试单个谓词并仅基于该谓词做出决定。
关于lisp - 从嵌套列表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234257/