I'm trying to write a sudoku generator/solver in Haskell as a learning exercise, but I'm running into difficulty producing a mutable array in the ST monad.
The input to my parse function will be a String of 81 characters containing the digits 1 through 9 and placeholders (., -, or 0).
This is the function that I wrote, but it will not compile and I can't figure out what types I need:
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import qualified Data.Array.Unboxed as U
import Data.Char (digitToInt, isDigit)
import Data.List (zip)
data Cell = Cell
{ values :: Word16
, original :: Bool
} deriving (Show)
parse input =
runSTUArray $
U.array ((0, 0), (8, 8)) $
map
(\(i, d) ->
( (quot i 9, mod i 9)
, if isDigit d && d /= '0'
then Cell {values = bit $ digitToInt d, original = True}
else Cell {values = 2 ^ 11 - 2, original = False})) $
zip [0 ..] input
The output of the function should be an immutable representation of a mutable 9 x 9 grid containing cells.
How can I fix this?