Elisp: Block of Expression (progn)

By Xah Lee. Date: . Last updated: .

progn

Sometimes you need to group several expressions together as one single expression. This can be done with progn.

(progn
  (message "a")
  (message "b"))

;; is equivalent to
(message "a")
(message "b")

The purpose of progn is similar to a block of code {…} in C-like languages. It is used to group together several expressions into one single expression. Most of the time it's used inside if.

(if 3
    (progn
      "some more code here"
      "is true")
  (progn
    "some more code here"
    "is false"))
;; "is true"

progn returns the last expression in its body.

(progn 3 4 ) ; 4

prog1, prog2

prog1 and prog2 are similar to progn.

(prog1 1 2 3)
;; 1

(prog2 1 2 3)
;; 2

Reference

Elisp, Boolean