我想计算列表中某个元素的出现次数 [英] I want to count the occurrences of an element in a list
问题描述
我想计算列表中某个元素的出现次数,如果有,则谓词唯一性为真,否则为假.但是,如果元素出现不止一次,Prolog 会发现它是真的.我不知道该怎么办...
I want to count the occurrences of an element in a list, and if there is one then the predicate unique would be true, else false. However, if the element occurs more than once, Prolog finds it true. I don't know what to do...
count([], X, 0).
count([X|T], X, Y) :- count(T, X, Z), Y is 1+Z, write(Z).
count([_|T], X, Z) :- count(T, X, Z).
unique(St, [Y|RestList]) :- count([Y|RestList], St, N), N =:= 1.
推荐答案
只要第一个参数是一个基本列表,该解决方案就可以工作.在其他一些情况下,它是不正确的:
The solution works as far as the first argument is a ground list. In some other cases, it is incorrect:
?- count([E], a, 0).
false.
我们在这里问
长度为 1 的列表的元素 E
必须如何使列表包含 0 个出现的 a
?
How must the element
E
of a list of length 1 look like such that the list contains 0 occurences ofa
?
事实上,这个问题也有答案,比如 E = b
或 E = c
:
And in fact there are answers to this, like E = b
or E = c
:
?- count([b],a,0).
true.
?- count([c],a,0).
true.
因此,Prolog 的回答是不完整.它应该说,是的.但是怎么做呢?
For this reason Prolog's answer was incomplete. It should have said, yes. But how?
count([], _, 0).
count([E|Es], F, N0) :-
count(Es, F, N1),
if_(E = F, D = 1, D = 0),
N0 is N1+D.
?- length(Xs, I), count_dif(Xs, a, N).
Xs = [],
I = N, N = 0
; Xs = [a],
I = N, N = 1
; Xs = [_A],
I = 1,
N = 0,
dif(_A, a)
; Xs = [a, a],
I = N, N = 2
; Xs = [_A, a],
I = 2,
N = 1,
dif(_A, a) ;
Xs = [a, _A],
I = 2,
N = 1,
dif(_A, a) ;
Xs = [_A, _B],
I = 2,
N = 0,
dif(_A, a),
dif(_B, a)
...
为了进一步改进这一点,我们可能会使用 library(clpfd)
,因为它在 SICStus、YAP 和 SWI 中可用.
To further improve this, we might use library(clpfd)
as it is available in SICStus, YAP, and SWI.
:- use_module(library(clpfd)).
count([], _, 0).
count([E|Es], F, N0) :-
N0 #>= 0,
if_(E = F, D = 1, D = 0),
N0 #= N1+D,
count(Es, F, N1).
现在甚至以下内容也终止了:
Now even the following terminates:
?- count([a,a|_], a, 1).
false.
?- N #< 2, count([a,a|_], a, N).
false.
这篇关于我想计算列表中某个元素的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!