なぜこのエラーが出るのか分かりません。私は既にこのエラーについて既に質問されているすべての質問を読んでおり、これらの問題はありません。誰かが助けてくれますか?コメントは与えられた例のハードコードなので、トップダウンアプローチを使用できます。私が関数で終わったら、ハードコードをコメントアウトします。私は最初の2つの機能しか実装していません。 ghciでファイルを実行するときに 'main'と入力すると、コードが正常に出力されます。私は両方の関数を別々にテストすると、真を返しますが、一緒に上記のエラーを返します。例外:Prelude.head:Haskellの空リスト
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"
のように書き換えることができますそれは何を行い、その入力と出力は何ですか) –
あなたのコードに 'head'を使用していないとすれば、あなたが得ているエラーはどこか別のものから来ているに違いありませんし、あなたの質問は現在解決できません。エラーが発生しているコード部分を投稿します。これには 'head'関数の使用が必要です。 –
@AlexisKing私はsudoku関数を別々にテストしましたが、私にエラーを出すわけではありませんでしたので、エラーが他の関数にあると仮定しました。 –