title: Prolog transpose of a Matrix using decompose
body: <p>First of all, I know there are other codes to make the transpose of a matrix in other questions, but my problem is specific one:</p>
  
  <p>In a homework I was asked to build a <code>decompose/3</code> code which separates the first element of each list from the rest of the elements.</p>
  
  <p>After this I was asked to create a code to <code>transpose/3</code> a matrix. <em>(If the input is not a matrix, it can fail, the purpose is not to check whether it is a matrix or not).</em></p>
  
  <p>I have the following codes <code>decompose/3</code>, <code>transpose/3</code>, and <code>transpose1/3</code> so far: (transpose and transpose1 are the same but with a different base case)</p>
  
  <pre><code>%4. transpose(M, R)
  %Input transpose([[1,2],[3,4],[5,6]], R).
  
  decompose([], [], []).
  decompose([[H|T]|T0], [H|L1], [T|L2]) :- 
      decompose(T0, L1, L2).
  
  transpose(M,[L1|Z]):-                       #This code works for 3 rows
      decompose(M,L1,L2),
      transpose(L2,Z).
  
  transpose1([[X],[Y],[Z],[B]],[[X,Y,Z,B]]).  #This code works for up to 4 rows
  transpose1([H|T],[L1|R]):-    
      decompose([H|T],L1,L2),
      transpose1(L2,R).
  </code></pre>
  
  <p>So the input matrix is a [3 x 2] in this example, and it works fine, for both, but if my matrix has more than 4 rows, <code>transpose</code> does not work anymore, and because of the base case <code>transpose1([[X],[Y],[Z],[B]],[[X,Y,Z,B]]).</code> it can manipulate up to 4 rows, but if the matrix is bigger, then the outcome is <code>false</code>.</p>
  
  <p>Any idea how to manipulate my code? How to make the base case apply for any number of rows on my matrix? Why is this not working?</p>
  
  <p>Thank you!</p>