And that can be shortened even more.
sub sum{@_&&(pop)+&sum}
| [reply] [d/l] |
What a beautiful catch. ++
On a tangent, as I was looking at that, my thinking got a little meta and I started wondering about it’s mathematical semantics. Its behaviour is wrong, no? Wouldn’t the sum of an empty set be undefined?
Makeshifts last the longest.
| [reply] |
No, in math a sum of 0 numbers is 0, because 0 is the identity for addition. The product of 0 numbers is 1, for similar reasons. And for 0 being the sum of 0 numbers is logical. For instance, the sum of "1, 2, 3, 4" is "10". If I remove 4 from the set, I subtract 4 from 10, leaving a sum of "6" for "1, 2, 3". Remove 3, get a sum of "3" for "1, 2". Remove 2, and get a sum of "1" for the set "1". Remove "1", and you get a sum of "0" for the set "". Also, if you define the sum of a set of 0 elements to be 0, it becomes easy to calculate the sum of the union of two sets: it's the sum of both subsets, and you don't have to special case the case where one of the sets is empty.
| [reply] |