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

  • 關於本站
  • 免責聲明