[Haskell] making inits less strict

Don Stewart dons at galois.com
Mon Apr 7 16:24:54 EDT 2008


john.tromp:
> The standard definition of inits:
> 
>   inits []     =  [[]]
>   inits (x:xs) =  [[]] ++ map (x:) (inits xs)
> 
> is unnecessarily strict, evaluating its argument
> before yielding the initial [] of the result.
> An improved version is:
> 
>   inits l = [] : case l of []     -> []
>                            (x:xs) -> map (x:) inits xs
> 
>  This allows one to define for instance
>    nats = map length (inits nats)
>  which loops for the standard definition.
> 

Can you forward this to the libraries at haskell.org list,
and file a proposal to replace the current definition?

    http://haskell.org/haskellwiki/Library_submissions

We noticed this while implementing lazy bytestrings, which had
a similar issue, and fixing it is cheap enough.

-- Don


More information about the Libraries mailing list