users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Parise P. - writing a predicate


From: Paolo Parise
Subject: Parise P. - writing a predicate
Date: Sat, 24 Sep 2011 21:32:00 +0200

Hi,
I'm trying implementing a predicate adjacent(W, Z) such that cell W ([X1,Y1]) is adjacent Z([X2,Y2]) iff:
X1 = X2 + 1 or X1 = X2 -1 or Y1 = Y2 + 1 or Y1 = Y2 -1 .
This predicate is used into an algorithm that make these two questions:
- given cell W, what are the cell Z adjacent to W?
- given cell Z, what are the cells W adjacent to Z?
I proposed the following solution:

adjacent(L1,L2) :- location_toward(L1,_,L2).
location_toward([X,Y],[New_X,Y]) :- New_X is X+1.
location_toward([X,Y],[X,New_Y]) :- New_Y is Y+1.
location_toward([X,Y],[New_X,Y]) :- New_X is X-1.
location_toward([X,Y],[X,New_Y]) :- New_Y is Y-1.

Calling the predicate in the form:
adjacent([1,1],Z) it lists all the adjacents cells (correct!)
adjacent(W,[1,2]) it gives these exception:

{trace}
| ?- adjacent(X,[1,1]).
      1    1  Call: adjacent(_16,[1,1]) ?
      2    2  Call: location_toward(_16,_51,[1,1]) ?
      3    3  Call: 1 is _77+1 ?
      3    3  Exception: 1 is _77+1 ?
      2    2  Exception: location_toward(_16,_51,[1,1]) ?
      1    1  Exception: adjacent(_16,[1,1]) ?
uncaught exception: error(instantiation_error,(is)/2)

Any suggestions?

Thanks!

Paolo P.

 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]