madlisp/mad/fact.mad

12 lines
366 B
Plaintext
Raw Normal View History

2020-06-06 09:25:49 +00:00
;; Functions to calculate factorial
2020-06-08 02:55:29 +00:00
;; Recursive version, not tail call optimized
2020-12-06 04:04:04 +00:00
(defn recFact (n) (if (< n 2) 1 (* n (recFact (dec n)))))
2020-06-06 09:25:49 +00:00
;; Apply version
2020-12-06 04:04:04 +00:00
(defn applyFact (n) (if (< n 2) 1 (apply * (range 1 (inc n)))))
2020-12-06 04:04:04 +00:00
;; Add docstrings
(doc recFact "Calculate the factor of n recursively.")
(doc applyFact "Calculate the factor of n iteratively using apply.")