问题
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