Skip to content

Instantly share code, notes, and snippets.

@hyotang666
Created March 18, 2015 05:51
Show Gist options
  • Save hyotang666/2d6487a05bb39615ed4d to your computer and use it in GitHub Desktop.
Save hyotang666/2d6487a05bb39615ed4d to your computer and use it in GitHub Desktop.
When tree is deep nested, on lisp's flatten gets stack over flow.
(defun flatten(tree)
(let((result nil)
(head(car tree))
(tail(cdr tree)))
(tagbody top
(if(and(null tail)(null head))
(return-from flatten (nreverse result))
(if(consp head)
(progn
(push(cdr head)tail)
(setf head(car head))
(go top))
(progn
(unless(null head)
(push head result))
(setf head(pop tail))
(go top)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment