Prolog Clear List of negative elements without using cuts

后端 未结 3 1759
慢半拍i
慢半拍i 2021-01-16 16:45

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.

3条回答
  •  一整个雨季
    2021-01-16 17:06

    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.
    

提交回复
热议问题