RosettaCodeData/Task/Factorial/Emacs-Lisp/factorial-1.l
2020-02-17 23:21:07 -08:00

11 lines
576 B
Common Lisp

;; Functional (most elegant and best suited to Lisp dialects):
(defun fact (n)
"Return the factorial of integer N, which require to be positive or 0."
;; Elisp won't do any type checking automatically, so
;; good practice would be doing that ourselves:
(if (not (and (integerp n) (>= n 0)))
(error "Function fact (N): Not a natural number or 0: %S" n))
;; But the actual code is very short:
(apply '* (number-sequence 1 n)))
;; (For N = 0, number-sequence returns the empty list, resp. nil,
;; and the * function works with zero arguments, returning 1.)