Proposal: making inits and tails less strict

Edward Kmett ekmett at gmail.com
Fri Mar 18 02:00:16 CET 2011


On Thu, Mar 17, 2011 at 7:58 PM, Ivan Lazar Miljenovic <
ivan.miljenovic at gmail.com> wrote:

> On 18 March 2011 06:25, Bas van Dijk <v.dijk.bas at gmail.com> wrote:
> > I would like to propose making inits and tails less strict:
> >
> >  inits                   :: [a] -> [[a]]
> > -inits []                =  [[]]
> > -inits (x:xs)            =  [[]] ++ map (x:) (inits xs)
> > +inits xs                =  [] : case xs of
> > +                                  []   -> []
> > +                                  x:xs -> map (x:) (inits xs)
> >
> >  tails                   :: [a] -> [[a]]
> > -tails []                =  [[]]
> > -tails xxs@(_:xs)        =  xxs : tails xs
> > +tails xxs               =  xxs : case xxs of
> > +                                   []   -> []
> > +                                   _:xs -> tails xs
> >
> > Having a lazier inits allows the elegant:
> > nats = map length (inits nats)
> > which loops for the current definition. This definition was due to John
> Tromp:
> > http://www.mail-archive.com/haskell@haskell.org/msg21044.html
>
> I'm not against this proposal, but am just curious: is there any
> reason/need for this lazier definition apart from an elegant (but
> possibly not actually needed/useful) trick?


In general with the standard library definitions have been made as lazy as
they reasonably can.

Here, clearly, the initial [] doesn't depend on the argument being resolved
to a cons cell or nil.

For me this is interesting mostly because of the nearly-comonadic

extend :: ([a] -> b) -> [a] -> [b]
extend f = map f . tails

which I would like to be as productive as possible.

-Edward
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20110317/952067e1/attachment.htm>


More information about the Libraries mailing list