What is tail recursion?

前端 未结 28 3581
一个人的身影
一个人的身影 2020-11-21 05:03

Whilst starting to learn lisp, I\'ve come across the term tail-recursive. What does it mean exactly?

28条回答
  •  一向
    一向 (楼主)
    2020-11-21 05:29

    Here is a Common Lisp example that does factorials using tail-recursion. Due to the stack-less nature, one could perform insanely large factorial computations ...

    (defun ! (n &optional (product 1))
        (if (zerop n) product
            (! (1- n) (* product n))))
    

    And then for fun you could try (format nil "~R" (! 25))

提交回复
热议问题