使用 swi-prolog 从用户输入创建列表
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:- 写('Creating a list'),nl,
写('Enter -1 to stop'),nl,
创建(L),
写('List is:'),
写(L).
这是我第一次使用 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:- 写('Creating a list'),nl, 写('Enter -1 to stop'),nl, 创建(L), 写('List is:'), 写(L).