首页 > 解决方案 > prolog:如果两个列表相同但没有特定元素,则谓词为真

问题描述

我想withoutElement(A,Lf,Lg)在 prolog 中编写一个谓词,如果 list与 list without elementLg相同,则该谓词为真。LfA

例如:

withoutElement(a,[a,b,c,d],[a,b,c,d]).

= 假

withoutElement(a,[a,b,c,d],[b,c,d]).

= 真

做这个的最好方式是什么?

标签: prolog

解决方案


使用递归定义:

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.

推荐阅读