Re: How can i get the count of each number in a file ?
"Richard A. O'Keefe" <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
Suppose you have a list of ground terms and want to get a list of (Term, Count) pairs. The Unix shell equivalent would be sort <input | uniq -c So member_counts(List, Member_Counts) :- attach_weights(List, List_With_Weights), keysort(List_With_Weights, Sorted_List_With_Weights), add_weights(Sorted_List_With_Weights, Member_Counts). attach_weights([], []). attach_weights([X|Xs], [X-1|Pairs]) :- attach_weights(Xs, Pairs). add_weights([], []). add_weights([X-W|Pairs], Member_Counts) :- add_corresponding_weights(Pairs, X, W, Member_Counts). add_corresponding_weights([X-W1|Pairs], X, W0, Member_Counts) :- !, W2 is W0 + W1, add_corresponding_weights(Pairs, X, W2, Member_Counts). add_corresponding_weights(Pairs, X, W, [X-W|Member_Counts]) :- add_corresponding_weights(Pairs, Member_Counts). I'd stick with Member - Count rather than (Member, Count) if I were you.