以下程序的意图如下 clist_d(f(a,f(a,a)),R) 结果是所有基本参数的列表,例如 R = [a,a,a]
%Difference list append.
diffapp(X-Y,Y-Z,X-Z).
%2 base case
clist_d(a,[a]).
clist_d(b,[b]).
clist_d(c,[c]).
%If we get a term f(X,Y) the first term of such list is the element in X,followed by the elements in Y
clist_d(f(X,Y),R) :-
clist_d(X,R1),
clist_d(Y,R2),
diffapp([R1|H]-H,R2-[],R-[]).
%We can also get a g
clist_d(g(X,Y),R) :-
clist_d(X,R1),
clist_d(Y,R2),
diffapp([R1|H]-H,R2-[],R-[]).
但是,该程序包含一个错误。使用以下查询运行程序:
?- clist_d(f(a,a),R).
R = [[a],a] ?
如您所见,产生一个错误,单独测试差异列表我得到以下结果
?- X = [a,b,c|T], Y = [1,2,3],diffapp(X-T,Y-[],R-[]).
X = [a,b,c,1,2,3],
T = [1,2,3],
Y = [1,2,3],
R = [a,b,c,1,2,3] ?
yes
我在我的主程序中犯了一个错误,但我不知道要添加什么来让我的 diffapp 在那里工作。