logo
  • 教程列表
  • SO官方文档
  • 递归
    • 递归模板 1 单条件单尾递归
    • 递归打印列表的元素
    • 计算整数的阶乘
    • 递归模板 2 多条件
    • 计算第 n 个斐波纳契数
  1. StackOverflow 文档
  2. common-lisp 教程
  3. 递归
  4. 计算整数的阶乘

计算整数的阶乘

Created: November-22, 2018

实现递归函数的一种简单算法是阶乘。

;;Compute the factorial for any n >= 0. Precondition: n >= 0, n is an integer.
(defun factorial (n)
    (cond
        ((= n 0) 1) ;; Special case, 0! = 1
        ((= n 1) 1) ;; Base case, 1! = 1
        (t
            ;; Recursive case
            ;; Multiply n by the factorial of n - 1.
            (* n (factorial (- n 1)))
        )
    )
)
  • 递归模板 2 多条件
  • 递归打印列表的元素

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 关于我们
  • 免责声明