0% found this document useful (0 votes)
94 views3 pages

Creating A List From User Input With Swi

The document discusses creating a program in Prolog that takes user input of symptoms and compares them to lists of diseases and their symptoms to diagnose a potential disease. It provides sample code to define lists of diseases with symptoms and an initial attempt at a getSymptoms predicate to build a list of symptoms from user input, but notes the syntax is incorrect. It then provides an improved getSymptoms predicate to recursively take user input of symptoms as a list until a stop value is entered.

Uploaded by

teddy demissie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views3 pages

Creating A List From User Input With Swi

The document discusses creating a program in Prolog that takes user input of symptoms and compares them to lists of diseases and their symptoms to diagnose a potential disease. It provides sample code to define lists of diseases with symptoms and an initial attempt at a getSymptoms predicate to build a list of symptoms from user input, but notes the syntax is incorrect. It then provides an improved getSymptoms predicate to recursively take user input of symptoms as a list until a stop value is entered.

Uploaded by

teddy demissie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy