From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: storm@cua.dk (Kim F. Storm) Newsgroups: gmane.emacs.devel Subject: Re: number-sequence Date: 20 Nov 2003 11:39:19 +0100 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: References: <200311200339.hAK3dGE07492@raven.dms.auburn.edu> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1069323641 25283 80.91.224.253 (20 Nov 2003 10:20:41 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 20 Nov 2003 10:20:41 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Thu Nov 20 11:20:37 2003 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AMlvR-000617-00 for ; Thu, 20 Nov 2003 11:20:37 +0100 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1AMlvQ-0002va-01 for ; Thu, 20 Nov 2003 11:20:36 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AMmam-0005Gp-4w for emacs-devel@quimby.gnus.org; Thu, 20 Nov 2003 06:03:20 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.24) id 1AMmac-0005GD-TO for emacs-devel@gnu.org; Thu, 20 Nov 2003 06:03:10 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.24) id 1AMma6-0005DK-LK for emacs-devel@gnu.org; Thu, 20 Nov 2003 06:03:09 -0500 Original-Received: from [212.88.64.25] (helo=mail-relay.sonofon.dk) by monty-python.gnu.org with smtp (Exim 4.24) id 1AMmFB-00087x-6e for emacs-devel@gnu.org; Thu, 20 Nov 2003 05:41:01 -0500 Original-Received: (qmail 74633 invoked from network); 20 Nov 2003 09:39:35 -0000 Original-Received: from unknown (HELO kfs-l.imdomain.dk.cua.dk) (213.83.150.2) by 0 with SMTP; 20 Nov 2003 09:39:35 -0000 Original-To: Luc Teirlinck In-Reply-To: <200311200339.hAK3dGE07492@raven.dms.auburn.edu> Original-Lines: 51 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:17956 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:17956 Luc Teirlinck writes: > ===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. TO is only inclusive if there is an N for which TO = FROM + N * INC. > 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)))) When we are using floats here, there is a risk of accumulating errors, and thus not getting the exact TO value into the list. This could be better (but marginally slower): (let (seq (n 0) (next from)) (if (> inc 0) (while (<= next to) (setq seq (cons next seq) n (1+ n) next (+ from (* inc n)))) (while (>= next to) (setq seq (cons next seq) n (1+ n) next (+ from (* inc n)))) -- Kim F. Storm http://www.cua.dk