[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: |
Thu, 27 Sep 2018 18:56:03 -0800 |
User-agent: |
Mutt/1.10.0 (2018-05-17) |
* Van L <van@scratch.space> [180927 17:39]:
>
> > 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.
>
> 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,
Good practices tip. Thanks Van ..
--
Tim Johnson
http://www.tj49.com