Created
November 25, 2023 10:13
-
-
Save grzs/a18ed7d00b8445b9796fe966c1a4897b to your computer and use it in GitHub Desktop.
Emacs Lisp range function (and factorial calculation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(cl-defun range (end &optional (start 0) (step 1) reverse) | |
"Returns a list of numbers from START to END incremented by STEP" | |
(let ((i start) (res (list start))) | |
(while (< i end) | |
(setq i (+ i step)) | |
(if reverse | |
(setq res (cons i res)) | |
(setcdr (last res) (cons i nil)))) | |
res)) | |
(defun ! (n) | |
"Factorial calculation for N" | |
(if (and (numberp n) (>= n 0)) | |
(if (eq n 0) (setq n 1)) | |
(error "N has to be a non-negative integer")) | |
(apply '* (range n 1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment