[Haskell-cafe] Is this haskelly enough?
Dan Weston
westondan at imageworks.com
Wed Jul 18 13:10:57 EDT 2007
> Btw, if you don't want the empty lists, you can use
>
> concatMap (init . tails) . tail . inits
Would it not be more efficient and perspicuous to keep the sublists
definition as is, just interchanging inits and tails?
where sublists = filter (not . null) . concatMap tails . inits
Or am I missing some argument about sublist sharing?
Dan
Bertram Felgenhauer wrote:
> 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