简体   繁体   English

使用swi-prolog从用户输入创建列表

[英]Creating a list from user input with swi-prolog

This is my first experience with Prolog. 这是我第一次接触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. 在开始比较之前,我需要使用从用户读取的值构建列表的尾部,但似乎语法不正确。

This is what I have so far: 这是我到目前为止的内容:

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]).

The error occurs on the append line. 错误发生在追加行上。 Syntax Error: Operator Expected. 语法错误:期望运算符。 Any help with this error or the design of the program in general would be greatly appreciated. 对此错误或程序设计的任何帮助将不胜感激。

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([]).

You type stop. 您键入停止。 when you want to finish the list. 当您想完成列表时。

You would then need to decide what logic you want to match the way you have represented a disease. 然后,您需要确定要与代表疾病的方式相匹配的逻辑。

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).

create(L1):-read(Elem),create(Elem,L1). 创建(L1): - 读取(ELEM),创建(ELEM,L1)。

create(-1,[]):-!. 创建(-1,[]): - ! create(Elem,[Elem|T]):-read(Nextel),create(Nextel,T). 创建(ELEM,[ELEM | T]): - 读取(Nextel的),创建(Nextel的,T)。

go:- write('Creating a list'),nl, write('Enter -1 to stop'),nl, create(L), write('List is:'), write(L). go:-write('创建列表'),nl,write('输入-1以停止'),nl,create(L),write('List is:'),write(L)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM