Reputation: 430
I am working on a program in prolog, and am stuck with the following issue: I have defined the predicate neighbors(+,+,+,+,?) like this:
neighbors(X, Y, Height, Width, Neighbors):-
Xup is X-1,
Xdown is X+1,
Yleft is Y-1,
Yright is Y+1,
findall((A,B,C),(
between(Xup, Xdown, A),
between(Yleft, Yright, B),
A>=1,
B>=1),
Neighbors).
Now the query neighbors(5,5,5,5,X) works as expected, unifying X with a list of it's neighbors, namely
X = [ (4, 4, _G2809), (4, 5, _G2800), (4, 6, _G2791), (5, 4, _G2782), (5, 5, _G2773), (5, 6, _G2764), (6, 4, _G2755), (6, ..., ...), (..., ...)] .
However a problem arises when I try to add the following lines to my findall Goal:
A<=Height,
B<=Width
the full predicate looks like this:
neighbors(X, Y, Height, Width, Neighbors):-
Xup is X-1,
Xdown is X+1,
Yleft is Y-1,
Yright is Y+1,
findall((A,B,C),(
between(Xup, Xdown, A),
between(Yleft, Yright, B),
A>=1,
B>=1,
A<=Height,
B<=Width
),
Neighbors).
Now the same query, neighbors(5,5,5,5,X). results in me getting the following error:
ERROR: Undefined procedure: neighbors/5
ERROR: However, there are definitions for:
ERROR: neighbor/2
ERROR: neighbors/2
false.
What could be the reason? I imagine it's got to do with the way I'm comparing those variables, but since Width and Height are instantiated I thought that should work. Thanks.
Upvotes: 1
Views: 677
Reputation: 3120
The problem is in your comparison operators. The syntax for the less than or equal to operator is =</2
. So your goals should be:
...
A=<Height,
B=<Width
...
Upvotes: 1