[Haskell-cafe] definition of sum
gale at sefer.org
Sun Mar 14 06:02:25 EDT 2010
Daniel Fischer wrote:
>> I'm not sure whether it's a wart or a bug, but I agree that it would be
>> better to have the default sum strict
David Leimbach wrote:
> That would be really inconsistent with the way the rest of the Haskell
> language and libraries works...
> I would propose that... sum be lazy...
Daniel meant "internally strict". All versions of "sum", even the
current one, are strict in the literal sense:
Prelude> sum undefined `seq` 42
*** Exception: Prelude.undefined
As for how it works internally, there is no assumption in
Haskell about that. Generally, it is up to the compiler to
decide in each individual case. Here the correct thing to
do in practice is to be strict, but only a very smart compiler
like GHC with -O2 can figure that out. So the library should
give that hint.
> all these exceptions to make algorithms like "sum" work more
> efficiently when the problem was one of documentation and
> education about the language.
This is not a question of efficiency. The current version of "sum"
is *broken* - it crashes programs.
Haskell education would be far better served by removing bugs
from the standard libraries than by needing to include rules like
"Never use the standard 'sum' function unless you are absolutely
certain that the input to the function will never exceed the stack
size of your runtime."
More information about the Haskell-Cafe