title: List - counting atoms related to their previous term
body: <p>I want to count the number of elements in a list which have a relation with the element following. 
  The predicate I have works by using an accumulator variable which it increments if the predicate related returns true. 
  The following example code is to check the number of times an element is greater than it's previous element.</p>
  
  <p>So for example</p>
  
  <pre><code>count_list([1,2,3,2,1,3,2],Count).
  </code></pre>
  
  <p>should return 3.</p>
  
  <p>The code almost works. It increments the accumulator variable correctly. However, the function returns false, when it tries to compare the final 2 at the end with the non-existent next term.</p>
  
  <pre><code>listofitems([],N,N).
  %count number of items which are related to the previous
  listofitems([A,B|T],Acc,N) :-
     write(A),write(' '), write(B),
     ( related(A,B) -&gt; Acc1 is Acc+1 ; Acc1 = Acc ),
     write(Acc1),write('\n'),
    listofitems([B|T],Acc1,N).
  
  count_list(L,N):-
     listofitems(L,0,N).
  
  %define the relationship to be counted
  related(A,B):-
     B&gt;A.
  </code></pre>
  
  <p>Does anyone have any suggestions as to how to create an elegant terminating condition so I can return the accumulated value?</p>