更高阶 Prolog 中更复杂的目标

More complicated goals in higher order Prolog

我举一个具体的例子来解释我的问题:

Given a list of lists LoL and a list Ex, construct X that contains all elements (sublists) of LoL after subtracting Ex from them.

举个例子:

?- my_exclude([[1, 2, 3], [1], [3, 4]], [2, 3], X).
X = [[1], [1], [4]].

我知道我可以使用 lists 库中的 subtract/3,但我不知道如何实现这种映射。

我知道 maplist,但我不知道如何在这里使用它。

我可能缺少的是某种 map 谓词,它会在对每个元素应用操作时将 LoL 转换为 X,该操作是 [= 的减法12=].

如果不诉诸“手动”在较低级别递归地实现它,我怎么能这样做呢?

(换句话说,也许我正在寻找标准 Prolog 中似乎不存在的 lambda)

你需要调用subtract(L<sub>i</sub>, [2,3], R<sub>i</sub>) 对列表 L 中的每个 L<sub>i</sub> 产生一个结果 R<sub>结果列表中的 i</sub> R.

您可以使用 lambda 表达式

my_exclude(L, E, R) :-
    maplist(<b>{E}/[Li, Ri]>>subtract(Li, E, Ri)</b>, L, R).

这里 {E}/[Li, Ri]>>subtract(Li, E, Ri) 是一个有两个变量 LiRi 的目标,当被调用时,将调用 subtract(Li, E, Ri) 其中 E 是列表我们要排除的元素。