From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Joost Kremers Newsgroups: gmane.emacs.help Subject: Re: Understanding dotimes skipping by 2 Date: Fri, 28 Sep 2018 11:11:14 +0200 Message-ID: <87efdem1fx.fsf@phil.uni-goettingen.de> References: <20180927224840.GA2161@mail.akwebsoft.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; format=flowed X-Trace: blaine.gmane.org 1538151902 22531 195.159.176.226 (28 Sep 2018 16:25:02 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 28 Sep 2018 16:25:02 +0000 (UTC) User-Agent: mu4e 1.1.0; emacs 26.1.50 Cc: Emacs To: Van L Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Sep 28 18:24:58 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 1g5vZZ-0005l6-J8 for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Sep 2018 18:24:57 +0200 Original-Received: from localhost ([::1]:44307 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5vbg-0000vW-6W for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Sep 2018 12:27:08 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37444) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5ooD-0001CO-83 for Help-gnu-emacs@gnu.org; Fri, 28 Sep 2018 05:11:38 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5oo8-0007rJ-Q8 for Help-gnu-emacs@gnu.org; Fri, 28 Sep 2018 05:11:35 -0400 Original-Received: from out2-smtp.messagingengine.com ([66.111.4.26]:35831) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g5oo6-0007hy-PB for Help-gnu-emacs@gnu.org; Fri, 28 Sep 2018 05:11:30 -0400 Original-Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 5CAEF21E1C; Fri, 28 Sep 2018 05:11:17 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Fri, 28 Sep 2018 05:11:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=ECwnI+GzTQZbGnaFDJxVumVhTxQzo HOCav8TaxiSo4U=; b=RiwpsLfwUhpn/0DIr8oAICvh+kNlzSK30wYR1VCYzmUZQ oTDlcelyOaAtfkLYqQs2ccmQjhtP6cGlBkvlLFrsMBHI4hoN9wS7FrRZi/69DRyT nMs4vVNW8MEIMZZwDt0dZlQ+tfuhLrS1Tr6NL1jxotgB4z+aGPga6N1HWLhOT/Mt ek191PawJPdRQNvCm06ZMUyE5exiWq6JJWNvkhSfqve0L5tCIuHC/0NDv/KCEXnL x5lvJI9P5h0CvWMhDr8IqVZfCWTfUoS3r+c59csaA2uFr5pZGdMVy8IhSShCW071 m1NPoRysdYt5EPc3SCpX0GJyVbxX4+ZssJA2nYY9A== X-ME-Proxy: X-ME-Sender: Original-Received: from IdeaPad.fastmail.com (ip5f5ac90a.dynamic.kabel-deutschland.de [95.90.201.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 4DF81E49C6; Fri, 28 Sep 2018 05:11:16 -0400 (EDT) In-reply-to: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.26 X-Mailman-Approved-At: Fri, 28 Sep 2018 12:26:24 -0400 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:118053 Archived-At: On Fri, Sep 28 2018, Van L wrote: >> the following code snippet is as follows: >> (setq l `(1 2 3 4 5 6 7 8 9 0)) >> (1 2 3 4 5 6 7 8 9 0) >> ;; iterate through a list two elements at a time >> (let ((x 0)) >> (dotimes (/ (length l) 2) >> (progn >> (insert (format "%s %s, " (nth x l) (nth (+ x 1) l))) >> (setq x (+ x 2))))) >> >> ;; and below are the results >> 1 2, 3 4, 5 6, 7 8, 9 0, nil nil, nil nil, nil nil, nil nil, >> nil nil, 2 >> >> I'm confused about the output (nil etc...)which follow the >> expected numbers. >> could someone explain? > > The first argument to dotimes needs a symbol to value binding at > a guess. There is, actually... 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. > The character l and 1 are too easy to confuse in reading you > might want to avoid that. > > (setq q '(1 2 3 4 5 6 7 8 9 0)) > (let ((x 0)) > (dotimes (i (/ (length q) 2)) > (progn > (insert (format "%s %s, " (nth x q) (nth (+ x 1) q))) > (setq x (+ x 2))))) > > ; 1 2, 3 4, 5 6, 7 8, 9 0, 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 -- Joost Kremers Life has its moments