Created
October 1, 2021 08:30
-
-
Save k3ut0i/c46e1b7c9d043553938861038c59128d to your computer and use it in GitHub Desktop.
tail call comparision
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 flip-adjacent (lst) | |
(cond | |
((null lst) lst) | |
((null (cdr lst)) lst) | |
(t (destructuring-bind (a b . r) lst | |
(cons b (cons a (flip-adjacent r))))))) | |
(defun flip-adjacent-2 (lst acc) | |
(cond | |
((null lst) (reverse acc)) | |
((null (cdr lst)) (reverse (cons (car lst) acc))) | |
(t (flip-adjacent-2 (cddr lst) (cons (car lst) | |
(cons (cadr lst) acc)))))) | |
;; The first implementation is almost as fast as the second for small inputs | |
;; I the only problem seems to be that stack is quickly run out of mem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment