I have been stumped for 3 hours now on this problem, I need to find the index of (A,B,C) where A is the index positions of B in list C (or -1 if not in the list). This is what I have so far,
indexof(A,0,[A|_]).
indexof(A,B,[_|C]):- Y is B-1, indexof(A,Y,C).
it gives the element at the index spot B, which is not what I want.
indexof(A,1,[1]).
should return A=0;A=-1.
I am horrible at Prolog, I’ve done Java my whole life, so please also provide explanations.
You can use the builtin predicate nth1/3 which can be used directly to achieve what you want.
[edited after OP rephrased question]
The first clause enumerates the index of the item in the list, and the second clause just unifies Index with -1 per OP requirement.