有没有用分隔符连接列表元素的功能?
例如:

> foobar " " ["is","there","such","a","function","?"]
["is there such a function ?"]

感谢您的回复!

最佳答案

是的,there is:

Prelude> import Data.List
Prelude Data.List> intercalate " " ["is","there","such","a","function","?"]
"is there such a function ?"

intersperse 比较通用:
Prelude> import Data.List
Prelude Data.List> concat (intersperse " " ["is","there","such","a","function","?"])
"is there such a function ?"

另外,对于要与空格字符连接的特定情况,还有 unwords :
Prelude> unwords ["is","there","such","a","function","?"]
"is there such a function ?"

unlines 的工作原理类似,只是使用换行符将字符串内插,并且最后还要添加一个换行符。 (这对于序列化文本文件很有用,根据POSIX标准,文本文件必须以尾随换行符结尾)

关于list - 是否有任何haskell函数将列表与分隔符连接起来?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9220986/

10-11 02:46