0

在底部,我给出了整个程序(ciao)的链接,以使帮助更容易。我尝试在 Prolog 函数中创建问题列表,例如

 questions([[[What, are,you,doing,?],[Where,am,I,Incorrect,?]]]).
 answers([[Im,doing,exercise],[I,do,nothing]],[[You,are,incorrect,at,'..'],[i,dont,know]]]).
 wordkeys([[[Incorrect,50],[doing,20]]]).

我知道它看起来很乱,但我真的需要帮助,我将不胜感激。主要功能是检查哪个答案是最好的(具有最大的关键字点总和)。我的问题是一切看起来都很好(做了一些 write() 来看看发生了什么),直到它进入最后一个函数:

count_pnt_keys()

Prolog 检查所有单词是否相等,但何时从关键字中退出应该返回到调用它的函数,但它只是 'no' 。也许我应该在只用 Tail 再次调用相同的函数之前检查列表是否为空?这个怎么做?

规则:

count_pnt([],[],[]).
count_pnt([Ah|At],Keys,RList) :-      %choose one answer from answer list and go further
    count_pnt_word(Ah,Keys,Pnts),     %return sum of points for one answer
    count_ADD_POINT(RList,Pnts),      %not important here
    count_pnt(At,Keys,RList).         %call again with next question
/*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
count_pnt_word([],[],0)
count_pnt_word([Ah|At],Keys,Pnts) :-  %choose one WORD from answer and go further
    count_pnt_keys(Ah,Keys,Pnts),
    count_pnt_word(At,Keys,Pnts).     %call recursion with next word from answer
/*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
count_pnt_keys([],[],0)
count_pnt_keys(AWord,[Kh|Kt],Pnts) :- %check word with all keys for first question
    get_tail_h(Kh,KWORD,KPOINTS),     %just return head and tail from first key
    AWord==KWORD,                     %check if they are equal
    /*counting points*/ !,            %counting not important when end counting points go out to next
    count_pnt_keys(AWord,Kt,Pnts). %call again if not equal and try with next key

我称之为:

test :-
write(AnswerFirst),
count_pnt(FirstPackOfAnswer,FirstPackofKeys,ReturnedListwithpoints),
write(ReturnedListwithpoints).

代码链接 (ciao) http://wklej.org/id/754478/

4

2 回答 2

0

count_pnt使用三个自由参数调用,这意味着count_pnt它将首先将其所有参数与一个空列表统一起来。在回溯时,递归count_pnt子句被调用,这又导致count_pnt_keys三个自由参数,这将导致Ah[]etc 统一而不是失败。

你真的 count_pnt按照代码的建议打电话test吗?

于 2012-05-17T12:41:33.327 回答
0
count_pnts(_,_,[],_).
count_pnt_word(_,[],_).
count_pnt_keys([],_,_).

应该看起来像这样这是一个问题

于 2012-05-17T14:15:17.893 回答