title: How to count how many times a character appears in a string list in Prolog?
body: <p>I want to check if a character exists in a string. So <code>Atom</code> is the string and <code>Ch</code> the character. <code>name</code> is a predicate that converts the string in a list of numbers according to the ASCII code. </p>
  
  <p><code>find_element</code> is a predicate that is supposed to be true only if element <code>X</code> is part of a list. <code>C</code> is a counter that tells us where exactly element <code>X</code> was found.</p>
  
  <p>This is the result I am getting: </p>
  
  <pre><code>?- exists(prolog,g). [103][112,114,111,108,111,103] false.
  </code></pre>
  
  <p>-------> 103 is the ASCII code of letter "g" and the list <code>[112,114,111,108,111,103]</code> is the list that represents the string "prolog". The question <code>exists(prolog,g)</code> should have provided a true response.</p>
  
  <p>However the <code>find_element</code> predicate is working correctly. I don't understand why this is happening because when I type for example </p>
  
  <pre><code>?- find_element(5,[3,4,5,6,5,2],X).
  </code></pre>
  
  <p>I am getting <code>X= 3 ; X = 5 ; false.</code>
   ----></p>
  
  <p>which is absolutely fine because it tells me that 5 is the 3rd and the 5th element of the list.</p>
  
  <p>So the problem is that <code>find_element</code> is working when I type something like <code>?- find_element(5,[3,4,5,6,5,2],X)</code> but it is not when I try to call the predicate exists (which calls <code>find_element</code>).</p>
  
  <p>This is the code:</p>
  
  <pre><code>find_element(X,[X|T],1).
  
  find_element(X,[H|T],C):- find_element(X,T,TEMPC), C is TEMPC +1.
  
  exists(Atom,Ch):- name(Atom,[X|T]), name(Ch,Z), write(Z), write([X|T]), find_element(Z,[X|T],Count).
  </code></pre>
  
  <p>Thanks in advance</p>