J. Garrett Morris wrote:
>    -- the tails function returns each tail of the given list; the
> inits function
>    -- is similar.  By mapping inits over tails, we get all the sublists.
>    where sublists = filter (not . null) . concatMap inits . tails

Nice, but

    concatMap tails . inits

is much better in my opinion, for several reasons:

- inits is expensive (O(n^2)) while tails is cheap (O(n)), so it's
  better to use inits only once.
- the result lists of inits can't be shared (which is essentially the
  reason why it's so expensive); tails shares the common part of the
  result lists.
- finally,  concatMap tails . inits  works nicely with infinite lists,
  with every substring occuring in the result eventually

Btw, if you don't want the empty lists, you can use

    concatMap (init . tails) . tail . inits

Bertram
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to