0 Replies - 1494 Views - Last Post: 13 December 2009 - 09:17 AM

#1 YamNad   User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 120
  • Joined: 11-July 09

[Lisp] Compute factorial in Clojure

Posted 13 December 2009 - 09:17 AM

Description: example usage: user=> (factorial 10) 3628800This function computes the factorial of n in Clojure.
(defn factorial 
"Computes the factorial of n."
[n]
 (if (< n 0) (throw (Exception. "Cannot find the factorial of a negative number.")))
  (if (zero? 0) 1
    (* n (factorial (dec n)))))

Is This A Good Question/Topic? 0
  • +

Replies To: [Lisp] Compute factorial in Clojure

#2 YamNad   User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 120
  • Joined: 11-July 09

Re: [Lisp] Compute factorial in Clojure

Posted 13 December 2009 - 09:17 AM

Description: example usage: user=> (factorial 10) 3628800This function computes the factorial of n in Clojure.
(defn factorial 
"Computes the factorial of n."
[n]
 (if (< n 0) (throw (Exception. "Cannot find the factorial of a negative number.")))
  (if (zero? n) 1
    (* n (factorial (dec n)))))
    
(def factorial (memoize factorial))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1