I have a matrix of 6x6 numbers and I'm writing a prolog code that gives me the numbers in a certain row, column or square. For instance:
0n 1n 2n 3n 4n 5n
0n [[1,2,3,4,5,6]
1n [2,3,4,5,6,1]
2n [3,4,5,6,1,2]
3n [4,5,6,1,2,3]
4n [5,6,1,2,3,4]
5n [6,1,2,3,4,5]]
I already have code for rows and columns, which is something like:
row(1,[A|_],A).
row(Y,[_|B],X) :-
Y-1 is Y1,
row(Y1,B,X).
But now I'm stuck on how to generate a 3x3 square. I want to work with coordinates, so the first argument should be something like (1,3) and it will give the square of row 1n and column 3n, then the matrix as the second and the numbers in the square as third argument. Does anyone have any tips? I was thinking I might have to work with a head tail pattern again; getting the first three numbers of the given row/column, then doing this three times, but I don't know how or if this is possible and effective.
Any comments are greatly appreciated!
nth1/3
predicate. – Willem Van Onsem