title: Update 'x'th element of list - Haskell
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5852722/replace-individual-list-elements-in-haskell">Replace individual list elements in Haskell?</a>  </p>
  </blockquote>
  
  
  
  <p>I have managed to make some progress in this part of my assignment but have attached part of the code below that I have made:</p>
  
  <pre><code>module Grid where
  
  data State = On | Off deriving (Eq, Show)
  
  next :: State -&gt; State
  next On = Off
  next Off = On
  
  type Row = [State]
  
  
  updateRow :: Row -&gt; Int -&gt; Row
  updateRow  (r:rs) x 
      | x == 0     = next r:rs
  --  | otherwise     = ........????
  </code></pre>
  
  <p>As shown in the last line just above, I have managed to get updateRow to work for when x = 0, as shown below (with the 0th element inverted).</p>
  
  <pre><code>*Grid&gt; updateRow [Off,Off,Off,Off] 0
  [On,Off,Off,Off]
  *Grid&gt; 
  </code></pre>
  
  <p>It all comes unstuck however when I try inverting other elements of this list. I can't seem to 'genralise' a formula in this function.</p>
  
  <p>I also MUST follow THIS type convention:</p>
  
  <pre><code>updateRow :: Row -&gt; Int -&gt; Row
  </code></pre>
  
  <p>Thanks in advance.</p>