0
votes

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

1
You should show all clauses for your predicate. It's impossible to tell what's wrong. - Fred Foo
When you say function you mean predicate - keyser
If you have fixed it, what is the actual question? - Alexander Serebrenik
At the end of the recursion getSomething([Head],List,MaxV,MaxK,BestThing) ... BestTHing = Head inititalize nothing neither Head, neither BestThing - joel76
BestTHing should be BestThing - Alexander Serebrenik

1 Answers

0
votes

Just copying my previous comment as an answer:

BestTHing should be BestThing in the first clause for getSomething,

which will unify the fouth argument of getSomething with the single element of its first argument.