質問

This part of a simple Prolog program I'm working on is supposed to find a list of coordinates that are at most some distance away from a given point. My code works but also produces an error for each element in the given list.

Code:

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.

Test run:

?- 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)].

All the tests I've made produce the correct result but also these errors. I haven't found anything even remotely helpful anywhere. I am using SWI-Prolog 5.10.4 on Linux.

Thank you for your time!

役に立ちましたか?

解決

Remove the Places, line from the second argument to findall/3. It is not necessary there and is interpreted as a call to consult probably:

findall(
        X,
        (
            member(X, Places),
            is_reachable(Position, X, Signal)
        ),
        NearbyPlaces
    ).
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top