好的,所以我试图在 Haskell 中创建一个数独求解器,但我收到一条错误消息,指出我无法将预期类型 [[Int]] 与实际类型 IO () 匹配。这是我对递归求解器、错误消息和其他相关代码的尝试:
递归求解器尝试:
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 完全陌生,这是我尝试过的第一个真正的程序。任何帮助将不胜感激。