该prolog程序不会询问用户的输入

时间:2017-11-07 10:49:15

标签: prolog

当与患者(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).

1 个答案:

答案 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进行统一,我们会检查是否确实如此。