Refresh 'x'th list item - Haskell

Possible duplicate:
Replace individual list items in Haskell?

I managed to make some progress in this part of my assignment, but I added part of the code below:

module Grid where

data State = On | Off deriving (Eq, Show)

next :: State -> State
next On = Off
next Off = On

type Row = [State]


updateRow :: Row -> Int -> Row
updateRow  (r:rs) x 
    | x == 0     = next r:rs
--  | otherwise     = ........????

As shown in the last line just above, I was able to get updateRow to work when x = 0, as shown below (with the inverted 0th element).

*Grid> updateRow [Off,Off,Off,Off] 0
[On,Off,Off,Off]
*Grid> 

All this disappears, however, when I try to invert other elements of this list. I cannot “decompose” the formula in this function.

I also MUST follow this convention like:

updateRow :: Row -> Int -> Row

Thanks in advance.

+3
source share
4 answers

Something like that:

module Grid where

data State = On | Off deriving (Eq, Show)

next :: State -> State
next On = Off
next Off = On

type Row = [State]


updateRow :: Row -> Int -> Row
updateRow  (r:rs) x 
    | x == 0     = next r:rs
    | otherwise  = r : (updateRow rs (x-1))
updateRow [] x = []
+2
source

, . , , , .

+2

?

update i a as = map repl $ zip as [0..] where
   repl (a',i') | i == i' = a
                | otherwise = a'

, , . i th ( ) as a.

+1

otherwise updateRow , replace question.

: x , r ( ) updateRow rs x - something ( something , ).

,

+1
source

All Articles