Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range data type or generator in Emacs Lisp (elisp)?

Tags:

range

elisp

What is elisp's equivalent for Python's range(start, end, [step])?

like image 982
mcandre Avatar asked Feb 22 '13 00:02

mcandre


People also ask

What is Emacs Lisp used for?

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.

What does #' mean in Emacs Lisp?

function (aka #' ) is used to quote functions, whereas quote (aka ' ) is used to quote data.

Is Emacs functional Lisp?

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.

How do I open Lisp in Emacs?

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.


2 Answers

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)
like image 179
michaelJohn Avatar answered Sep 28 '22 05:09

michaelJohn


(number-sequence FROM &optional TO INC)
like image 33
mcandre Avatar answered Sep 28 '22 04:09

mcandre