5

我正在为大学考试学习 Prolog,但我在这个练习中遇到了问题:

not_member(X,L)如果元素X不属于列表,则实现为 TRUE 的谓词L

如果我的推理是正确的,我已经找到了解决方案:

% FACT (BASE CASE): It is TRUE that X is not in the list if the list is empty.
not_member(_,[]).

% RULE (GENERAL CASE): If the list is non-empty, I can divide it in its Head
%   element and the sublist Tail. X does not belong to the list if it is different 
%   from the current Head element and if it does not belong to the sublist Tail.
not_member(X,[Head|Tail]) :-
   X =\= Head,
   not_member(X,Tail).

此代码适用于数字列表,如以下查询所示:

2 ?- not_member(4, [1,2,3]).
true.

3 ?- not_member(1, [1,2,3]).
false.

但是,对于包含一些非数字元素的列表,它不起作用并报告错误:

4 ?- not_member(a, [a,b,c]).
ERROR: =\=/2: Arithmetic: `a/0' is not a function

为什么?

4

2 回答 2

7

让我们检查文档!

(=\=)/2是算术运算符。

+Expr1 =\= +Expr2 如果表达式 Expr1 的计算结果不等于 Expr2,则为真。

您必须使用(\=)/2来比较两个通用术语:

not_member(_, []) :- !.

not_member(X, [Head|Tail]) :-
     X \= Head,
    not_member(X, Tail).

和:

?- not_member(d, [a,b,c]).
true.
于 2013-04-07T17:15:11.640 回答
5

使用获得逻辑上合理的答案——适用地面非地面情况!

就像在这个答案中一样,我们定义non_member(E,Xs)maplist(dif(E),Xs)

让我们 通过@Hailemaplist(dif(E),Xs)进行测试!not_member(E,Xs)

?- not_member (E,[1,2,3])。
的。%错了!`E=4` 呢?

?- maplist(dif(E),[1,2,3])。
差(E,1),差(E,2),差(E,3)。未决目标的成功百分比

是坚定的吗?(有关此重要问题的更多信息,请阅读 答案。)

?- E=d, not_member (E,[a,b,c])。
E = d。
?-       not_member (E,[a,b,c]), E=d。
的。%不坚定

?- E=d, maplist(dif(E),[a,b,c])。
E = d。
?- maplist(dif(E),[a,b,c]), E=d。% 坚定不移
E = d。

让我们不要忘记最一般的用途!

?- not_member (E,Xs)。               
Xs = []。%缺少很多解决方案!

?- maplist(dif(E),Xs)。
  Xs = []
; Xs = [_A] , 差异 (E,_A)
; Xs = [_A,_B] ,差异(E,_A),差异(E,_B)
; Xs = [_A,_B,_C],差异(E,_A),差异(E,_B),差异(E,_C)
...
于 2015-07-09T01:54:58.517 回答