Skip to content

Instantly share code, notes, and snippets.

@jhannes
Created April 29, 2011 10:22
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 jhannes/948150 to your computer and use it in GitHub Desktop.
Save jhannes/948150 to your computer and use it in GitHub Desktop.
Tree pretty printer in clojure
(defn print-subtree [subtree prefix]
(if (empty? subtree)
""
(apply str prefix (first subtree) "\n"
(map #(print-subtree %1 (str "\t" prefix)) (rest subtree)))))
(defn print-tree [tree]
(print-subtree tree ""))
(deftest print-empty-tree
(is (= (print-tree
'("parent"
("child1")
("child2"
("grand1")
("grand2"))))
"parent\n\tchild1\n\tchild2\n\t\tgrand1\n\t\tgrand2\n")))
@fatso83
Copy link

fatso83 commented Apr 29, 2011

Ganske lik mitt forsøk på å skrive lisp i Java, men det ser litt mer elegant ut :-)
https://github.com/fatso83/Code-Snippets/blob/master/Functional%20Dojo/src/no/kopseng/dojo/functional/Node.java (tabbedString3 versjonen)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment