I wrote a predicate fib/2 to calculate the Fibonacci numbers in Prolog. Though it works, it always says "out of local stack" and the error looks like:
?- fib(10, F).
F = 55 ;
ERROR: Out of local stack
my predicate is below:
fib(0, 0).
fib(1, 1).
fib(N, NF) :-
A is N - 1,
B is N - 2,
fib(A, AF),
fib(B, BF),
NF is AF + BF.
Anyone knows why this is and how to fix it to get the following stuff::
% or the search might stop immediately, without pressing space.
?- fib2(10, F).
F = 55 ;
false.
Thanks in advance!