Last active
August 29, 2015 14:01
-
-
Save jdiez17/ffa24cb9fae0b2609b0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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