#elisp progn 특수 형식(special form)은 언제 쓰는 걸까?

1 minute read

When/why should I use progn?’ 질문한 사람처럼 나도 궁금했다. progn 특수 형식(special form)은 언제 쓰는 건가? 안 써도 똑같이 실행되던데.

식 여러 개를 순서대로 실행하는 데 필요하다. 그럼 progn 안 쓰면 한 개만 허용한단 말인가? 예전에는 그랬나 보다. 하지만 불편했는지 implicit progn이란 걸 적용했다. progn을 쓴 것처럼 body에서 여러 줄을 순서대로 실행할 수 있다.

As a result, progn is not used as much as it was many years ago. It is needed now most often inside an unwind-protect, and, or, or in the then-part of an if. - 10.1 Sequencing

그래서 몇몇 경우를 제외하곤 거의 쓸 일이 없다.

(prog1 '1 '2 '3)
;;=> 1

(prog2 '1 '2 '3)
;;=> 2

(progn '1 '2 '3)
;;=> 3

prog는 program 줄임말인 것 같은데, 딱 부러지게 설명한 곳이 없다. common lisp에서 가져온 용어인가보다. 뒤에 붙는 건 return 값 위치를 의미한다.

(if (file-exists-p path)
    (progn
      (find-file path)
      (auto-revert-tail-mode 1)
      (read-only-mode 1)
      (goto-char (point-max))))

if 특수 형식의 then 파트처럼 implicit progn을 지원 안 하는 곳에서 식 여러 개를 실행할 때, 쓰거나

(use-package org
  ;;...
  ;; org-clock persistence 설정. 컴퓨터 꺼도 emacs 시계는 굴러간다.
  (progn
    (org-clock-persistence-insinuate)
    (setq org-clock-persist t)
    (setq org-clock-in-resume t)
    (setq org-clock-persist-query-resume nil))
  ;;...
  )

progn을 안 써도 되지만 그룹을 만들어 읽기 좋게 하려고 쓴다.

참고

C-x C-s C-x C-c