Common Lisp the Language, 2nd Edition

Next: Establishing New Variable Up: Control Structure Previous: Function Invocation

# 7.4. Simple Sequencing

Each of the constructs in this section simply evaluates all the argument forms in order. They differ only in what results are returned.

[Special Form]
`progn` `{form}*`

The `progn` construct takes a number of forms and evaluates them sequentially, in order, from left to right. The values of all the forms but the last are discarded; whatever the last form returns is returned by the `progn` form. One says that all the forms but the last are evaluated for effect, because their execution is useful only for the side effects caused, but the last form is executed for value.

`progn` is the primitive control structure construct for ``compound statements,'' such as begin-end blocks in Algol-like languages. Many Lisp constructs are ``implicit `progn`'' forms: as part of their syntax each allows many forms to be written that are to be evaluated sequentially, discarding the results of all forms but the last and returning the results of the last form.

If the last form of the `progn` returns multiple values, then those multiple values are returned by the `progn` form. If there are no forms for the `progn`, then the result is `nil`. These rules generally hold for implicit `progn` forms as well.

[Macro]
`prog1` `first` `{form}*`

`prog1` is similar to `progn`, but it returns the value of its first form. All the argument forms are executed sequentially; the value of the first form is saved while all the others are executed and is then returned.

`prog1` is most commonly used to evaluate an expression with side effects and to return a value that must be computed before the side effects happen. For example:

    (prog1 (car x) (rplaca x 'foo))

alters the car of `x` to be `foo` and returns the old car of `x`.

`prog1` always returns a single value, even if the first form tries to return multiple values. As a consequence, `(prog1 x)` and `(progn x)` may behave differently if x can produce multiple values. See `multiple-value-prog1`. A point of style: although `prog1` can be used to force exactly a single value to be returned, it is conventional to use the function `values` for this purpose.

[Macro]
`prog2` `first` `second` `{form}*`

`prog2` is similar to `prog1`, but it returns the value of its second form. All the argument forms are executed sequentially; the value of the second form is saved while all the other forms are executed and is then returned. `prog2` is provided mostly for historical compatibility.

    (prog2 a b c ... z) == (progn a (prog1 b c ... z))

Occasionally it is desirable to perform one side effect, then a value-producing operation, then another side effect. In such a peculiar case, `prog2` is fairly perspicuous. For example:

    (prog2 (open-a-file) (process-the-file) (close-the-file))
                         ;value is that of process-the-file

`prog2`, like `prog1`, always returns a single value, even if the second form tries to return multiple values. As a consequence of this, `(prog2 x y)` and `(progn x y)` may behave differently if y can produce multiple values.

Next: Establishing New Variable Up: Control Structure Previous: Function Invocation