inits

Tim Toorop timtoorop at quicknet.nl
Sat Apr 8 19:57:29 EDT 2006


Josef Svenningsson wrote:
> On 4/8/06, Tim Toorop <timtoorop at quicknet.nl> wrote:
>   
>> On the other hand, I think the new spiffy inits is quite clear once you
>> notice why its  map (const xn) $ undefined:xn
>>
>>     
>
> I think it would be even clearer if it were defined like this:
>
>   
>> inits xs = zipWith take [0..] $ map (const xs) xs
>>     
>
> That way you don't have this ugly special case for the first element
> in the list. But there is an even better advantage: it is strict
> again! So it's termination properties are exactly like the Prelude
> one. This version *can* be used as a drop-in replacement of the
> Prelude version.
>
> Cheers,
>
> /Josef
>   
Yes it is very clear ... except for the fact that it doesn't work correctly.
It misses the last element.
now we could write
      inits2' xn = (zipWith take [0..] $ map (const xn) xn ) ++ [xn]
Though I dont know if this really is clearer, and it seems to make the 
garbage collector work slightly more
And which I think does exactly the same as
      inits' xn@(_:_) = zipWith take [0..] $ map (const xn) $ undefined:xn



More information about the Libraries mailing list