From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Luc Teirlinck Newsgroups: gmane.emacs.devel Subject: Re: number-sequence Date: Sun, 23 Nov 2003 10:09:25 -0600 (CST) Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <200311231609.hANG9P823917@raven.dms.auburn.edu> References: <200311200339.hAK3dGE07492@raven.dms.auburn.edu> <87smkji35l.fsf@mail.jurta.org> NNTP-Posting-Host: deer.gmane.org X-Trace: sea.gmane.org 1069604321 15356 80.91.224.253 (23 Nov 2003 16:18:41 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 23 Nov 2003 16:18:41 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Sun Nov 23 17:18:38 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 1ANwwY-0006ov-00 for ; Sun, 23 Nov 2003 17:18:38 +0100 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1ANwwY-0000uI-00 for ; Sun, 23 Nov 2003 17:18:38 +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 1ANxrs-0002Fz-1n for emacs-devel@quimby.gnus.org; Sun, 23 Nov 2003 12:17:52 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.24) id 1ANxrO-0002Fi-Mj for emacs-devel@gnu.org; Sun, 23 Nov 2003 12:17:22 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.24) id 1ANxqs-0002Ay-HW for emacs-devel@gnu.org; Sun, 23 Nov 2003 12:17:21 -0500 Original-Received: from [131.204.53.104] (helo=manatee.dms.auburn.edu) by monty-python.gnu.org with esmtp (Exim 4.24) id 1ANxqs-0002Av-6B for emacs-devel@gnu.org; Sun, 23 Nov 2003 12:16:50 -0500 Original-Received: from raven.dms.auburn.edu (raven.dms.auburn.edu [131.204.53.29]) by manatee.dms.auburn.edu (8.12.10/8.12.10) with ESMTP id hANGFSKk000741; Sun, 23 Nov 2003 10:15:28 -0600 (CST) Original-Received: (from teirllm@localhost) by raven.dms.auburn.edu (8.11.6+Sun/8.11.6) id hANG9P823917; Sun, 23 Nov 2003 10:09:25 -0600 (CST) X-Authentication-Warning: raven.dms.auburn.edu: teirllm set sender to teirllm@dms.auburn.edu using -f Original-To: juri@jurta.org In-reply-to: <87smkji35l.fsf@mail.jurta.org> (message from Juri Linkov on Thu, 20 Nov 2003 15:27:22 +0200) 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:18054 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:18054 Juri Linkov wrote: There is another case that makes sense: if INC is nil and TO is less than FROM, then INC could default to -1, i.e. (number-sequence 3 1) => (3 2 1) I originally agreed with this, but I changed my mind. Indeed, the function's main motivation was the INC equal 1 case and there is no sense in making the main case tricky to make a much less used case slightly more convenient. My original change only made the function do something sensible in cases where it used to do something disastrous by senselessly chewing up CPU and memory. Changing the default would change the behavior in the by far most often used case and could break existing code. I will wait till Monday evening before committing, and, of course, changes can still be made after I commit. Latest version: ===File ~/latest-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 and defaults to 1. 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 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 is 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. Alternatively, you can, of course, also replace TO with a slightly larger value \(or a slightly more negative value if INC is negative)." (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 (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)))) ============================================================