Matrix transposition: Difference between revisions

Line 1,103:
endfor;
enddefine;</lang>
=={{header|Prolog}}==
In Prolog, a matrix is a list of lists. Works with SWI-Prolog.
 
<lang Prolog>% transposition of a rectangular matrix
% e.g. [[1,2,3,4], [5,6,7,8]]
% give [[1,5],[2,6],[3,7],[4,8]]
 
transpose(In, Out) :-
In = [H | _],
length(H, Len),
length(L, Len),
maplist(initdl, L),
work(L, In, Out).
 
% we use the difference list to make "quick" appends (one inference)
initdl(X-X).
 
work(Lst, [], Out) :-
maplist(dl2l, Lst, Out).
 
work(Lst, [H | T], Out) :-
maplist(my_append, Lst, H, Lst1),
work(Lst1, T, Out).
 
my_append(X-Y, C, X1-Y1) :-
append_dl(X-Y, [C | U]- U, X1-Y1).
 
% "quick" append
append_dl(X-Y, Y-Z, X-Z).
 
% transforms difference list in list
dl2l(X-[], X).
</lang>
 
 
=={{header|PureBasic}}==
Matrices represented by integer arrays using rows as the first dimension and columns as the second dimension.
Anonymous user