Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why + (or *) act different for than - (or /) with zero arguments?

Tags:

clojure

When you call + with zero arguments

user=> (+)
0

I get 0 because it is invariant element to +. It works similar for *

user=> (*)
1

Why this does not work for - and / ?

user=> (-)
ArityException Wrong number of args (0) passed to: core/-  clojure.lang.AFn.throwArity (AFn.java:429)

user=> (/)
ArityException Wrong number of args (0) passed to: core//  clojure.lang.AFn.throwArity (AFn.java:429)
like image 971
boucekv Avatar asked Dec 06 '25 02:12

boucekv


1 Answers

Note that - and / work differently when they are given a single argument: (- x 0) is different from (- x). The same for (/ x 1) and (/ x). The practical argument for + and * is that when your arguments may not be known beforehand, you can just apply or reduce over a list (possibly empty). The same is not true for division and negation, because you seldom need:

(apply / list)

You at least have one argument:

#(apply / (cons % list))

This is not authoritative, just a guess.

like image 160
coredump Avatar answered Dec 08 '25 15:12

coredump



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!