以下是我的第 N 个斐波那契数查找谓词,可以:
f(0,0).
f(1,1).
f(N,R):-P is N-1,Q is N-2,f(P,T1),f(Q,T2),R is T1+T2.
我正在尝试使用以下谓词生成斐波那契数:
fgen(0,0).
fgen(1,1).
fgen(A,B):-fgen(X,Y),A is X+1,f(A,T),B is T.
当我查询时fgen(X,Y).
表明:
?- fgen(X,Y).
X = 0
Y = 0 ;
X = 1
Y = 1 ;
X = 1
Y = 1 ;
ERROR: Out of local stack
我使用了 trace 命令,结果如下:
?- trace,fgen(X,Y).
Call: (9) fgen(_G281, _G282) ? creep
Exit: (9) fgen(0, 0) ? creep
X = 0
Y = 0 ;
Redo: (9) fgen(_G281, _G282) ? creep
Exit: (9) fgen(1, 1) ? creep
X = 1
Y = 1 ;
Redo: (9) fgen(_G281, _G282) ? creep
Call: (10) fgen(_L178, _L189) ? creep
Exit: (10) fgen(0, 0) ? creep
^ Call: (10) _G281 is 0+1 ? creep
^ Exit: (10) 1 is 0+1 ? creep
Call: (10) f(1, _L179) ? creep
Exit: (10) f(1, 1) ? creep
^ Call: (10) _G282 is 1 ? creep
^ Exit: (10) 1 is 1 ? creep
Exit: (9) fgen(1, 1) ? creep
X = 1
Y = 1 ;
Redo: (10) f(1, _L179) ? creep
^ Call: (11) _L207 is 1-1 ? creep
^ Exit: (11) 0 is 1-1 ? creep
^ Call: (11) _L208 is 1-2 ? creep
^ Exit: (11) -1 is 1-2 ? creep
Call: (11) f(0, _L209) ? creep
Exit: (11) f(0, 0) ? abort
% Execution Aborted
我试图找到错误,但失败了。如何解决问题?