简体   繁体   中英

create a matrix and permutate each line

i can create a list from 0 to N and permutation it. But how can i make it into matrix and permutation each line just from matrix(2,L) .

add(X,L,[X|L]).
add(X,[L|H],[L|R]):- add(X,H,R).

permut([],[]).
permut([L|H],R):- permut(H,R1),add(L,R1,R).

permutations(L,R):- findall(P,permut(L,P),R).

do_list(N, L) :- do_list1(N, [], L).

do_list1(0, L, L) :- !.
do_list1(N, R, L) :- N > 0,
               N1 is N-1, 
               do_list1(N1, [N|R], L).

matrix(N,L):-
    do_list(N,R),
    permut(R,L).

not sure about your request. Here is a possible answer based on builtins...

3 ?- [user].
|: matrix(N, Mat) :- length(Rows, N), maplist(numlist(1,N), Rows), maplist(permutation, Rows, Mat).

% user://1 compiled 0.01 sec, 2 clauses
true.

4 ?- matrix(3, M).
M = [[1, 2, 3], [1, 2, 3], [1, 2, 3]] ;
M = [[1, 2, 3], [1, 2, 3], [1, 3, 2]] ;
M = [[1, 2, 3], [1, 2, 3], [2, 1, 3]] ;
M = [[1, 2, 3], [1, 2, 3], [2, 3, 1]] ;
...

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