How to find the positive numbers in a list in Prolog? -
i want write code in prolog gets list , find positive numbers , adds them new list below :
?- findpositives([-1,2,3,-5,-7,9],result) result : [2,3,9]
how can write code?
using tfilter/3
:
positive_truth(n, true) :- n >= 0. positive_truth(n, false) :- n < 0. ?- tfilter(positive_truth, [-1,2,3,-5,-7,9],result). result = [2,3,9]
alternatively, using library(clpfd)
:
pos_truth(expr, truth) :- expr #>= 0 #<==> bool, bool01_truth(bool, truth). bool01_truth(0,false). bool01_truth(1,true). ?- tfilter(pos_truth, [-1,2,3,-5,-7,9],result). result = [2,3,9]. ?- tfilter(pos_truth, [x,y],result). result = [], x in inf.. -1, y in inf.. -1 ; result = [y], x in inf.. -1, y in 0..sup ; result = [x], x in 0..sup, y in inf.. -1 ; result = [x, y], x in 0..sup, y in 0..sup.
Comments
Post a Comment