是否有任何haskell函数来连接列表与分隔符? [英] Is there any haskell function to concatenate list with separator?

查看:116
本文介绍了是否有任何haskell函数来连接列表与分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有函数用分隔符连接列表元素?
例如:

 > foob​​ar[is,there,such,a,function,?] 
[是否有这样的函数?]

$ b

感谢您的回复!

解决方案

是的,

  Prelude> import Data.List 
Prelude Data.List> intercalate[is,there,such,a,function,?]
是否有这样的功能?

intersperse 更具普遍性:

  Prelude> import Data.List 
Prelude Data.List> concat(intersperse[is,there,such,a,function,?])
有没有这样的函数?

另外,对于要用空格字符连接的特定情况, unwords

  Prelude> unwords [is,there,such,a,function,?] 
是否有这样的功能?

unlines 的作用相似,只是字符串使用换行符字符,而且换行符也添加到最后。 (这对于序列化文本文件很有用,它必须按照POSIX标准结尾并带有尾随换行符)


Is there a function to concatenate elements of a list with a separator? For example:

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

Thanks for any reply!

解决方案

Yes, there is:

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

intersperse is a bit more general:

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

Also, for the specific case where you want to join with a space character, there is unwords:

Prelude> unwords ["is","there","such","a","function","?"]
"is there such a function ?"

unlines works similarly, only that the strings are imploded using the newline character and that a newline character is also added to the end. (This makes it useful for serializing text files, which must per POSIX standard end with a trailing newline)

这篇关于是否有任何haskell函数来连接列表与分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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