このディレクトリの索引

#  
#  I need to write a small Prolog program to count the number of occurrence
#  of each element in a list.
#  
#  numberOfRepetition(input, result)
#  For example:
#  
#  numberOfRepetition([a,b,a,d,c,a,b], X)
#  can be satisfied with X=[a/3,b/2,d/1,c/1] because a occurs three times,
#  b occurs 2 times and c and d one time.

numberOfRepetition(_input,_result) :-
        setof(A,member(A,_input),L1),
        findall(B/Count,(
                    member(B,L1),
                    count(B,_input,Count)),
                _result).

count(A,[],0).
count(A,[A|R],X) :-
        count(A,R,Y),
        X is Y + 1.
count(A,[_|R],X) :-
        count(A,R,X).