I'm writing a program in Haskell that can pretty print a table and do basic queries on it. The following function is a snippet of the code which prints a 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]
Note: Table == [[String]]
After calling this function with the 'unlines' function, the table is printed.
If I test this function, giving it a [[String]]
argument, it works correctly. However, if I test this function in my 'main' code, I get the error:
Non-exhaustive patterns in function printTable
The only difference is that in my main code, the user of the program can give a text file as an input:
main :: IO()
main = interact (lines >>> exercise >>> unlines)
exercise :: [String] -> [String]
exercise = parseTable >>> select "gender" "male"
>>> project ["last", "first", "salary"] >>> printTable
Any help to solve this problem is more than welcome!
When you pattern match on (x:xs)
, it will only match if there is at least one item in the list.
You need to handle the case of an empty Table
parameter.
printTable [] = ...