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-05 03:39:58 +00:00
|
|
|
|
2020-12-06 04:04:04 +00:00
|
|
|
;; Add docstrings
|
2020-12-05 03:39:58 +00:00
|
|
|
(doc recFact "Calculate the factor of n recursively.")
|
|
|
|
(doc applyFact "Calculate the factor of n iteratively using apply.")
|