1
data Sudoku = Sudoku [[Maybe Int]]
blank :: Sudoku -> Pos
blank (Sudoku rs) = [(y,x) | (r,y) <- rs `zip` [0..8], (c,x) <- r `zip` [0..8], c == Nothing] !! 0
空白返回的第一個位置是Nothing
。如果它不是Nothing
,我想返回一些合理的,而不是錯誤!我怎麼能這樣做?需要一個返回列表中的位置的基準點
輸出:
Main> blank example
(0,2)
Main> blank sud
*** Exception: Prelude.(!!): index too large
(SUD包含什麼都沒有)
example =
Sudoku
[ [Just 3, Just 6, Nothing, Nothing, Just 7, Just 0, Just 2, Nothing, Nothing]
, [Nothing, Just 5, Nothing, Nothing, Nothing, Nothing, Just 1, Just 8, Nothing]
, [Nothing, Nothing, Just 9, Just 2, Nothing, Just 4, Just 7, Nothing, Nothing]
, [Nothing, Nothing, Nothing, Nothing, Just 1, Just 3, Nothing, Just 2, Just 8 ]
, [Just 4, Nothing, Nothing, Just 5, Nothing, Just 2, Nothing, Nothing, Just 9 ]
, [Just 2, Just 7, Nothing, Just 4, Just 6, Nothing, Nothing, Nothing, Nothing]
, [Nothing, Nothing, Just 5, Just 3, Nothing, Just 8, Just 9, Nothing, Nothing]
, [Nothing, Just 8, Just 3, Nothing, Nothing, Nothing, Nothing, Just 6, Nothing]
, [Nothing, Nothing, Just 7, Just 6, Just 9, Nothing, Nothing, Just 4, Just 3 ]
]
可能您可以爲輸入添加預期輸出的示例? – Sibi 2014-10-09 12:27:05
如何返回'Maybe Pos'呢? – MathematicalOrchid 2014-10-09 12:33:54
剛剛添加的信息。不,任務說我應該返回一個Pos .. – Josu24 2014-10-09 12:45:53