inits

Christian Maeder maeder at tzi.de
Tue Apr 11 06:39:45 EDT 2006


>> Ross Paterson wrote:
>>> On Mon, Apr 10, 2006 at 03:54:09PM +0100, Chris Kuklewicz wrote:
>>>>
>>>> inits' = helper id
>>>>     where helper f [] = (f []):[]
>>>>           helper f (x:xs) = (f []):helper (f.(x:)) xs
>>>
>>> 	inits = map reverse . scanl (flip (:)) []

> It's not really surprising: the nested composition built by helper is
> essentially a list, which is traversed by ($ []).  If scanl were defined
> using build it might run a tiny bit faster.

an alternative helper (are expanding scanl) would be:

    inits = helper []
      where helper l xs = reverse l : case xs of
                        [] -> []
                        x : r -> helper (x : l) r

C.


More information about the Libraries mailing list