Fop*_*tin 116 haskell concat list
是否有一个函数来连接列表的元素与分隔符?例如:
> foobar " " ["is","there","such","a","function","?"]
["is there such a function ?"]
Run Code Online (Sandbox Code Playgroud)
谢谢你的回复!
Nik*_* B. 208
是的,有:
Prelude> import Data.List
Prelude Data.List> intercalate " " ["is","there","such","a","function","?"]
"is there such a function ?"
Run Code Online (Sandbox Code Playgroud)
intersperse
有点笼统:
Prelude> import Data.List
Prelude Data.List> concat (intersperse " " ["is","there","such","a","function","?"])
"is there such a function ?"
Run Code Online (Sandbox Code Playgroud)
此外,对于您想要与空格字符连接的特定情况,还有unwords
:
Prelude> unwords ["is","there","such","a","function","?"]
"is there such a function ?"
Run Code Online (Sandbox Code Playgroud)
unlines
工作方式类似,只是字符串使用换行符进行内爆,并且还会在末尾添加换行符.(这使得序列化文本文件非常有用,每个POSIX标准结尾必须有一个尾随换行符)
使用foldr写一行字并不难
join sep xs = foldr (\a b-> a ++ if b=="" then b else sep ++ b) "" xs
join " " ["is","there","such","a","function","?"]
Run Code Online (Sandbox Code Playgroud)