update factorial functions

This commit is contained in:
Pekka Laiho 2020-12-08 17:00:39 +07:00
parent 86cba08088
commit 212c0780fd

View File

@ -1,11 +1,11 @@
;; Functions to calculate factorial ;; Functions to calculate factorial
;; Recursive version, not tail call optimized ;; Recursive, tail call optimized
(defn recFact (n) (if (< n 2) 1 (* n (recFact (dec n))))) (defn factRec (i n a) (if (= i n) (* a i) (factRec (inc i) n (* a i))))
(defn fact (n) (if (< n 2) 1 (factRec 2 n 1)))
;; Return a vector of n factorials
(defn factVec (n) (map fact (range n)))
;; Apply version ;; Apply version
(defn applyFact (n) (if (< n 2) 1 (apply * (range 1 (inc n))))) (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.")