help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Understanding dotimes skipping by 2


From: Tim Johnson
Subject: Re: Understanding dotimes skipping by 2
Date: Fri, 28 Sep 2018 09:47:45 -0800
User-agent: Mutt/1.10.0 (2018-05-17)

* Joost Kremers <joost.kremers@phil.uni-goettingen.de> [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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]