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!
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.