好吧,所以我試圖在Haskell中創建一個Sudoku求解器,但我得到一個錯誤,說我無法與實際類型IO()匹配預期類型[[Int]]。這是我嘗試在遞歸求解器,錯誤信息,以及其它代碼的相關部分:遞歸回溯Haskell
遞歸求解嘗試:
test i j q s_board = if ((valid_row i q s_board)&&(valid_column j q s_board)&& (valid_sub_board i j q s_board)) then (solve (set_value i j q s_board)) else s_board
foo i j s_board = if ((get_value i j s_board) == 0) then [test i j q s_board | q <- [1..9]] else s_board
solve s_board = if (full_board s_board) then (print_board s_board) else [foo i j s_board | i <- [0..8], j <- [0..8]]
如果我包括爲有效的所有定義這個問題將是非常長列,行等功能,但我已檢查確保這些功能。有了這個代碼,我發現了以下錯誤消息:
Couldn't match expected type `[[Int]]' with actual type `IO()'
In the return type of a call of `print_board'
In the expression: (print_board s_board)
In the expression:
if (full_board s_board) then
(print_board s_board)
else
[foo i j s_board | i <- [0 .. 8], j <- [0 .. 8]]
而且,這裏是我使用打印我的板代碼:
-- showLine: this function provides formating for a single row
showLine :: [Int] -> String
showLine = intercalate " | "
. map unwords
. chunksOf 3
. map show
-- showBoad: this function provides formating for the entire board
showBoard :: [[Int]] -> String
showBoard = intercalate "---------------------\n"
. map unlines
. chunksOf 3
. map showLine
-- print_board: this function is meant to print out the entire board
print_board :: [[Int]] -> IO()
print_board s_board = putStrLn $ showBoard s_board
。你們看看有什麼有什麼問題我到目前爲止。我對Haskell完全陌生,這是我嘗試的第一個真正的程序。任何幫助將不勝感激。