简体   繁体   中英

Prolog: list of lists (size N x N)

我需要列出列表,这些列表将是某种矩阵,大小为N x N,对于相同的N,我不知道如何声明列表的所有元素必须是包含N个元素的列表?

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).

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM