# 11.5 迭代（DONE）

迭代是指重复地运行程序的某个部分。比如，你想让计算机对某个列表中的每个元素做一次计算，或者对从0到n中每个整数做一次计算。在Emacs Lisp 中，你可以使用while特殊表达式来实现迭代：

\[Special Form] while condition forms...

while 首先会对condition进行求值。若求值结果为non-nil，则会继续对forms部分的表达式依次求值。然后对condition重新求值，若求值结果为non-nil，则继续对forms部分求值。这个过程会一直持续，直到condition求值为nil。

迭代并没有次数限制。该循环会一直运行，直到condition求值结果为nil或出现抛出一个error。

while的求值结果始终为nil。

```
(setq num 0)
=> 0
(while (< num 4)
  (princ (format "Iteration %d." num))
  (setq num (1+ num)))
    ⊣ Iteration 0. 
    ⊣ Iteration 1. 
    ⊣ Iteration 2. 
    ⊣ Iteration 3.
    ⇒ nil
```

如果想要写一个repeat-until循环，也就是先求值，后做结束测试的循环，可以这样实现：将循环体包裹在progn写在while的condition部分，并在循环体的最后一句加上终止测试：

```lisp
(while (progn
         (forward-line 1)
         (not (looking-at "^$"))))
```

这段代码会不断前移，直到遇到一个空行。

这个看起来会有点奇怪，因为这个while没有循环体，只有终止测试（实际的循环体也写在了这里）。

宏dolist和宏dotimes提供两种常见循环的便利方式。

\[Macro] dolist (car list \[result]) body...

该控制结构将list中的元素逐次绑定到临时变量上，并对body做一次求值。最后返回求值结果 result，若result缺失，则返回nil。举个例子，这里有一个使用dolist实现的reverse函数：

```lisp
(defun reverse (list)
  (let (value)
    (dolist (elt lis value)
      (setq value (cons elt value)))))
```

\[Macro] dotimes (var count \[result]) body...

该控制结构将依次从0（包含0）到count（不包含count）中的每个整数绑定到var上，并对body做求值。最后返回result作为求值结果，若没提供result，则返回nil。现在result参数已经废弃了。这里有一个做100次事情的例子：

```lisp
(dotimes (i 100)
  (insert "I will not obey absurd orders\n"))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://emacs-lisp.ivory.cafe/kong-zhi-jie-gou/die-dai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
