[Haskell] specification of sum

Cale Gibbard cgibbard at gmail.com
Wed Nov 2 12:23:34 EST 2005


On 02/11/05, Scherrer, Chad <Chad.Scherrer at pnl.gov> wrote:
>
>
> > Surely not... sum is defined by Haskell 98 as:
> >
> >  sum = foldl (+) 0
> >
> > and this is exactly what GHC provides.  Furthermore we have
> > specialised strict versions for Int and Integer.
> >
>
> I'd been using ghci for testing along the way and getting terrible
> results; does the specialization only apply to ghc per se?
>
> >
> > Cheers,
> >       Simon
> >
>
> Also, Cale, I was thinking about your comment about formal power series,
> and I don't see that (+) should not be strict in this case. In
> particular, if they are represented as infinite lists, I agree that
> zipWith (+) works just fine, though it is strict but lazy.
>
> Here is the strictness:
> zipWith (+) undefined [1,2,3] == undefined
> zipWith (+) [1,2,3] undefined == undefined
>
> And here is the laziness:
> head $ zipWith (+) (1:undefined) (2:undefined) == 3
>
> Or am I missing something?
>
> -Chad
>

Oh, well that's true, but I suppose that by strictness I mean that it
doesn't completely force the evaluation of its arguments to normal
form, as shown in the second example. If it was more strict, that
would also be undefined, as would be the sum of any two infinite
lists. I suppose it's somewhat of a matter of perspective: are you
passing it a list, or a cons cell? In the case of power series, I was
thinking of the whole power series as the parameter, not just the
outermost data constructor.

 - Cale


More information about the Haskell mailing list