我正在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]]
参数,它可以正常工作。但是,如果我在'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
非常欢迎任何解决此问题的帮助!
答案 0 :(得分:3)
当您在(x:xs)
上进行模式匹配时,只有在列表中至少有一个项目时才匹配。
您需要处理空Table
参数的情况。
printTable [] = ...