0
votes

I am trying following code:

disease(flu, [fever, chills, nausea]).
disease(cold, [cough, runny_nose, sore_throat]).
disease(hungover, [headache, nausea, fatigue]).

getSymptoms([Symptom|List]):-
    writeln('Enter Symptom:'),
    read(Symptom),
    dif(Symptom,stop),
    getSymptoms(List),
    intersection(disease(_,List1), List, Outlist).

getSymptoms([]).

The entry method is from: Creating a list from user input with swi-prolog

The user starts with:

getSymptoms(L).

I need to collect patients symptoms (say following are entered: fever, chills)

I then need to compare this list with symptom list of each disease and find out how many symptoms are common between 2 lists.

The getSymptoms(L) is collecting symptoms all right but is not giving intersection and returning an empty list:

?- getSymptoms(L).
Enter Symptom:
|: fever.
Enter Symptom:
|: stop.
L = [].

Following also did not work:

intersection(findall(L,disease(_,L),Out), List, Outlist).

How can I correct this? Thanks for your help.

Edit: with code suggested by @wouter_beek, I get:

disease(_, List1),
sort(List1, SList1),
sort(List, SList),
intersection(SList1, SList, OutList).

?- getSymptoms(L).
Enter Symptom:
|: fever.
Enter Symptom:
|: cough.
Enter Symptom:
|: stop.
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever, cough] ;
L = [fever] ;
L = [fever] ;
L = [fever] ;
L = [].

I want disease with number of common symptoms to be shown.

1
This doesn't make any sense. The code you are showing, with Wouter Beek's changes, doesn't generate the results you are showing. Please be clear about what code you actually tried and the actual results.lurker

1 Answers

1
votes

You are calling the intersection/3 predicate as if it is a function:

intersection(findall(L,disease(_,L),Out), List, Outlist).

But this is not how Prolog works. intersection(X, Y, Z) denotes a relation between three sets. findall(L, disease(_, L), Out) does not denote a set. Out is a list, but not necessarily a set. You can use sort/2 for that.

You should do something similar with the following line:

intersection(disease(_,List1), List, Outlist).

to:

disease(_, List1),
sort(List1, SList1),
sort(List, SList),
intersection(SList1, SList, OutList).