So, I have a prolog predicate that for example is something like this:
getSomething([Head|Tail],List,MaxV,MaxK,BestThing) :-
%I call other functions that work just fine
makeaverage(List,Med),
compare(Med,MaxV,Comp),
(Comp < MaxK,
getSomething(Tail,List,MaxV,Comp,Head);
getSomething(Tail,List,MaxV,MaxS,BestThing)),
write(BestThing).
And the problem is that Best thing is actually _G267 and write returns :
Thing1 Thing2 Thing3 _G267
and I really need Thing1 but the when the predicate returns recursively screws everything up... so... some help would be great :).
I fixed it :
getSomething([Head],List,MaxV,MaxK,BestThing) :-
makeaverage(List,Med),
compare(Med,MaxV,Comp),
MaxK = Comp,
BestTHing = Head .
getSomething([Head|Tail],List,MaxV,MaxK,BestThing) :-
getSomething(Tail,List,MaxV,Comp1,Head1),
makeaverage(List,Med),
compare(Med,MaxV,Comp),
(Comp < MaxK
->
MaxK = Comp,
BestThing = Head;
MaxK = Comp,
BestThing = Head1).
Or something like that the idea is that you go recursively with not initalized variables and you initialize them at the end of the last recursive call in the one element list part...
BestTHingshould beBestThing- Alexander Serebrenik