使用swi-prolog从用户输入创建列表 [英] Creating a list from user input with swi-prolog

查看:60
本文介绍了使用swi-prolog从用户输入创建列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用Prolog.我正处于编写程序的开始阶段,该程序将接受用户(症状)的输入并使用该信息来诊断疾病.我最初的想法是创建列表,列表中的疾病名称位于列表的开头,症状位于列表的尾部.然后提示用户输入症状,并使用用户输入创建列表.然后比较列表以查看尾巴是否匹配.如果尾部匹配,则我创建的列表的开头将是诊断.首先,我将程序缩小为只有三种症状的三种疾病.在开始比较之前,我需要使用从用户读取的值来构建列表的尾部,但是我似乎无法正确理解语法.

This is my first experience with Prolog. I am at the beginning stages of writing a program that will take input from the user (symptoms) and use that information to diagnose a disease. My initial thought was to create lists with the disease name at the head of the list and the symptoms in the tail. Then prompt the user for their symptoms and create a list with the user input. Then compare the list to see if the tails match. If the tails match then the head of the list I created would be the diagnosis. To start I scaled the program down to just three diseases which only have a few symptoms. Before I start comparing I need to build the tail of list with values read from the user but I can't seem to get the syntax correct.

这是我到目前为止所拥有的:

This is what I have so far:

disease([flu,fever,chills,nausea]).
disease([cold,cough,runny-nose,sore-throat]).
disease([hungover,head-ache,nausea,fatigue]).

getSymptoms :-
    write('enter symptoms'),nl,
    read(Symptom),
    New_Symptom = [Symptom],
    append ([],[New_symptom],[[]|New_symptom]),
    write('are their more symptoms? y or n '),
    read('Answer'),
    Answer =:= y
    -> getSymptoms
    ; write([[]|New_symptom]).

该错误发生在附加行上.语法错误:期望运算符. 对此错误或程序设计的任何帮助将不胜感激.

The error occurs on the append line. Syntax Error: Operator Expected. Any help with this error or the design of the program in general would be greatly appreciated.

推荐答案

这是读取以下症状列表的一种方法:

This is one way to read a list of symptoms in:

getSymptoms([Symptom|List]):-
    writeln('Enter Symptom:'),
    read(Symptom),
    dif(Symptom,stop),
    getSymptoms(List).

getSymptoms([]).

您键入停止.当您想完成列表时.

You type stop. when you want to finish the list.

然后,您需要确定要与代表疾病的方式相匹配的逻辑.

You would then need to decide what logic you want to match the way you have represented a disease.

完整示例:

:-dynamic symptom/1.

diagnose(Disease):-
    retractall(symptom(_)),
    getSymptoms(List),
    forall(member(X,List),assertz(symptom(X))),
    disease(Disease).



getSymptoms([Symptom|List]):-
    writeln('Enter Symptom:'),
    read(Symptom),
    dif(Symptom,stop),
    getSymptoms(List).

getSymptoms([]).



disease(flue):-
    symptom(fever),
    symptom(chills),
    symptom(nausea).

disease(cold):-
   symptom(cough),
   symptom(runny_nose),
   symptom(sore_throat).

disease(hungover):-
   symptom(head_ache),
   symptom(nausea),
   symptom(fatigue).

这篇关于使用swi-prolog从用户输入创建列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆