Created
October 10, 2016 04:52
-
-
Save RutledgePaulV/73fb66663811565e8a1c3d2e4ae7bdba to your computer and use it in GitHub Desktop.
Given a tree shaped list, decompose it into all subtrees.
This file contains 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 subtrees [tree] | |
(if (or (nil? tree) (empty? tree)) | |
'() | |
(let [parent (first tree) children (take-while list? (rest tree))] | |
(if (not-empty children) | |
(concat | |
(map #(list parent %) children) | |
(mapcat subtrees children) | |
(subtrees (drop (inc (count children)) tree))) | |
(subtrees (next tree)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment