簡體   English   中英

使用swi-prolog從用戶輸入創建列表

[英]Creating a list from user input with swi-prolog

這是我第一次接觸Prolog。 我正處於編寫程序的開始階段,該程序將接受用戶的輸入(症狀)並使用該信息來診斷疾病。 我最初的想法是創建一個列表,列表中的疾病名稱位於列表的開頭,症狀位於列表的尾部。 然后提示用戶輸入症狀,並使用用戶輸入創建列表。 然后比較列表以查看尾巴是否匹配。 如果尾部匹配,則我創建的列表的開頭將是診斷。 首先,我將程序縮小為只有三種症狀的三種疾病。 在開始比較之前,我需要使用從用戶讀取的值構建列表的尾部,但似乎語法不正確。

這是我到目前為止的內容:

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]).

錯誤發生在追加行上。 語法錯誤:期望運算符。 對此錯誤或程序設計的任何幫助將不勝感激。

這是讀取以下症狀列表的一種方法:

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

getSymptoms([]).

您鍵入停止。 當您想完成列表時。

然后,您需要確定要與代表疾病的方式相匹配的邏輯。

一個完整的例子:

:-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).

創建(L1): - 讀取(ELEM),創建(ELEM,L1)。

創建(-1,[]): - ! 創建(ELEM,[ELEM | T]): - 讀取(Nextel的),創建(Nextel的,T)。

go:-write('創建列表'),nl,write('輸入-1以停止'),nl,create(L),write('List is:'),write(L)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM