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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
<html><head>
<title>cl:multiple-value-setq</title>
<style type="text/css">
.example {
color: #000000;
background-color: #F5F5F5;
padding: 8px;
border: #808080;
border-style: solid;
border-width: 1px;
width:auto;
}
.button {
color: #000000;
background-color: #F5F5F5;
padding-top: 1px;
padding-bottom: 1px;
padding-left: 4px;
padding-right: 8px;
border: #808080;
border-style: solid;
border-width: 1px;
white-space: pre;
}
.box {
color: #000000;
padding-top: 4px;
padding-bottom: 4px;
padding-left: 16px;
padding-right: 16px;
border: #808080;
border-style: solid;
border-width: 1px;
}
</style>
</head>
<body>
<a href="../../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../../manual/contents.htm">Contents</a> |
<a href="../../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples.htm">Examples</a> |
<a href="../../reference/reference-index.htm">Reference</a>
<hr>
<h1>cl:multiple-value-setq</h1>
<hr>
<p>The <nobr>cl:<b>multiple-value-setq</b></nobr> macro assigns multiple
values to multiple variables:</p>
<p><div class="box">
<dl>
<dt>(cl:<b>multiple-value-setq</b> <i>symbols expr</i>)</dt>
<dd><i>symbols</i> - a list of symbols<br>
<i>expr</i> - a Lisp expression returning one or more values<br>
returns - the primary value returned by evaluating the expression</dd>
</dl>
</div></p>
<pre class="example">
(defmacro <font color="#0000CC">cl:multiple-value-setq</font> (symbols expr)
(and (or (not (consp symbols))
(eq 'quote (first symbols))
(dolist (symbol symbols)
(or (symbolp symbol) (return t))))
(error <font color="#880000">"not a list of symbols"</font> symbols))
(setq <font color="#AA5500">cl:*multiple-values*</font> nil)
(let* ((result (eval expr))
(values (if <font color="#AA5500">cl:*multiple-values*</font>
<font color="#AA5500">*rslt*</font>
(list result)))
(number-of-symbols (length symbols))
(number-of-values (length values))
(assignments (list 'setq)))
(dotimes (index number-of-symbols)
(push (nth index symbols) assignments)
(push (when (< index number-of-values)
(list 'quote (nth index values)))
assignments))
(setq assignments (reverse assignments))
`(progn ,assignments ',result)))
</pre>
<p>The expression is evaluated, and each symbol is assigned to the
corresponding value returned by the evaluation. <nobr>If there</nobr> are
more symbols than values returned,
<a href="../../reference/nil.htm">NIL</a> is assigned to the extra symbols.
<nobr>If there</nobr> are more values than symbols, the extra values are
discarded.</p>
<p><nobr>The
<a href="#cl-global-multiple-values">cl:*multiple-values*</a></nobr>
variable <nobr>is <a href="../../reference/t.htm"> T </a></nobr>
if evaluating the expression returns multiple values and
<a href="../../reference/nil.htm">NIL</a> with a normal return value.</p>
<p>The <nobr>cl:<b>multiple-value-setq</b></nobr> macro assigns multiple
values to multiple variables
<nobr>using <a href="../../reference/setq.htm">setq</a>:</nobr></p>
<pre class="example">
> (macroexpand-1 '(cl:multiple-value-setq (a b c)
(cl:values 1 2 3)))
(PROGN (SETQ A (QUOTE 1)
B (QUOTE 2)
C (QUOTE 3))
(QUOTE 1))
</pre>
<p>The <a href="../../reference/quote.htm">quote</a>s are necessary to
prevent 'unbound variable' and 'unbound function' errors with symbols and
lists.</p>
<p>Examples:</p>
<p><b>1.</b> The <nobr>cl:<b>multiple-value-setq</b></nobr> macro assigns
values returned by the <a href="values.htm">cl:values</a> function:</p>
<pre class="example">
(defun <font color="#0000CC">multiple-value-function</font> ()
(cl:values 1 2 3)) <font color="#008844">; multiple return values</font>
> (let ((a 'A) (b 'B) (c 'C))
(cl:multiple-value-setq (a b c)
(multiple-value-function))
(list a b c))
(1 2 3)
</pre>
<p><b>2.</b> If there are no values returned in the Nyquist
<a href="../../reference/global-rslt.htm">*rslt*</a> variable, the normal
function return value is assigned to the first symbol and all remaining
symbols are assigned
<nobr>to <a href="../../reference/nil.htm">NIL</a></nobr>:</p>
<pre class="example">
(defun <font color="#0000CC">normal-lisp-function</font> ()
(+ 1 1)) <font color="#008844">; normal return value</font>
> (let ((a 'A) (b 'B) (c 'C))
(cl:multiple-value-setq (a b c)
(normal-lisp-function))
(list a b c))
(2 NIL NIL)
</pre>
<p><b>3.</b> If there are less values than symbols, the extra symbols are
assigned <nobr>to <a href="../../reference/nil.htm">NIL</a></nobr>:</p>
<pre class="example">
(defun <font color="#0000CC">not-enough-values-function</font> ()
(cl:values 1 2)) <font color="#008844">; multiple return values</font>
> (let ((a 'A) (b 'B) (c 'C))
(cl:multiple-value-setq (a b c)
(not-enough-values-function))
(list a b c))
(1 2 NIL)
</pre>
<p><b>4.</b> If there are more values than symbols, the extra values are
ignored:</p>
<pre class="example">
(defun <font color="#0000CC">too-many-values-function</font> ()
(cl:values 1 2 3 4 5)) <font color="#008844">; multiple return values</font>
> (let ((a 'A) (b 'B) (c 'C))
(cl:multiple-value-setq (a b c)
(too-many-values-function))
(list a b c))
(1 2 3)
</pre>
<p><b>5.</b> Symbols not contained in the
<nobr>cl:<b>multiple-value-setq</b></nobr> <nobr>symbol-list</nobr> are not
changed:</p>
<pre class="example">
(defun <font color="#0000CC">multiple-value-function</font> ()
(cl:values 1 2 3)) <font color="#008844">; multiple return values</font>
> (let ((a 'A) (b 'B) (c 'C) (d 'D) (e 'E))
(cl:multiple-value-setq (a b c)
(multiple-value-function))
(list a b c d e))
(1 2 3 D E)
</pre>
<p><b>5.</b> If no bindings exist, new variables will be created:</p>
<pre class="example">
(defun <font color="#0000CC">multiple-value-function</font> ()
(cl:values 1 2 3)) <font color="#008844">; multiple return values</font>
> (let ((c 'C) (d 'D) (e 'E))
(cl:multiple-value-setq (a b c)
(multiple-value-function))
(list a b c d e))
(1 2 3 D E)
</pre>
<p><b>Caution:</b> In the last example, two global variables 'a' and 'b'
were created, while the lexical <a href="../../reference/let.htm">let</a>
variable 'c' was assigned to the <nobr>value 3</nobr>:</p>
<pre class="example">
> (list a b)
(1 2)
> (list a b c)
<font color="#AA0000">error: unbound variable - C</font>
</pre>
<p>The lexical <a href="../../reference/let.htm">let</a> binding of 'c' does
not exist in the global <nobr>top-level</nobr>.
<nobr>See <a href="../environment.htm">Environment</a></nobr> for more
details about variables.</p>
<p><nobr> <a href="#top">Back to top</a></nobr></p>
<hr>
<a href="../../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../../manual/contents.htm">Contents</a> |
<a href="../../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples.htm">Examples</a> |
<a href="../../reference/reference-index.htm">Reference</a>
</body></html>
|