Last active
December 18, 2015 07:19
-
-
Save knjname/5745607 to your computer and use it in GitHub Desktop.
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
| ;; simple | |
| (defn factorial [n] | |
| (if (zero? n) | |
| 1 | |
| (* n (factorial (dec n))))) | |
| ;; tail recursion | |
| (defn factorial | |
| ([n] (factorial n 1)) | |
| ([n sum] | |
| (if (zero? n) | |
| sum | |
| (recur (dec n) (* sum n))))) | |
| (defn foldl [init folding-fn seq] | |
| (if (empty? seq) | |
| init | |
| (recur (folding-fn init (first seq)) folding-fn (rest seq)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment