Considering that a matrix is a list of rows, how to check if my matrix is truly a matrix.
I need to write a predicate size(M, Row, Col)
.
If is not a matrix, it should fail.
If it is a matrix, it should count the number of rows and columns.
Sample queries with expected answer(s):
?- size([[1,2],[3,4],[5,6]], Rows, Cols).
Rows = 3, Cols = 2.
?- size([[1,2],[3,4,5]], Rows, Cols).
no
I have written the next code, where I simply get the number of Rows and the number of columns, but I do not know how to compare each row. If the length of the row are not the same, then it should return "no" and not print the number of Rows and Columns.
Code:
size([], 0, 0).
size(M, R, C) :-
length(L, R).
size([H|T], R, N) :-
length(H, N),
!.