madlisp/mad/fact.mad
2020-12-06 11:04:04 +07:00

12 lines
366 B
Plaintext

;; Functions to calculate factorial
;; Recursive version, not tail call optimized
(defn recFact (n) (if (< n 2) 1 (* n (recFact (dec n)))))
;; Apply version
(defn applyFact (n) (if (< n 2) 1 (apply * (range 1 (inc n)))))
;; Add docstrings
(doc recFact "Calculate the factor of n recursively.")
(doc applyFact "Calculate the factor of n iteratively using apply.")