LISP过滤功能

时间:2018-01-05 16:16:09

标签: lisp common-lisp depth

我必须定义一个函数filter,它具有谓词和列表作为参数,并返回值为初始列表的唯一原子 - 对于每个深度级别 - 满足初始谓词(保持注意力) NIL值以保持列表结构。)

示例:

  

(filter 'evenp '(24 5 (7) d (((4))) 3 ()))

(24 () (((4))) ())

我想的代码是这样的:

(defun filter (pred list)
  (cond ((null list) nil)
        ((funcall pred (car list))
         (cons (car list)
               (filter pred (cdr list))))
        (T (filter pred (cdr list)))))

如何实现深度事实,保持示例中显示的圆括号?

谢谢大家

1 个答案:

答案 0 :(得分:2)

这是一个可能的解决方案:

(defun filter (predicate x)
   (if (consp x)  ; if x is a cons, that is a tree:
       (let ((ca (car x))
             (cd (filter predicate (cdr x)))) ; filter always the cdr
         (if (listp ca)                       ; if the car of x is a list (nil or cons)
             (cons (filter predicate ca) cd)  ; then filter also the car
             (if (funcall predicate ca) (cons ca cd) cd))) ; car is a non-nil atom!
       x))        ; if x is a atom (nil or the last cdr of an improper list), return x

CL-USER> (filter 'evenp '(24 5 (7) 5 (((4))) 3 ()))
(24 NIL (((4))) NIL)