<br><div class="gmail_quote">On Thu, Mar 17, 2011 at 7:58 PM, Ivan Lazar Miljenovic <span dir="ltr"><<a href="mailto:ivan.miljenovic@gmail.com">ivan.miljenovic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 18 March 2011 06:25, Bas van Dijk <<a href="mailto:v.dijk.bas@gmail.com">v.dijk.bas@gmail.com</a>> wrote:<br>
> I would like to propose making inits and tails less strict:<br>
><br>
> inits :: [a] -> [[a]]<br>
> -inits [] = [[]]<br>
> -inits (x:xs) = [[]] ++ map (x:) (inits xs)<br>
> +inits xs = [] : case xs of<br>
> + [] -> []<br>
> + x:xs -> map (x:) (inits xs)<br>
><br>
> tails :: [a] -> [[a]]<br>
> -tails [] = [[]]<br>
> -tails xxs@(_:xs) = xxs : tails xs<br>
> +tails xxs = xxs : case xxs of<br>
> + [] -> []<br>
> + _:xs -> tails xs<br>
><br>
> Having a lazier inits allows the elegant:<br>
> nats = map length (inits nats)<br>
> which loops for the current definition. This definition was due to John Tromp:<br>
> <a href="http://www.mail-archive.com/haskell@haskell.org/msg21044.html" target="_blank">http://www.mail-archive.com/haskell@haskell.org/msg21044.html</a><br>
<br>
</div></div>I'm not against this proposal, but am just curious: is there any<br>
reason/need for this lazier definition apart from an elegant (but<br>
possibly not actually needed/useful) trick?</blockquote><div> </div><div>In general with the standard library definitions have been made as lazy as they reasonably can. </div><div><br></div><div>Here, clearly, the initial [] doesn't depend on the argument being resolved to a cons cell or nil.</div>
<div><br></div><div>For me this is interesting mostly because of the nearly-comonadic </div><div><br></div><div>extend :: ([a] -> b) -> [a] -> [b]</div><div>extend f = map f . tails</div><div><br></div><div>which I would like to be as productive as possible.</div>
<div><br></div><div>-Edward</div></div>