Skip to content

Instantly share code, notes, and snippets.

@dyba
Created January 21, 2012 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyba/1650384 to your computer and use it in GitHub Desktop.
Save dyba/1650384 to your computer and use it in GitHub Desktop.
Clojure Koans - Factorial
;; Earlier attempt
(defn factorial [n]
(loop [n n
result n]
(if (<= n 1)
1
result)
(recur (dec n) (* result (dec n)))))
;; Working so far
(defn factorial [n]
(if (<= n 1)
1
1))
;; Causes StackOverflow
(defn factorial [n]
(if (<= n 1)
1
(* n (factorial (dec n)))))
;; Works!
(defn factorial [n]
(loop [n n
result n]
(if (<= n 1)
result
(recur (dec n) (* result (dec n))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment