简体   繁体   English

Haskell - Concat的字符串列表

[英]Haskell - Concat a list of strings

Im trying to create a list of strings using some recursion. 我试图使用一些递归创建一个字符串列表。

Basically i want to take a part of a string up to a certain point. 基本上我想把一个字符串的一部分提到某一点。 Create a list from that and then process the rest of the string through recursion. 从中创建一个列表,然后通过递归处理其余的字符串。

type DocName = FilePath
type Line = (Int,String)
type Document = [Line]

splitLines :: String -> Document
splitLines [] = []
splitLines str | length str == 0 = []
                             | otherwise = zip [0..(length listStr)] listStr
                                    where 
                                        listStr = [getLine] ++ splitLines getRest
                                        getLine = (takeWhile (/='\n') str)
                                        getRest =  (dropWhile (=='\n') (dropWhile (/='\n') str))

Thats what i got. 多数民众赞成我得到的。 But it just concats the strings back together since they are list of characters themselves. 但它只是将字符串重新连接在一起,因为它们是字符列表本身。 But i want to create a list of strings. 但我想创建一个字符串列表。

["test","123"] if the input was "test\\n123\\n" [“test”,“123”]如果输入是“test \\ n123 \\ n”

Thanks 谢谢

If you try to compile your code, you'll get an error message telling you that in the line 如果您尝试编译代码,您将收到一条错误消息,告诉您该行

listStr = [getLine] ++ splitLines getRest

splitLines getRest has type Document , but it should have type [String] . splitLines getRest具有Document类型,但它应该具有类型[String] This is easy enough to understand, since [getLine] is a list of strings (well a list of one string) and so it can only be concatenated with another list of strings, not a list of int-string-tuples. 这很容易理解,因为[getLine]是一个字符串列表(以及一个字符串的列表),所以它只能与另一个字符串列表连接,而不是int-string-tuples列表。

So to fix this we can use map to replace each int-string-tuple in the Document with only the string to get a list of strings, ie: 所以为了解决这个问题,我们可以使用map来替换Document中的每个int-string-tuple,只用字符串来获取字符串列表,即:

listStr = [getLine] ++ map snd (splitLines getRest)

After changing the line to the above your code will compile and run just fine. 将行更改为上面后,您的代码将编译并运行正常。

But it just concats the strings back together since they are list of characters themselves. 但它只是将字符串重新连接在一起,因为它们是字符列表本身。

I'm not sure why you think that. 我不确定你为什么这么认为。

The reason your code did not compile was because of the type of splitLines as I explained above. 你的代码没有编译的原因是因为我上面解释的splitLines的类型。 Once you fix that error, the code behaves exactly as you want it to, returning a list of integer-string-tuples. 一旦修复了该错误,代码就会按照您的意愿运行,返回整数字符串元组的列表。 At no point are strings concatenated. 字符串连接在一起。

Well, if you wrote this just to practice recursion then it is fine once you fix error mentioned by sepp2k. 好吧,如果你写这个只是为了练习递归,那么一旦你修复了sepp2k提到的错误就没问题了。 But in real code, I would prefer - 但在实际代码中,我更愿意 -

splitLines str = zip [0..] (lines str)

Or even 甚至

splitLines = zip [0..] . lines

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM