私は最近Prologを学習していて、それをどのように機能させるのに問題がありますか。一例として、プロローグの精神科医を作成し、それを質問に変える作業を設定しました。私は「私がうまくいないと思う」と思うのはなぜあなたが気分が悪いと思いますか?Prolog write無限ループ
しかし、これまでのところ、私のコードは終了時に無限ループを生成しています。それはprintSentenceに対してredoを呼び出し、出力リストの最後にそれを固定し、これを永久に行う、別のバインドされていない変数を呼び出します。ここで
は私のコードです:
/* printSentence simply calls the in-built Prolog write function. */
printSentence([]) :- write('?').
printSentence([H|T]) :- write(H),write(' '), printSentence(T).
answer([], _) :- write('Why are you silent? Talk to me.').
answer(Input, Output) :- thinkMatch(Input, Output), printSentence(Output).
thinkMatch(['I', 'think'|Rest], ['Why', 'do', 'you', 'think'|SwitchedRest]) :- switchPronouns(Rest, SwitchedRest).
switchPronouns([], _).
switchPronouns([H|T], [R|SwitchedRest]) :- switchWord(H, R),switchPronouns(T, SwitchedRest).
switchWord('I', 'you').
switchWord('myself', 'yourself').
switchWord('am', 'are').
switchWord('you', 'me').
switchWord('yourself', 'myself').
switchWord(H, H).
入力、
answer(['I', 'think', 'therefore', 'I', 'am'],Output).
はそれが小さく、ダム何かあれば、私はきた、永遠にこれらの結果は起こって
?- Input = ['I', 'think', 'therefore', 'I', 'am'],answer(Input, Output).
Why do you think therefore you are ?
Input = ['I', think, therefore, 'I', am],
Output = ['Why', do, you, think, therefore, you, are] ;
_G4395 ?
Input = ['I', think, therefore, 'I', am],
Output = ['Why', do, you, think, therefore, you, are, _G4395] ;
_G4398 ?
Input = ['I', think, therefore, 'I', am],
Output = ['Why', do, you, think, therefore, you, are, _G4395, _G4398] ;
_G4401 ?
Input = ['I', think, therefore, 'I', am],
Output = ['Why', do, you, think, therefore, you, are, _G4395, _G4398|...]
謝罪を生成しますまだPrologの内部の仕掛けを完全に把握する。
ありがとうございます。
おかげでたくさんの非常に徹底した答えです!この再現は、Prologがどのように動作するかを理解するのに役立ちました。 – SHolmes