Home > Enterprise >  Prolog, unknown argument solving
Prolog, unknown argument solving

Time:09-25

I have a question about my rule. It should return true if Var1 and Var2 are first elements in two given lists.

It works okay here:

?- testpos([test1, test2, test3], [testA, testB, testC], test1, testA).
true.

?- testpos([test1, test2, test3], [testA, testB, testC], test1, testB).
false.

But I want the rule be able to solve it for unkown input.

For example(expected output):

?- testpos([test1, test2, test3], [testA, testB, testC], Z, X).
Z = test1
X = testA
false.

My code now:

testpos([X| _], [Y| _], Var1, Var2) :-
    X == Var1, Y == Var2.

Solved

My mistake was that I used == instead of =

CodePudding user response:

You are almost there

testpos([X| _], [Y| _], Var1, Var2) :-
    X = Var1, Y = Var2.

==/2 succeeds when both it's arguments are equivalent without further unification, where as =/2 succeeds when they are unifiable and the arguments are unified.

Look at this answer: What is the difference between == and = in Prolog?

You can just as well write:

testpos([X| _], [Y| _], X, Y).

Prolog does unification to match the clause, so they are essentially equivalent.

  • Related