功能中的非详尽模式

Non Exhaustive Patterns in function

我正在 Haskell 中编写一个程序,可以漂亮地打印 table 并对其进行基本查询。以下函数是打印 table:

的代码片段
printTable :: Table -> [String]
printTable table@(header:rows) = [addLine] ++ addHeader ++ [addLine] ++ addRows rows ++ [addLine]
    where widthList            = columnWidths table
      makeTupleList []         = []
      makeTupleList (x:xs)     = zip widthList x : makeTupleList (xs)
      addRows line             = map printRow (makeTupleList line)
      addLine                  = printLine widthList
      addHeader                = addRows [(map.map) toUpper header]

注:Table == [[String]]

用'unlines'函数调用此函数后,打印table。

如果我测试这个函数,给它一个 [[String]] 参数,它可以正常工作。但是,如果我在我的 'main' 代码中测试这个函数,我会得到错误:

Non-exhaustive patterns in function printTable

唯一不同的是,在我的主要代码中,程序的用户可以提供一个文本文件作为输入:

main :: IO()
main = interact (lines >>> exercise >>> unlines)

exercise :: [String] -> [String]
exercise = parseTable >>> select "gender" "male" 
                  >>> project ["last", "first", "salary"] >>> printTable

非常欢迎任何解决此问题的帮助!

当您对 (x:xs) 进行模式匹配时,它只会匹配列表中至少有一项的情况。

您需要处理 Table 参数为空的情况。

printTable [] = ...