我正在尝试在Haskell中建立一个回溯数独解算器。但是我陷入了最后的一点。我创建了一个名为nextBoards的函数,它返回所有可能的soduko板,其中下一个空的位置用可能的值填充。现在我试图在Haskell中使用回溯,但是我不能使用while循环之类的东西,现在我对如何执行此操作感到困惑。我之前在Java中做过一个数独求解器,但我现在完全停留在如何在Haskell中完成它。在Haskell回溯数独
-- Generate the next boards for a given board
nextBoards :: Board -> [Board]
nextBoards b =
let position = findFirstEmpty b
in [update z (snd position) (fst position) b | z <- options b (snd position) (fst position)]
-- We found the real solution
solve :: Board -> [Board] -> Board
solve focus options
| not (notEmpty focus) = focus
-- We hit a dead path try the next option
solve focus options
| solve (head options) (tail options)
-- We are solving the focus, generate the next boards
-- and save the rest in the options
solve focus options
| solve (head (nextBoards focus)) (tail (nextBoards focus))
我真的不知道如何继续。
你有候选人板的名单,并解决了.. –
@牛米的功能。是的,我知道,我不太清楚你的意思? –
更多提示:[filter](http://hackage.haskell.org/package/base-4.8.2.0/docs/Prelude.html#v:filter) – isanco