Proposal: Make intersperse lazier
Christian Maeder
Christian.Maeder at dfki.de
Fri Sep 24 11:55:15 EDT 2010
Am 24.09.2010 16:53, schrieb Daniel Fischer:
[...]
> However, I benchmarked some more today, and the results suggest that
>
> intersperse :: a -> [a] -> [a]
> intersperse _ [] = []
> intersperse s (x:xs) = x : foo s xs
>
> foo :: a -> [a] -> [a]
> foo s (x:xs) = s : x : foo s xs
> foo _ [] = []
>
> is a little faster than the SATed isgo (about 4-5% in the benchmark).
> Compiled with optimisations, it makes no difference whether foo (of course
> we need a better name) is a top-level function or local to intersperse, but
> without optimisations, a top-level function is better.
> Also nice is that that gives identical core for all optimisation levels
> (0,1,2) [apart from the strictness/unfolding information, which is absent
> with -O0].
> (Tested with 6.12.3 and HEAD.)
>
> So far, the above is IMO the best implementation.
> A good name for foo remains to be found.
prependToAll
> I don't like 'prepend' because prepend suggests only putting something
> before a list (with the given type, it should be (:)) and not changing
> anything inside.
>
> If it's not to be exported from Data.List (and I don't consider it useful
> enough to be), maybe intersperseLoop wouldn't be too daft.
I consider it useful, because the "natural" implementation:
prependToAll s = foldr (\ x r -> s : x : r) []
seems to leak without optimization.
Cheers Christian
>
> Cheers,
> Daniel
>
More information about the Libraries
mailing list