簡體   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