[Haskell-cafe] cutting long strings into lines
fis at wiwi.hu-berlin.de
Sat Sep 30 13:18:46 EDT 2006
On Sat, Sep 30, 2006 at 04:36:02PM +0100, Neil Mitchell wrote:
> (if you can't be bothered to do that, the answer is "lines" ;)
although this wasn't the original problem, i like it, too :). but now
i am stuck in finding an optimal implementation for lines. the
following implementation is slightly slower than the built-in
function, and i suspect this to stem from the occurrance of reverse
for each line:
cut1 :: String -> [String]
cut1 = f ""
f x "" = [reverse x]
f x ('\n':xs) = reverse x : f "" xs
f x (c:xs) = f (c:x) xs
i vaguely remember having seen a CPS trick here before, but all i can
come up with is the yet a little slower
cut2 :: String -> [String]
cut2 = f id
f k "" = [k ""]
f k ('\n':xs) = k "" : f id xs
f k (c:xs) = f k' xs where k' cs = k (c:cs)
also, i think both implementations are line-strict, that is, each line
is fully evaluated once touched in the first character.
is there a similar implementation, with CPS or not, that is lazy in
the lines and more efficient?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20060930/144e03d8/attachment.bin
More information about the Haskell-Cafe