将字符串列表连接到另一个字符串
Concatenate to a list of string another string
下面的代码返回一个字符串列表,但我希望它适用于多种情况。问题是我无法通过递归创建完全相同的结果。
该程序返回以下结果:
replaceTabs 6 ["\thello world"]
=> [" hello world"]
现在这应该适用于更长的列表,例如:
replaceTabs 6 ["asd dsa","\thello world"]
=> ["asd dsa"," hello world"]
简单的连接不起作用,因为它会返回未定义的模式。
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
其中 "fixes" 一个 String
。那么 replaceTabs
就是
replaceTabs :: Int -> [String] -> [String]
replaceTabs n = map (replaceTab n)
下面的代码返回一个字符串列表,但我希望它适用于多种情况。问题是我无法通过递归创建完全相同的结果。 该程序返回以下结果:
replaceTabs 6 ["\thello world"]
=> [" hello world"]
现在这应该适用于更长的列表,例如:
replaceTabs 6 ["asd dsa","\thello world"]
=> ["asd dsa"," hello world"]
简单的连接不起作用,因为它会返回未定义的模式。
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
其中 "fixes" 一个 String
。那么 replaceTabs
就是
replaceTabs :: Int -> [String] -> [String]
replaceTabs n = map (replaceTab n)