Created
January 20, 2015 22:16
-
-
Save rsslldnphy/86fd186d90068fb39729 to your computer and use it in GitHub Desktop.
Lazy factorials in Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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