Emacs Lisp: The iota function for number ranges in Emacs Lisp
This is the iota function that I had to make today. I have shamelessly borrowed the description from Guile reference manual on iota function from SRFI-1 and tried to make it compliant. I guess this could be done more elegantly. For me is important that it gives me the same result as intended.
Thus (iota 10)
would evaluate to (0 1 2 3 4 5 6 7 8 9)
.
(iota 10 1)
would evaluate to (1 2 3 4 5 6 7 8 9 10)
.
And (iota 10 1 2)
would evaluate to (1 3 5 7 9 11 13 15 17 19)
.
The behavior is the same as in SRFI-1.
By using this iota
function I can generate the number range and then iterate over the list with the dolist' function. I could then iterate with
(dolist (num (iota 10 20)))` from the number 20 to number 29.
defun iota (count &optional start step)
("Return a list containing COUNT numbers, starting from START
and adding STEP each time. The default START is 0, the default
STEP is 1"
let* ((start (if start start 0))
(step (if step step 1))
(last (+ start count))
(0)
(counter list '())
(elt start))
(< counter count)
(while (push elt list)
(setq elt (+ elt step))
(setq counter (1+ counter)))
(reverse list))) (
iota is often used in Scheme programming language. Guile and other scheme implementations already have it, and I have got used to it. There probably exists in Emacs lisp equivalent function or somebody else already made it, but I do not know anything about it. I had to make it myself.
Leave Your Comment or Contact GNU.Support
Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know