Natural number in SWI-prolog & recursive procedure

99封情书 提交于 2019-12-19 04:33:34

问题


I have the next procedure for natural number is SWI-prolog:

natural_number(0).
natural_number(s(X)) :- natural_number(X).

Now I want to do a recursive call, that stop when we arrive to 0.

My natural number is represented by - s(0)=0, s(s(0))=1, s(s(s(0)))=2, etc

So I define:

recommend(A, B, natural_number(0)) :-
    dosomeFINITEfunction (a,b).
recommend(a,b,mynumber):-
    dosomeFINITEfunction(a,b),
    recommend (a,b, natural_number(mynumber)).

and call with: 3,5,s(0). But it gives me the error: out of local stack.

What is the problem? Thank you.


回答1:


natural_number(s(0), 0).
natural_number(s(s(X)), N) :- natural_number(s(X), S), N is S + 1.


来源:https://stackoverflow.com/questions/6237162/natural-number-in-swi-prolog-recursive-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!