inits

Tim Toorop timtoorop at quicknet.nl
Mon Apr 10 10:36:50 EDT 2006


Christian Maeder wrote:
> Simon Marlow wrote:
>>>> inits xs = [] : (zipWith take [1..] $ map (const xs) xs)
>>
>> I propose to replace inits in Data.List with this one.  
>
> I think such a function should not use arithmetics (comparisons of 
> ints via take)
>
> Christian
>
> for finite lists "reverse . map reverse . tails . reverse" is quite good
> (but it is 80% garbage collecting on my machine)
This seems faster when you want to actually use each value of the inits. 
(say the sum of the map sum on inits)
But it won't work for infinite lists, and will take longer to get say, 
the 6th element of the inits,
depending on the length of the list.
So inits [1..1000000] !! 100   will take longer then inits [1..1000] !! 100
with your version. Since it is going to reverse the whole list.


Tim Toorop.


More information about the Libraries mailing list