in reply to List::Util::sum() and empty lists...

Mathematically, it makes much more sense to return 0.

Yes but logically (and mathematically too), the sum of an undefined list ought to be an undefined value, don't you think?

sum() returning undef or 0 helps discern whether the argument list passed to it was an empty one or not.

$ perl -MList::Util=sum -Wle 'printf "sum returns [%s]", sum()' Use of uninitialized value in printf at -e line 1. sum returns [] $ perl -MList::Util=sum -Wle 'printf "sum returns [%s]", sum(undef) Use of uninitialized value in subroutine entry at -e line 1. sum returns [0]


perl -e '$,=$",$_=(split/\W/,$^X)[y[eval]]]+--$_],print+just,another,split,hack'er

Replies are listed 'Best First'.
Re^2: List::Util::sum() and empty lists...
by kaif (Friar) on Dec 04, 2006 at 11:39 UTC
    the sum of an undefined list ought to be an undefined value

    The empty list is not undefined; it's very clearly defined: it's empty.

    Speaking as a mathematician, of course the sum of an empty list is 0, but this is hardly the first nor the most important time I've encountered software that doesn't follow mathematical conventions. Regardless, the sum 0, @list fix is completely fine.

    A reply falls below the community's threshold of quality. You may see it by logging in.