Debian Codegraph @

docsrc/xlisp/xlisp-doc/examples/common-lisp/global-multiple-values.htm

raw
1 <html><head>
2
3 <title>cl:*multiple-values*</title>
4
5 <style type="text/css">
6 .example {
7 color: #000000;
8 background-color: #F5F5F5;
9 padding: 8px;
10 border: #808080;
11 border-style: solid;
12 border-width: 1px;
13 width:auto;
14 }
15 .button {
16 color: #000000;
17 background-color: #F5F5F5;
18 padding-top: 1px;
19 padding-bottom: 1px;
20 padding-left: 4px;
21 padding-right: 8px;
22 border: #808080;
23 border-style: solid;
24 border-width: 1px;
25 white-space: pre;
26 }
27 .box {
28 color: #000000;
29 padding-top: 4px;
30 padding-bottom: 4px;
31 padding-left: 16px;
32 padding-right: 16px;
33 border: #808080;
34 border-style: solid;
35 border-width: 1px;
36 }
37 </style>
38
39 </head>
40
41 <body>
42
43 <a href="../../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
44 <a href="../../manual/contents.htm">Contents</a> |
45 <a href="../../tutorials/tutorials.htm">Tutorials</a> |
46 <a href="../examples.htm">Examples</a> |
47 <a href="../../reference/reference-index.htm">Reference</a>
48
49 <hr>
50
51 <h1>cl:*multiple-values*</h1>
52
53 <hr>
54
55 <p>The <nobr>cl:<b>*multiple-values*</b></nobr> variable is used to signal
56 if a function has returned multiple values:</p>
57
58 <p><div class="box">
59
60 <dl>
61 <dt>cl:<b>*multiple-values*</b></dt>
62 <dd>returns - <a href="../../reference/t.htm">&nbsp;T&nbsp;</a> if a
63 function returned multiple values</dd>
64 </dl>
65
66 </div></p>
67
68 <p>Test if a function has returned multiple values:</p>
69
70 <pre class="example">
71 (setf cl:*multiple-values* nil)
72 (let ((result (function ...)))
73 (if cl:*multiple-values*
74 (do-something-with *rslt* ...)
75 (do-something-with result ...))
76 ... )
77 </pre>
78
79 <p>Do not use <nobr>cl:<b>*multiple-values*</b></nobr> with
80 <a href="../../reference/let.htm">let</a> like this:</p>
81
82 <pre class="example">
83 (let ((cl:*multiple-values* nil)
84 (result (function ...)))
85 (if cl:*multiple-values*
86 (do-something-with *rslt* ...)
87 (do-something-with result ...))
88 ... )
89 </pre>
90
91 <p>This doesn't work because 'function' is evaluated in the global XLISP
92 environment, where the lexical <a href="../../reference/let.htm">let</a>
93 binding of the <nobr>cl:<b>*multiple-values*</b></nobr> variable does not
94 exist, while the <a href="../../reference/if.htm">&nbsp;if&nbsp;</a> form
95 inside the <a href="../../reference/let.htm">let</a> form cannot see a
96 global change of the <nobr>cl:<b>*multiple-values*</b></nobr> variable,
97 because the global value is shadowed by the lexical
98 <a href="../../reference/let.htm">let</a> binding.
99 <nobr>See <a href="../environment.htm">Environment</a></nobr> for more
100 details about variables.</p>
101
102 <p>The XLISP <a href="../../reference/progv.htm">progv</a> special form can
103 be used to encapsulate a multiple value call while automatically restoring
104 the old values at the end like this:</p>
105
106 <pre class="example">
107 (values 1 2 3) =&gt; 1
108
109 cl:*multiple-values* =&gt; T
110 *rslt* =&gt; (1 2 3)
111
112 (progv '(cl:*multiple-values* *rslt*) '(nil nil)
113 (let ((result (function ...)))
114 (if cl:*multiple-values*
115 (do-something-with *rslt* ...)
116 (do-something-with result ...))))
117
118 cl:*multiple-values* =&gt; T
119 *rslt* =&gt; (1 2 3)
120 </pre>
121
122 <p><div class="box">
123
124 <p><b>Note:</b> All functions returning multiple values set
125 <nobr>cl:<b>*multiple-values*</b></nobr> to
126 <a href="../../reference/t.htm">&nbsp;T&nbsp;</a>, but it's up to the
127 Lisp programmer to reset the variable
128 <nobr>to <a href="../../reference/nil.htm">NIL</a></nobr>.</p>
129
130 </div></p>
131
132 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
133
134 <hr>
135
136 <a href="../../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
137 <a href="../../manual/contents.htm">Contents</a> |
138 <a href="../../tutorials/tutorials.htm">Tutorials</a> |
139 <a href="../examples.htm">Examples</a> |
140 <a href="../../reference/reference-index.htm">Reference</a>
141
142 </body></html>