title: Modifying elements of Haskell 2D array
body: <p>I am writing a function to modify elements of a given 2D "array" given below:</p>
  
  <pre><code>change_elem :: Int -&gt; Int -&gt; a -&gt; [[a]] -&gt; [[a]]
  -- empty list case
  change_elem _ _ _ [] = []
  
  -- have arrived at the element to change`
  change_elem 0 0 x ((y:ys):ls) = (x:ys):ls
  
  -- reduce the column until we find the element to change
  change_elem 0 col x ((y:ys):ls) = [[y]] ++ change_elem 0 (col-1) x (ys:ls)
  
  -- reduce the row until we find the column to change
  change_elem row col x (l:ls) = l : change_elem (row-1) col x ls
  </code></pre>
  
  <p>It works for input such as <code>change_elem 1 0 3 [[1,2],[4,5]]</code> and produces <code>[[1,2],[3,5]]</code>.  However, my problem is when I attempt to change an element that is not in column 0, so the problem clearly lies in the "reducing column" step.</p>
  
  <p><code>change_elem 2 1 7 [[1,2,3],[4,5,6],[1,2,0]]</code> gives the output 
  <code>[[1,2,3],[4,5,6],[1],[7,0]]</code>.  Its behavior is that it separates the earlier elements of a given row into singleton lists.</p>
  
  <p><code>change_elem 0 4 10 [[0,1,2,3,4,5,6]]</code> yields <code>[[0],[1],[2],[3],[10,5,6]]</code></p>
  
  <p>Shouldn't the (++) operator return the earlier elements back into the list, leaving a single unified row?</p>
  
  <p>I have tried calling concat on the resulting list, but when I modify the line to read:</p>
  
  <pre><code>... concat $ [[y]] ++ change_elem 0 (col-1) x (ys:ls)
  </code></pre>
  
  <p>I get a long error, but it seems like this should work.
  The error:</p>
  
  <pre><code>Couldn't match expected type ‘[a]’ with actual type ‘a’
    ‘a’ is a rigid type variable bound by
        the type signature for
          change_elem :: Int -&gt; Int -&gt; a -&gt; [[a]] -&gt; [[a]]
        at test.hs:15:16
  Relevant bindings include
    ls :: [[a]] (bound at test.hs:23:29)
    ys :: [a] (bound at test.hs:23:25)
    y :: a (bound at test.hs:23:23)
    x :: a (bound at test.hs:23:19)
    change_elem :: Int -&gt; Int -&gt; a -&gt; [[a]] -&gt; [[a]]
      (bound at test.hs:17:1)
  In the expression: y
  In the expression: [y]
  </code></pre>