PDA

View Full Version : Desperate PROLOG question!


Neo_22
12-04-2003, 03:25 AM
I am a bit desperate here. I have to finish a prolog program by tomorrow, and I just can't come up with the answer. Anyways, here is the question:

Write prolog rules to remove all negative numbers from a list of integes.

Anybody want to give it a shot?

-Neo_22-

Neo_22
12-04-2003, 01:45 PM
OK, so after thinking about it for some time, and drinking some coffee (that always helps) I think I figured out the answer ... it would be nice if someone can tell me if I am on the right track ...

I did some reading and found a function that is similar to the one I need. After making some modifications, I think this should be my "delete negatives" function:


/* split([H|T],P) is true if P is the result of deleting all negative elements in the list [H|T].*/

split([],[]).
split([H|T], [H|P]) :- H >= 0, split(T, P).
split([H|T], [_|P]) :- H < 0, split(T, P).


PS: I realized that I cannot except any of you guys to do this project for me, and I am sorry if that is what it seemed like at first. Just so you know, after posting the thread I continued to think about the problem and came up with this solution. It would be nice if anyone could verify it. Thanx!

-Neo_22-:computer:

jemfinch
12-31-2003, 07:10 AM
Why's that function named split?

Why do you have two Hs in your first definition of it?

Why do you have a variable named P?

Jeremy