Created
September 14, 2011 04:46
-
-
Save ghoseb/1215871 to your computer and use it in GitHub Desktop.
Flatten a nested collection using Tail Recursion.
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
(defn my-flatten | |
"Flatten a nested collection using Tail Recursion." | |
[coll] | |
(letfn [(step [[fst & more :as coll] res] | |
(if (seq coll) | |
(if (coll? fst) | |
(recur (concat fst more) res) | |
(recur more (cons fst res))) | |
(reverse res)))] | |
(step coll nil))) | |
;; (my-flatten [1 [2 [3 [4 [5 [6 [7] 8] 9] 10] 11] 12] 13]) | |
;; => (1 2 3 4 5 6 7 8 9 10 11 12 13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good :-) Similar to mine: