title: Count number of occurrences in a list in Prolog
body: <p>I write this little code in Prolog that counts the number of occurrences of some term in a list. It works, but it is not tail recursive (so no recursive optimization).</p>
  
  <p>How could I write the same program but with tail recursion?</p>
  
  <pre><code>counter(T,[],X) :- 
     X is 0.
  counter(T,[T|D],X1) :-
     !,
     counter(T,D,X),
     X1 is X+1.
  counter(T,[_|D],X1) :-
     counter(T,D,X1).
  </code></pre>
  
  <p>I think that I should use an accumulator but I've no idea how to implement. Any help?</p>