[String]-> String中的非穷举模式? [英] Non exhaustive pattern in [String] ->String?

查看:59
本文介绍了[String]-> String中的非穷举模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个像这样的函数:

so I'm creating an function like this:

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

给出了像这样的字符串列表["arroz","atum"]应该可以给出"arroz\natum\n"的问题是,我创建的此函数表示非穷尽模式.

that given a list of strings like this ["arroz","atum"] is supposed to give "arroz\natum\n" the problem is that this function that I created says non-exhaustive patterns.

推荐答案

您的第一个模式[x]是带有一个元素的列表.因此,Haskell编译器想知道如何处理空列表.

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

此外,在第一行中,您编写的是uninhas而不是unlinhas,因此Haskell编译器认为您编写了两个不同的函数.

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

这篇关于[String]-> String中的非穷举模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆