
% search a list for an element, return its position
%  (positions are 0-based, returns -1 if not found)
% -------------------------------------------------

% case where E matches head
position(E, [E|_], 0).

% base case, empty list
position(_, [], -1).

% case where E is found later in list
position(E, [_|T], Pos) :- position(E, T, R),
    R > -1, P is R + 1, P = Pos.

% otherwise it isn't found
position(_, _, -1).

