当与患者(X)查询时,该程序直接返回错误
patient(X):-cancer(X);diabetes(X).
cancer(X):-pain(X).
diabetes(X):-sugar(X).
pain(X):-readPain(X)=='y'.
sugar(X):-readSugar(X)=='y'.
readPain(X):-write('is it painful?'),nl,read(X).
readSugar(X):-write('Do you have sugar problems?'),nl,read(X).
答案 0 :(得分:1)
你写readPain(X)=='y'.
。但Prolog将此解释为(==)(readPain(X),'y')
。 Prolog将readPain(X)
视为术语,不是作为谓词调用。
为了询问一个人是否有pain
,您应该使用:
pain :-
readPain(y).
现在它将查询用户,如果用户写入y.
,谓词将成功。所以可以问:
patient :- cancer; diabetes.
cancer :- pain.
diabetes :- sugar.
pain :-
readPain(y).
sugar:-
readSugar(y).
readPain(X) :-
write('is it painful?'),nl,read(X).
readSugar(X) :-
write('Do you have sugar problems?'),nl,read(X).
read/1
谓词向用户查询术语,解析该术语,并将该参数与该术语统一。这意味着在您调用readPain(X)
之后,X
将具有值y
以防用户编写y.
。通过与y
进行统一,我们会检查是否确实如此。