Prolog:第一个重复值 [英] Prolog: First duplicate value
问题描述
我需要在列表中找到第一个重复值.
prep(3,[1,3,5,3,5]).
应该是真的.
prep(5,[1,3,5,3,5]).
应该是假的.
我想检查当前值和以前的列表成员是否相等,直到找到重复项,如果找到一个,它将测试与 X 是否相等,但我不知道如何在 Prolog 中做到这一点!
感谢您的帮助!谢谢
这是一个使用 dif/2
实现声音不等式的纯版本.dif/2
由 B-Prolog、YAP-Prolog、SICStus-Prolog 和 SWI-Prolog 提供.
优点是它也可以用于更一般的查询:
<上一页>?- firstdup(E, [A,B,C]).E = A,A = B;E = A,A = C;E = C,B = C,差异(A,C);错误的.在这里,我们得到三个答案:A
是前两个答案的副本,但有两个不同的理由:A
可能等于 B
或 C
.在第三个答案中,B
是重复的,但只有当 C
与 A
不同时才会重复.
要理解定义,请阅读 :-
作为箭头 ←所以右边是你知道的,左边是你得出的结论.在开始的时候读那个方向的谓词通常会有点烦人,毕竟你可能很想遵循执行线程".但通常这条线索无处可去 –它变得太复杂而无法理解.
第一条规则如下:
如果E
是列表L
的一个元素,我们得出[E|L]
有E
作为第一个副本.
第二条规则如下:
提供的E
是L
的第一个副本(不要惊慌说我们不知道...) 并且假设N
不是L
的元素,我们得出结论[N|L]
具有E
为第一次复制.
member/2
谓词在许多 Prolog 系统中提供,non_member(X,L)
可以定义为 maplist(dif(X),L)代码>.因此,人们会将
firstdup/2
定义为:
I need to find the first duplicate value in a list.
prep(3,[1,3,5,3,5]).
Should be true.
prep(5,[1,3,5,3,5]).
Should be false.
I thought checking for equality with the current value and the previous list members until I find a duplicate, if it finds one it will test for equality with X but I have no idea how to do that in Prolog!
I appreciate any help! Thanks
Here is a pure version using dif/2
which implements sound inequality. dif/2
is offered by B-Prolog, YAP-Prolog, SICStus-Prolog and SWI-Prolog.
firstdup(E, [E|L]) :- member(E, L). firstdup(E, [N|L]) :- non_member(N, L), firstdup(E, L). member(E, [E|_L]). member(E, [_X|L]) :- member(E, L). non_member(_E, []). non_member(E, [F|Fs]) :- dif(E, F), non_member(E, Fs).
The advantages are that it can also be used with more general queries:
?- firstdup(E, [A,B,C]). E = A, A = B ; E = A, A = C ; E = C, B = C, dif(A, C) ; false.
Here, we get three answers: A
is the duplicate in the first two answers, but on two different grounds: A
might be equal to B
or C
. In the third answer, B
is the duplicate, but it will only be a duplicate if C
is different to A
.
To understand the definition, read :-
as an arrow ← So what is on the right-hand side is what you know and on the left is what you conclude. It is often in the beginning a bit irritating to read predicates in that direction, after all you might be tempted to follow "the thread of execution". But often this thread leads to nowhere – it gets too complex to understand.
The first rule reads:
ProvidedE
is an element of listL
we conclude that[E|L]
hasE
as first duplicate.
The second rule reads:
ProvidedE
is the first duplicate ofL
(don't panic here and say we don't know that ...) and provided thatN
is not an element ofL
we conclude that[N|L]
hasE
as first duplicate.
The member/2
predicate is provided in many Prolog systems and non_member(X,L)
can be defined as maplist(dif(X),L)
. Thus one would define firstdup/2
rather as:
firstdup(E, [E|L]) :- member(E, L). firstdup(E, [N|L]) :- maplist(dif(N), L), firstdup(E, L).
这篇关于Prolog:第一个重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!