title: Prolog : Find the kth element of this list : What's the difference between those 2 implementations?
body: <p>I am playing with some beginner problems in prolog.<br />
  &quot;Find the k'th element of a list&quot;   .<br />
  I wrote that:</p>
  <pre><code>kth(X,[X|_],1).
  kth(X,[H|R],K):- kth(X,R,K-1).
  </code></pre>
  <p>It was <strong>wrong</strong>. Most test-cases resulted into false.<br />
  The suggested solution was:</p>
  <pre><code>element_at(X,[X|_],1).
  element_at(X,[_|L],K) :- 
     K &gt; 1, 
     K1 is K - 1, 
     element_at(X,L,K1).
  </code></pre>
  <p>Where did my code go wrong? I am interpeting my code as : &quot;if you are looking for the first element it the the head. If you are looking for the kth element in [H|R] it is the (k-1)th element in R .&quot;<br />
  even when I tried to include <code>X&gt;1</code> in my code, it did not work .</p>
  <p>This <code>K1 is K-1</code> instead of simply writing <code>K-1</code>  inside the function seems like it played a huge role. Can you help me understand?</p>