Solved

How to remove negative numbers from a list in Prolog

Posted on 2008-10-07
3
1,148 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

839 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