我正在处理的一个简单 Prolog 程序的这一部分应该找到一个距离给定点最多有一段距离的坐标列表。我的代码有效,但也会为给定列表中的每个元素产生错误。
代码:
nearby_places(Places, Position, Signal, NearbyPlaces) :-
findall(
X,
(
Places,
member(X, Places),
is_reachable(Position, X, Signal)
),
NearbyPlaces
).
is_reachable(Position, Target, Signal) :-
manhattan_distance(Position, Target, Distance),
Distance =< Signal * 3.
manhattan_distance(pos(X1, Y1), pos(X2, Y2), Distance) :-
Dx is X1 - X2,
Dy is Y1 - Y2,
ADx is abs(Dx),
ADy is abs(Dy),
Distance is ADx + ADy.
试运行:
?- nearby_places([pos(0,0), pos(1,1), pos(2, 50), pos(4, 9)], pos(0,0), 10, N).
ERROR: Type error: `file_path' expected, found `pos(0,0)'
ERROR: Type error: `file_path' expected, found `pos(1,1)'
ERROR: Type error: `file_path' expected, found `pos(2,50)'
ERROR: Type error: `file_path' expected, found `pos(4,9)'
N = [pos(0, 0), pos(1, 1), pos(4, 9)].
我所做的所有测试都产生了正确的结果,但也产生了这些错误。我在任何地方都没有发现任何有帮助的东西。我在 Linux 上使用 SWI-Prolog 5.10.4。
感谢您的时间!