Python counter in Prolog
Question: In Python you can do

    
    
    >>> import from collections counter
    >>> Counter(['a','b','b','c'])
    >>> Counter({'b': 2, 'a': 1, 'c': 1})
    

Is there something similar in Prolog? Like so:

    
    
    counter([a,b,b,c],S). 
    S=[a/1,b/2,c/1].
    

This is my implementation:

    
    
    counter([],List,Counts,Counts).
    
    counter([H|T],List,Counts0,[H/N|Counts]):-
      findall(H, member(H,List), S),
      length(S,N),
      counter(T,List,Counts0,Counts).
    
    counter(List,Counts):-
      list_to_set(List,Set),
      counter(Set,List,[],Counts).
    

It's rather verbose, so I wondered if there was a builtin predicate or a more
terse implementation.


Answer: There is no builtin predicate, here is another way to do that :

    
    
    counter([X], [X/1]).
    
    counter([H | T], R) :-
        counter(T, R1),
        (   select(H/V, R1, R2)
        ->  V1 is V+1,
            R = [H/V1 | R2]
        ;   R = [H/1 | R1]).
    

