【问题标题】:Non Exhaustive Patterns in function [duplicate]功能中的非详尽模式[重复]
【发布时间】:2016-09-22 16:49:56
【问题描述】:

我正在用 Haskell 编写一个程序,它可以漂亮地打印一个表并对其进行基本查询。以下函数是打印表格的代码的 sn-p:

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

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

【问题讨论】:

    标签: haskell input non-exhaustive-patterns


    【解决方案1】:

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

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

    printTable [] = ...
    

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多