仍然在 lisp 中解决递归问题,这就是问题所在
编写一个有两个参数 x 和 y 的函数,其中 y 是一个列表,x 是一个原子。该函数必须从 Yy 中删除与 x 匹配的所有元素。 初始列表可以是多级的。
一般来说,我设法编写了这样的代码。
下面是一个递归函数,它从多级列表中生成单级列表。
(defun toSameLvl (lst)
(cond
((null lst) nil)
((atom (car lst)) (cons (car lst) (toSameLvl (cdr lst)) ) )
(T (append (toSameLvl (car lst)) (toSameLvl (cdr lst))))
)
)
(toSameLvl `((1 2 4) 1 2 4 2 (1 2 (1 2 3) 3)))
>>>(1 2 4 1 2 4 2 1 2 1 2 3 3)
以下是从兄弟列表中删除给定元素的递归函数。
(defun deepRemove (x y)
(cond
((null y) nil)
((not(atom x)) nil)
((= x (car y)) (deepRemove x (cdr y)))
(T (cons (car y) (deepRemove x (cdr y))))
)
)
(deepRemove 0 `(0 2 3 4 0 2 0 4 2 0))
>>>>(2 3 4 2 4 2)
但是,最重要的是,您需要使用一个函数来编写它,而不是像我那样使用两个函数,而不使用函数、循环和赋值函数!
请写下如何正确执行的代码,像往常一样,从我这里,没有任何回报ツ