Simon Marlow wrote:

 | lines        :: String -> [String]
 | lines s = lines' s ""
 |   where
 |   lines' []       acc = [acc]
 |   lines' ('\n':s) acc = reverse acc : lines' s ""
 |   lines' (c:s)    acc = lines' s (c:acc)
 | 
 | This one is more than twice as fast as the foldr
 | version, despite the fact that it needs to reverse the
 | accumulating parameter for each line.

Unfortunately, it is less lazy too, so it will blow out of
heapspace on very long lines.

And it is not correct for strings not ending with a '\n'
(you forgot to reverse acc in the recursion base case...)

/Koen.

--
Koen Claessen         http://www.cs.chalmers.se/~koen     
phone:+46-31-772 5424      e-mail:[EMAIL PROTECTED]
-----------------------------------------------------
Chalmers University of Technology, Gothenburg, Sweden


Reply via email to