接受两个谓词列表和元素列表并相应地划分元素列表的函数

Function that accepts two lists of predicates and elements list and partitions the elements list accordingly

我想编写一个函数,它接受两个谓词函数列表和元素列表以及 returns 原始列表中的所有成员,这些成员包含 pre1_list 中的所有谓词并删除其中的成员取消保留 pre2_list

中所有谓词的原始列表

我正在用一种叫做 mini lisp 的语言编写这段代码,它类似于 lisp 但更简单,但这没关系,我只是想获得有关如何做这样的事情的帮助?关于如何实现此类代码的想法!

我是怎么想的:(只是我的想法的开始)

(defun get_pred_1_not_2 (pre1_list pre2_list list)
  (cond
    ((null list) NIL)
      ; return all the members in the original list that hold all the predicates in pre1_list
    (pre1_list (get_pred_1_not_2 (cdr pre1_list) pre2_list (filter_pre list (car pre1_list))))
      ; delete all the members in the original list that unhold all the predicates in pre2_list
    (pre2_list (get_pred_1_not_2 pre1_list (cdr pre2_list) ;.... ( 

其中 filter_pre 是一个函数,returns 列表中的所有元素都包含给它的谓词

我希望有人能提供帮助!因为这个函数真的很难写,我不想放弃 谢谢

不要尝试一次构建整个解决方案。而是通过为自己构建构建块,一个函数一个函数地进行,以完成足够简单的任务,以便每个函数都易于编写。这就是“函数式编程”方法。

从一个谓词的简单 filter 函数开始。然后将其修改为您的 filter-not.

然后用它们来实现filter-psfilter-not-ps

或者,通过编写 all-psall-ps-not 函数将您的谓词列表变成一个谓词。

生成的 predicate 将一次与 一个 元素一起使用,因此您可以将其与简单的 filter 一起使用得到你想要的,两次调用 filter - 首先将 all-ps 的结果应用于你的第一个谓词列表;第二个 all-ps-not 的结果应用于您的第二个谓词列表。

编辑: 如果您 删除 所有谓词“未保留”的所有元素,即 return false,您将剩下具有至少一个谓词 return 为真的所有元素。因此,您不妨说,对于任务的第二部分,您希望 保留 至少有一个谓词适用的所有元素。仍然适用相同的一般准则,只是您需要定义 at-least-one-of-ps 而不是定义 all-ps-not,或者将其命名为 some-ps.