Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Search - Complete beginner

Status
Not open for further replies.

bikerbabe

Programmer
Mar 23, 2003
3
IE
Hiya,
I have just started learning Prolog and I can't work out how to search a list for a member. I want to be able to search all the lists for a number of symptoms and then produce a list of all possible diseases. I can't even start by checking my lists for one symptom. The code is:

member(H,[H|T]).
member(X,[H|T]) :- member(X,T).

disease(cancer_pancreas([fatigue, pain, appetiteloss, nausea])).
disease(common_cold([fatigue, headache])).

When I use the listener and put in :
member(fatigue, disease(X)).

I always get no.

Am I making a really basic mistake? Any help appreciated.
Thanks
bb
 
well actually i dont know where to start... the list of things which i have to say is long :)
(sorry im just teasing)

first of all ur member predicate. i dont know which Prolog you are using but my implementation has already a built-in member predicate. thus i get an error if i start ur script. if ur prolog doesnt have a built-in member u should modify it anywa because u have singelton variables

modify it like this:

mymember(H,[H|_]).
mymember(H,[_|T]) :-
mymember(H,T).

I hope u can see the difference. i used 2 anonymus variables because we dont eed the names there

ok but know the mainpart. the thing u are trying cant work and the style doesnt look like prolog neither

u should become familiar how to do the unification in prolog. a better way would be this one

desease(X), mymember(fatigue,X).

the thing u were trying is more a functional thing like lisp and scheme and hardly works in prolog and in this case it doesnt. PROLOG WORKS WITH RELATIONS NOT FUNCTIONAL :)
im not scraming its a very important fact

ok now the last thing. X unifies with the "contents" of the disease predicate. well X is not a list. X will be a new predicate just like common_cold([fatigue, headache]) and mymember(fatigue,common_cold([fatigue, headache])). will certainly fail because its not a list.

thats why i would recommand toget a new structure of ur diseases maybe like this:

disease(common_cold,[fatigue, headache]).
disease(cancer_pancreas,[fatigue, pain, appetiteloss, nausea]).

and so on. now u can search like this

disease(_,List), mymember(fatigue,List).
and u will get yes

or:
disease(Dis,Symptomes), mymember(fatigue,Symptomes).
and u will get
Dis=comman_cold;

Dis=cancer_pancreas;

no

...

i hope this helps and good luck. for further questions feel free to ask anything u dont know. i really didnt mean to be rude if hat the impression...

Prolog is powerfull. Just learn to make it work for u :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top