What's wrong with this Clojure function? [duplicate]

Solution 1:

You've got an extra parenthesis around apply:

(def fact (fn [n]
              (apply * (drop 1 (range n)))))

Solution 2:

There's extra pair of parentheses- when you call for example (fact 3), result will be (2) (not list, but call of function) and 2 isn't function, so it throws error.

When you remove these parentheses, like this:

(def fact (fn [n]
    (apply * (drop 1 (range n)))))

, call (fact 3) returns 2- but is this result correct? If you want to return factorial for given number, you have to use range correctly:

(defn fact [n]
  (->> (range 1 (inc n))
       (apply *)))

(->> is thread-last macro)

Example:

(fact 3)
=> 6