Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1225
  • Last Modified:

How to remove negative numbers from a list in Prolog

I'm trying to take a list, and remove only the negative numbers. So far i have this

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

I'm pretty sure the error is in the 3rd line because when i query with an empty list or a list consisting of only negaive numbers it works fine. Sorry i'm a noob.
0
toymachiner62
Asked:
toymachiner62
  • 2
1 Solution
 
ozoCommented:
What is the second arg of non_neg?
0
 
toymachiner62Author Commented:
The list after the negative number has been removed
0
 
ozoCommented:
In that case, shouldn't
non_neg([H|T],S) :- H>=0, non_neg(T,H).
be
non_neg([H|T],S) :- H>=0, non_neg(T,[H|S]).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now