unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Luc Teirlinck <teirllm@dms.auburn.edu>
Cc: emacs-devel@gnu.org
Subject: Re: number-sequence
Date: Thu, 20 Nov 2003 22:37:18 -0600 (CST)	[thread overview]
Message-ID: <200311210437.hAL4bIU14845@raven.dms.auburn.edu> (raw)
In-Reply-To: <87smkji35l.fsf@mail.jurta.org> (message from Juri Linkov on Thu,  20 Nov 2003 15:27:22 +0200)

Below is the latest revised version of number-sequence, taking the
suggestions of Kim and Juri into account.  Even though Kim's changes
improved number-sequence's handling of floating point arguments, the
function does not handle floats ideally, but I do not believe that
anything can be done about that.  The function was mainly designed
for, and, I believe, is mainly useful, for integer arguments.  I
pointed this out in the doc string (this is an additional change from
the previous version).

Juri Linkov wrote:

   BTW, it couldn't hurt to add examples to the docstring:

The doc string is already getting long and there already are examples
in the Elisp manual which I will update.  After updating, depending on
the actual examples in the updated version, I might add a sentence:

For examples, see Info node `(elisp)Building Lists'.

to the very end of the doc string, if this would appear to be useful.

I will wait a while (at least till Sunday) to see whether there are
any objections or further suggestions, after which I will commit,
update the Elisp manual and the NEWS.

===File ~/new-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.
TO is only included if there is an N for which TO = FROM + N * INC.  
If INC is nil, it defaults to 1 (one) if TO is larger than FROM,
or to -1 if TO is less than FROM.
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.

This function was primarily designed for integer arguments.
Nevertheless, FROM, TO and INC can be integer or float.  However,
floating point arithmetic is inexact.  For instance, depending on
the machine, it may quite well happen that
\(number-sequence 0.4 0.6 0.2) returns the one element list (0.4),
whereas \(number-sequence 0.4 0.8 0.2) returns a list with three
elements.  Thus, if some of the arguments are floats and one
wants to make sure that TO is included, one may have to
explicitly write TO as \(+ FROM \(* N INC)) or use a variable
whose value was computed with this exact expression."
  (if (or (not to) (= from to))
      (list from)
    (or inc (setq inc (if (< from to) 1 -1)))    
    (when (zerop inc) (error "The increment can not be zero"))
    (let (seq (n 0) (next from))
      (if (> inc 0)
          (while (<= next to)
            (setq seq (cons next seq)
                  n (1+ n)
                  next (+ from (* n inc))))
        (while (>= next to)
          (setq seq (cons next seq)
                n (1+ n)
                next (+ from (* n inc)))))
      (nreverse seq))))
============================================================

  reply	other threads:[~2003-11-21  4:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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   ` Luc Teirlinck [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200311210437.hAL4bIU14845@raven.dms.auburn.edu \
    --to=teirllm@dms.auburn.edu \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).