Created
March 3, 2021 12:16
-
-
Save dlbas/743c6db3d6406b69c21eb4e64686d15c to your computer and use it in GitHub Desktop.
Illustrates tail-recursion
This file contains hidden or 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 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