Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Creating a list from user input with swi-prolog

This is my first experience with Prolog. I am at the beginning stages of writing a program that will
take input from the user (symptoms) and use that information to diagnose a disease. My initial
thought was to create lists with the disease name at the head of the list and the symptoms in the
tail. Then prompt the user for their symptoms and create a list with the user input. Then compare
the list to see if the tails match. If the tails match then the head of the list I created would be the
diagnosis. To start I scaled the program down to just three diseases which only have a few
symptoms. Before I start comparing I need to build the tail of list with values read from the user
but I can't seem to get the syntax correct.

disease([flu,fever,chills,nausea]).
disease([cold,cough,runny-nose,sore-throat]).
disease([hungover,head-ache,nausea,fatigue]).

getSymptoms :-
write('enter symptoms'),nl,
read(Symptom),
New_Symptom = [Symptom],
append ([],[New_symptom],[[]|New_symptom]),
write('are their more symptoms? y or n '),
read('Answer'),
Answer =:= y
-> getSymptoms
; write([[]|New_symptom]).
This is one way to read a list of symptoms in:

getSymptoms([Symptom|List]):-
writeln('Enter Symptom:'),
read(Symptom),
dif(Symptom,stop),
getSymptoms(List).
getSymptoms([]

A complete example:

:-dynamic symptom/1.

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(flue):-
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).

You might also like