Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell-如何剥离尾随空格_Haskell - Fatal编程技术网

Haskell-如何剥离尾随空格

Haskell-如何剥离尾随空格,haskell,Haskell,我想把[1,2,3]转换成“1,2,3”。现在我得到了“1233”。 我使用 import Data.Text (strip) 我的代码如下所示: ar2str ar = (concatMap (\x -> (show x)++" " ) ar) 如何修改ar2str,使其与strip一起工作 找到了 Rosetta拯救代码:-)您可能希望使用Data.Text.unwords而不是所有这些。或者,如果您使用的是String而不是Text,Data.List.unwords您可能希望

我想把[1,2,3]转换成“1,2,3”。现在我得到了“1233”。 我使用

import Data.Text (strip)
我的代码如下所示:

ar2str ar = (concatMap (\x -> (show x)++" " ) ar)
如何修改ar2str,使其与strip一起工作

找到了


Rosetta拯救代码:-)

您可能希望使用Data.Text.unwords而不是所有这些。或者,如果您使用的是
String
而不是
Text
Data.List.unwords

您可能希望使用
Data.Text.unwords
而不是所有这些。或者,如果使用的是
String
而不是
Text
Data.List.unwords

如果处理给定的输入字符串,则去除尾随空格是有意义的。如果生成字符串,最好不要首先附加空格。特别是考虑到Haskell列表的性质,从末尾删除元素等于重建列表

以下是达到预期效果的一种方法:

ar2str :: Show a => String -> [a] -> String
ar2str _ [x] = show x
ar2str sep (x:x':xs) = show x ++ sep ++ ar2str sep (x':xs)

如果处理给定的输入字符串,则剥离尾随空间是有意义的。如果生成字符串,最好不要首先附加空格。特别是考虑到Haskell列表的性质,从末尾删除元素等于重建列表

以下是达到预期效果的一种方法:

ar2str :: Show a => String -> [a] -> String
ar2str _ [x] = show x
ar2str sep (x:x':xs) = show x ++ sep ++ ar2str sep (x':xs)

你的意思肯定是
unwords
@rjanJohansen,是的,确实,或者可能是
插入
。你的意思肯定是
unwords
@rjanJohansen,是的,确实,或者可能是
插入