position/3 is really just the basic pattern with no additions. The
idea is that we know we have got to a particular position when the
counter has been reduced to '1':
% 1 terminating condition
position(1, Elem, [Elem|_]).
% 2 recursive
position(Cnt, Elem, [_|Tail]) :-
Cnt1 is Cnt - 1,
position(Cnt1, Elem, Tail).
Try the following goals:
| ?- position(0, Elem, [a,b,c]).
| ?- position(1, Elem, [a,b,c]).
| ?- position(2, Elem, [a,b,c]).
| ?- position(3, Elem, [a,b,c]).
| ?- position(4, Elem, [a,b,c]).
|