delete-if中谓词的多个参数

时间:2017-10-29 05:21:36

标签: lisp common-lisp sbcl

假设我想从状态列表中删除一个步骤(元素)。

(defparameter *states* '((:top nil nil) (:subjects nil nil)))
 ;predicate
(defun equal-state? (step state)
  (equal (car step) state))

如果我使用(delete-if #'equal-state? *states*)那么第二个参数(state)如何传递给谓词?

编辑:我终于找到了一个类似的问题,但由于问题的清晰,我倾向于保留它。

1 个答案:

答案 0 :(得分:3)

CL-USER 67 > (let ((state :top))
               (delete-if (lambda (step)
                            (equal-state? step state))
                          *states*))
((:SUBJECTS NIL NIL))

CL-USER 68 > (defun make-predicate (state)
               (lambda (step)
                 (equal-state? step state)))
MAKE-PREDICATE

CL-USER 69 > (delete-if (make-predicate :subjects)
                        *states*)
((:TOP NIL NIL))

当用户 coredump 提到时,delete-if是一种潜在的破坏性操作。 delete-if的非破坏性替代是remove-if

也可以使用remove / delete

CL-USER 77 > (remove :subjects
                     '((:top nil nil)
                       (:subjects nil nil))
                     :key #'first
                     :test #'equal)
((:TOP NIL NIL))