Prolog Clear List of negative elements without using cuts
问题 How do I write a procedure in Prolog that clears a list of integers of its negative elements and returns the result in a new list? Without using cuts but can use negation. For example: ?- filter([1,0,-6,7,-1],L). L = [1,0,7]; no 回答1: You have it almost right. Your solution was: filter([],[]). filter([H|T],S) :- H<0, filter(T,S). filter([H|T],S) :- H>=0, filter(T,[H|S]). The base case and the case where the item is negative are right. The problem is with the last case. Once you checked that