
alwaysterminating :-
	transpose([[1]], X).

alwaysterminating2 :-
	transpose2([[1]], X).

symmetric3(M) :-
	M = [_,_,_],
	symmetric(M).

symmetric(M) :-
	transpose(M, M).

% an improved transpose

transpose2([],[]).
transpose2(M, T) :-
	M = [_|_],
	T = [_|_],
	transpose(M, T).

% Source: http://www.cs.kuleuven.ac.be/~dtai/prototypes/dppd/transpose.html
transpose(Xs,[]) :-
	nullrows(Xs).
transpose(Xs,[Y|Ys]) :-
	makerow(Xs,Y,Zs),
	transpose(Zs,Ys).

makerow([],[],[]).
makerow([[X|Xs]|Ys],[X|Xs1],[Xs|Zs]) :-
	makerow(Ys,Xs1,Zs).

nullrows([]).
nullrows([[]|Ns]) :-
	nullrows(Ns).

