我正在尝试使用递归创建字符串列表。
基本上,我想要取出字符串的一部分直到某个特定点。从那里创建一个列表,然后通过递归处理剩余的字符串。
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))
这是我得到的结果。但它仅仅是将这些字符串连接在一起,因为它们本身就是字符列表。但我想创建一个字符串列表。
如果输入是 "test\n123\n",那么输出应该是 ["test", "123"]。
谢谢!
[(1,"test"),(2,"123")]
对吧?Line
是一个元组,不只是一个字符串。 - kennytm