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

查看:9
本文介绍了使用 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.

这是我目前所拥有的:

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天全站免登陆