Here you have an extra pruning operator after isEven(X)
, the oddness condition and the condition X > Z
common and if at least one of these parts is false, then you need to continue searching for the solution further, that is, go to the third rule add_even
. And with the clipping operator when finding an odd number less than Z, you do not allow Prolog to use the third rule.
UPD
The add_to_list
predicate returns true when the third parameter is a list with head X
and body L
during the first pass, the ResultList
is not yet specified, therefore the matching occurs, during the second pass, the ResultList
already specified, and the matching is impossible, since the head of the list is not equal to X
In this case, adding items to the result list should be used using either another parameter or the FirstList
list:
add_even(X, [], X, _). add_even(FirstList, [X|SecondList], ResultList, Z):- isEven(X), X > Z,!, add_even([X| FirstList], SecondList, ResultList, Z). add_even(FirstList, [_|SecondList], ResultList, X):- add_even(FirstList, SecondList, ResultList, X).
Adding elements to the ResultList
immediately ResultList
not work, because when you exit the recursion, the values of the ResultList
will be rolled back.