title: Prolog Recursion and Termination
body: <p>first StackOverflow question.</p>
  
  <p>I'm writing a predicate in prolog that takes three parameters. These are (respectively) a character, a list of strings, and the last parameter is a list of all the strings in the second parameter that start with the first parameter. My write statements are a substitution for my complete lack of knowledge on how to trace in SWI-Prolog. Anyway, on to the code!</p>
  
  <hr>
  
  <pre><code>startString(C, [H1|T1], [H2|T2]) :-
      atom_chars(H1, [C| _ ]),
      H2 = H1,
      startString(C, T1, T2).
  
  startString(C, [ _ |T1], Y) :-
      startString(C, T1, Y),
      write(foo).
  
  startString(_, [], []) :-
      write(foo).
  </code></pre>
  
  <hr>
  
  <p>Which outputs:</p>
  
  <hr>
  
  <pre><code>foofoofoo
  
  X = [some, simple]
  </code></pre>
  
  <hr>
  
  <p>My methodology is correct, but the predicate doesn't <em>terminate</em> (the lack of period after the write of X isn't a mistake). My question is, why isn't it? From the limited examples of recursion I've found on the internet, the third version of my predicate should terminate the predicate and make x a definite answer.</p>
  
  <p>When I press enter I'm able to enter another query, but I have this same little "issue" in another predicate I wrote in the same program. Any help with this predicate should also carry over to the other. Thanks!</p>