我正在嘗試在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