2017-09-13 28 views
0

我可以快速解釋一下如何使用map將字符串列表連接成一個字符串嗎?我試圖使用插入,但我意識到這是用來組合列表而不是字符串。即[[char]]而不是[String]有沒有辦法映射一個函數,它將讀取字符串列表並將它們連接成一個大字符串?

type InformationList = (String , [String]) 

concatenateList :: String -> [InformationList] -> String 
concatenateList n cs = do 
    let [informationlist] = intercalate " " cs 
    let toWrite = n ++ [informationList] 
    return toWrite 

我更新了這個,以便您可以看到我一直在使用的代碼。這是說,[[字符]]是不一樣的[informationlist]但應該是一樣的[字符串]

+0

'String'是'[Char]'的別名。可以通過'插 「」[ 「一」, 「B」, 「C」]'得到' 「A,B,C」' –

+0

'String類型= [字符]',所以'字符串的隔板串聯'是'[Char]'的別名。這意味着'[[Char]]'等於'[String]'。 –

+0

@everyone我已經更新了我的問題,所以希望它更徹底。我很抱歉。 – hawkeye

回答

4

一個StringChar小號一個列表。一個String是一個類型別名定義爲:

type String = [Char] 

如果你想連接的String秒的名單沒有任何分隔符,你可以使用concat :: Foldable t => t [a] -> [a]

Prelude> concat ["foo", "bar", "qux"] 
"foobarqux" 

如果你想插入a 字符串作爲分隔符,您可以使用intercalate :: [a] -> [[a]] -> [a]

Prelude> import Data.List(intercalate) 
Prelude Data.List> intercalate "," ["foo","bar","qux"] 
"foo,bar,qux" 
+0

可能是值得一提的'unwords :: [字符串] - > String'基本上做什麼'插「「',不過它從前奏更專業的簽名和直接。 – Alec

相關問題