Re: Count lists elements
- From: "Carlo Capelli" <carlo.capelli@xxxxxxxx>
- Date: Thu, 16 Nov 2006 08:47:25 +0100
a solution using more idiomatic Prolog:
most_often(L,M) :-
retractall(count(_,_)),
forall(member(X,L),(
(retract(count(X,N)) -> N1 is N+1 ; N1 = 1 ),
assert(count(X,N1))
)
),
findall(K/C, count(K,C), [H|T]),
findmax(H,T,M/_).
findmax(H,[],H).
findmax(K/Nk,[X/Nx|T],R) :-
Nk > Nx ->
findmax(K/Nk,T,R) ;
findmax(X/Nx,T,R).
<Rapolas@xxxxxxxxx> ha scritto nel messaggio
news:1163632144.097642.125230@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,
I have a problem, I need to write a program, which finds the element in
a list which repeats most often time. Lets say, I have such query:
?- most_often([a,b,c,a,v,s,a,b,t,a], K).
K = a.
I tried several ways, but no luck... Can anybody help me?
.
- Follow-Ups:
- Re: Count lists elements
- From: Jan Wielemaker
- Re: Count lists elements
- References:
- Count lists elements
- From: Rapolas
- Count lists elements
- Prev by Date: Re: defining operators with a result
- Next by Date: Re: Count lists elements
- Previous by thread: Re: Count lists elements
- Next by thread: Re: Count lists elements
- Index(es):