Created
September 16, 2014 14:55
-
-
Save westerp/cbc1b0434cc2b52e9b10 to your computer and use it in GitHub Desktop.
Tail recursive flatten in Common Lisp
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
(defun flatten (lst &optional backtrack acc) | |
(cond ((consp lst) (flatten (car lst) (cons (cdr lst) backtrack) acc)) | |
(lst (flatten (car backtrack) (cdr backtrack) (cons lst acc))) | |
(backtrack (flatten (car backtrack) (cdr backtrack) acc)) | |
(t (nreverse acc)))) |
after some more tweaking, here's one without the reversing in the end, although it does the reversing while working:
(defun flatten (lst &optional back acc)
(loop
(cond
((consp lst) (setq back (cons (car lst) back)
lst (cdr lst)))
(back
(if (consp (car back))
(setq lst (cdar back)
back (cons (caar back) (cdr back)))
(setq acc (cons (car back) acc)
back (cdr back))))
(t (return acc)))))
PSETQ is much safer to use than SETQ though, no timing issues.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a direct translation of your code: