Simon Marlow writes:
You didn't mention the accumulating parameter version:
[[[with correction pointed out by Koen Claessen:]]]
lines :: String - [String]
lines s = lines' s ""
where
lines' [] acc = [reverse acc]
lines' ('\n':s) acc = reverse acc : lines' s ""
lines'
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
|
Shin-Cheng Mu [EMAIL PROTECTED]
is puzzled why the derived foldr version of lines
is significantly faster than the prelude version,
which he recognises as an unfold:
I am curious why the Prelude version is less efficient than the
your fold version? It seems to me the fold version construct
Yes, break is very expensive due to its pairing up of results
for every
character consumed. You didn't mention the accumulating
parameter version:
lines :: String - [String]
lines s = lines' s ""
where
lines' [] acc = [acc]
lines' ('\n':s) acc = reverse acc : lines' s
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,
Hi,
Is there an explanation to this phenom
Wolfram Kahl wrote:
To my surprise,
the prelude definition of lines:
lines :: String - [String]
lines "" = []
lines s = let (l, s') = break (== '\n') s
in l : case s' of
[] - []