在结果Lisp中没有nil的列表

时间:2015-12-17 19:18:53

标签: lisp

我必须从所有级别删除列表中元素的所有出现。 我的代码是:

(defun sterge(e l)
   (cond
    ((and (atom l) (equal e l)) nil)
    ((atom l) (list l))
    (t (append (apply #'list (mapcar #' (lambda (l) (sterge e l)) l))))
  )
)
(defun sterg(e l)
  (car (sterge e l))
)

当我给:

(sterg 1 '(1 2 1 ( 1 2 1( 1 (1) (1)) (1) 3) (1)(2)))

它显示了输出:

 ((2 (2 (NIL NIL) NIL 3) NIL (2)))

如何删除nil ??谢谢。

1 个答案:

答案 0 :(得分:1)

不要返回nil,而是考虑将sterge应用于实体的其余部分lmapcar不是解决此问题的最佳方式;递归函数更好(当然,除非赋值使用mapcar指定。)

提示:将l视为列表,然后测试(car l),例如(atom (car l)),将sterge应用于(cdr l)