I need to get the index of a term's argument in Prolog. Predicate arg/3 seems to do the opposite of what I need:
arg(Index, Term, Value).
arg/3 fails if Index is a variable, so it's not possible to get the index of a given value and term. You guys know any other way to achieve this (I can't use external libraries)?
An example of the expected behaviour would be:
?- arg_(Index, regs(a,b,c), c).
Index = 3
Not all Prolog implementations seem to behave like SWI-Prolog does when the index is a variable. Its behavior might be an extension to the standard.
Here is what GNU Prolog 1.4.5 does:
So you'll have to backtrack over the valid indices yourself. You can use
functor/3to find out how many arguments there are:And many Prologs (including GNU Prolog) have a
between/3predicate for enumerating integers in a range:So overall you can do: