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

再帰パターン: keep

3つ目の再帰パターンはkeepパターンと呼ばれます。keep再帰パターンでは、リストの各要素が判定され、その要素が条件を満たす場合にのみ処理が行われ、結果が保持されます。

繰り返しになりますが、これはeveryパターンに非常によく似ており、条件を満たさない限り要素がスキップされる点だけが異なります。

このパターンは3つの部分で構成されています:

cond を使用した例を以下に示します。

(defun keep-three-letter-words (word-list)
  "Keep three letter words in WORD-LIST."
  (cond
   ;; First do-again-test: stop-condition
   ((not word-list) nil)

   ;; Second do-again-test: when to act
   ((eq 3 (length (symbol-name (car word-list))))
    ;; combine acted-on element with recursive call on shorter list
    (cons (car word-list) (keep-three-letter-words (cdr word-list))))

   ;; Third do-again-test: when to skip element;
   ;;   recursively call shorter list with next-step expression
   (t (keep-three-letter-words (cdr word-list)))))

(keep-three-letter-words '(one two three four five six))
    ⇒ (one two six)

停止条件として nil を使用する必要がないのは言うまでもありませんし、もちろん、これらのパターンを組み合わせることも可能です。