【发布时间】:2010-09-29 18:50:39
【问题描述】:
有这个
type DocName = FilePath
type Line = (Int,String)
type Document = [Line]
splitLines :: String -> Document
splitLines [] = []
splitLines str = zip [0..(length listStr)] listStr
where
listStr = [getLine] ++ map snd (splitLines getRest)
getLine = (takeWhile (/='\n') str)
getRest = (dropWhile (=='\n') (dropWhile (/='\n') str))
工作正常,但我想我也需要空行。
splitLines "test\nthis\nstring\n" should be
[(0,"test"),(1,"this"),(2,"string"),(3,"")]
不完全确定我怎么能做到这一点。有任何想法吗? 我需要用其他东西重写它吗?
我应该使用像 foldr 这样的高阶函数吗? 谢谢。
终于搞定了,谢谢。
splitLines :: String -> Document
splitLines "" = [(0,"")]
splitLines str = zip [0..(length listStr)] listStr
where
listStr = [getLine] ++ map snd (splitLines getRest)
getLine = takeWhile (/='\n') str
getRest = tail (dropWhile (/='\n') str)
【问题讨论】:
-
为什么你的元组需要复制索引?
lines不是你想要的吗? -
不能使用线条。必须创建我们自己的线条功能。我怎么能使用索引?不知道该怎么做。