Solved

How to remove negative numbers from a list in Prolog

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to adjust sheet 1 81
sumDigits  challenge 7 60
Sorting in Excel with Group Headers if the Exist 2 68
mapBully challenge 6 89
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now