From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tim Johnson Newsgroups: gmane.emacs.help Subject: Re: Understanding dotimes skipping by 2 Date: Fri, 28 Sep 2018 09:47:45 -0800 Organization: AkWebsoft Message-ID: <20180928174745.GE2161@mail.akwebsoft.com> References: <20180927224840.GA2161@mail.akwebsoft.com> <87efdem1fx.fsf@phil.uni-goettingen.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1538157231 923 195.159.176.226 (28 Sep 2018 17:53:51 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 28 Sep 2018 17:53:51 +0000 (UTC) User-Agent: Mutt/1.10.0 (2018-05-17) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Sep 28 19:53:47 2018 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1g5wxW-00007q-QM for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Sep 2018 19:53:46 +0200 Original-Received: from localhost ([::1]:44690 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5wzd-0005ek-A6 for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Sep 2018 13:55:57 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:37717) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5wyo-0005XR-TE for help-gnu-emacs@gnu.org; Fri, 28 Sep 2018 13:55:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5wsu-0005qm-5E for help-gnu-emacs@gnu.org; Fri, 28 Sep 2018 13:49:07 -0400 Original-Received: from tjohnson.mtaonline.net ([64.4.232.191]:42362) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5wsl-000435-5Z for help-gnu-emacs@gnu.org; Fri, 28 Sep 2018 13:48:53 -0400 Original-Received: by linus (Postfix, from userid 1000) id A0A18240040; Fri, 28 Sep 2018 09:47:45 -0800 (AKDT) Mail-Followup-To: help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: <87efdem1fx.fsf@phil.uni-goettingen.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 64.4.232.191 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:118054 Archived-At: * Joost Kremers [180928 08:32]: <...> The first argument of `dotimes' should be a list of > three elements: a symbol to be bound as a list variable, an expression to > calculate the upper bound of the loop and an expression to be returned as > the final result. > > In the OP's code, the first element of the list is `/', so that gets bound > as list variable. (Yes, in Lisp that's possible). > > The second element is `(length l)', which returns the value 10, so that the > loop is executed 10 times. The first five of these, `(nth x l)' and `(nth (+ > x 1) l)' refer to elements in the list, after that, the return value of both > function calls in `nil', hence the list of nil's in the output. > > The third element of the first argument of `dotimes' here is the value 2, so > that is returned as the final result, which is why the `2' appears at the > end. <...> > A few comments: > > - `progn' really isn't necessary here, so should be avoided. > - `dotimes' isn't really appropriate here, either, because you're not doing > anything with the loop variable `i'. A `while' loop would be more > idiomatic: > > ``` > (let ((x 0)) > (while (< x (length l)) > (insert (format "%s %s, " (nth x l) (nth (1+ x) l))) > (setq x (+ x 2)))) > ``` > > - Note also the use of `(1+ x)' instead of (+ x 1). Though honestly I don't > know what the difference really is. It's just the idiom I'm used to. > > HTH Yes, helps a lot. I've learned (or relearned) much. thank you -- Tim Johnson http://www.tj49.com