Reply to comment

Clojure version of iterative factorial

Clojure doesn’t have tail recursion due to limitations with the JVM so you have to explicitly use ‘recur’:

(defn factorial [n]
  (fact-iter 1 1 n))
(defn fact-iter [product counter max-count]
  (if (> counter max-count)
    product
    (recur (* counter product)
                (+ counter 1)
                max-count)))

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <hr> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options