Skip to content

Instantly share code, notes, and snippets.

@k3ut0i
Created October 1, 2021 08:30
Show Gist options
  • Save k3ut0i/c46e1b7c9d043553938861038c59128d to your computer and use it in GitHub Desktop.
Save k3ut0i/c46e1b7c9d043553938861038c59128d to your computer and use it in GitHub Desktop.
tail call comparision
(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