【发布时间】:2016-07-07 18:16:58
【问题描述】:
我需要像这样将列表中的一些元素附加到另一个列表中:
find_same(pt(1,1),pt(2,2),6,[slope(6,pt(3,3)),slope(6,pt(4,4)),slope(7,pt(3,2)),slope(9,pt(5,5))],NL).
结果
NL=[pt(1,1),pt(2,2),pt(3,3),pt(4,4)]
我尝试过使用 append,但我遇到了一些问题,代码如下:
find_same(_,_,_,[],_):-!.
find_same(pt(X,Y),pt(Xa,Ya),R,Slopes,Nl):-X\=a,
append(Nla,[pt(X,Y),pt(Xa,Ya)],Nl),
find_same(pt(a,a),pt(b,b),R,Slopes,Nla).
find_same(pt(X,Y),pt(Xa,Ya),R,[slope(R,pt(Xs,Ys))|Ss],Nl):-X=a,
append(Nla,[pt(Xs,Ys)],Nl),
find_same(pt(X,Y),pt(Xa,Ya),R,Ss,Nla).
find_same(_,_,R1,[slope(R2,_)|_],_):-R1\=R2,!.
因为返回给我很多列表。
然后我尝试使用其他代码:
find_same2(_,_,_,[],_):-!.
find_same2(pt(X,Y),pt(Xa,Ya),R,Slopes,_):-X\=a,
find_same2(pt(a,a),pt(b,b),R,Slopes,[pt(X,Y),pt(Xa,Ya)]).
find_same2(pt(X,Y),pt(Xa,Ya),R,[slope(R,pt(Xd,Yd))|Ss],[pt(Xd,Yd)|Nl]):-
X=a,!,
find_same2(pt(X,Y),pt(Xa,Ya),R,Ss,Nl).
find_same2(_,_,R1,[slope(R2,_)|_],_):-R1\=R2.
但它只返回 false。
我该如何解决这个问题?谢谢
【问题讨论】:
标签: prolog