use inc/dec for factorial

This commit is contained in:
Pekka Laiho 2020-10-17 14:18:16 +07:00
parent 475ddf5a20
commit 94a69e7b76

View File

@ -1,7 +1,7 @@
;; Functions to calculate factorial ;; Functions to calculate factorial
;; Recursive version, not tail call optimized ;; Recursive version, not tail call optimized
(def recFact (fn (n) (if (< n 2) 1 (* n (recFact (- n 1)))))) (def recFact (fn (n) (if (< n 2) 1 (* n (recFact (dec n))))))
;; Apply version ;; Apply version
(def applyFact (fn (n) (if (< n 2) 1 (apply * (range 1 (+ n 1)))))) (def applyFact (fn (n) (if (< n 2) 1 (apply * (range 1 (inc n))))))