Prolog递归停止

时间:2012-05-16 12:09:44

标签: recursion prolog

在底部,我提供了整个程序(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检查所有单词,如果它们相等但什么时候离开关键字应该回到函数调用它,但它只是'不'。也许我应该检查列表是否为空,然后我再次调用与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/

2 个答案:

答案 0 :(得分:0)

使用三个空闲参数调用count_pnt,这意味着count_pnt将首先用空列表统一其所有参数。在回溯时,会调用递归count_pnt子句,这会导致count_pnt_keys再次出现三个自由参数,这将导致Ah[]等统一而不是失败。< / p>

您是否真的按照count_pnt的代码建议致电test

答案 1 :(得分:0)

count_pnts(_,_,[],_).
count_pnt_word(_,[],_).
count_pnt_keys([],_,_).

应该看起来像是一个问题