[Haskell-cafe] definition of sum

David Leimbach leimy2k at gmail.com
Thu Mar 11 10:21:01 EST 2010


>
>
> > > However, with optimisations turned on... GHC knows that sum is
> > > actually strict
> >
> > GHC does that when optimizations are turned on, but that behavior
> > is not required by the Haskell standard. So there is no guarantee
> > that any given compiler will produce usable output if you use foldl
> > instead of foldl' for sum.
> >
> > In GHCi sum is broken, because optimizations are not in
> > effect there. You have to define your own version of sum
> > using foldl' for every GHCi session (or put it in your .ghci file).
> >
> > So it's a trade-off between a slight convenience in a bizarre
> > corner case and general usability. I agree with Don that this is
> > a bug in the Haskell 98 standard.
>
> 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 (and provide lazysum for the cases
> where it's useful).
>

That would be really inconsistent with the way the rest of the Haskell
language and libraries works.

Note that foldl' has a ' to indicate that it's not the same as foldl
exactly.  I would propose that sum' exist as well as sum, and that sum be
lazy.

Part of what's interesting about Haskell is the default non-strict
evaluation of the language, and even if it's hard for newcomers to get used
to it, they're better off having a consistent space to work in rather than
all these exceptions to make algorithms like "sum" work more efficiently
when the problem was one of documentation and education about the language.

Of course if you want a strict functional language there's plenty of those
out there.

Dave


>
> >
> > Regards,
> > Yitz
>
> Cheers,
> Daniel
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100311/ebd81bb5/attachment.html


More information about the Haskell-Cafe mailing list