如何限制prolog中的递归深度

时间:2013-11-17 20:15:01

标签: prolog

我在这里有一个递归:

located_on(A,B,[move(A,B)|_]).
located_on(B,L,[_|S]) :- located_on(B,L,S).

当我问located_on(a,b,X).时,我得到如下,这是无限的。

X = [] ;
X = [move(a, b)|_G4037] ;
X = [_G4036] ;
X = [_G4036, move(a, b)|_G4040] ;
X = [_G4036, _G4039] ;
X = [_G4036, _G4039, move(a, b)|_G4043] ;
X = [_G4036, _G4039, _G4042] ;
X = [_G4036, _G4039, _G4042, move(a, b)|_G4046] ;
X = [_G4036, _G4039, _G4042, _G4045] ;
X = [_G4036, _G4039, _G4042, _G4045, move(a, b)|_G4049] 

如何限制递归深度并获得有限数量的结果?

我试图使用:

located_on(A,B,[move(A,B)|_]).
located_on(B,L,[_|S]) :- located_on(B,L,S),length(S,N),N<5.

但是我从本地堆栈中

1 个答案:

答案 0 :(得分:2)

length(X,N),
located_on(a,b,X).

称为iterative deepening

得知 luv Prolog!