[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1250
  • 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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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