Printing a tree lazily in Newick format
I wish to print a binary tree in Newick format , showing each node's distance to its parent. At the moment I haven't had an issue with the following code, which uses regular recursion, but a tree too deep may produce a stack overflow. (defn tree->newick [tree] (let [{:keys [id children to-parent]} tree dist (double to-parent)] ; to-parent may be a rational (if children (str "(" (tree->newick (first children)) "," (tree->newick (second children)) "):" dist) (str (name id) ":" dist)))) (def example {:id nil :to-parent 0.0 :children [{:id nil :to-parent 0.5 :children [{:id "A" :to-parent 0.3