prolog:如果两个列表相同但没有特定元素,则为true的谓词

prolog:如果两个列表相同但没有特定元素,则为true的谓词,prolog,Prolog,我想在prolog中编写一个不带元素(a,Lf,Lg)的谓词,如果listLg与listLf相同,但不带元素a,则该谓词为true 例如: withoutElement(a,[a,b,c,d],[a,b,c,d]). =错误 及 =正确 执行此操作的最佳方法是什么?使用递归定义: without_element(A, [A|Lg], Lg). % True if element A is % head of Lf a

我想在prolog中编写一个不带元素(a,Lf,Lg)的谓词
,如果list
Lg
与list
Lf
相同,但不带元素
a
,则该谓词为true

例如:

withoutElement(a,[a,b,c,d],[a,b,c,d]).
=错误

=正确


执行此操作的最佳方法是什么?

使用递归定义:

without_element(A, [A|Lg], Lg).    % True if element A is 
                                   % head of Lf and the tail (Lg)
                                   % is the same.
without_element(A, Lf, [A|Lf]).    % If you want to check the other way round
without_element(A, [H|T1], [H|T2]) :- without_element(A, T1, T2).  % ↵
% Check first elements are the same, continue checking rest of lists.

好的,好计划。有什么问题吗?你可能还想考虑代码[XOUT(x,[a,b,c,d],r)< /c> >代码> > OutOnEnter(a,a,b,c,a],l)。
不使用元素(a[a,a,b,c,a],[b,c])是成功还是失败?它看起来几乎不错,但只有当元素a是Lf的第一个元素(列表头)时才起作用。如果A在列表Lf中的任何位置,该解决方案是否也可以工作?提前谢谢!无论A在哪里,它都可以工作,因为它是递归的。当它在头部找到一个时,它退出递归。它会检查Lf和Lg是否相等,直到找到A为止。在找到A之后,它会检查Lf和Lg的其余部分是否相等。请使用一些示例进行尝试。若要停止寻找多个解决方案,请将第一个To子句更改为使主体
true,例如不带_元素的
(A[A | Lg],Lg):-true
without_element(A, [A|Lg], Lg).    % True if element A is 
                                   % head of Lf and the tail (Lg)
                                   % is the same.
without_element(A, Lf, [A|Lf]).    % If you want to check the other way round
without_element(A, [H|T1], [H|T2]) :- without_element(A, T1, T2).  % ↵
% Check first elements are the same, continue checking rest of lists.