[Haskell-beginners] Average of numeric list

Daniel Fischer daniel.is.fischer at googlemail.com
Tue Apr 5 11:28:28 CEST 2011


On Tuesday 05 April 2011 10:58:36, David Virebayre wrote:
> 2011/4/4 Daniel Fischer <daniel.is.fischer at googlemail.com>
> 
> > Note however, that that's not a particularly efficient way to
> > calculate the mean, since the compiler isn't smart enough to
> > transform it into a loop traversing the list once (so allowing it to
> > be garbage collected as it is consumed and the computation to run in
> > constant space) and keeping the accumulators evaluated.
> 
> I've always wondered, is it imaginable that a compiler would be able to
> do such transforms (assuming very intelligent people (i.e. not me) 
> have plenty of time to work on it), or is it way too complicated ?

It is imaginable, within some limits.
I'm pretty sure one can't write an algorithm that spots *all* occurrences 
of code where such a rewrite would be good, but one can write algorithms to 
spot specific patterns.

Spotting more patterns would require more code in the compiler, and it 
would increase compile times.

So one has to decide whether the cost is worth the benefit.
Apparently, so far it has not been considered worthwhile.



More information about the Beginners mailing list