common-lisp Progn


Example

The general purpose special operator progn is used for evaluating zero or more forms. The value of the last form is returned. For instance, in the following, (print 'hello) is evaluated (and its result is ignored), and then 42 is evaluated and its result (42) is returned:

(progn 
  (print 'hello)
  42)
;=> 42

If there are no forms within the progn, then nil is returned:

(progn)
;=> NIL

In addition to grouping a series of forms, progn also has the important property that if the progn form is a top-level form, then all the forms within it are processed as top level forms. This can be important when writing macros that expand into multiple forms that should all be processed as top level forms.

Progn is also valuable in that it returns all the values of the last form. For instance,

(progn
  (print 'hello)
  (values 1 2 3))
;;=> 1, 2, 3

In contrast, some grouping expressions only return the primary value of the result-producing form.

Implicit Progns

Some forms use implicit progns to describe their behavior. For instance, the when and unless macros, which are essentially one-sided if forms, describe their behavior in terms of an implicit progn. This means that a form like

(when (foo-p foo)
  form1
  form2)

is evaluated and the condition (foo-p foo) is true, then the form1 and form2 are grouped as though they were contained within a progn. The expansion of the when macro is essentially:

(if (foo-p foo)
  (progn
    form1
    form2)
  nil)