From: Joost Kremers <joost.kremers@phil.uni-goettingen.de>
To: Van L <van@scratch.space>
Cc: Emacs <Help-gnu-emacs@gnu.org>
Subject: Re: Understanding dotimes skipping by 2
Date: Fri, 28 Sep 2018 11:11:14 +0200 [thread overview]
Message-ID: <87efdem1fx.fsf@phil.uni-goettingen.de> (raw)
In-Reply-To: <E2A31A87-7F54-4BB7-A48B-24EBAF1FAF26@scratch.space>
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
next prev parent reply other threads:[~2018-09-28 9:11 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-27 22:48 Understanding dotimes skipping by 2 Tim Johnson
2018-09-28 1:29 ` Van L
2018-09-28 2:56 ` Tim Johnson
2018-09-28 9:11 ` Joost Kremers [this message]
2018-09-28 9:50 ` Van L
2018-09-28 12:23 ` Eli Zaretskii
2018-09-28 12:26 ` Van L
2018-09-28 17:47 ` Tim Johnson
2018-09-28 18:16 ` Noam Postavsky
[not found] <mailman.1418.1538089091.1284.help-gnu-emacs@gnu.org>
2018-09-27 23:14 ` Marco Wahl
2018-09-27 23:17 ` Eric Abrahamsen
2018-09-27 23:59 ` Tim Johnson
2018-09-27 23:32 ` Tim Johnson
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87efdem1fx.fsf@phil.uni-goettingen.de \
--to=joost.kremers@phil.uni-goettingen.de \
--cc=Help-gnu-emacs@gnu.org \
--cc=van@scratch.space \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.