[Haskell-cafe] Is this haskelly enough?
Bertram Felgenhauer
bertram.felgenhauer at googlemail.com
Wed Jul 18 11:31:07 EDT 2007
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
More information about the Haskell-Cafe
mailing list