madlisp/mad/fact.mad

12 lines
377 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-10-17 07:18:16 +00:00
(def recFact (fn (n) (if (< n 2) 1 (* n (recFact (dec n))))))
2020-06-06 09:25:49 +00:00
;; Apply version
2020-10-17 07:18:16 +00:00
(def applyFact (fn (n) (if (< n 2) 1 (apply * (range 1 (inc n))))))
;; Add documentation
(doc recFact "Calculate the factor of n recursively.")
(doc applyFact "Calculate the factor of n iteratively using apply.")