I'm just starting to learn prolog. I have a question where the object is a slot in a 3x4 matrix
a b c d
e f g h
i j k l
Each element is a slot
Slot(Name,leftNeighbor,UpperNeighbor, RightNeighbor,LowerNeighbor).
I need to create a query called diagonal(Name1,Name2)
that returns true if two slots are diagonal( example: a,f,k
are diagonal, d,j,g
are too).
I was thinking about checking for each element if his left/right neighbor's lower neighbor is the Name2, or if that element's own left/right neighbor's Lower neighbor is Name2. Is there a simpler way to find this? it looks too long and inefficient to me.
These are my current rules ~
slot(name,LeftNeighbor,UpperNeighbor,RightNeighbor,LowerNeighbor).
slot(a,none,none,b,e).
slot(b,a,none,c,f).
slot(c,b,none,d,g).
slot(d,c,none,none,h).
slot(e,none,a,f,i).
slot(f,e,b,g,j).
slot(g,f,c,h,k).
slot(h,g,d,none,l).
slot(i,none,e,j,none).
slot(j,i,f,k,none).
slot(k,j,g,l,none).
slot(l,k,h,none,none).
Thanks.
Worked it out a few days ago.
Here is the solution for all who may need it:
diag_1(Name1,Name2) :- slot(Name1,_,_,A,_), A \= none, slot(Name2,_,A,_,_).
diag_1(Name1,Name2) :- slot(Name1,_,_,A,_), A \= none, slot(Name2,_,B,_,_), B \= none, diag_1(A,B).
diag_2(Name1,Name2) :- slot(Name1,_,_,_,A), A \= none, slot(Name2,_,_,A,_). %same on the other diagonal
diag_2(Name1,Name2) :- slot(Name1,_,_,_,A), A \= none, slot(Name2,_,_,B,_), B \= none, diag_2(A,B).
diagonal(Name1,Name2) :- diag_1(Name1,Name2) ; diag_1(Name2,Name1) ; diag_2(Name1,Name2) ; diag_2(Name2,Name1).