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:
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With