'spilt list into to positive and negative lists in visual prolog
Solution 1:[1]
As an example of simple list processing:
split_list(PosNegLst, PosLst, NegLst) :-
must_be(list, PosNegLst),
split_list_(PosNegLst, PosLst, NegLst).
split_list_([], [], []).
split_list_([Head|Tail], PosLst, [Head|NegLst]) :-
Head < 0, !,
split_list_(Tail, PosLst, NegLst).
split_list_([Head|Tail], [Head|PosLst], NegLst) :-
Head >= 0,
split_list_(Tail, PosLst, NegLst).
Result in swi-prolog:
?- time(split_list([5, -2, -3, 6, 0, 8], PosLst, NegLst)).
% 21 inferences, 0.000 CPU in 0.000 seconds (89% CPU, 406677 Lips)
PosLst = [5,6,0,8],
NegLst = [-2,-3].
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | brebs |

