Last active
February 4, 2025 14:49
-
-
Save Denommus/7533834 to your computer and use it in GitHub Desktop.
A simple tail-recursive factorial example
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 factorial (number) | |
| (labels ((factorial-helper (x accumulator) | |
| (if (zerop x) | |
| accumulator | |
| (factorial-helper (- x 1) (* accumulator x))))) | |
| (factorial-helper number 1))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cooleynal
labelsis how you declare local functions in Common Lisp. Remember that Common Lisp is a Lisp-2, so function names and variable names live in different namespaces, usingletwould create a binding in the variable namespace instead of creating it in the function namespace, which would require me to do(funcall factorial-helper number 1)