Prolog simple predicate how?

I have a predicate:

neig(I, J, I1, J1):- I1 is I - 1, I1 >= 0, J1 is J. neig(I, J, I1, J1):- I1 is I + 1, not(I1 > 8), J1 is J. neig(I, J, I1, J1):- J1 is J - 1, J1 >= 0, I1 is I. neig(I, J, I1, J1):- J1 is J + 1, not(J1 > 8), I1 is I. neig(I, J, I1, J1):- I1 is I - 1, J1 is J - 1, I1 >= 0, J1 >= 0. neig(I, J, I1, J1):- I1 is I + 1, J1 is J + 1, not(I1 > 8), not(J1 > 8). neig(I, J, I1, J1):- I1 is I + 1, J1 is J - 1, J1 >= 0, not(I1 > 8). neig(I, J, I1, J1):- I1 is I - 1, J1 is J + 1, I1 >= 0, not(J1 > 8). 

How to write the predicate all_neighs(I, J, L) , where L is a list and contains all the different elements [I1, J1] , such that neigh(I, J, I1, J1) ?

+4
source share
1 answer

I think you need this built-in predicate.

 findall(Things,GoalCondition, Bag) 

Which would look something like this:

 all_neighs(I,J,L) :- findall([H|T],neig(I,J,H,T), L). 

you may need to check if T is an atom if you want to. But with this my result with some examples.

 1 ?- all_neighs(0,0,X). X = [[1|0], [0|1], [1|1]]. 2 ?- all_neighs(1,1,X). X = [[0|1], [2|1], [1|0], [1|2], [0|0], [2|2], [2|0], [0|...]]. 

You should also take a look at this: [1] it explains how you can easily implement the findall (...) predicate yourself.

[1] http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_12.html

+1
source

All Articles