我最近开始学习Prolog,我无法解决如何组合三个列表的问题.
我能够组合2个列表:
%element
element(X,[X|_]).
element(X,[_|Y]):-
element(X,Y).
%union
union([],M,M).
union([X|Y],L,S) :- element(X,L),union(Y,L,S).
union([X|Y],L,[X|S]) :- (not(element(X,L))),union(Y,L,S).
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
我正在为大学考试学习 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]) …Run Code Online (Sandbox Code Playgroud)