Prolog-距两点的距离

时间:2019-05-19 14:23:41

标签: prolog position distance

我想计算两个城市之间的距离,我有cityState,列表中的

[NameCity,[X,Y],Neighbours],[],..]

我试图在Prolog中编写代码来计算两个城市之间的距离,但是当我尝试执行谓词“距离”时,获得

ERROR: Syntax error: Operator expected
ERROR: distance
ERROR: ** here **
ERROR:  (a,b,Distance) . 

我的实现:

cityState([[a,[2,3],[b,d]],[b,[5,1],[a,c,d]], [c, [3, 2], [b, d]], [d, [6, 3], [a, b, c]]]).

city(Name,Position,Neighbors):-
cityState(L),
member([Name,Position,Neighbors],L),
!.
position(Name,Position):-
city(Name,Position,_Neighbors).

distance(City1,City2,Distance):-
position(City1,[X1,Y1]),
position(City2,[X2,Y2]),
X_Dist is (X1-X2) ** 2,
Y_Dist is (Y1-Y2) ** 2,
Distance is sqrt(X_Dist + Y_Dist).

我希望Distance的输出是..我该怎么办?

0 个答案:

没有答案