[Haskell-cafe] Is this haskelly enough?

Dan Weston westondan at imageworks.com
Tue Jul 17 19:00:01 EDT 2007

Bjorn Bringert wrote:
> import Data.List
> maxsubarrays xs = maximumBy (compare `on` sum)
>   [zs | ys <- inits xs, zs <- tails ys]

I love this solution: simple, understandable, elegant.

As a nit, I might take out the ys and zs names, which obscure the fact 
that there is a hidden symmetry in the problem:

maxsubarrays xs = pickBest  (return xs >>= inits >>= tails)
  where pickBest = maximumBy (compare `on` sum)
   -- NOTE: Since pickBest is invariant under permutation of its arg,
   --       the order of inits and tails above may be reversed.

Dan Weston

More information about the Haskell-Cafe mailing list