What is elisp's equivalent for Python's range(start, end, [step])
?
Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.
function (aka #' ) is used to quote functions, whereas quote (aka ' ) is used to quote data.
Emacs Lisp supports multiple programming styles or paradigms, including functional and object-oriented. Emacs Lisp is not a purely functional programming language since side effects are common. Instead, Emacs Lisp is considered an early functional flavored language.
In a fresh Emacs window, type ESC-x lisp-interaction-mode . That will turn your buffer into a LISP terminal; pressing Ctrl+j will feed the s-expression that your cursor (called "point" in Emacs manuals' jargon) stands right behind to LISP, and will print the result.
number-sequence
is similar to python's range
but its output is quite different.
For example:
(number-sequence 5)
=> (5)
(number-sequence 1 5)
=> (1 2 3 4 5)
(number-sequence 1 5 2)
=> (1 3 5)
I use this function to give me an output like that from python's range
:
(defun py-range (start &optional end step)
(unless end
(setq end start
start 0))
(number-sequence start (1- end) step))
Now everything works as expected:
(py-range 5)
=> (0 1 2 3 4)
(py-range 1 5)
=> (1 2 3 4)
(py-range 1 5 2)
=> (1 3)
(number-sequence FROM &optional TO INC)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With