title: Prolog, recursive functions, and returning value by argument of a function
body: <p>Question is simply... why this doesn't work?</p>
  
  <pre><code>replace([l|[r|[r|[l|R]]]], Result) :- append([f,f],R,Result).
  
  replace([HEAD|TAIL], Result) :- replace(TAIL, Y), append(HEAD,Y,Result).
  </code></pre>
  
  <p><code>?- replace([l,r,r,l,r,r],X).</code></p>
  
  <p>returns</p>
  
  <p><code>X = [f, f, r, r] .</code></p>
  
  <p>but</p>
  
  <p><code>?- replace([r,l,r,r,l,r,r],X).</code></p>
  
  <p>gives</p>
  
  <p><code>false</code></p>
  
  <p>why not <code>X = [r, f, f, r, r] .</code> ?</p>
  
  <p>please help.</p>
  
  <hr>
  
  <p>PS. I am still learning English, however sometimes I am making stupid mistakes. I am doing my best, writing questions and answers in English, however I would be very glad if you will edit my post in case of any mistakes. I promise, your effort will not be wasted.</p>