Reputation: 11
I need to make list of the lists witch will be some kind of matrix, in size N x N for same N, and I don't know how to declare that all the elements of list must be lists with N elements?
Upvotes: 1
Views: 1275
Reputation: 40768
Consider:
n_matrix(N, Rows) :-
length(Rows, N),
maplist(length_list(N), Rows).
length_list(L, Ls) :- length(Ls, L).
Example:
?- n_matrix(2, Rows).
Rows = [[_G294, _G297], [_G300, _G303]].
Alternatively, you can use findall/3
:
n_matrix(N, Rows) :-
findall(Row, (between(1,N,_),length(Row, N)), Rows).
Upvotes: 3
Reputation: 22803
Well, the easiest way to make a list of length N is with length/2
:
?- length(L, 5).
L = [_G1233, _G1236, _G1239, _G1242, _G1245].
You could easily apply that a number of times to get the result you want:
make_matrix(N, M) :- once(make_matrix(0, N, M)).
make_matrix(N, N, []).
make_matrix(I, N, [Row|Rows]) :-
length(Row, N),
I1 is I + 1,
make_matrix(I1, N, Rows).
Used:
?- make_matrix(5, M).
M = [[_G479, _G482, _G485, _G488, _G491],
[_G497, _G500, _G503, _G506, _G509],
[_G515, _G518, _G521, _G524, _G527],
[_G533, _G536, _G539, _G542, _G545],
[_G551, _G554, _G557, _G560|...]].
There are probably more elegant ways to do this.
Edit: here's a somewhat more general way to do this without cuts and with clpfd
:
:- use_module(library(clpfd)).
matrix(1,M,[L]) :- length(L, M).
matrix(N,M,[Row1|L]) :-
N #> 1,
N0 #= N-1,
length(L, M),
matrix(N0,M,L).
Upvotes: 1