Proposal: Make intersperse lazier
Christian Maeder
Christian.Maeder at dfki.de
Fri Sep 17 08:45:12 EDT 2010
Am 17.09.2010 14:21, schrieb Bas van Dijk:
> On Fri, Sep 17, 2010 at 10:48 AM, Christian Maeder
> <Christian.Maeder at dfki.de> wrote:
>> Am 16.09.2010 16:39, schrieb Daniel Fischer:
>>> The proposed implementation,
>>>
>>> intersperse :: a -> [a] -> [a]
>>> intersperse _ [] = []
>>> intersperse sep (x:xs) = x : go xs
>>> where
>>> go [] = []
>>> go (y:ys) = sep : y : go ys
>> ...
>> Therefore I proposed the following implementation:
>>
>> intersperse :: a -> [a] -> [a]
>> intersperse s l = case l of
>> [] -> l
>> x : r -> x : if null r then r else
>> s : intersperse s r
>
> One additional benefit about the original proposed implementation is
> that it applies the static argument transformation: the 'sep' argument
> is brought into scope of the worker function 'go' which then doesn't
> need to pass it to each recursion as in the original implementation.
This is the point. Taking the simpler prepend function, do we did not write:
prepend :: a -> [a] -> [a]
prepend sep = go where
go [] = []
go (x : xs) = sep : x : go xs
for more efficiency (instead of the direct recursion or a concatMap or
folding solution)?
Cheers Christian
> It would be interesting to see if this provides a noticeable
> performance benefit. Criterion anyone?
>
> Bas
More information about the Libraries
mailing list