I want to modify the code on Creating a list from user input with swi-prolog :
diagnose(Disease):-
retractall(symptom(_)),
getSymptoms(List),
forall(member(X,List),assertz(symptom(X))),
disease(Disease).
getSymptoms([Symptom|List]):-
writeln('Enter Symptom:'),
read(Symptom),
dif(Symptom,stop),
getSymptoms(List).
getSymptoms([]).
disease(flu):-
symptom(fever),
symptom(chills),
symptom(nausea).
disease(cold):-
symptom(cough),
symptom(runny_nose),
symptom(sore_throat).
disease(hungover):-
symptom(head_ache),
symptom(nausea),
symptom(fatigue).
If I enter 3 symptoms: cough, nausea, fatigue, I should get output of number of symptoms present for different diseases, e.g.:
flu: 1
cold: 1
hungover: 2
How can I get this in Prolog? I know SWI-Prolog has an intersection function that can be used, but how exactly to apply it here?
?- intersection([fever, chills, nausea] ,[cough, nausea, fatigue], X).
X = [nausea].
symptom(fever, flu).
andsymptom(chills, flu).
etc. If you want to collect all of the symptoms for a given illness, you could use,findall(S, symptom(S, Illness), Symptoms)
and use the list ofSymptoms
as needed. – lurkerclause/2
to get the symptoms of the diseases as tuples, though, so that refactor is not entirely necessary. I feel like adding an answer that uses it could help OP and other people in the long run – vmgclause/2
could be used here but I still do not think it is the best approach. – lurker