summaryrefslogtreecommitdiff
path: root/devdocs/elisp/iteration.html
blob: a0102686dcb053887c001b3003d38276f385cf5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 <h3 class="section">Iteration</h3>    <p>Iteration means executing part of a program repetitively. For example, you might want to repeat some computation once for each element of a list, or once for each integer from 0 to <var>n</var>. You can do this in Emacs Lisp with the special form <code>while</code>: </p> <dl> <dt id="while">Special Form: <strong>while</strong> <em>condition forms…</em>
</dt> <dd>
<p><code>while</code> first evaluates <var>condition</var>. If the result is non-<code>nil</code>, it evaluates <var>forms</var> in textual order. Then it reevaluates <var>condition</var>, and if the result is non-<code>nil</code>, it evaluates <var>forms</var> again. This process repeats until <var>condition</var> evaluates to <code>nil</code>. </p> <p>There is no limit on the number of iterations that may occur. The loop will continue until either <var>condition</var> evaluates to <code>nil</code> or until an error or <code>throw</code> jumps out of it (see <a href="nonlocal-exits">Nonlocal Exits</a>). </p> <p>The value of a <code>while</code> form is always <code>nil</code>. </p> <div class="example"> <pre class="example">(setq num 0)
     ⇒ 0
</pre>
<pre class="example">(while (&lt; num 4)
  (princ (format "Iteration %d." num))
  (setq num (1+ num)))
     -| Iteration 0.
     -| Iteration 1.
     -| Iteration 2.
     -| Iteration 3.
     ⇒ nil
</pre>
</div> <p>To write a repeat-until loop, which will execute something on each iteration and then do the end-test, put the body followed by the end-test in a <code>progn</code> as the first argument of <code>while</code>, as shown here: </p> <div class="example"> <pre class="example">(while (progn
         (forward-line 1)
         (not (looking-at "^$"))))
</pre>
</div> <p>This moves forward one line and continues moving by lines until it reaches an empty line. It is peculiar in that the <code>while</code> has no body, just the end test (which also does the real work of moving point). </p>
</dd>
</dl> <p>The <code>dolist</code> and <code>dotimes</code> macros provide convenient ways to write two common kinds of loops. </p> <dl> <dt id="dolist">Macro: <strong>dolist</strong> <em>(var list [result]) body…</em>
</dt> <dd>
<p>This construct executes <var>body</var> once for each element of <var>list</var>, binding the variable <var>var</var> locally to hold the current element. Then it returns the value of evaluating <var>result</var>, or <code>nil</code> if <var>result</var> is omitted. For example, here is how you could use <code>dolist</code> to define the <code>reverse</code> function: </p> <div class="example"> <pre class="example">(defun reverse (list)
  (let (value)
    (dolist (elt list value)
      (setq value (cons elt value)))))
</pre>
</div> </dd>
</dl> <dl> <dt id="dotimes">Macro: <strong>dotimes</strong> <em>(var count [result]) body…</em>
</dt> <dd>
<p>This construct executes <var>body</var> once for each integer from 0 (inclusive) to <var>count</var> (exclusive), binding the variable <var>var</var> to the integer for the current iteration. Then it returns the value of evaluating <var>result</var>, or <code>nil</code> if <var>result</var> is omitted. Use of <var>result</var> is deprecated. Here is an example of using <code>dotimes</code> to do something 100 times: </p> <div class="example"> <pre class="example">(dotimes (i 100)
  (insert "I will not obey absurd orders\n"))
</pre>
</div> </dd>
</dl><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Iteration.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Iteration.html</a>
  </p>
</div>