Skip to content

Instantly share code, notes, and snippets.

@Denommus
Last active February 4, 2025 14:49
Show Gist options
  • Select an option

  • Save Denommus/7533834 to your computer and use it in GitHub Desktop.

Select an option

Save Denommus/7533834 to your computer and use it in GitHub Desktop.
A simple tail-recursive factorial example
(defun factorial (number)
(labels ((factorial-helper (x accumulator)
(if (zerop x)
accumulator
(factorial-helper (- x 1) (* accumulator x)))))
(factorial-helper number 1)))
@Denommus

Copy link
Copy Markdown
Author

@cooleynal labels is 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, using let would 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment