Proposal: Make intersperse lazier

Bas van Dijk v.dijk.bas at gmail.com
Fri Sep 17 08:21:36 EDT 2010


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.

It would be interesting to see if this provides a noticeable
performance benefit. Criterion anyone?

Bas


More information about the Libraries mailing list