变量仅使用一次
我正在使用古老的 Turbo Prolog,因为它包含在我们的课程中。为什么这个程序不起作用?
domains
disease, indication = symbol
Patient = string
Fe,Ra,He,Ch,Vo,Ru = char
predicates
hypothesis(Patient,disease)
symptom(Patient,indication,char)
response(char)
go
clauses
go:-
write("What is patient's name?"),
readln(Patient),
symptom(Patient,fever,Fe),
symptom(Patient,rash,Ra),
symptom(Patient,head_ache,He),
symptom(Patient,chills,Ch),
symptom(Patient,runny_nose,Ru),
symptom(Patient,head_ache,He),
symptom(Patient,vomit,Vo),
hypothesis(Patient,Disease),
write(Patient," probably has ", Disease , "."),nl.
go:-
write("Sorry unable to seem to be diagnose disease"),nl.
symptom(Patient,Fever,Feedback) :-
Write("Does " , Patient , " have " , Fever , "(y/n) ?"),
response(Reply),
Feedback = Reply.
hypothesis(Patient, chicken_pox) :-
Fe = Ra = He = Ch = 'y'.
hypothesis(Patient, caner) :-
Ru = Ra = He = Vo = 'y'.
hypothesis(Patient, measles) :-
Vo = Ra = Ch = Fe = He = 'y'.
response(Reply):-
readchar(Reply),
write(Reply),nl.
我得到警告变量仅在包含 symtoms
的所有行中使用。参数传递不是按引用调用吗?当我将 Fe
传递给 symptoms
时,该值应该复制到 Fe
,当我在假设中比较它时,它应该相应地工作。 Turbo Prolog 中的 =
运算符的工作方式非常奇怪。当它没有绑定到任何变量时,语句 a = 3
会将 3 赋给 a,当 a 已经包含值时 a = 5
将检查 a 的值是否为 5或不。
请帮助我为什么程序无法运行?
提前致谢 :)
I am using ancient Turbo Prolog since it is included in our curriculum. Why is this program not working?
domains
disease, indication = symbol
Patient = string
Fe,Ra,He,Ch,Vo,Ru = char
predicates
hypothesis(Patient,disease)
symptom(Patient,indication,char)
response(char)
go
clauses
go:-
write("What is patient's name?"),
readln(Patient),
symptom(Patient,fever,Fe),
symptom(Patient,rash,Ra),
symptom(Patient,head_ache,He),
symptom(Patient,chills,Ch),
symptom(Patient,runny_nose,Ru),
symptom(Patient,head_ache,He),
symptom(Patient,vomit,Vo),
hypothesis(Patient,Disease),
write(Patient," probably has ", Disease , "."),nl.
go:-
write("Sorry unable to seem to be diagnose disease"),nl.
symptom(Patient,Fever,Feedback) :-
Write("Does " , Patient , " have " , Fever , "(y/n) ?"),
response(Reply),
Feedback = Reply.
hypothesis(Patient, chicken_pox) :-
Fe = Ra = He = Ch = 'y'.
hypothesis(Patient, caner) :-
Ru = Ra = He = Vo = 'y'.
hypothesis(Patient, measles) :-
Vo = Ra = Ch = Fe = He = 'y'.
response(Reply):-
readchar(Reply),
write(Reply),nl.
I get the warning variable is only used at all lines which contains symtoms
. Isn't the parameter passing call by reference? When i pass Fe
to symptoms
the value should be copied to Fe
and when i compare it in hypothesis it should work accordingly. =
operator in Turbo Prolog works very strangely. When it is not bound to any variable, the statement a = 3
will assign 3 to a and when a already contains a value a = 5
will check whether a's value is 5 or not.
Kindly help me why is the program not working?
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不在于您的
symptoms/3
谓词,它们会将第三个参数绑定(统一)到response/1
给出的内容。问题是这些值永远不会传递到go/0
中的hypothesis/2
过程中,因此它们永远不会用于尝试生成假设。 Prolog 没有全局变量,因此您必须显式传递所有值,尽管您可以将内容保留在数据库中,如果您不小心,很容易导致问题。这意味着在
hypothesis/2
中,您不是测试Fe
、Ra
、He
等的值,而是测试绑定具有相同名称的局部变量。这也是为什么您会收到变量仅被引用一次的警告,您绑定它们但从不使用它们。请记住它们是局部的,所有变量对于它们出现的子句来说都是局部的。所有这些都适用于标准prolog,我从未使用过Turbo Prolog。
The trouble is not with your
symptoms/3
predicate, they will bind (unify) their 3rd argument to whatresponse/1
gives. The problem is that these values are never passed into yourhypothesis/2
procedure ingo/0
so they are never used to try and generate a hypothesis. Prolog doesn't have global variables so you have to explicitly pass all values, though you can keep things in the database which can easily cause problems if you are not careful.This means that in
hypothesis/2
you are not testing the values ofFe
,Ra
,He
, etc but binding local variables with the same names. This is also why you get warnings of the variables only being referenced once, you bind them but never use them. Remember they are local, all variables are local to the clause in which they occur.All this applies to standard prolog, I have never used Turbo Prolog.