How to make a Clojure function take a variable number of parameters?

In general, non-commutative case you can use apply:

(defn sum [& args] (apply + args))

Since addition is commutative, something like this should work too:

(defn sum [& args] (reduce + args))

& causes args to be bound to the remainder of the argument list (in this case the whole list, as there’s nothing to the left of &).

Obviously defining sum like that doesn’t make sense, since instead of:

(sum a b c d e ...)

you can just write:

(+ a b c d e ....)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)