Skip to content

Instantly share code, notes, and snippets.

@RutledgePaulV
Created October 10, 2016 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RutledgePaulV/73fb66663811565e8a1c3d2e4ae7bdba to your computer and use it in GitHub Desktop.
Save RutledgePaulV/73fb66663811565e8a1c3d2e4ae7bdba to your computer and use it in GitHub Desktop.
Given a tree shaped list, decompose it into all subtrees.
(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