Solved

How to remove negative numbers from a list in Prolog

Posted on 2008-10-07
3
1,194 Views
Last Modified: 2011-10-03
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
Comment
Question by:toymachiner62
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22666117
What is the second arg of non_neg?
0
 
LVL 2

Author Comment

by:toymachiner62
ID: 22666406
The list after the negative number has been removed
0
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 22666466
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Introduction to Processes
Starting up a Project

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question