如何从 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.
这是我到目前为止所拥有的,但我想有一种更直接的方法来做到这一点。任何帮助表示赞赏!谢谢!
I'm just picking up Prolog now, so I'm unfamiliar with the normal way of doing most things.
Essentially I have a rule which gives a value from an input:
ScoreFromInput(Input, Score) :- ...
And I have a list of inputs, which are just numbers. I'm having trouble figuring out how to find the input which yields the maximum score.
This is what I have right now, but I think it recurses infinitely:
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.
This is what I have so far, but I imagine there is a much more straightforward way of doing it. Any help is appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的说法是,如果没有更好的输入,那么输入就是最好的:
要查看您自己的代码有什么问题,请尝试使用 SWI-Prolog 的图形跟踪器:
and please_use_read_names inSteadOfUnreadOnes。
A simple way to state it is that an input is best if there is no better input:
To see what is wrong with your own code, try for example SWI-Prolog's graphical tracer:
And please_use_readable_names inSteadOfUnreadableOnes.
尽管 Chris 对 Prolog 不太熟悉,但他概述的方法可能比 mat 的方法更有效地找到具有最大分数的输入。像克里斯这样的方法可以线性扫描可能的输入,而不是进行二次比较。
这里 maxScoreOfList/3 将返回有效输入列表的最佳项目 Z 和最佳分数 B 作为第三个参数。谓词将在空列表上失败。
需要一个“辅助”函数,如下所示,它说明了添加一些额外参数的“技巧”,以便当到达输入列表的末尾时,输出 Z 和 B 可以绑定到找到的最佳项目和分数“所以远的”:
Despite Chris's lack of familiarity with Prolog, the approach he outlined may be a more efficient way of finding the input with the maximum score than mat's. Instead of doing a quadratic number of comparisons, an approach like Chris's is possible that linearly scans the possible inputs.
Here maxScoreOfList/3 will return the best item Z and the best score B for a list of valid inputs as the third argument. The predicate will fail on an empty list.
A "helper" function is needed as follows, which illustrates the "trick" of adding some extra arguments so that when the end of the input list is reached, the outputs Z and B can be bound to the best item and score found "so far":