我无法弄清楚为什么我会收到此错误。我已经阅读了有关此错误的所有问题,但我没有遇到任何问题。有人可以请帮助。注释是给定示例的硬编码,因此我们可以使用自上而下的方法。当我完成一个函数时,我会注释掉硬代码。我只实现了前两个功能。在运行文件的ghci中,键入' main',如果它打印为true,则代码正常工作。当我单独测试两个函数时,它们返回true,但它们一起返回上述错误。
import Data.List ((\\), sort)
type Board = [[Int]]
inputBoard :: Board
inputBoard =
[[5,3,0, 0,7,0, 0,0,0],
[6,0,0, 1,9,5, 0,0,0],
[0,9,8, 0,0,0, 0,6,0],
[8,0,0, 0,6,0, 0,0,3],
[4,0,0, 8,0,3, 0,0,1],
[7,0,0, 0,2,0, 0,0,6],
[0,6,0, 0,0,0, 2,8,0],
[0,0,0, 4,1,9, 0,0,5],
[0,0,0, 0,8,0, 0,7,9]]
solvedBoard :: Board
solvedBoard =
[[5,3,4, 6,7,8, 9,1,2],
[6,7,2, 1,9,5, 3,4,8],
[1,9,8, 3,4,2, 5,6,7],
[8,5,9, 7,6,1, 4,2,3],
[4,2,6, 8,5,3, 7,9,1],
[7,1,3, 9,2,4, 8,5,6],
[9,6,1, 5,3,7, 2,8,4],
[2,8,7, 4,1,9, 6,3,5],
[3,4,5, 2,8,6, 1,7,9]]
type Coords = (Int,Int)
type BoardElement = (Coords,Int)
inputBoardElements :: [BoardElement]
inputBoardElements =
[((0,0),5),((0,1),3),((0,4),7),((1,0),6),((1,3),1),((1,4),9),((1,5),5),
((2,1),9),((2,2),8),((2,7),6),((3,0),8),((3,4),6),((3,8),3),((4,0),4),
((4,3),8),((4,5),3),((4,8),1),((5,0),7),((5,4),2),((5,8),6),((6,1),6),
((6,6),2),((6,7),8),((7,3),4),((7,4),1),((7,5),9),((7,8),5),((8,4),8),
((8,7),7),((8,8),9)]
inputBoardEmpty :: [Coords]
inputBoardEmpty =
[(0,2),(0,3),(0,5),(0,6),(0,7),(0,8),(1,1),(1,2),(1,6),(1,7),(1,8),
(2,0),(2,3),(2,4),(2,5),(2,6),(2,8),(3,1),(3,2),(3,3),(3,5),(3,6),
(3,7),(4,1),(4,2),(4,4),(4,6),(4,7),(5,1),(5,2),(5,3),(5,5),(5,6),
(5,7),(6,0),(6,2),(6,3),(6,4),(6,5),(6,8),(7,0),(7,1),(7,2),(7,6),
(7,7),(8,0),(8,1),(8,2),(8,3),(8,5),(8,6)]
solvedBoardElements :: [BoardElement]
solvedBoardElements =
[((0,0),5),((0,1),3),((0,2),4),((0,3),6),((0,4),7),((0,5),8),((0,6),9),
((0,7),1),((0,8),2),((1,0),6),((1,1),7),((1,2),2),((1,3),1),((1,4),9),
((1,5),5),((1,6),3),((1,7),4),((1,8),8),((2,0),1),((2,1),9),((2,2),8),
((2,3),3),((2,4),4),((2,5),2),((2,6),5),((2,7),6),((2,8),7),((3,0),8),
((3,1),5),((3,2),9),((3,3),7),((3,4),6),((3,5),1),((3,6),4),((3,7),2),
((3,8),3),((4,0),4),((4,1),2),((4,2),6),((4,3),8),((4,4),5),((4,5),3),
((4,6),7),((4,7),9),((4,8),1),((5,0),7),((5,1),1),((5,2),3),((5,3),9),
((5,4),2),((5,5),4),((5,6),8),((5,7),5),((5,8),6),((6,0),9),((6,1),6),
((6,2),1),((6,3),5),((6,4),3),((6,5),7),((6,6),2),((6,7),8),((6,8),4),
((7,0),2),((7,1),8),((7,2),7),((7,3),4),((7,4),1),((7,5),9),((7,6),6),
((7,7),3),((7,8),5),((8,0),3),((8,1),4),((8,2),5),((8,3),2),((8,4),8),
((8,5),6),((8,6),1),((8,7),7),((8,8),9)]
main :: IO ()
main = print (sudoku inputBoard == solvedBoard)
sudoku :: Board -> Board
sudoku [] = []
sudoku b =
let bde = fst (toElements b)
cd = snd (toElements b)
allboards = sudokuElements [bde] cd
in fromElements (head allboards)
--sudoku b
--| b == inputBoard = solvedBoard
--| otherwise = error "sudoku not implemented"
sudokuElements :: [[BoardElement]] -> [Coords] -> [[BoardElement]]
sudokuElements a [] = a
sudokuElements [] _ = []
sudokuElements (be:bes) (cd:cds) =
let xs = validVals be cd
temp = [[(cd,x)] | x <- xs]
in sudokuElements temp cds
-- | head bes == inputBoardElements && empty == inputBoardEmpty =
-- [solvedBoardElements]
-- | otherwise = error "sudokuElements not implemented"
validVals :: [BoardElement] -> Coords -> [Int]
validVals bes rc
| bes == tail solvedBoardElements && rc==(8,6) = [1]
| bes \\ solvedBoardElements == [] = [1..9]
| otherwise = []
toElements :: Board -> ([BoardElement],[Coords])
toElements b
| b==inputBoard = (inputBoardElements, inputBoardEmpty)
| otherwise = error "toElements not implemented"
fromElements :: [BoardElement] -> Board
fromElements bes
| sort bes == solvedBoardElements = solvedBoard
| otherwise = error "fromElements not implemented"
答案 0 :(得分:2)
问题是allBoards
/对sudokuElements
的调用返回一个空列表。
如果没有看到整个程序,很难给出确切原因。
我建议一般不要使用head
,因为它是部分功能。相反,使用模式匹配。
在你的情况下,这个
fromElements (head allBoards)
可以改写为
case allBoards of
[] -> error "allBoards returned an empty list." -- Or whatever
(first:_) -> fromElements first