[Haskell-cafe] Lists of Lists

Dmitry V'yal akamaus at gmail.com
Thu Mar 9 03:23:12 EST 2006


zell_ffhut ?????:
> Last attempt, as its due in a couple of hours
> 
> Here's what i have so far..
> 
>> charToGrid :: Char -> Position -> Grid -> Grid
>> charToGrid c [] (row,col) xs = xs
>> charToGrid c (row,col) xs = (changeValue c (concat xs (row*9 + col)))
> 
> Using changeValue - 
> 
> changeValue x 0 (y:ys) = (x:ys) 
> changeValue x n (y:ys) = y:(changeValue x (n-1) ys)
> 
> Would really appritiate any help
> --
> View this message in context: http://www.nabble.com/Lists-of-Lists-t1245394.html#a3315187
> Sent from the Haskell - Haskell-Cafe forum at Nabble.com.
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

You can easily do it using updateList as Robert Dockins proposed.

updateList :: (a -> a) -> Int -> [a] -> [a]
updateList f i l = begin ++ (f x : end)
  where (begin, x : end) = splitAt i l

changeGrid :: Char -> (Int,Int) -> Grid -> Grid
changeGrid c (row,col) grid = updateList updateRow row grid
    where updateRow l = updateList (const c) col l

Here we replace a row with a new one, using a helper function updateRow which,
given the row, changes one symbol at position col in it. updateList is used
twice here. First it's applied to outer list and then to inner one. It is
possible because of higher-order nature of updateList. It's behavior can be
changed by suppling an appropriate function f as it's first argument.


More information about the Haskell-Cafe mailing list