2

Possible Duplicate:
Why clojure's vector function definition is so verbose?

To clarify my question, let's take the definition of list* as example.

(defn list*
  "Creates a new list containing the items prepended to the rest, the
  last of which will be treated as a sequence."
  {:added "1.0"
   :static true}
  ([args] (seq args))
  ([a args] (cons a args))
  ([a b args] (cons a (cons b args)))
  ([a b c args] (cons a (cons b (cons c args))))
  ([a b c d & more]
    (cons a (cons b (cons c (cons d (spread more)))))))

My question is, why not define list* like this:

(defn list*
  "Creates a new list containing the items prepended to the rest, the
  last of which will be treated as a sequence."
  {:added "1.0"
   :static true}
  ([args] (seq args))
  ([a & more] (cons a (spread more))))
Community
  • 1
  • 1
xiaowl
  • 5,177
  • 3
  • 27
  • 28
  • 2
    Check out: http://stackoverflow.com/questions/11570782/why-clojures-vector-function-definition-is-so-verbose?rq=1 – Ankur Aug 10 '12 at 06:02

1 Answers1

5

The main reason is performance.

It can help the Clojure compiler to create more optimised code if you explicitly provide a few extra versions with small arities (especially since the small arity cases are the most commonly used)

This is particularly true if the overloaded versions avoid the need to process a variable length argument list (& more), since processing a variable-length argument list results in more overhead than normal positional parameters.

mikera
  • 105,238
  • 25
  • 256
  • 415