madlisp/mad/fact.mad

12 lines
348 B
Plaintext
Raw Normal View History

2020-06-06 09:25:49 +00:00
;; Functions to calculate factorial
2020-12-08 10:00:39 +00:00
;; 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)))
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)))))