如何从列表中找到输出,从而产生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.
这是我到目前为止所做的,但我想有一种更简单的方法。任何帮助表示赞赏!谢谢!
没有找到相关结果
已邀请:
2 个回复
先对冈蒲
要查看您自己的代码有什么问题,请尝试使用SWI-Prolog的图形跟踪器:
并且Please_use_readable_names inSteadOfUnreadableOnes。
徘廷
需要一个“辅助”函数,如下所示,它说明了添加一些额外参数的“技巧”,以便在到达输入列表的末尾时,输出Z和B可以绑定到最佳项目并且得分“so so远”: