简体   繁体   English

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

[英]Concatenate to a list of string another string

The below code gives back a list of String but I want it work on multiple cases. 下面的代码返回了String的列表,但我希望它能在多种情况下工作。 The problem is that I can't create the same exact result with recursion. 问题是我无法通过递归创建相同的确切结果。 The program gives back the following result: 该程序返回以下结果:

replaceTabs 6 ["\thello world"]

=> ["      hello world"]

Now this should work with a longer list like: 现在,这应该可以使用更长的列表,例如:

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

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

Simple concat doesn't work, because it will give back undefined pattern. 简单的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))])

This 这个

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

is the same as, 是相同的,

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

What you should focus on is implementing a function, 您应该专注于实现功能,

replaceTab :: Int -> String -> String

which "fixes" a single String . 可以“修复”单个String Then replaceTabs is simply, 那么replaceTabs很简单,

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

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

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