首页 文章

Prolog递归调用

提问于
浏览
0

所以,我有一个prolog谓词,例如这样的事情:

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

问题是最好的事情实际上是_G267并且写回报:

Thing1 Thing2 Thing3 _G267

而且我真的需要Thing1但是当谓词返回时递归所有东西......所以...一些帮助会很棒:) .

我修好了它 :

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

或类似的东西,你的想法是你递归地没有初始化的变量,你在一个元素列表部分的最后一次递归调用结束时初始化它们...

1 回答

  • 0

    只需复制我以前的评论作为答案:

    BestTHing 应该是 BestThing 的第一个子句中的 BestThing

    这将使 getSomething 的fouth参数与其第一个参数的单个元素统一起来 .

相关问题