title: Exception: Prelude.head: empty list in Haskell
body: <p>I can't figure out why I'm getting this error. I have already read all the questions already asked about this error and I don't have any of those issues. Can someone please help. The comments are the hard code for the given example so we can use a top-down approach. When I get done with a function I comment out the hard code. I have only implemented the first 2 functions. In ghci when you run the file, type 'main', if it prints true the code is working. When I test both functions separately they return true but together they return the above error.</p>
  
  <pre><code>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 -&gt; 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]] -&gt; [Coords] -&gt; [[BoardElement]]
  sudokuElements a [] = a
  sudokuElements [] _ = []
  sudokuElements (be:bes) (cd:cds) =
      let xs = validVals be cd
          temp = [[(cd,x)] | x &lt;- xs]
      in sudokuElements temp cds
  
  -- | head bes == inputBoardElements &amp;&amp; empty == inputBoardEmpty = 
  --     [solvedBoardElements]
  -- | otherwise = error "sudokuElements not implemented"
  
  validVals :: [BoardElement] -&gt; Coords -&gt; [Int]
  validVals bes rc
      | bes == tail solvedBoardElements &amp;&amp; rc==(8,6) = [1]
      | bes \\ solvedBoardElements == [] = [1..9]
      | otherwise = []
  
  toElements :: Board -&gt; ([BoardElement],[Coords])
  toElements b 
      | b==inputBoard = (inputBoardElements, inputBoardEmpty)    
      | otherwise = error "toElements not implemented"
  
  
  fromElements :: [BoardElement] -&gt; Board
  fromElements bes 
      | sort bes == solvedBoardElements = solvedBoard
      | otherwise = error "fromElements not implemented"
  </code></pre>