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. regards, -John
On Mon, Apr 07, 2008 at 03:11:16PM -0400, John Tromp wrote:
An improved version is:
inits l = [] : case l of [] -> [] (x:xs) -> map (x:) inits xs
For information on how to propose this, please see http://www.haskell.org/haskellwiki/Library_submissions Thanks Ian
participants (2)
-
Ian Lynagh -
John Tromp