Proposal: making inits and tails less strict

wren ng thornton wren at freegeek.org
Thu Mar 17 23:02:23 CET 2011


On 3/17/11 3:25 PM, Bas van Dijk 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

+1.

-- 
Live well,
~wren



More information about the Libraries mailing list