1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<h3 class="section">Sequencing</h3> <p>Evaluating forms in the order they appear is the most common way control passes from one form to another. In some contexts, such as in a function body, this happens automatically. Elsewhere you must use a control structure construct to do this: <code>progn</code>, the simplest control construct of Lisp. </p> <p>A <code>progn</code> special form looks like this: </p> <div class="example"> <pre class="example">(progn <var>a</var> <var>b</var> <var>c</var> …)
</pre>
</div> <p>and it says to execute the forms <var>a</var>, <var>b</var>, <var>c</var>, and so on, in that order. These forms are called the <em>body</em> of the <code>progn</code> form. The value of the last form in the body becomes the value of the entire <code>progn</code>. <code>(progn)</code> returns <code>nil</code>. </p> <p>In the early days of Lisp, <code>progn</code> was the only way to execute two or more forms in succession and use the value of the last of them. But programmers found they often needed to use a <code>progn</code> in the body of a function, where (at that time) only one form was allowed. So the body of a function was made into an implicit <code>progn</code>: several forms are allowed just as in the body of an actual <code>progn</code>. Many other control structures likewise contain an implicit <code>progn</code>. As a result, <code>progn</code> is not used as much as it was many years ago. It is needed now most often inside an <code>unwind-protect</code>, <code>and</code>, <code>or</code>, or in the <var>then</var>-part of an <code>if</code>. </p> <dl> <dt id="progn">Special Form: <strong>progn</strong> <em>forms…</em>
</dt> <dd>
<p>This special form evaluates all of the <var>forms</var>, in textual order, returning the result of the final form. </p> <div class="example"> <pre class="example">(progn (print "The first form")
(print "The second form")
(print "The third form"))
-| "The first form"
-| "The second form"
-| "The third form"
⇒ "The third form"
</pre>
</div> </dd>
</dl> <p>Two other constructs likewise evaluate a series of forms but return different values: </p> <dl> <dt id="prog1">Special Form: <strong>prog1</strong> <em>form1 forms…</em>
</dt> <dd>
<p>This special form evaluates <var>form1</var> and all of the <var>forms</var>, in textual order, returning the result of <var>form1</var>. </p> <div class="example"> <pre class="example">(prog1 (print "The first form")
(print "The second form")
(print "The third form"))
-| "The first form"
-| "The second form"
-| "The third form"
⇒ "The first form"
</pre>
</div> <p>Here is a way to remove the first element from a list in the variable <code>x</code>, then return the value of that former element: </p> <div class="example"> <pre class="example">(prog1 (car x) (setq x (cdr x)))
</pre>
</div> </dd>
</dl> <dl> <dt id="prog2">Special Form: <strong>prog2</strong> <em>form1 form2 forms…</em>
</dt> <dd>
<p>This special form evaluates <var>form1</var>, <var>form2</var>, and all of the following <var>forms</var>, in textual order, returning the result of <var>form2</var>. </p> <div class="example"> <pre class="example">(prog2 (print "The first form")
(print "The second form")
(print "The third form"))
-| "The first form"
-| "The second form"
-| "The third form"
⇒ "The second form"
</pre>
</div> </dd>
</dl><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Sequencing.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Sequencing.html</a>
</p>
</div>
|