inits

Ross Paterson ross at soi.city.ac.uk
Mon Apr 10 12:25:53 EDT 2006


On Mon, Apr 10, 2006 at 03:54:09PM +0100, Chris Kuklewicz wrote:
> If the goal is speed, then this definition is running over 10% faster with ghc
> -O2 on my powerbook for (sum $ map length $ inits [1..10000])
> 
> inits' = helper id
>     where helper f [] = (f []):[]
>           helper f (x:xs) = (f []):helper (f.(x:)) xs

I rather like

	inits = map ($ []) . scanl (.) id . map (:)

but this is also competitive:

	inits = map reverse . scanl (flip (:)) []



More information about the Libraries mailing list