我試圖創建一個使用某種遞歸的字符串列表。Haskell - Concat的字符串列表
基本上我想取一個字符串的一部分到某一點。從中創建一個列表,然後通過遞歸處理字符串的其餘部分。
type DocName = FilePath
type Line = (Int,String)
type Document = [Line]
splitLines :: String -> Document
splitLines [] = []
splitLines str | length str == 0 = []
| otherwise = zip [0..(length listStr)] listStr
where
listStr = [getLine] ++ splitLines getRest
getLine = (takeWhile (/='\n') str)
getRest = (dropWhile (=='\n') (dropWhile (/='\n') str))
那就是我得到的。但它只是將字符串連接在一起,因爲它們是字符本身的列表。但是我想創建一個字符串列表。
[「測試」,「123」]如果輸入是「測試\ N123 \ n」個
感謝
你的意思是'[(1, 「測試」),(2, 「123」)]'對不對? 'Line'是一個元組,不只是一個String。 – kennytm 2010-09-29 17:52:17
是的,但我專注於「listStr = [getLine] ++ splitLines getRest」那部分 – Matt 2010-09-29 17:56:28
你的問題的標題很混亂 - 你不是試圖連接字符串,你想分開它們,這是相反的。 – Amoss 2010-09-29 18:36:32