madlisp/mad/fact.mad
2020-12-08 17:00:39 +07:00

12 lines
348 B
Plaintext

;; Functions to calculate factorial
;; Recursive, tail call optimized
(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
(defn applyFact (n) (if (< n 2) 1 (apply * (range 1 (inc n)))))