Skip to content

Instantly share code, notes, and snippets.

@dlbas
Created March 3, 2021 12:16
Show Gist options
  • Save dlbas/743c6db3d6406b69c21eb4e64686d15c to your computer and use it in GitHub Desktop.
Save dlbas/743c6db3d6406b69c21eb4e64686d15c to your computer and use it in GitHub Desktop.
Illustrates tail-recursion
(defun add (n)
(if (= n 0)
n
(+ n (add (- n 1)))
)
)
(defun add_optimized (n)
(defun iter (x acc)
(if (= x 0)
acc
(iter (- x 1) (+ acc x))
)
)
(iter n 0)
)
(format t "~a%" (add 3))
(format t "~a%" (add_optimized 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment