I have the following 4 lists:
A= [1,2,3],
B=[4,5,6],
C=[7,8,9],
D=[10,11,12]
I want to get another list of lists, whose first element gets the first element of each list, second element gets the second elements of each list etc. For example:
[1,2,3], [4,5,6], [7,8,9], [10,11,12]
becomes
[[1,4,7,10], [2,5,8,11],[3,6,9,12]].
I have tried using
findall([X,Y,Z,T],(member(X,A),member(Y,B),member(Z,C),member(T,D)),ModifiedList).
But it didn't work.
How can i do this in Prolog?
A solution would be:
% auxiliary predicate to group the first elements of
% all input lists and return the tails of the lists
group_first([], [], []).
group_first([[X| Xs]| Lists], [X| Tail], [Xs| Tails]) :-
group_first(Lists, Tail, Tails).
% main predicate; we separate the first list from the other
% lists to take advantage of first-argument indexing
group([], []).
group([List| Lists], Groups) :-
group(List, Lists, Groups).
group([], _, []).
group([X| Xs], Lists, [Group| Groups]) :-
group_first([[X| Xs]| Lists], Group, Tails),
group(Tails, Groups).
Sample call:
| ?- group([[1,2,3],[a,b,c],['A','B','C']], R).
R = [[1,a,'A'],[2,b,'B'],[3,c,'C']]
yes
To help understand the solution:
| ?- group_first([[1,2,3],[a,b,c],['A','B','C']], Group, Tails).
Group = [1,a,'A']
Tails = [[2,3],[b,c],['B','C']]
yes
what you describe is just transpose/2:
?- [library(clpfd)].
true.
?- transpose([[1,2],[3,4],[5,6]],T).
T = [[1, 3, 5], [2, 4, 6]].
Note that Paulo' answer is interesting: here is his group_first/3 in library(yall)
group_first(A,B,C) :- maplist([U,V,Z]>>(U=[X|Xs],V=X,Z=Xs),A,B,C).
or more efficiently
group_first(A,B,C) :- maplist([[X|Xs],X,Xs]>>true,A,B,C).
group/2 is fairly faster that clpfd:transpose:
?- N=100,length(M,N),maplist({N}/[R]>>length(R,N),M),time(group(M,T)).
% 20,402 inferences, 0.009 CPU in 0.009 seconds (100% CPU, 2165467 Lips)
?- N=100,length(M,N),maplist({N}/[R]>>length(R,N),M),time(transpose(M,T)).
% 30,708 inferences, 0.010 CPU in 0.010 seconds (100% CPU, 3192701 Lips)
and still better in original Paulo answer (group_first not inlined):
?- N=100,length(M,N),maplist({N}/[R]>>length(R,N),M),time(group(M,T)).
% 10,302 inferences, 0.004 CPU in 0.004 seconds (100% CPU, 2513070 Lips)
and is (almost) reversible:
?- group(T,[[1,2]]).
T = [[1], [2|_5420]].
Seems a good candidate for a pull request on library(clpfd)...
Here is another take:
nifty([], []).
nifty([[]|X], []) :-
nifty(X, []).
nifty([], [[]|X]) :-
nifty([], X).
nifty([[X|Y]|Z], [[X|Q]|R]) :-
swap(Y, R, N),
swap(Q, Z, M),
nifty(M, N).
swap([], [], []).
swap([X|Y], [[X|P]|Q], [P|R]) :-
swap(Y, Q, R).
It ranks second concerning speed:
?- dim(77,66,L), time((between(1,100,_), transpose(L,_), fail; true)).
% 1,577,201 inferences, 0.103 CPU in 0.104 seconds (99% CPU, 15244107 Lips)
?- dim(77,66,L), time((between(1,100,_), nifty(L,_), fail; true)).
% 521,601 inferences, 0.062 CPU in 0.063 seconds (99% CPU, 8368647 Lips)
?- dim(77,66,L), time((between(1,100,_), group(L,_), fail; true)).
% 528,300 inferences, 0.048 CPU in 0.049 seconds (98% CPU, 11105739 Lips)
But it is fully bidirectional, even in SWI-Prolog:
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.4)
?- nifty([[1,2,3],[4,5,6]],X).
X = [[1, 4], [2, 5], [3, 6]].
?- nifty([[1,4],[2,5],[3,6]],X).
X = [[1, 2, 3], [4, 5, 6]] ;
false.
?- nifty(X,[[1,2,3],[4,5,6]]).
X = [[1, 4], [2, 5], [3, 6]] ;
false.
?- nifty(X,[[1,4],[2,5],[3,6]]).
X = [[1, 2, 3], [4, 5, 6]].
And if the Prolog system provides just in time multi-argument indexing, as in Jekejeke Prolog, it even leaves no choice points without much ado:
Jekejeke Prolog 3, Runtime Library 1.3.8 (23 May 2019)
?- nifty([[1,2,3],[4,5,6]], X).
X = [[1,4],[2,5],[3,6]]
?- nifty([[1,4],[2,5],[3,6]], X).
X = [[1,2,3],[4,5,6]]
?- nifty(X, [[1,2,3],[4,5,6]]).
X = [[1,4],[2,5],[3,6]]
?- nifty(X, [[1,4],[2,5],[3,6]]).
X = [[1,2,3],[4,5,6]]
Related
I'd like to have a Prolog predicate that can replace the nth item in the list with the first.
Example:
% replace(+List,+Counter,-New List, %-First Item).
?- replace([1,2,3,4,5],3,L,Z).
L = [1, 2, 1, 4, 5]
Z = 1
I don't know how to do this. Thanks for your help!
Try using the predicate nth1(Index, List, Item, Rest):
?- nth1(3, [1,2,3,4,5], Item, Rest).
Item = 3,
Rest = [1, 2, 4, 5].
?- nth1(3, List, 1, [1,2,4,5]).
List = [1, 2, 1, 4, 5].
Putting it all together:
replace(List, Index, NewList, First) :-
List = [First|_],
nth1(Index, List, _Removed, Rest),
nth1(Index, NewList, First, Rest).
Examples:
?- replace([1,2,3,4,5], 3, L, Z).
L = [1, 2, 1, 4, 5],
Z = 1.
?- replace([one,two,three,four,five], 4, NewList, First).
NewList = [one, two, three, one, five],
First = one.
Another method, slightly faster (replacing succ/2 with simple arithmetic helped performance):
replace_nth_with_first(Nth1, Lst, LstReplaced, First) :-
must_be(positive_integer, Nth1),
Lst = [First|_Tail],
replace_nth_with_first_(Nth1, Lst, LstReplaced, First).
% Keeping the arguments simple, to ensure that Nth1 = 1 matches
replace_nth_with_first_(1, Lst, LstReplaced, First) :-
!,
Lst = [_Head|Tail],
LstReplaced = [First|Tail].
replace_nth_with_first_(Nth1, [H|Lst], [H|LstReplaced], First) :-
Nth is Nth1 - 1,
replace_nth_with_first_(Nth, Lst, LstReplaced, First).
Result in swi-prolog:
?- replace_nth_with_first(3, [a, b, c, d, e], R, F).
R = [a,b,a,d,e],
F = a.
Performance comparison:
?- numlist(1, 2000000, L), time(replace_nth_with_first(1000000, L, R, First)).
% 1,000,004 inferences, 0.087 CPU in 0.087 seconds (100% CPU, 11428922 Lips)
% slago's
?- numlist(1, 2000000, L), time(replace_f(L, 1000000, R, First)).
% 2,000,011 inferences, 0.174 CPU in 0.174 seconds (100% CPU, 11484078 Lips)
Note the argument order, as per https://swi-prolog.discourse.group/t/split-list/4836/8
You might use append/3. Nice, concise, and declarative:
replace( [X|Xs] , 1 , [X|Xs], X ) . % replacing the first item in the list is a no-op
replace( Ls , I , L1 , X ) :- % for anything else...
I > 1 , % - the index must be greater than 1
J is I-1 , % - the length of the prefix is I-1
length( [X|Xs], J ) , % - construct an empty/unbound prefix list of the desired length
append( [X|Xs] , [_|Ys] , Ls ) , % - break the source list up into the desired bits
append( [X|Xs] , [X|Ys] , L1 ) % - and then put everything back together in the desired manner
. % Easy!
I need to delete all duplicates from a list and then show it in reverse order.
So far I have this:
reverseInSet([], Y,R):-
R=Y.
reverseInSet([H|T], Y, R):-
removeElement(H,T,T1),
reverseInSet(T1, [H|Y], R).
removeElement(_,[],[]).
removeElement(X,[X|T],L):-
removeElement(X,T,L).
removeElement(X,[Y|T],L):-
removeElement(X,T,L1),
L=[Y|L1].
And the output is this:
reverseInSet([1,2,3,3,9,3],[],P).
P = [9, 3, 2, 1]
P = [3, 9, 3, 2, 1]
P = [9, 3, 3, 2, 1]
P = [9, 3, 3, 2, 1]
P = [3, 9, 3, 3, 2, 1]
Any ideas?
This is about the simplest way to do it. In the case of duplicate entries in the source list, the last such entry is kept and the ones preceding it are discarded.
list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, Zs ) , reverse(Zs,Ys) .
list_to_set( [] , [] ) .
list_to_set( [X|Xs] , Ys ) :- member(X,Xs), !, list_to_set(Xs,Ys) .
list_to_set( [X|Xs] , [X|Ys] ) :- list_to_set(Xs,Ys) .
You could also do it in a single pass, too. You just need to use an accumulator to build the result list in reverse order:
list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, [], Ys ) .
list_to_set( [] , Ys , Ys ) .
list_to_set( [X|Xs] , Ts , Ys ) :- member(X,Xs), !, list_to_set(Xs,Ts,Ys) .
list_to_set( [X|Xs] , Ts , Ys ) :- list_to_set(Xs,[X|Ts],Ys) .
This also keeps the last duplicates in the source list. But, since we're using an accumulator here to build the result list, it's easy enough to rejigger it to keep the first of any duplicate items: we just check to see if a candidate item is already in the result set or not (rather than check to see if it occurs later in the source list).
list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, [], Ys ) .
list_to_set( [] , Ys , Ys ) .
list_to_set( [X|Xs] , Ts , Ys ) :- member(X,Ts), !, list_to_set(Xs,Ts,Ys).
list_to_set( [X|Xs] , Ts , Ys ) :- list_to_set(Xs,[X|Ts],Ys) .
Using reif library, to be both pure and reasonably deterministic:
:- use_module(library(reif)).
reverse_without_duplicates(Lst, LstRevDeDup) :-
reverse_without_duplicates_(Lst, [], LstRevDeDup).
reverse_without_duplicates_([], Lst, Lst).
reverse_without_duplicates_([H|T], Upto, LstRevDeDup) :-
memberd_t(H, T, Bool),
reverse_without_duplicates_bool_(Bool, T, H, Upto, LstRevDeDup).
reverse_without_duplicates_bool_(true, T, _H, Upto, LstRevDeDup) :-
reverse_without_duplicates_(T, Upto, LstRevDeDup).
reverse_without_duplicates_bool_(false, T, H, Upto, LstRevDeDup) :-
% Include head
reverse_without_duplicates_(T, [H|Upto], LstRevDeDup).
Result in swi-prolog:
?- time(reverse_without_duplicates([1, 2, 3, 3, 9, 3], X)).
% 52 inferences, 0.000 CPU in 0.000 seconds (88% CPU, 1025277 Lips)
X = [3,9,2,1].
This pure method shows sensible answers with:
?- time(reverse_without_duplicates([1, 2, N, 3, 9, 3], L)).
% 49 inferences, 0.000 CPU in 0.000 seconds (87% CPU, 1030603 Lips)
N = 1,
L = [3,9,1,2] ;
% 127 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 845646 Lips)
N = 2,
L = [3,9,2,1] ;
% 152 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1051910 Lips)
N = 3,
L = [3,9,2,1] ;
% 178 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1229859 Lips)
N = 9,
L = [3,9,2,1] ;
% 88 inferences, 0.000 CPU in 0.000 seconds (97% CPU, 648633 Lips)
L = [3,9,N,2,1],
dif(N,1),
dif(N,9),
dif(N,3),
dif(N,2).
I want to write a function that returns true if two lists are exactly the same(order of elements matters).
I tried it this way:
same([ ], [ ]).
same([H1|R1], [H2|R2]):-
H1 == H2,
same(R1, R2).
It returns true while two lists are the same, also I expect if I have
?- same(X, [1, 2, 3]).
I want it to return
X = [1, 2, 3].
But it doesn't work if input is like this. Here are some sample outputs I got:
?- same([1, 2], [1, 2]).
true.
?- same([2, 1], [1, 2]).
false.
?- same(X, [1, 2, 3]).
false.
?- same([1, 2, 3], [1, 2, X]).
false.
How to fix it?
The problem is that you're using ==/2 (checking whether two items are instantiated the same) rather than =/2 (checks if two items are unified or unifiable). Just change to unification:
same([], []).
same([H1|R1], [H2|R2]):-
H1 = H2,
same(R1, R2).
Then this will have the behavior you're looking for:
| ?- same(X, [1, 2, 3]).
X = [1,2,3] ? a
no
| ?- same([1, 2], [1, 2]).
(1 ms) yes
| ?- same([2, 1], [1, 2]).
no
| ?- same([1, 2, 3], [1, 2, X]).
X = 3
(1 ms) yes
| ?- same([A,B,C], L).
L = [A,B,C]
yes
% In this last example, A, B, and C are variables. So it says L is [A,B,C],
% whatever A, B, and C are.
If you query X == 3 in Prolog, and X is not bound to the value 3, or it is just unbound, it will fail. If X is unbound and you query, X = 3, then Prolog will unify X (bind it) with 3 and it will succeed.
For more regarding the difference between =/2 and ==/2, see What is the difference between == and = in Prolog?
You can also use maplist for a nice, compact solution. maplist is very handy for iterating through a list:
same(L1, L2) :- maplist(=, L1, L2).
Here, unification (=/2) is still used for exactly the same reason as above.
Finally, as #Boris points out, in Prolog, the unification predicate will work on entire lists. In this case, this would suffice:
same(L1, L2) :- L1 = L2.
Or equivalently:
same(L, L). % Would unify L1 and L2 queried as same(L1, L2)
This will succeed if the lists are the same, or will attempt to unify them by unifying each element in turn.
| ?- same([1,2,X], [1,2,3]). % Or just [1,2,X] = [1,2,3]
X = 3
yes
| ?- same([1,2,X], [1,2,3,4]). % Or just [1,2,X] = [1,2,3,4]
no
The prior more elaborate approaches are considered an exercise in list processing for illustration. But the simplest and most correct method for comparison and/or unification of lists would be L1 = L2.
sort([ [30,100], [10,11] ], X).
gets
X = [[10,11],[30,100]]
How can I sort only by the first index of each sublist?
i.e
X = [[10,100], [30, 11]]
Thanks
The simpler way should be perusing the available builtins. Then take the first element from each sublist, sort them, and replace in the original:
sortfirst(L, S) :-
maplist(get_first, L, A),
msort(A, B),
maplist(set_first, L, B, S).
get_first([E|_], E).
set_first([_|R], E, [E|R]).
edit: note that msort is required, to avoid losing duplicates.
test:
?- sortfirst([ [30,100], [10,11] ], X).
X = [[10, 100], [30, 11]].
get/set first are just needed to adjust the arguments from maplist: if we use lambda, we can write a true 'one liner' procedure:
:- [lambda].
sortfirst_lambda(L, S) :-
maplist(\X^Y^(X = [E|_], Y = E), L, A),
msort(A, B),
maplist(\X^Y^Z^(X = [_|R], Y = E, Z = [E|R]), L, B, S).
Simple identities can simplify a little that expressions:
sortfirst_lambda(L, S) :-
maplist(\X^Y^(X = [Y|_]), L, A),
msort(A, B),
maplist(\X^Y^Z^(X = [_|R], Z = [Y|R]), L, B, S).
edit: or still more simplified:
sortfirst_lambda(L, S) :-
maplist(\[Y|_]^Y^true, L, A),
msort(A, B),
maplist(\[_|R]^Y^[Y|R]^true, L, B, S).
Here you can see that, as in the original get/set first, just the unification of arguments is needed.
Thus lambda it's syntactically convenient, but has its cost:
?- randomlists(100000, 3, -30,+30, L),
time(sortfirst(L,A)),
time(sortfirst_lambda(L,B)),
assertion(A=B).
% 400,012 inferences, 0,482 CPU in 0,483 seconds (100% CPU, 830072 Lips)
% 1,700,012 inferences, 1,717 CPU in 1,721 seconds (100% CPU, 990302 Lips)
L = [[-8, -13, 11], [-13, -27, -29], [5, 10, -24], [-8, -7, -6], [3, -24, -9], [-13, -20, -24], [7, 27|...], [-5|...], [...|...]|...],
A = B, B = [[-30, -13, 11], [-30, -27, -29], [-30, 10, -24], [-30, -7, -6], [-30, -24, -9], [-30, -20, -24], [-30, 27|...], [-30|...], [...|...]|...].
here are the service predicates to build sized test data:
randomlist(Length, Low, High, List) :-
findall(E, (between(1, Length, _),
random(Low, High, E)), List).
randomlists(Length1, Length2, Low, High, ListOfLists) :-
findall(E, (between(1, Length1, _),
randomlist(Length2, Low, High, E)), ListOfLists).
#chac(+1 btw): there's no need of lambda to one-line this (in swi at least!):
sortfirst(L, Res) :-
maplist(selectchk, X, L, R),
msort(X, XS),
maplist(selectchk, XS, Res, R).
but lambda versions or your first version are less tricky and more readable I think!
The below is my untested code.. there may be one/two cosmetic errors... The input list is split into two based on the head value on the list and the resulted two lists are recursively processed to finally result the sorted output.
sort(Input,Output):-sort(Input,[],Output).
sort([],SortedOut,SortedOut).
sort([[Index1,Index2]|Tail],SortedBig,Out):-
split(Tail,[Index1,Index2],LessList,BigList),
!,sort(BigList,SortedBig,NewSort),
sort(LessList,[[Index1,Index2]|NewSort],Out).
split([],[_D],[],[]).
split([[Index1,Index2]|Rem],[Index21,Index22],[[Index1,Index1]|L1],L2):-
Index1<Index21,
!,split(Rem,[Index21,Index22],L1,L2).
split([[Index1,Index2]|Rem],[Index21,Index22],L1,[[Index1,Index1]|L2]):-
!,split(Rem,[Index21,Index22],L1,L2).
Try this and let me know...
How can I transpose a list like [[1,2,3][4,5,6][6,7,8]] to [[1,4,6],[2,7,8],[3,6,9]]?
To depict it: I'd like to flip the matrix 90 degree to the left. How can I do that?
Not sure your example is correct, but I get the idea.
If using SWI-PROLOG, you can use the CLPFD module, like so:
:- use_module(library(clpfd)).
Allowing you to use the transpose/2 predicate, like this:
1 ?- transpose([[1,2,3],[4,5,6],[6,7,8]], X).
X = [[1, 4, 6], [2, 5, 7], [3, 6, 8]].
Otherwise (if no SWI-PROLOG), you could simply use this implementation (which happened to be an old one in SWI's clpfd):
transpose([], []).
transpose([F|Fs], Ts) :-
transpose(F, [F|Fs], Ts).
transpose([], _, []).
transpose([_|Rs], Ms, [Ts|Tss]) :-
lists_firsts_rests(Ms, Ts, Ms1),
transpose(Rs, Ms1, Tss).
lists_firsts_rests([], [], []).
lists_firsts_rests([[F|Os]|Rest], [F|Fs], [Os|Oss]) :-
lists_firsts_rests(Rest, Fs, Oss).
For an updated version which uses foldl and maplist built-ins, see clpfd.pl.
This is the smallest solution I could come up with.
Code
transpose([[]|_], []).
transpose(Matrix, [Row|Rows]) :- transpose_1st_col(Matrix, Row, RestMatrix),
transpose(RestMatrix, Rows).
transpose_1st_col([], [], []).
transpose_1st_col([[H|T]|Rows], [H|Hs], [T|Ts]) :- transpose_1st_col(Rows, Hs, Ts).
Test
:- transpose([[1,2,3],
[4,5,6],
[7,8,9]], R),
print(R).
Prints:
[[1,4,7],
[2,5,8],
[3,6,9]]
Explanation
The way it works is that transpose will recursively call transpose_1st_col which extracts and transposes the first column of the matrix. For example:
:- transpose_1st_col([[1,2,3],
[4,5,6],
[7,8,9]], Row, RestMatrix),
print(Row),
print(RestMatrix).
will print
[1,4,7]
and
[[2,3],
[5,6],
[8,9]]
This is repeated until the input matrix is empty, at which point all columns have been transposed. The transposed columns are then joined into the transposed matrix.
Here's a fragment of a larger answer:
% transposed(+A, ?B) iff matrix B is transposed matrix A
transposed(A, B) :- transposed(A, [], B).
transposed(M, X, X) :- empty(M), !.
transposed(M, A, X) :- columns(M, Hs, Ts), transposed(Ts, [Hs|A], X).
% empty(+A) iff A is empty list or a list of empty lists
empty([[]|A]) :- empty(A).
empty([]).
% columns(+M, ?Hs, ?Ts) iff Hs is the first column
% of matrix M and Ts is the rest of matrix M
columns([[Rh|Rt]|Rs], [Rh|Hs], [Rt|Ts]) :- columns(Rs, Hs, Ts).
columns([[]], [], []).
columns([], [], []).
simpler approach:
trans(M, [P|T]):- first(M, P, A), trans(A, T).
trans(Empty, []):- empty(Empty).
empty([[]|T]):- empty(T).
empty([[]]).
first([[P|A]|R], [P|Ps], [A|As]):- first(R, Ps, As).
first([], [], []).
efficient also
[debug] 36 ?- time(trans([[1,2,3],[4,5,6],[7,8,9]],A)).
% 21 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
A = [[1,4,7],[2,5,8],[3,6,9]] ;
% 12 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
false.
Another simple approach:
transpose(M0, M) :-
nonvar(M0),
findall(L, maplist(nth1(_), M0, L), M).
?- transpose([[1,2,3],[4,5,6],[7,8,9]], M).
M = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]. `
An iterative approach:
trans([H|R],[H1|R1]):-trans2([H|R],[H|R],[],[H1|R1],0),!.
trans2([A|_],_,_,[],N):-length(A,N).
trans2(M,[],H1,[H1|R1],N):-N1 is N+1, trans2(M,M,[],R1,N1).
trans2(M,[H|R],L,[H1|R1],N):-nth0(N,H,X),
append(L,[X],L1),trans2(M,R,L1,[H1|R1],N).
My solution with full names for a better understanding:
% emptyMatrix(Line, EmptyMatrix)
emptyMatrix([],[]).
emptyMatrix([_|T1],[[]|T2]):-emptyMatrix(T1,T2).
% only length of parameter 'Line' is interesting. It ignores its content.
% appendElement(Element, InputList, OutputList)
appendElement(E,[],[E]).
appendElement(E,[H|T],[H|L]):-appendElement(E,T,L).
% appendTransposed(NestedList, InputMatrix, OutputMatrix)
appendTransposed([],[],[]).
appendTransposed([X|T1],[],[[X]|T3]):-appendTransposed(T1,[],T3).
appendTransposed([X|T1],[R|T2],[C|T3]):-appendElement(X,R,C),appendTransposed(T1,T2,T3).
% transposeMatrix(InputMatrix, TransposedMatrix)
transposeMatrix([L|M],T):-emptyMatrix(L,A),transpose([L|M],T,A).
transpose([],T,T).
transpose([L|M],T,A):-appendTransposed(L,A,B),transpose(M,T,B).
A 'line' can be a col or a row.
The idea lies in appending the elements into the lists of an empty matrix.
(e.g. all elements of the first row = the first elements of all cols
=> all elements of the first i-nth row = the i-nth elements of all cols)
It works on my machine as this session protocol shows to me:
5 ?- transposeMatrix([[1,2],[3,4]],T).
T = [[1, 3], [2, 4]] ;
false.
6 ?- transposeMatrix([[1],[2]],T).
T = [[1, 2]] ;
false.
7 ?- transposeMatrix([[1,2,3],[4,5,6]],T).
T = [[1, 4], [2, 5], [3, 6]] ;
false.
8 ?- transposeMatrix([[1]],T).
T = [[1]] ;
false.
Another approach:
delete_one_list([], []).
delete_one_list([[_|L]|LLs], [L|Ls]) :-
delete_one_list(LLs, Ls).
transpose_helper([], []).
transpose_helper([[X|_]|Xs], [X|Ys]) :-
transpose_helper(Xs, Ys).
transpose([[]|_], []).
transpose(List, [L|Ls]) :-
transpose_helper(List, L),
delete_one_list(List, NewList),
transpose(NewList, Ls).