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 ""
    where
    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
    where
    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?


thanks,
matthias

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to