title: Elegantly printing 2d array in SML
body: <p>I was studying some SML/NJ and I came through the following question to myself: </p>
  
  <p>Given an <code>Array2</code> <code>grid</code> which contains strings in each cell and in the end of each row a <code>\n</code> find a way to elegantly print the array (using folds and stuff like that) </p>
  
  <p>My attempt was </p>
  
  <pre><code> print (Array2.fold Array2.RowMajor (op ^) "" grid)
  </code></pre>
  
  <p>which printed all the rows in the correct order but each row was reversed.</p>
  
  <p>If that was a list it would support <code>foldl</code> and <code>foldr</code> functions so the whole process would be trivial. </p>
  
  <p>And the conventional way to print it is</p>
  
  <pre><code>fun printGrid i N M grid =
    if (i &gt;= N) then ()
    else
      (
        let
          fun printRow j M =
            if (j &gt;= M + 1) then ()
            else (print (Array2.sub(grid, i, j)); printRow (j+1) M)
        in
          printRow 0 M;
          printGrid (i + 1) N M grid
        end
      )
  </code></pre>
  
  <p>which is of course not so elegant! </p>
  
  <p>Is there an elegant (one-line) way to achieve printing it correctly? </p>