mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-25 14:44:27 +00:00
12 lines
348 B
Plaintext
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)))))
|