Prolog Clear List of negative elements without using cuts

不羁岁月 提交于 2019-12-19 11:57:49

问题


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 the item is nonnegative you know that the item will be on the resulting list. Therefore you should do the recursion and return a list that contains the element you have checked (H) and the list returned by recursion. Thus, the last clause should be

filter([H|T],[H|S]) :-
  H>=0,
  filter(T,S).



回答2:


In SWI-Prolog you can use exclude/3

?- exclude(negative, [-1, -0.5, 0, 0.5, 1], L).
L = [0, 0.5, 1].

provided that you have a definition for negative/1:

negative(Number) :-
    Number < 0.



回答3:


Using recursion for the last case,

I'd write :

filter([],[]).

filter([H|T],S) :-
  H<0,
  filter(T,S).

filter([H|T], L) :- 
 H>=0, 
 filter(T, S),
 append([H],S,L).


来源:https://stackoverflow.com/questions/6670603/prolog-clear-list-of-negative-elements-without-using-cuts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!