unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* number-sequence
@ 2003-11-20  3:39 Luc Teirlinck
  2003-11-20 10:39 ` number-sequence Kim F. Storm
  2003-11-20 13:27 ` number-sequence Juri Linkov
  0 siblings, 2 replies; 7+ messages in thread
From: Luc Teirlinck @ 2003-11-20  3:39 UTC (permalink / raw)


The function number-sequence in its present form is dangerous.  If the
increment accidentally winds up being 0 or negative and FROM is less
than TO, Emacs will start to construct an infinite list and keep
chewing up CPU and worse, memory.  Just try:

(number-sequence 1 3 -1) or (number-sequence 1 3 0)

If nothing else, if number-sequence can not handle negative increments
correctly, it should signal an error instead of trying to construct
the infinite sequence.  However, I believe there is better.  A zero
increment definitely should throw an error, except in some harmless
cases.  If TO is less than FROM however, negative increments do make
perfect sense and they are trivial to handle.  Hence, it seems stupid
to throw an error instead of doing the logical and expected thing.
The included version of number-sequence does that:

===File ~/number-sequence.el================================
(defun number-sequence (from &optional to inc)
  "Return a sequence of numbers from FROM to TO (both inclusive) as a list.
INC is the increment used between numbers in the sequence.
So, the Nth element of the list is (+ FROM (* N INC)) where N counts from
zero.
If INC is nil, it defaults to 1 (one).
If TO is nil or numerically equal to FROM, return (FROM).
If INC is positive and TO is less than FROM, or INC is negative
and TO is larger than FROM, return nil.
If INC is zero and TO is neither nil nor numerically equal to
FROM, signal an error.
Note that FROM, TO and INC can be integer or float."
  (if (or (not to) (= from to))
      (list from)
    (or inc (setq inc 1))
    (when (zerop inc) (error "The increment can not be zero"))
    (let (seq)
      (if (> inc 0)
	  (while (<= from to)
	    (setq seq (cons from seq)
		  from (+ from inc)))
	(while (>= from to)
	  (setq seq (cons from seq)
		from (+ from inc))))
      (nreverse seq))))
============================================================

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-11-23 16:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-20  3:39 number-sequence Luc Teirlinck
2003-11-20 10:39 ` number-sequence Kim F. Storm
2003-11-20 13:27 ` number-sequence Juri Linkov
2003-11-21  4:37   ` number-sequence Luc Teirlinck
2003-11-21 12:06     ` number-sequence Kim F. Storm
2003-11-21 14:43       ` number-sequence Luc Teirlinck
2003-11-23 16:09   ` number-sequence Luc Teirlinck

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).