繁体   English   中英

连接到一个字符串列表,另一个字符串

[英]Concatenate to a list of string another string

下面的代码返回了String的列表,但我希望它能在多种情况下工作。 问题是我无法通过递归创建相同的确切结果。 该程序返回以下结果:

replaceTabs 6 ["\thello world"]

=> ["      hello world"]

现在,这应该可以使用更长的列表,例如:

replaceTabs 6 ["asd dsa","\thello world"]

    => ["asd dsa","      hello world"]

简单的concat不起作用,因为它将返回未定义的模式。

replaceTab' :: Int -> [[Char]] -> [Char]
replaceTab' n [[x]] =
 if x == '\t' then replicate n ' '
 else [x]

replaceTabs :: Int -> [String]-> [String]
replaceTabs n [""] = [""]
replaceTabs n (x:xs) = (return . concat $ [replaceTab' n [a] | a <- (map (:[]) (x))])

这个

replaceTab' :: Int -> [[Char]] -> [Char]

是相同的,

replaceTab' :: Int -> [String] -> String

您应该专注于实现功能,

replaceTab :: Int -> String -> String

可以“修复”单个String 那么replaceTabs很简单,

replaceTabs :: Int -> [String] -> [String]
replaceTabs n = map (replaceTab n) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM