我正在用 Haskell 编写一个程序,它可以漂亮地打印表格并对其进行基本查询。以下函数是打印表格的代码片段:
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”函数调用此函数后,将打印表格。
如果我测试这个函数,给它一个[[String]]
论证,它工作正常。但是,如果我在“主”代码中测试此函数,则会收到错误:
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 [] = ...
关于haskell - 功能中的非详尽模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39644700/