inits

Chris Kuklewicz haskell at list.mightyreason.com
Sat Apr 8 14:45:53 EDT 2006


Tim Toorop wrote:
> Chris Kuklewicz wrote:
>> Aaron Denney wrote:
>>  
>>> On 2006-04-08, Nils Anders Danielsson <nad at cs.chalmers.se> wrote:
>>>    
>>>> On Fri, 07 Apr 2006, "Spencer Janssen" <spencerjanssen at gmail.com>
>>>> wrote:
>>>>
>>>>      
>>>>>> inits xs = [] : (zipWith take [1..] $ map (const xs) xs)
>>>>>>           
>>>>> As this version performs much better and will work as a drop in
>>>>> replacement, I suggest that it be included in the hierarchical
>>>>> libraries.
>>>>>         
>>>> It is not a drop in replacement. The original inits is strict, this
>>>> one isn't.
>>>>
>>>> The specification of inits (from the Haskell 98 report):
>>>>
>>>>   inits                   :: [a] -> [[a]]
>>>>   inits []                =  [[]]
>>>>   inits (x:xs)            =  [[]] ++ map (x:) (inits xs)
>>>>       
>>> Is that a property many programs depend on?  I'd actually call that a
>>> bug of the original.
>>>
>>>     
>>
>> It may break some things:
>>
>> head (Data.List.inits undefined) is an error
>>
>> head (New.Spiffy.inits undefined) is []
>>
>>   
> Is the head of the inits of undefined really an error?
> Since the head of inits []  is also []  ...
> But if you really want that undefined to produce an error.. you could
> just :
>        inits' xn@(_:_) = zipWith take [0..] $ map (const xn) $ undefined:xn
>        inits' _        = undefined
> 
> 

Exactly.  Now inits' *is* a drop in replacement for inits.

-- 
Chris


More information about the Libraries mailing list