Thursday 14 May 2009

Factorial function in clojure

This example is probably the most elegant factorial function that I've seen to date, in the Clojure JVM language.

(defn factorial [n]
(reduce * (range 1 (inc n))))



There's a longer discussion on the google group too. Other languages that support "reduce" such has Python and Lisp clones probably might achieve the same succint syntax.

No comments: