Skip to content

Instantly share code, notes, and snippets.

@rsslldnphy
Created January 20, 2015 22:16
Show Gist options
  • Save rsslldnphy/86fd186d90068fb39729 to your computer and use it in GitHub Desktop.
Save rsslldnphy/86fd186d90068fb39729 to your computer and use it in GitHub Desktop.
Lazy factorials in Clojure
(ns factorial)
(defn factorials
"Generates an infinite sequence of factorials."
([]
(map first (factorials 1 2)))
([product counter]
(cons [product counter]
(lazy-seq (factorials (* product counter) (inc counter))))))
(defn factorial
"Calculate the nth factorial."
[n]
(nth (factorials) n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment