Skip to content

Instantly share code, notes, and snippets.

@grzs
Created November 25, 2023 10:13
Show Gist options
  • Save grzs/a18ed7d00b8445b9796fe966c1a4897b to your computer and use it in GitHub Desktop.
Save grzs/a18ed7d00b8445b9796fe966c1a4897b to your computer and use it in GitHub Desktop.
Emacs Lisp range function (and factorial calculation)
(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