如何从列表中找到输出,从而产生SWI-Prolog中某些查询的最大结果?

我现在只是拿起Prolog,所以我不熟悉做大多数事情的正常方式。 基本上我有一个规则,它从输入中给出一个值:
ScoreFromInput(Input, Score) :- ...
我有一个输入列表,它们只是数字。我无法弄清楚如何找到产生最高分数的输入。 这就是我现在所拥有的,但我认为它是无限的:
bestInput(BestInput) :-
    %#Bind the list of valid inputs
    legalInputs(ValidInputs),
    %# -1000 is a dummy score, it should get replaced on the first call to bestInputHelper
    bestInputHelper(ValidInputs,-1000,BestInput).

%#I think this rule should work if the first input in the list is not the best one
bestInputHelper([Input|RestOfInputs],BestScore,BestInput):-
    bestInputHelper(RestOfInputs,RestBestScore,BestInput),
    ScoreFromInput(Input,BestScore),
    RestBestScore > BestScore.

%#And this one if it is the best input
bestInputHelper([Input|RestOfInputs],BestScore,Input):-
    bestInputHelper(RestOfInputs,RestBestScore,_RestBestInput),
    ScoreFromInput(Input,BestScore),
    RestBestScore =< BestScore.
这是我到目前为止所做的,但我想有一种更简单的方法。任何帮助表示赞赏!谢谢!     
已邀请:
说明它的一个简单方法是,如果没有更好的输入,输入是最好的:
best_input(Best) :-
    legal_inputs(Inputs),
    member(Best, Inputs),
    input_score(Best, Score),
    + ( member(Better, Inputs), input_score(Better, S), S > Score).
要查看您自己的代码有什么问题,请尝试使用SWI-Prolog的图形跟踪器:
?- gtrace, best_input(Best).
并且Please_use_readable_names inSteadOfUnreadableOnes。     
尽管克里斯对Prolog缺乏了解,但他所概述的方法可能是一种更有效的方法,可以找到最高得分而不是垫子的输入。像Chris这样的方法可以线性扫描可能的输入,而不是进行二次数的比较。 这里maxScoreOfList / 3将返回最佳项目Z,并将有效输入列表的最佳得分B作为第三个参数。谓词将在空列表中失败。
maxScoreOfList(Z,B,[H|T]) :-
    scoreFromInput(H,S),
    maxScoreOfListAux(Z,B,H,S,T).
需要一个“辅助”函数,如下所示,它说明了添加一些额外参数的“技巧”,以便在到达输入列表的末尾时,输出Z和B可以绑定到最佳项目并且得分“so so远”:
maxScoreOfListAux(Z,B,Z,B,[ ]).
maxScoreOfListAux(Z,B,X,S,[H|T]) :-
    scoreFromInput(H,Q),
    (   S >= Q
     -> ( Y = X, R = S )
     ;  ( Y = H, R = Q )
    ),
    maxScoreOfListAux(Z,B,Y,R,T).
    

要回复问题请先登录注册