I'm new to Prolog and have a question to a programming exercise:
I have a program, that is for my opinion working most of the time, but with a specific query, I do not get an answer
is_number(0).
is_number(s(N)) :-
is_number(N).
numberpair(pair(X,Y)) :-
is_number(X),
is_number(Y).
?- numberpair(pair(A,B)), A=s(s(0)), B=s(s(s(0))).
So I understand, that Prolog now tries every possible number for A and B -> [0,s(0),s(s(0)),s(s(s(0))), ...] but if it founds an answer for A (namely s(s(0))
) it fails at B and in the next call, it tries the next answer for A (namely s(s(s(0)))
) and so on.
Now the thing is, that I want Prolog to stop, if it founds an answer for A, and only searches now for an answer for B.
Can anybody give me a hint, how to solve this?
numberpairD
-->numberpair
? – lurkerB=s(s(s(0)))
) and have it backtrack to the first goal (numberpair(pair(A,B))
) but to skip over the second goal (A=s(s(0))
) while backtracking. You need to redefine your query so that it makes sense. – EnigmativityD
was a very broad hint how to solve this. – false