GNU Emacs 32.0.50 (9008bf64c3) のマニュアルを PLaMo 翻訳で機械翻訳したものです。誤訳を含むことがあります。原文: https://www.gnu.org/software/emacs/manual/html_node/eintr/dotimes.html ・ emacs-doc.conao3.com

dotimes マクロ

dotimes マクロは dolist と同様ですが、特定の回数だけループする点が異なります。

dotimes の第1引数には、ループが回るたびに 0, 1, 2 といった数値が代入されます。第2引数の値、つまりマクロが何回ループするかを指定する必要があります。

例えば、以下では0から3未満までの数値を第1引数 number にバインドし、3つの数値からなるリストを構築します。(1番目の数値は0、2番目の数値は1、3番目の数値は2であり、0を1番目として合計3つの数値となります。)

(let (value)      ; otherwise a value is a void variable
  (dotimes (number 3)
    (setq value (cons number value)))
  value)

⇒ (2 1 0)

dotimes の使い方は、ある式 number に対して数回演算を行い、その結果をリストまたはアトムとして返すというものです。

以下は、dotimes を用いて三角形の中にある小石の数を合計する defun の例です。

(defun triangle-using-dotimes (number-of-rows)
  "Using `dotimes', add up the number of pebbles in a triangle."
(let ((total 0))  ; otherwise a total is a void variable
  (dotimes (number number-of-rows)
    (setq total (+ total (1+ number))))
  total))

(triangle-using-dotimes 4)