Non exhaustive pattern in [String] ->String?

后端 未结 1 1744
清酒与你
清酒与你 2021-01-28 07:17

so I\'m creating an function like this:

 unlinhas::[String]->String
 uninhas [x] = \"\"
 unlinhas (x:xs) = x ++ \"\\n\" ++(unlinhas( xs))

th

相关标签:
1条回答
  • 2021-01-28 08:09

    Your first pattern [x] is a list with one element. So the Haskell compiler is wondering what to do with an empty list.

    Furthermore in your first line you write uninhas, instead of unlinhas, as a result the Haskell compiler thinks that you write two different functions.

    Based on your specifications however, you want to process the empty list, so you can fix it with:

    unlinhas:: [String] -> String
    unlinhas [] = ""
    unlinhas (x:xs) = x ++ "\n" ++(unlinhas( xs))

    You can further cleanup the code, and write it as:

    unlinhas:: [String] -> String
    unlinhas [] = ""
    unlinhas (x:xs) = x ++ '\n' : unlinhas xs
    0 讨论(0)
提交回复
热议问题