我现在只是拿起Prolog,所以我不熟悉做大多数事情的正常方式。如何从SWI-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.
这是我到目前为止,但我想有这样做的一个更直接的方式。任何帮助表示赞赏!谢谢!
有关Prolog命名的一件事是以小写字母(大写或下划线开始变量名称)开始谓词。所以我在我的答案中将ScoreFromInput更改为scoreFromInput。 – hardmath 2011-04-05 15:00:04
@hardmath:啊,是的,谢谢你指出。在我的实际计划中,情况就是这样。我只是在这里将事物转换为“输入”和“得分”,以便他们的目的对不熟悉我的计划的人很清楚,我想我错过了这一点。 – 2011-04-05 18:03:21