Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!howland.reston.ans.net!Germany.EU.net!Munich.Germany.EU.net!ecrc!acrab60!thom
From: thom@ecrc.de (Thom Fruehwirth)
Subject: Re: new kid have question, show number by
Message-ID: <D6vL2o.JJB@ecrc.de>
Sender: news@ecrc.de
Reply-To: thom@ecrc.de
Organization: European Computer-Industry Research Centre GmbH.
References: <3mc0s2$gg8@irix.uci.kun.nl>
Date: Tue, 11 Apr 1995 14:47:11 GMT
Lines: 22

/*
Here is a cleaner way to compute the number of occurences of values in a list.
(You don't need assert, retract, forall, sort, findall - recursion does it).

Example - printing routines are omitted
[eclipse]: score([1, 2, 1, 1, 1, 1, 2, 1, 11, 2, 1, 1, 1, 1, 4, 111, 22, 33, 111, 8],S).

S = [1 - 10, 2 - 3, 11 - 1, 4 - 1, 111 - 2, 22 - 1, 33 - 1, 8 - 1] 
*/


score(List,Scores):- score(List,[],Scores).

score([],S,S).
score([N|L],S1,S3):-
	update_score(N,S1,S2),
	score(L,S2,S3).

   update_score(N,[],[N-1]).
   update_score(N,[N-F|S],[N-F1|S]):- F1 is F+1.
   update_score(N,[M-F|S],[M-F|S1]):- N\==M, update_score(N,S,S1).

