[英]Creating a list from user input with swi-prolog
这是我第一次接触Prolog。 我正处于编写程序的开始阶段,该程序将接受用户的输入(症状)并使用该信息来诊断疾病。 我最初的想法是创建一个列表,列表中的疾病名称位于列表的开头,症状位于列表的尾部。 然后提示用户输入症状,并使用用户输入创建列表。 然后比较列表以查看尾巴是否匹配。 如果尾部匹配,则我创建的列表的开头将是诊断。 首先,我将程序缩小为只有三种症状的三种疾病。 在开始比较之前,我需要使用从用户读取的值构建列表的尾部,但似乎语法不正确。
这是我到目前为止的内容:
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]).
错误发生在追加行上。 语法错误:期望运算符。 对此错误或程序设计的任何帮助将不胜感激。
这是读取以下症状列表的一种方法:
getSymptoms([Symptom|List]):-
writeln('Enter Symptom:'),
read(Symptom),
dif(Symptom,stop),
getSymptoms(List).
getSymptoms([]).
您键入停止。 当您想完成列表时。
然后,您需要确定要与代表疾病的方式相匹配的逻辑。
一个完整的例子:
:-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).
创建(L1): - 读取(ELEM),创建(ELEM,L1)。
创建(-1,[]): - ! 创建(ELEM,[ELEM | T]): - 读取(Nextel的),创建(Nextel的,T)。
go:-write('创建列表'),nl,write('输入-1以停止'),nl,create(L),write('List is:'),write(L)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.