Terminate Query after Failure Prolog

71 views Asked by At

I have this list in Prolog and a predicate check_lower(N1, N2) that determines what numbers N1 are lower than N2. Now since the list is in order as shown below, I made the predicate to iterate over the list until it encounters the same number. Once it encounters the same number it terminates.

The problem I'm having at the moment is that it backtracks after it finds the same number and displays the rest of the list. Is there a way I could terminate after a failure.

numbers([one,two, three, four, five]).
number(X) :- numbers(N), member(X, N).
check_lower(N1,N2) :- number(N1), N1\=N2.

This is my query: ?- check_lower(N, three).

I have traced my program to no avail, any suggestions are appreciated, thanks. Also, sorry if this post is clustered, it's my first time posting here.

1

There are 1 answers

2
knetsi On BEST ANSWER

I went with another approach. Used your predicate for list of numbers that are ordered, and with the help of the append I return a list of the "numbers" (elements) that are smaller (appear earlier in the list) this can easily adapt for greater values.

numbers([one,two, three, four, five]).
check_lower(N1,N2):-
    numbers(ALL),
    append(N1,[N2|_],ALL).