List 需要一个basecase来返回列表中的位置
(sud不包含任何内容)List 需要一个basecase来返回列表中的位置,list,haskell,sudoku,List,Haskell,Sudoku,(sud不包含任何内容) (!!0)是头,但是头只对非空列表有效,因此您需要检查是否是这种情况: 所以 Prelude> :m +Data.Maybe Prelude Data.Maybe> listToMaybe [1..5] Just 1 Prelude Data.Maybe> listToMaybe [] Nothing blankMaybe::数独->Maybe Pos blankMaybe(数独rs)=listToMaybe[(y,x)|(r,y)可能您可以为输入添加一个预期输出的示例
(!!0)
是头
,但是头
只对非空列表有效,因此您需要检查是否是这种情况:
所以
Prelude> :m +Data.Maybe
Prelude Data.Maybe> listToMaybe [1..5]
Just 1
Prelude Data.Maybe> listToMaybe []
Nothing
blankMaybe::数独->Maybe Pos
blankMaybe(数独rs)=listToMaybe[(y,x)|(r,y)可能您可以为输入添加一个预期输出的示例?不如返回Maybe Pos
?只是添加了mor信息。不,任务说我应该返回一个Pos。那么您收到的作业是错误的。您可以返回(-1,-1)
正如@WillNess所建议的,但是如果你被哈斯克尔教授而没有被教授,这可能是的工作,那么你的老师并没有真正做好他们的工作。另外,某物!!0
通常被写成头某物。
Main> blank example
(0,2)
Main> blank sud
*** Exception: Prelude.(!!): index too large
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 ]
]
blank :: Sudoku -> Pos
blank (Sudoku rs) = case [(y,x) | (r,y) <- rs `zip` [0..8]
, (c,x) <- r `zip` [0..8]
, c == Nothing]
of (pos:_) -> pos; _ -> (-1, -1)
Prelude> :m +Data.Maybe
Prelude Data.Maybe> listToMaybe [1..5]
Just 1
Prelude Data.Maybe> listToMaybe []
Nothing
blankMaybe :: Sudoku -> Maybe Pos
blankMaybe (Sudoku rs) = listToMaybe [(y,x) | (r,y) <- rs `zip` [0..8]
, (c,x) <- r `zip` [0..8]
, c == Nothing]