listprologvisual-prolog

Visual Prolog: Maximum number of anyflow


I get an error message Maximum number of anyflow variants (1000) exceeded when trying to execute this code:

findNegative([], []).
findNegative([Q|V], Y) :-
   Q > 0,
   !,
   findNegative(V, Y).
findNegative([H1|T1], S) :-
   findNegative(T1, [H1|S]).

Same when trying to execute code from this answer: https://stackoverflow.com/a/6671142/4829408


Solution

  • Consider the following code:

    find_negatives([],        [] ).
    find_negatives([E|Es],    Xs ) :- E >= 0, find_negatives(Es, Xs).
    find_negatives([E|Es], [E|Xs]) :- E <  0, find_negatives(Es, Xs).
    

    Sample query:

    ?- find_negatives([1,2,3,-1,-2,-3,0,1,2,0,-1], Xs).
       Xs = [-1,-2,-3,-1]
    ;  false.