gpt4 book ai didi

list - 是否有任何 haskell 函数可以将列表与分隔符连接起来?

转载 作者:行者123 更新时间:2023-12-03 04:34:15 26 4
gpt4 key购买 nike

是否有一个函数可以用分隔符连接列表的元素?例如:

> 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com