I'm trying to write a code in Prolog to count the distance between two points but when I tried to execute it , it told me that out of local stack any body know what that means and how i can solve it this my code by the way:
我正在尝试在Prolog中编写一个代码来计算两点之间的距离,但是当我尝试执行它时,它告诉我,从本地堆栈中任何人都知道这意味着什么以及我如何解决它这个我的代码由办法:
point(a,5,2).
point(b,4,0).
point(c,2,3).
point(d,5,2).
distance(N1,N2,D) :-
distance(point(N1,X1,Y2),point(N2,X2,Y2),Z),
Z=sqrt(((X1-X2)*(X1-X2))+((Y1-Y2)*(Y1-Y2))).
line(N1,N2,D) :-
distance(N1,N2,Z).
tangent(X,Y,M) :-
tangent(point(N1,X1,Y2),point(N2,X2,Y2),M),
M=(Y1-Y2)/(X1-X2).
2 个解决方案
#1
You are defining distance
in terms of distance
. Once there's a query for distance
, the computer will call distance
, which will again call distance
, and so forth. This is known as an infinite recursion.
你是用距离来定义距离。一旦有距离查询,计算机将调用距离,这将再次调用距离,依此类推。这被称为无限递归。
See also this SO question.
另见这个问题。
You should change your code so that the right hand side of
您应该更改您的代码,以便右侧
distance(N1,N2,D):-distance(point(N1,X1,Y2),point(N2,X2,Y2),Z)
does not always refer to the left hand side.
并不总是指左侧。
#2
I tried:
distance(N1,N2,D) :- point(N1,X1,Y1),
point(N2,X2,Y2),
D is sqrt((X2-X1)^2 + (Y2-Y1)^2).
ex.
?- distance(a,b,D).
D = 2.23606797749979.
#1
You are defining distance
in terms of distance
. Once there's a query for distance
, the computer will call distance
, which will again call distance
, and so forth. This is known as an infinite recursion.
你是用距离来定义距离。一旦有距离查询,计算机将调用距离,这将再次调用距离,依此类推。这被称为无限递归。
See also this SO question.
另见这个问题。
You should change your code so that the right hand side of
您应该更改您的代码,以便右侧
distance(N1,N2,D):-distance(point(N1,X1,Y2),point(N2,X2,Y2),Z)
does not always refer to the left hand side.
并不总是指左侧。
#2
I tried:
distance(N1,N2,D) :- point(N1,X1,Y1),
point(N2,X2,Y2),
D is sqrt((X2-X1)^2 + (Y2-Y1)^2).
ex.
?- distance(a,b,D).
D = 2.23606797749979.