I made a Prolog predicate posAt(List1,P,List2)
that tests whether the element at position P
of List1
and List2
are equal:
posAt([X|Z], 1, [Y|W]) :-
X = Y.
posAt([Z|X], K, [W|Y]) :-
K > 1,
Kr is K - 1,
posAt(X, Kr, Y).
When testing:
?- posAt([1,2,3], X, [a,2,b]).
I expected an output of X = 2
but instead I got the following error:
ERROR: >/2: Arguments are not sufficiently instantiated
Why am I getting this error?
A Prolog predicate is a relation between arguments, and your statement
the element at position P of List1 and List2 are equal
is clearly an example where multiple solutions are possible.
?- posAt([1,2,3],X,[1,5,3,7]).
X = 1.
So the answer from sharky, while clearly explains why the technical error arises, requires a small correction:
posAt([X0|_], Pos, Pos, [X1|_]) :-
X0 == X1.
Now it works as expected.
?- posAt([1,2,3],X,[1,5,3,7]).
X = 1 ;
X = 3 ;
false.
Writing simple predicates for list processing it's a very valuable apprenticeship practice, and the main way to effectively learn the language. If you are incline also to study the available library predicates, here is a version using nth1/3 from library(lists)
posAt(L0, P, L1) :-
nth1(P, L0, E),
nth1(P, L1, E).
This outputs:
?- posAt([1,2,3],X,[1,5,3,7]).
X = 1 ;
X = 3.
Could be interesting to attempt understanding why in this case SWI-Prolog 'top level' interpreter is able to infer the determinacy of the solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With