我是一个新手,我被困在一个程序中

时间:2014-03-03 07:47:42

标签: prolog

目标: 在上面给出的代码中应用一个循环,这样它就会问:“你想继续吗? (是/否)“然后采取相应行动。 (提示:做一个谓词'再次/ 0'来执行上述任务)。

go:-
    write('What is the name of Patient?'), 
    read(Patient),nl, 
    hypo(Patient, Disease), 
    write(Patient),
    write(' probably has '),
    write(Disease),nl. 

go:-  
    write('Sorry I am unable to diagnose the Disease.'),nl.  

hypo(Patient,flu):-  
    sym(Patient,fever),  
    sym(Patient,cough).  
hypo(Patient,headche):-  
    sym(Patient,bodypain).  
sym(Patient,fever):-  
    write(' Does '),
    write(Patient), 
    write(' has fever (y/n)?'),  
    res(R),  R='y'.  
sym(Patient,cough):-  
    write('Does '), 
    write(Patient),
    write(' has cough (y/n)?'),  
    res(R),  R='y'.  
sym(Patient,bodypain):-  
    write('Does '),
    write(Patient),
    write(' has bodypain (y/n)?'),  
    res(R),  R='y'. 
res(R):-  
    read(R),nl. 

1 个答案:

答案 0 :(得分:0)

您已经为模型提供了go谓词。诀窍是,如果R='y'没有失败,则通过递归再次调用谓词来获得循环。

again :- 
   write('Do you want to continue? (Y/ N)'),  
   res(R),  R='y', 
   go,
   again. 
again :- 
   write('OK, bye'),nl.