[Haskell-cafe] Reasoning about performance

Richard A. O'Keefe ok at cs.otago.ac.nz
Wed Sep 4 03:48:01 CEST 2013


allPairs2 can be simplified using a trick I wouldn't dare use in
any language but Haskell:

triangle4 xs = fused undefined [] xs
  where fused x (y:ys) zs     = (x,y) : fused x ys zs
        fused _ []     (z:zs) = fused z zs zs
        fused _ []     []     = []

I submit this just for grins; it happens to be a touch faster but
not enough to bother about.

While the result _isn't_ all possible pairs of elements, making
the allPairs name a bit dodgy, it _does_ have O(|xs|**2) of them...

I would be surprised if the relative performance of two
approaches in ghci were always the same as their relative
performance in ghc.





More information about the Haskell-Cafe mailing list