首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在GNU-Prolog中运行这样的程序?

如何在GNU-Prolog中运行这样的程序?
EN

Stack Overflow用户
提问于 2018-12-07 22:04:41
回答 1查看 154关注 0票数 0

每次我尝试编译它时,编译似乎总是失败。我对这门语言非常陌生,在寻找使用Prolog的程序示例时发现了这个程序,但我对如何运行它一无所知。使用这个程序片段作为例子的原因是我想做一个我自己的程序,它将能够让用户根据用户输入的症状知道他们得了什么病。

代码语言:javascript
复制
domains
disease,indication = symbol.
Patient,name = string.
predicates
hypothesis(string,disease).
symptom(name,indication).
response(char).
go.
clauses

程序会在第一行检测到错误,我不确定为什么。

代码语言:javascript
复制
go :-
    write("What is the patient's name? "),
    readln(Patient),
    hypothesis(Patient,Disease),
    write(Patient,"probably has ",Disease,"."),nl.

go :-
    write("Sorry, I don't seem to be able to"),nl,
    write("diagnose the disease."),nl.

symptom(Patient,fever) :-
    write("Does ",Patient," have a fever (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,rash) :-
    write("Does ",Patient," have a rash (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,headache) :-
    write("Does ",Patient," have a headache (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,runny_nose) :-
    write("Does ",Patient," have a runny_nose (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,conjunctivitis) :-
    write("Does ",Patient," have a conjunctivitis (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,cough) :-
    write("Does ",Patient," have a cough (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,body_ache) :-
    write("Does ",Patient," have a body_ache (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,chills) :-
    write("Does ",Patient," have a chills (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,sore_throat) :-
    write("Does ",Patient," have a sore_throat (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,sneezing) :-
    write("Does ",Patient," have a sneezing (y/n) ?"),
    response(Reply),
    Reply='y'.

symptom(Patient,swollen_glands) :-
    write("Does ",Patient," have a swollen_glands (y/n) ?"),
    response(Reply),
    Reply='y'.

hypothesis(Patient,measles) :-
    symptom(Patient,fever),
    symptom(Patient,cough),
    symptom(Patient,conjunctivitis),
    symptom(Patient,runny_nose),
    symptom(Patient,rash).

hypothesis(Patient,german_measles) :-
    symptom(Patient,fever),
    symptom(Patient,headache),
    symptom(Patient,runny_nose),
    symptom(Patient,rash).

hypothesis(Patient,flu) :-
    symptom(Patient,fever),
    symptom(Patient,headache),
    symptom(Patient,body_ache),
    symptom(Patient,conjunctivitis),
    symptom(Patient,chills),
    symptom(Patient,sore_throat),
    symptom(Patient,runny_nose),
    symptom(Patient,cough).    

hypothesis(Patient,common_cold) :-
    symptom(Patient,headache),
    symptom(Patient,sneezing),
    symptom(Patient,sore_throat),
    symptom(Patient,runny_nose),
    symptom(Patient,chills).

hypothesis(Patient,mumps) :-
    symptom(Patient,fever),
    symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :-
    symptom(Patient,fever),
    symptom(Patient,chills),
    symptom(Patient,body_ache),
    symptom(Patient,rash).

hypothesis(Patient,measles) :-
    symptom(Patient,cough),
    symptom(Patient,sneezing),
    symptom(Patient,runny_nose).

response(Reply) :-
    readchar(Reply),
    write(Reply),nl.
EN

回答 1

Stack Overflow用户

发布于 2018-12-07 22:49:25

您的代码似乎是TurboProlog或Visual Prolog代码。首先删除从domains开始直到clauses的代码。您还需要将对readchar/1readln/1谓词的调用替换为对标准Prolog谓词(如read/1read_term/3 )的调用。在readchar/1的特殊情况下,并且只能在GNU Prolog下运行,您可以将其定义为:

代码语言:javascript
复制
    readchar(Char) :-
        get_key(Code), char_code(Char, Code), nl.

其他一些Prolog系统提供了readchar功能,但没有标准。与标准get_char/1谓词相比,这些谓词的主要区别在于在顶级使用时不需要return/enter。

此外,用一系列对标准write/1谓词的调用替换所有对write的调用,并将这些调用中的双引号替换为单引号。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53671146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档