我试图编写一个简单的过程来检查一个列表是否有任何副本。这就是我迄今尝试过的:
% returns true if the list has no duplicate items.
no_duplicates([X|XS]) :- member(X,XS) -> false ; no_duplicates(XS).
no_duplicates([]) :- true. 如果我尝试no_duplicates([1,2,3,3])。这是真的。为什么会这样呢?我可能误解了Prolog,但是任何帮助都是非常感谢的。
发布于 2014-09-26 02:13:22
下面是另一种方法,因为sort/2删除了重复项:
no_duplicates(L) :-
length(L, N),
sort(L, LS),
length(LS, N).发布于 2014-09-25 21:04:33
列表中的重复项是相同的元素,在列表中的位置不同,因此可以编写no_duplicates:
no_duplicates(L) :-
\+((nth0(Id1, L, V), nth0(Id2, L, V), Id1 \= Id2)).发布于 2014-09-25 22:02:24
Jay已经注意到您的代码正在工作。另一种,稍微不那么冗长
no_duplicates(L) :- \+ (append(_, [X|XS], L), memberchk(X, XS)).https://stackoverflow.com/questions/26047172
复制相似问题