title: Getting all the diagonals of a matrix in Haskell
body: <p>The two-dimensional list looks like:</p>
  <pre><code>1 | 2 | 3
  - - - - -
  4 | 5 | 6
  - - - - -
  7 | 8 | 9
  </code></pre>
  <p>Or in pure haskell</p>
  <pre><code>[ [1,2,3], [4,5,6], [7,8,9] ]
  </code></pre>
  <p>The expected output for <code>diagonals [ [1,2,3], [4,5,6], [7,8,9] ]</code> is</p>
  <pre><code>[ [1], [4, 2], [7, 5, 3], [8, 6], [9] ]
  </code></pre>
  <p>Writing <code>allDiagonals</code> (to include anti-diagonals) is then trivial:</p>
  <pre><code>allDiagonals :: [[a]] -&gt; [[a]]
  allDiagonals xss = (diagonals xss) ++ (diagonals (rotate90 xss))
  </code></pre>
  <h1>My research on this problem</h1>
  <p><strong>Similar question here at StackOverflow</strong></p>
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/6313308/get-all-the-diagonals-in-a-matrix-list-of-lists-in-python">Python</a> this question is about the same problem in Python, but Python and Haskell are very different, so the answers to that question are not relevant to me.</p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/3998891/what-is-the-best-way-to-extract-a-diagonal-from-a-matrix-in-haskell">Only one</a> This question and answer are in Haskell, but are only about the central diagonal.</p>
  </li>
  </ul>
  <p><strong>Hoogle</strong></p>
  <p>Searching for <code>[[a]] -&gt; [[a]]</code> gave me no interesting result.</p>
  <p><strong>Independent thinking</strong></p>
  <p>I think the the indexing follows a kind of count in base x where x is the number of dimensions in the matrix, look at:</p>
  <pre><code>1 | 2
  - - -
  3 | 4
  </code></pre>
  <p>The diagonals are <code>[ [1], [3, 2], [4] ]</code></p>
  <ul>
  <li><code>1</code> can be found at <code>matrix[0][0]</code></li>
  <li><code>3</code> can be found at <code>matrix[1][0]</code></li>
  <li><code>2</code> can be found at <code>matrix[0][1]</code></li>
  <li><code>1</code> can be found at <code>matrix[1][1]</code></li>
  </ul>
  <p>That is similar to counting in base 2 up to 3, that is the matrix size minus one. But this is far too vague to be translated into code.</p>