Re: [Haskell-cafe] Two-iteration optimisation (was: GHC Predictability)

2008-05-19 Thread Spencer Janssen
On Wed, May 14, 2008 at 06:08:28PM +0100, Paul Johnson wrote: We've had a big debate over mean xs = foldl' (+) 0 xs / fromIntegral (length xs) For anyone who didn't follow it, the problem is that mean needs to traverse its argument twice, so the entire list has to be held in memory.

Re: [Haskell-cafe] Two-iteration optimisation (was: GHC Predictability)

2008-05-15 Thread Yitzchak Gale
Don Stewart wrote: You'd want a general fusion framework for this... Stream fusion... at least does this for zips... but for an arbitrary 'f' instead of zip, seems harder. And of course, you wouldn't want that: f xs = xs : map expensiveCalculation xs Please don't fuse those two loops into

Re: [Haskell-cafe] Two-iteration optimisation (was: GHC Predictability)

2008-05-15 Thread Andrew Coppin
Yitzchak Gale wrote: And of course, you wouldn't want that: f xs = xs : map expensiveCalculation xs Please don't fuse those two loops into one. ...doesn't type check. Did you mean (++)? In the case of mean, the outer function in question is /, and that is a good candidate for fusion

Re: [Haskell-cafe] Two-iteration optimisation (was: GHC Predictability)

2008-05-15 Thread Luke Palmer
On Thu, May 15, 2008 at 8:45 PM, Andrew Coppin [EMAIL PROTECTED] wrote: Yitzchak Gale wrote: And of course, you wouldn't want that: f xs = xs : map expensiveCalculation xs Please don't fuse those two loops into one. ...doesn't type check. Did you mean (++)? Hmm? While the name might

[Haskell-cafe] Two-iteration optimisation (was: GHC Predictability)

2008-05-14 Thread Paul Johnson
We've had a big debate over mean xs = foldl' (+) 0 xs / fromIntegral (length xs) For anyone who didn't follow it, the problem is that mean needs to traverse its argument twice, so the entire list has to be held in memory. So if xs = [1..10] then mean xs uses all your memory, but

Re: [Haskell-cafe] Two-iteration optimisation (was: GHC Predictability)

2008-05-14 Thread Albert Y. C. Lai
Paul Johnson wrote: The solution is for the programmer to rewrite mean to accumulate a pair containing the running total and count together, then do the division. This makes me wonder: could there be a compiler optimisation rule for this, collapsing two iterations over a list into one. Do

Re: [Haskell-cafe] Two-iteration optimisation (was: GHC Predictability)

2008-05-14 Thread Andrew Coppin
Albert Y. C. Lai wrote: If you worry that the sum thread and the length thread are not synchronized and therefore there is still no bound on the list prefix kept in memory, I'm sure you can improve it by one of the chunking strategies. I'm more worried that data now has to go through tiny