Skip to content

Instantly share code, notes, and snippets.

@jdiez17
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save jdiez17/ffa24cb9fae0b2609b0b to your computer and use it in GitHub Desktop.

Select an option

Save jdiez17/ffa24cb9fae0b2609b0b to your computer and use it in GitHub Desktop.
module Sudoku where
import Control.Monad (guard)
data Digit = D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | D9
deriving (Eq, Ord, Read, Enum)
mkDigit :: Int -> Maybe Digit
mkDigit n
| n > 0 && n < 10 = Just $ toEnum (n-1)
| otherwise = Nothing
instance Show Digit where show x = show $ fromEnum x+1
newtype Sudoku = Sudoku { toList :: [[Digit]] }
deriving (Show)
mkSudoku :: [[Int]] -> Maybe Sudoku
mkSudoku xss = return xss >>= checkDimensions >>= buildSudoku
where
checkDimensions :: [[Int]] -> Maybe [[Int]]
checkDimensions xss = do
let lengthOuter = length xss
lengthInner <- map length xss
guard (lengthInner == lengthOuter)
return xss
buildSudoku :: [[Int]] -> Maybe Sudoku
buildSudoku _ = Just $ Sudoku [[D1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment