On Jan 28, 2009, at 6:48 PM, Timothy Pratley wrote:
You didn't do anything wrong, there is a definition of + with no arguments which just returns 0, but no definition of - with no arguments. Similarly (*) is defined as 1, but (/) is undefined. I guess there is no such thing as negative 0, then again there is no such thing as positive zero! Why would you call these functions like this with no arguments?
One reason that the zero argument cases for + and * exist is to support smooth operation with a collection of numbers even when the collection is empty.
For example: Clojure user=> (defn sum-of-elements [coll] (apply + coll)) #'user/sum-of-elements user=> (sum-of-elements [1 2 3]) 6 user=> (sum-of-elements []) 0 user=>Using the additive and multiplicative identity elements (of the set of numbers) for zero-argument cases for + and * seems natural and uncontroversial.
Not allowing the zero argument case for - and / is harder to understand. Here are some likely motivations:
- 0 is both a "left identity element" and a "right identity element" for +, but is only a "right identity element" for -. The corresponding argument holds for 1 and /.
- + and * are commutative and - and / are not - Clojure's behavior in this is consistent with Common Lisp (see http://en.wikipedia.org/wiki/Identity_element)Here's another argument in favor of this choice: consider how each function works with initial sequences of increasing lengths from an overall sequence of numbers. For "+" the value of the n argument addition is the value of an n-1 argument addition plus the nth element.
Let the sequence be [1 2 3 4 5 6 7 8 9] (+) => 0 (+ 1) => 1 = 0 + 1 (+ 1 2) => 3 = 1 + 2 (+ 1 2 3) => 6 = 3 + 3 (+ 1 2 3 4) => 10 = 6 + 4 ... The same progression doesn't work for -: (-) => 0 (hypothetically) (- 1) => -1 = 0 - 1 (works for this case) (- 1 2) => -1 does not equal -1 - 2 --Steve
smime.p7s
Description: S/MIME cryptographic signature