| 1 |
<html><head> |
| 2 |
|
| 3 |
<title>cl:multiple-value-bind</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> - |
| 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-value-bind</h1> |
| 52 |
|
| 53 |
<hr> |
| 54 |
|
| 55 |
<p>The <nobr>cl:<b>multiple-value-bind</b></nobr> macro creates new bindings |
| 56 |
for a list of symbols and evaluates expressions that use these bindings:</p> |
| 57 |
|
| 58 |
<p><div class="box"> |
| 59 |
|
| 60 |
<dl> |
| 61 |
<dt>(cl:<b>multiple-value-bind</b> <i>symbols values</i> [<i>expr1</i> ...])</dt> |
| 62 |
<dd><i>symbols</i> - a list of symbols<br> |
| 63 |
<i>values</i> - a Lisp expression returning one or more values<br> |
| 64 |
<i>exprN</i> - arbitrary Lisp expressions<br> |
| 65 |
returns - the value[s] returned by the last expression</dd> |
| 66 |
</dl> |
| 67 |
|
| 68 |
</div></p> |
| 69 |
|
| 70 |
<pre class="example"> |
| 71 |
(defmacro <font color="#0000CC">cl:multiple-value-bind</font> (symbols expr &rest body) |
| 72 |
(and (or (not (consp symbols)) |
| 73 |
(eq 'quote (first symbols)) |
| 74 |
(dolist (symbol symbols) |
| 75 |
(or (symbolp symbol) (return t)))) |
| 76 |
(error <font color="#880000">"not a list of symbols"</font> symbols)) |
| 77 |
(setq <font color="#AA5500">cl:*multiple-values*</font> nil) |
| 78 |
(let* ((result (eval expr)) |
| 79 |
(values (if <font color="#AA5500">cl:*multiple-values*</font> |
| 80 |
<font color="#AA5500">*rslt*</font> |
| 81 |
(list result))) |
| 82 |
(number-of-symbols (length symbols)) |
| 83 |
(number-of-values (length values)) |
| 84 |
(bindings nil)) |
| 85 |
(dotimes (index number-of-symbols) |
| 86 |
(push (if (< index number-of-values) |
| 87 |
(list (nth index symbols) |
| 88 |
(list 'quote (nth index values))) |
| 89 |
(nth index symbols)) |
| 90 |
bindings)) |
| 91 |
(setq bindings (reverse bindings)) |
| 92 |
`(let ,bindings ,@body))) |
| 93 |
</pre> |
| 94 |
|
| 95 |
<p>The 'values' expression is evaluated, and each of the symbols is bound to |
| 96 |
the respective value returned by the evaluation. <nobr>If there</nobr> are |
| 97 |
more symbols than values returned, extra values of |
| 98 |
<a href="../../reference/nilo.htm">NIL</a> are bound to the remaining |
| 99 |
symbols. <nobr>If there</nobr> are more values than symbols, the extra |
| 100 |
values are ignored. The symbols are bound to the values <nobr>by |
| 101 |
<a href="../../reference/let.htm">let</a></nobr>, behaving like an |
| 102 |
<nobr>implicit <a href="../../reference/progn.htm">progn</a></nobr>.</p> |
| 103 |
|
| 104 |
<p><nobr>Before evaluating 'expr1', the |
| 105 |
<a href="global-multiple-values.htm">cl:*multiple-values*</a></nobr> |
| 106 |
variable <nobr>is <a href="../../reference/t.htm"> T </a></nobr> |
| 107 |
if evaluating the 'values' expression returned multiple values and |
| 108 |
<a href="../../reference/nil.htm">NIL</a> with a normal return value.</p> |
| 109 |
|
| 110 |
<p>The <nobr>cl:<b>multiple-value-bind</b></nobr> macro binds multiple |
| 111 |
values to local <a href="../../reference/let.htm">let</a> variables::</p> |
| 112 |
|
| 113 |
<pre class="example"> |
| 114 |
> (macroexpand-1 '(cl:multiple-value-bind (a b c) |
| 115 |
(cl:values 1 2 3) |
| 116 |
(list a b c))) |
| 117 |
(LET ((A (QUOTE 1)) |
| 118 |
(B (QUOTE 2)) |
| 119 |
(C (QUOTE 3))) |
| 120 |
(LIST A B C)) |
| 121 |
</pre> |
| 122 |
|
| 123 |
<p>The <a href="../../reference/quote.htm">quote</a>s are necessary to |
| 124 |
prevent 'unbound variable' and 'unbound function' errors with symbols and |
| 125 |
lists.</p> |
| 126 |
|
| 127 |
<p>Examples:</p> |
| 128 |
|
| 129 |
<p><b>1.</b> The <nobr>cl:<b>multiple-value-bind</b></nobr> macro binds |
| 130 |
values returned by the <a href="values.htm">cl:values</a> function:</p> |
| 131 |
|
| 132 |
<pre class="example"> |
| 133 |
(defun <font color="#0000CC">multiple-value-function</font> () |
| 134 |
(cl:values 1 2 3)) <font color="#008844">; multiple return values</font> |
| 135 |
|
| 136 |
> (cl:multiple-value-bind (a b c) |
| 137 |
(multiple-value-function) |
| 138 |
(list a b c)) |
| 139 |
(1 2 3) |
| 140 |
</pre> |
| 141 |
|
| 142 |
<p><b>2.</b> If there are no values returned in the Nyquist |
| 143 |
<a href="../../reference/global-rslt.htm">*rslt*</a> variable, the normal |
| 144 |
function return value is bound to the first symbol and all remaining symbols |
| 145 |
are bound <nobr>to <a href="../../reference/nil.htm">NIL</a></nobr>:</p> |
| 146 |
|
| 147 |
<pre class="example"> |
| 148 |
(defun <font color="#0000CC">normal-lisp-function</font> () |
| 149 |
(+ 1 1)) <font color="#008844">; normal return value</font> |
| 150 |
|
| 151 |
> (cl:multiple-value-bind (a b c) |
| 152 |
(normal-lisp-function) |
| 153 |
(list a b c)) |
| 154 |
(2 NIL NIL) |
| 155 |
</pre> |
| 156 |
|
| 157 |
<p><b>3.</b> If there are less values than symbols, the extra symbols are |
| 158 |
bound <nobr>to <a href="../../reference/nil.htm">NIL</a></nobr>:</p> |
| 159 |
|
| 160 |
<pre class="example"> |
| 161 |
(defun <font color="#0000CC">not-enough-values-function</font> () |
| 162 |
(cl:values 1 2)) <font color="#008844">; multiple return values</font> |
| 163 |
|
| 164 |
> (cl:multiple-value-bind (a b c) |
| 165 |
(not-enough-values-function) |
| 166 |
(list a b c)) |
| 167 |
(1 2 NIL) |
| 168 |
</pre> |
| 169 |
|
| 170 |
<p><b>4.</b> If there are more values than symbols, the extra values are |
| 171 |
ignored:</p> |
| 172 |
|
| 173 |
<pre class="example"> |
| 174 |
(defun <font color="#0000CC">too-many-values-function</font> () |
| 175 |
(cl:values 1 2 3 4 5)) <font color="#008844">; multiple return values</font> |
| 176 |
|
| 177 |
> (cl:multiple-value-bind (a b c) |
| 178 |
(too-many-values-function) |
| 179 |
(list a b c)) |
| 180 |
(1 2 3) |
| 181 |
</pre> |
| 182 |
|
| 183 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 184 |
|
| 185 |
<hr> |
| 186 |
|
| 187 |
<a href="../../start.htm">Nyquist / XLISP 2.0</a> - |
| 188 |
<a href="../../manual/contents.htm">Contents</a> | |
| 189 |
<a href="../../tutorials/tutorials.htm">Tutorials</a> | |
| 190 |
<a href="../examples.htm">Examples</a> | |
| 191 |
<a href="../../reference/reference-index.htm">Reference</a> |
| 192 |
|
| 193 |
</body></html> |