create a matrix and permutate each line

515 views Asked by At

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

There are 1 answers

0
CapelliC On

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]] ;
...