Debian Codegraph @

docsrc/xlisp/xlisp-doc/tutorials/lisp-hints.htm

raw
1 <html><head>
2
3 <title>Lisp Hints</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.htm">Tutorials</a> |
46 <a href="../examples/examples.htm">Examples</a> |
47 <a href="../reference/reference-index.htm">Reference</a>
48
49 <hr>
50
51 <h1>Lisp Hints</h1>
52
53 <hr>
54
55 <p>The original document was written by <nobr>Geoffrey J. Gordon</nobr>
56 [<nobr>[email protected]</nobr>], <nobr>Friday, February 5, 1993</nobr>,
57 <nobr>for <a href="http://www.cons.org/cmucl/">CMUCL</a></nobr>.</p>
58
59 <p>Modified by:</p>
60
61 <ul>
62
63 <li><nobr>Bruno Haible for <a href="http://www.gnu.org/software/clisp/">CLISP</a>.</nobr></li>
64
65 <li><nobr>Tom Almy in 1995 for <a href="http://almy.us/xlisp.html">XLISP Plus</a></nobr>.</li>
66
67 <li><nobr>Edgar M. Franke [[email protected]] in 2010 for
68 <a href="http://www.cs.cmu.edu/~music/music.software.html">Nyquist</a></nobr>.</li>
69
70 </ul>
71
72 <p>All examples tested with <nobr>Nyquist 3.03</nobr> in <nobr>November
73 2010</nobr>.</p>
74
75 <hr>
76
77 <h2>Table of Contents</h2>
78
79 <hr>
80
81 <ol>
82 <li><nobr><a href="#1">Symbols</a></nobr></li>
83 <li><nobr><a href="#2">Numbers</a></nobr></li>
84 <li><nobr><a href="#3">Conses</a></nobr></li>
85 <li><nobr><a href="#4">Lists</a></nobr></li>
86 <li><nobr><a href="#5">Functions</a></nobr></li>
87 <li><nobr><a href="#6">Printing</a></nobr></li>
88 <li><nobr><a href="#7">Forms and the Top-Level Loop</a></nobr></li>
89 <li><nobr><a href="#8">Special Forms</a></nobr></li>
90 <li><nobr><a href="#9">Binding</a></nobr></li>
91 <li><nobr><a href="#10">Dynamic Scoping</a></nobr></li>
92 <li><nobr><a href="#11">Arrays</a></nobr></li>
93 <li><nobr><a href="#12">Strings</a></nobr></li>
94 <li><nobr><a href="#13">Setf</a></nobr></li>
95 <li><nobr><a href="#14">Booleans and Conditionals</a></nobr></li>
96 <li><nobr><a href="#15">Iteration</a></nobr></li>
97 <li><nobr><a href="#16">Non-local Exits</a></nobr></li>
98 <li><nobr><a href="#17">Funcall, Apply, and Mapcar</a></nobr></li>
99 <li><nobr><a href="#18">Lambda</a></nobr></li>
100 <li><nobr><a href="#19">Sorting</a></nobr></li>
101 <li><nobr><a href="#20">Equality</a></nobr></li>
102 <li><nobr><a href="#21">Some Useful List Functions</a></nobr></li>
103 </ol>
104
105 <a name="1"></a>
106
107 <hr>
108
109 <h2>1&nbsp; Symbols</h2>
110
111 <hr>
112
113 <p>A symbol is just a sequence of characters. There are restrictions on what
114 you can include in a symbol and what the first character can be, but as long
115 as you stick to letters, digits, and hyphens, you'll be safe. [Except that
116 if you use only digits and possibly an initial hyphen, Lisp will think you
117 typed an integer rather than a symbol.] Some examples of symbols:</p>
118
119 <pre class="example">
120 a
121 b
122 c1
123 foo
124 bar
125 baaz-quux-garply
126 </pre>
127
128 <p>Some things you can do with symbols follow. Things after a <nobr>'>'
129 prompt</nobr> are what you type to the Lisp interpreter, while other things
130 are what the Lisp interpreter prints back to you. The <nobr>semicolon
131 ';'</nobr> is Lisp's comment character. Everything from a ';' to the end of
132 line is ignored.</p>
133
134 <pre class="example">
135 &gt; (setq a 5) <font color="#008844">; store a number as the value of a symbol</font>
136 5
137
138 &gt; a <font color="#008844">; take the value of a symbol</font>
139 5
140
141 &gt; (let ((a 6)) <font color="#008844">; bind the value of a symbol temporarily to 6</font>
142 a)
143 6
144
145 &gt; a <font color="#008844">; the value returns to 5 once the let is finished</font>
146 5
147
148 &gt; (+ a 6) <font color="#008844">; use the value of a symbol as an argument to a function</font>
149 11
150
151 &gt; b <font color="#008844">; try to take the value of a symbol which has no value</font>
152 <font color="#AA0000">error: unbound variable - b</font>
153 </pre>
154
155 <p>There are two special symbols,
156 <a href="../reference/t.htm">&nbsp;T&nbsp;</a>
157 <nobr>and <a href="../reference/nil.htm">NIL</a></nobr>. <nobr>The
158 value</nobr> of <a href="../reference/t.htm">&nbsp;T&nbsp;</a> is defined
159 always to <nobr>be <a href="../reference/t.htm">&nbsp;T&nbsp;</a></nobr>,
160 and the value of <a href="../reference/nil.htm">NIL</a></nobr> is defined
161 always to <nobr>be <a href="../reference/nil.htm">NIL</a></nobr></nobr>.
162 Lisp uses <a href="../reference/t.htm">&nbsp;T&nbsp;</a> and
163 <a href="../reference/nil.htm">NIL</a></nobr> to represent true and false.
164 <nobr>An example</nobr> of this use is in the if statement, described more
165 fully later:</p>
166
167 <pre class="example">
168 &gt; (if t 5 6)
169 5
170
171 &gt; (if nil 5 6)
172 6
173
174 &gt; (if 4 5 6)
175 5
176 </pre>
177
178 <p>The last example is odd but correct.
179 <nobr><a href="../reference/nil.htm">NIL</a> means</nobr> false, and
180 anything else means true. Unless we have a reason to do otherwise, we use
181 <a href="../reference/t.htm">&nbsp;T&nbsp;</a> to mean true, just for the
182 sake of clarity.</p>
183
184 <p>Symbols like <a href="../reference/t.htm">&nbsp;T&nbsp;</a> and
185 <a href="../reference/nil.htm">NIL</a> are called
186 '<nobr>self-evaluating</nobr>' symbols, because they evaluate to themselves.
187 There is a whole class of <nobr>self-evaluating</nobr> symbols called
188 'keywords'. Any symbol whose name starts with a colon is a keyword. [See
189 below for some uses for keywords.] Some examples:</p>
190
191 <pre class="example">
192 &gt; :this-is-a-keyword
193 :THIS-IS-A-KEYWORD
194
195 &gt; :so-is-this
196 :SO-IS-THIS
197
198 &gt; :me-too
199 :ME-TOO
200 </pre>
201
202 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
203
204 <a name="2"></a>
205
206 <hr>
207
208 <h2>2&nbsp; Numbers</h2>
209
210 <hr>
211
212 <p>An integer number [FIXNUM] is a sequence of digits optionally preceded by
213 a plus <nobr>sign '+'</nobr> or a minus <nobr>sign '-'</nobr>. <nobr>A
214 floating</nobr> point number [FLONUM] looks like an integer, except that it
215 has a decimal point and optionally can be written in scientific notation.
216 Here are some numbers:</p>
217
218 <pre class="example">
219 5
220 17
221 -34
222 +6
223 3.1415
224 1.722e-15
225 </pre>
226
227 <p>The standard arithmetic functions are all available:</p>
228
229 <p><table cellpadding="0" cellspacing="0"><tbody>
230 <tr valign="top">
231 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
232 <td class="button"><nobr><code>+</code></nobr></td>
233 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
234 <td width="100%"><nobr><a href="../reference/addition.htm">addition</a></nobr></td>
235 </tr>
236 <tr>
237 <td height="2px"></td>
238 </tr>
239 <tr valign="top">
240 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
241 <td class="button"><nobr><code>-</code></nobr></td>
242 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
243 <td width="100%"><nobr><a href="../reference/subtraction.htm">subtraction</a></nobr></td>
244 </tr>
245 <tr>
246 <td height="2px"></td>
247 </tr>
248 <tr valign="top">
249 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
250 <td class="button"><nobr><code>*</code></nobr></td>
251 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
252 <td width="100%"><nobr><a href="../reference/multiplication.htm">multiplication</a></nobr></td>
253 </tr>
254 <tr>
255 <td height="2px"></td>
256 </tr>
257 <tr valign="top">
258 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
259 <td class="button"><nobr><code>/</code></nobr></td>
260 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
261 <td width="100%"><nobr><a href="../reference/division.htm">division</a></nobr></td>
262 </tr>
263 <tr>
264 <td height="2px"></td>
265 </tr>
266 <tr valign="top">
267 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
268 <td class="button"><nobr><code>truncate</code></nobr></td>
269 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
270 <td width="100%"><nobr><a href="../reference/truncate.htm">truncate</a> a float to an integer</nobr></td>
271 </tr>
272 <tr>
273 <td height="2px"></td>
274 </tr>
275 <tr valign="top">
276 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
277 <td class="button"><nobr><code>rem</code></nobr></td>
278 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
279 <td width="100%"><nobr><a href="../reference/rem.htm">remainder</a> of a division</nobr></td>
280 </tr>
281 <tr>
282 <td height="2px"></td>
283 </tr>
284 <tr valign="top">
285 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
286 <td class="button"><nobr><code>sin</code></nobr></td>
287 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
288 <td width="100%"><nobr><a href="../reference/sin.htm">sine</a></nobr></td>
289 </tr>
290 <tr>
291 <td height="2px"></td>
292 </tr>
293 <tr valign="top">
294 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
295 <td class="button"><nobr><code>cos</code></nobr></td>
296 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
297 <td width="100%"><nobr><a href="../reference/cos.htm">cosine</a></nobr></td>
298 </tr>
299 <tr>
300 <td height="2px"></td>
301 </tr>
302 <tr valign="top">
303 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
304 <td class="button"><nobr><code>tan</code></nobr></td>
305 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
306 <td width="100%"><nobr><a href="../reference/tan.htm">tangent</a></nobr></td>
307 </tr>
308 <tr>
309 <td height="2px"></td>
310 </tr>
311 <tr valign="top">
312 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
313 <td class="button"><nobr><code>sqrt</code></nobr></td>
314 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
315 <td width="100%"><nobr><a href="../reference/sqrt.htm">square root</a></nobr></td>
316 </tr>
317 <tr>
318 <td height="2px"></td>
319 </tr>
320 <tr valign="top">
321 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
322 <td class="button"><nobr><code>exp</code></nobr></td>
323 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
324 <td width="100%"><nobr>natural <a href="../reference/exp.htm">exponent</a></nobr></td>
325 </tr>
326 <tr>
327 <td height="2px"></td>
328 </tr>
329 <tr valign="top">
330 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
331 <td class="button"><nobr><code>expt</code></nobr></td>
332 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
333 <td width="100%"><nobr>math <a href="../reference/expt.htm">exponent</a></nobr></td>
334 </tr>
335 </tbody></table></p>
336
337
338 <p><nobr><a href="../reference/addition.htm">&nbsp;+&nbsp;</a> [addition]</nobr>,
339 <nobr><a href="../reference/subtraction.htm">&nbsp;-&nbsp;</a> [subtraction]</nobr>,
340 <nobr><a href="../reference/multiplication.htm">&nbsp;*&nbsp;</a> [multiplication]</nobr>,
341 and <nobr><a href="../reference/division.htm">&nbsp;/&nbsp;</a> [division]</nobr>
342 accept any number of arguments and return a number according to type
343 contagion. This means that as long as only integer numbers are given as
344 arguments the result will always be an integer number, but as soon as at
345 least one of the arguments is a floating number then the result will be a
346 floating point number. Here are some examples:</p>
347
348 <pre class="example">
349 &gt; (/ 3 2) <font color="#008844">; integer division causes rounding error</font>
350 1
351
352 &gt; (/ 3 2.0) <font color="#008844">; the 2.0 forces floating point computation</font>
353 1.5
354
355 &gt; (exp 1) <font color="#008844">; e</font>
356 2.71828
357
358 &gt; (exp 3) <font color="#008844">; e * e * e</font>
359 20.0855
360
361 &gt; (expt 3 4.2) <font color="#008844">; exponent with a base other than e</font>
362 100.904
363
364 &gt; (+ 5 6 7 (* 8 9 10)) <font color="#008844">; the functions + - * / accept multiple arguments</font>
365 738
366 </pre>
367
368 <p>In Nyquist the valid range of integer numbers is limited by the size of a
369 C 'long' value on the machine on which Nyquist is running.</p>
370
371 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
372
373 <a name="3"></a>
374
375 <hr>
376
377 <h2>3&nbsp; Conses</h2>
378
379 <hr>
380
381 <p>A <a href="../reference/cons.htm">cons</a> is just a
382 <nobr>two-field</nobr> record. <nobr>The fields</nobr> are called
383 <a href="../reference/car.htm">car</a> and
384 <a href="../reference/cdr.htm">cdr</a>, for historical reasons.
385 <nobr>On the</nobr> first machine where Lisp was implemented, there
386 were two assembly language instructions CAR and CDR which stood for
387 'contents of address register' and 'contents of decrement register'.
388 Conses were implemented using these two registers.</p>
389
390 <p>Conses are created by the <a href="../reference/cons.htm">cons</a>
391 function:</p>
392
393 <pre class="example">
394 &gt; (cons 4 5) <font color="#008844">; Allocate a cons. Set the car to 4 and the cdr to 5</font>
395 (4 . 5)
396
397 &gt; (cons (cons 4 5) 6)
398 ((4 . 5) . 6)
399
400 &gt; (car (cons 4 5))
401 4
402
403 &gt; (cdr (cons 4 5))
404 5
405 </pre>
406
407 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
408
409 <a name="4"></a>
410
411 <hr>
412
413 <h2>4&nbsp; Lists</h2>
414
415 <hr>
416
417 <p>You can build many structures out of conses. Perhaps the simplest is a
418 linked list. <nobr>The <a href="../reference/car.htm">car</a></nobr>
419 <nobr>[the <a href="../reference/first.htm">first</a></nobr> element] of
420 each cons points to one of the elements of the list, and the
421 <a href="../reference/cdr.htm">cdr</a> <nobr>[the
422 <a href="../reference/rest.htm">rest</a></nobr> of the elements] points
423 either to another cons or to <a href="../reference/nil.htm">NIL</a>.
424 <nobr>You can</nobr> create such a linked list with the
425 <a href="../reference/list.htm">list</a> function:</p>
426
427 <pre class="example">
428 &gt; (list 4 5 6)
429 (4 5 6)
430 </pre>
431
432 <p>Notice that Lisp prints linked lists a special way. <nobr>It omits</nobr>
433 some of the periods and parentheses. The rule is that if the
434 <a href="../reference/cdr.htm">cdr</a> of a
435 <a href="../reference/cons.htm">cons</a> is
436 <a href="../reference/nil.htm">NIL</a>, Lisp doesn't bother to print the
437 period or the <a href="../reference/nil.htm">NIL</a>, and if the
438 <a href="../reference/cdr.htm">cdr</a> of
439 <nobr><a href="../reference/cons.htm">cons</a> A</nobr> is
440 <nobr><a href="../reference/cons.htm">cons</a> B</nobr>, then Lisp doesn't
441 bother to print the period for
442 <nobr><a href="../reference/cons.htm">cons</a> A</nobr> or the parentheses
443 for <nobr><a href="../reference/cons.htm">cons</a> B. So:</nobr></p>
444
445 <pre class="example">
446 &gt; (cons 4 nil)
447 (4) <font color="#008844">; (4 . nil)</font>
448
449 &gt; (cons 4 (cons 5 6))
450 (4 5 . 6) <font color="#008844">; (4 . (5 . 6))</font>
451
452 &gt; (cons 4 (cons 5 (cons 6 nil)))
453 (4 5 6) <font color="#008844">; (4 . (5 . (6 . nil)))</font>
454 </pre>
455
456 <p>The last example is exactly equivalent to the call:</p>
457
458 <pre class="example">
459 (list 4 5 6)
460 </pre>
461
462 <p>Note that <a href="../reference/nil.htm">NIL</a> now means the list with
463 no elements. <nobr>The <a href="../reference/cdr.htm">cdr</a></nobr> of
464 <nobr>(a b)</nobr>, a list with <nobr>2 elements</nobr>, <nobr>is
465 (b)</nobr>, a list with 1 element, and the
466 <a href="../reference/cdr.htm">cdr</a> of (b), a list with <nobr>1
467 element</nobr>, <nobr>is <a href="../reference/nil.htm">NIL</a></nobr>,
468 which therefore must be a list with no elements.</p>
469
470 <p><div class="box">
471
472 <p>The <a href="../reference/car.htm">car</a> and
473 <a href="../reference/cdr.htm">cdr</a> of
474 <a href="../reference/nil.htm">NIL</a> are defined to
475 <nobr>be <a href="../reference/nil.htm">NIL</a></nobr>.</p>
476
477 </div></p>
478
479 <p>If you store your list in a variable, you can make it act like a
480 stack:</p>
481
482 <pre class="example">
483 &gt; (setq a nil)
484 NIL <font color="#008844">; A = ()</font>
485
486 &gt; (push 4 a)
487 (4) <font color="#008844">; A = (4)</font>
488
489 &gt; (push 5 a)
490 (5 4) <font color="#008844">; A = (5 4)</font>
491
492 &gt; (pop a)
493 5 <font color="#008844">; A = (4)</font>
494
495 &gt; (pop a)
496 4 <font color="#008844">; A = ()</font>
497
498 &gt; (pop a)
499 NIL <font color="#008844">; A = ()</font>
500 </pre>
501
502 <p>See <a href="../reference/pop.htm">pop</a>,
503 <a href="../reference/push.htm">push</a>,
504 <a href="../reference/setq.htm">setq</a>.</p>
505
506 <a name="list-accessors"></a>
507
508 <p><b>List Accessors</b></p>
509
510 <p>There are several different approaches to name the accessor
511 functions for elements of conses and lists. <nobr>The 'traditional'</nobr>
512 Lisp still uses function names like
513 <a href="../reference/car.htm">car</a> and
514 <a href="../reference/cdr.htm">cdr</a> while the 'modern' Lisp uses more
515 descriptive names like <a href="../reference/first.htm">first</a> and <a
516 href="../reference/rest.htm">rest</a>. This leads to the situation that in
517 most Lisps today the list accessor functions are available under different
518 names for the same functions:</p>
519
520 <p><div class="box">
521
522 <p><table cellpadding="0" cellspacing="0"><tbody>
523 <tr>
524 <td align="right"><nobr>modern</nobr></td>
525 <td><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
526 <td align="left"><nobr>traditional</nobr></td>
527 <td></td>
528 <td>equivalent to</td>
529 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
530 <td>(1 2 3 4 5 6 7 8)</td>
531 </tr>
532 <tr>
533 <td height="2px"></td>
534 </tr>
535 <tr>
536 <td align="right"><nobr><a href="../reference/first.htm">first</a></nobr></td>
537 <td><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
538 <td align="left"><nobr><a href="../reference/car.htm">car</a></nobr></td>
539 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
540 <td>(<a href="../reference/nth.htm">nth</a> 0 ... )</td>
541 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
542 <td>1</td>
543 </tr>
544 <tr>
545 <td align="right"><nobr><a href="../reference/second.htm">second</a></nobr></td>
546 <td><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
547 <td align="left"><nobr><a href="../reference/caar.htm">cadr</a></nobr></td>
548 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
549 <td>(<a href="../reference/nth.htm">nth</a> 1 ... )</td>
550 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
551 <td>2</td>
552 </tr>
553 <tr>
554 <td align="right"><nobr><a href="../reference/third.htm">third</a></nobr></td>
555 <td><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
556 <td align="left"><nobr><a href="../reference/caaar.htm">caddr</a></nobr></td>
557 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
558 <td>(<a href="../reference/nth.htm">nth</a> 2 ... )</td>
559 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
560 <td>3</td>
561 </tr>
562 <tr>
563 <td align="right"><nobr><a href="../reference/fourth.htm">fourth</a></nobr></td>
564 <td><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
565 <td align="left"><nobr><a href="../reference/caaaar.htm">cadddr</a></nobr></td>
566 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
567 <td>(<a href="../reference/nth.htm">nth</a> 3 ... )</td>
568 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
569 <td>4</td>
570 </tr>
571 <tr>
572 <td colspan="3"></nobr></td>
573 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
574 <td>(<a href="../reference/nth.htm">nth</a> 4 ... )</td>
575 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
576 <td>5</td>
577 </tr>
578 <tr>
579 <td colspan="3"></nobr></td>
580 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
581 <td>&nbsp;&nbsp;...</td>
582 </tr>
583 <tr>
584 <td height="2px"></td>
585 </tr>
586 <tr>
587 <td colspan="4"></td>
588 <td>(<a href="../reference/nthcdr.htm">nthcdr</a> 0 ... )</td>
589 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
590 <td>(1 2 3 4 5 6 7 8)</td>
591 </tr>
592 <tr>
593 <td align="right"><nobr><a href="../reference/rest.htm">rest</a></nobr></td>
594 <td><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
595 <td align="left"><nobr><a href="../reference/cdr.htm">cdr</a></nobr></td>
596 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
597 <td>(<a href="../reference/nthcdr.htm">nthcdr</a> 1 ... )</td>
598 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
599 <td>(2 3 4 5 6 7 8)</td>
600 </tr>
601 <tr>
602 <td colspan="2"></td>
603 <td align="left"><nobr><a href="../reference/caar.htm">cddr</a></nobr></td>
604 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
605 <td>(<a href="../reference/nthcdr.htm">nthcdr</a> 2 ... )</td>
606 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
607 <td>(3 4 5 6 7 8)</td>
608 </tr>
609 <tr>
610 <td colspan="2"></td>
611 <td align="left"><nobr><a href="../reference/cdddr.htm">cdddr</a></nobr></td>
612 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
613 <td>(<a href="../reference/nthcdr.htm">nthcdr</a> 3 ... )</td>
614 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
615 <td>(4 5 6 7 8)</td>
616 </tr>
617 <tr>
618 <td colspan="2"></td>
619 <td align="left"><nobr><a href="../reference/cddddr.htm">cddddr</a></nobr></td>
620 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
621 <td>(<a href="../reference/nthcdr.htm">nthcdr</a> 4 ... )</td>
622 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
623 <td>(5 6 7 8)</td>
624 </tr>
625 <tr>
626 <td colspan="4"></td>
627 <td>(<a href="../reference/nthcdr.htm">nthcdr</a> 5 ... )</td>
628 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
629 <td>(6 7 8)</td>
630 </tr>
631 <tr>
632 <td colspan="3"></nobr></td>
633 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
634 <td>&nbsp;&nbsp;...</td>
635 </tr>
636 <tr>
637 <td align="right"><nobr><a href="../reference/last.htm">last</a></nobr></td>
638 <td colspan="4"></td>
639 <td><nobr><code>&nbsp;&nbsp;</code>&rarr;&nbsp;&nbsp;</nobr></td>
640 <td>(8)</td>
641 </tr>
642 </tbody></table></p>
643
644 </div></p>
645
646 <p>The traditional <nobr>c..r-functions</nobr> are available in even more
647 variations, see <a href="../reference/car.htm">car</a>,
648 <a href="../reference/cdr.htm">cdr</a>,
649 <nobr><a href="../reference/cddr.htm">cadr, cddr</a></nobr>,
650 <nobr><a href="../reference/caaar.htm">caaar...caddr</a></nobr>,
651 <nobr><a href="../reference/cdddr.htm">cdaar...cdddr</a></nobr>,
652 <nobr><a href="../reference/caaaar.htm">caaaar...cadddr</a></nobr>,
653 <nobr>and <a href="../reference/cddddr.htm">cdaaar...cddddr</a></nobr>.</p>
654
655 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
656
657 <a name="5"></a>
658
659 <hr>
660
661 <h2>5&nbsp; Functions</h2>
662
663 <hr>
664
665 <p>You saw one example of a function above. Here are some more:</p>
666
667 <pre class="example">
668 &gt; (+ 3 4 5 6) <font color="#008844">; this function takes any number of arguments</font>
669 18
670
671 &gt; (+ (+ 3 4) (+ (+ 4 5) 6)) <font color="#008844">; isn't prefix notation fun?</font>
672 22
673
674 &gt; (defun foo (x y) <font color="#008844">; defining a function</font>
675 (+ x y 5))
676 FOO
677
678 &gt; (foo 5 0) <font color="#008844">; calling a function</font>
679 10
680
681 &gt; (defun fact (x) <font color="#008844">; a recursive function</font>
682 (if (&gt; x 0)
683 (* x (fact (- x 1)))
684 1))
685 FACT
686
687 &gt; (fact 5)
688 120
689
690 &gt; (defun a (x)
691 (if (= x 0)
692 t
693 (b (- x)))) <font color="#008844">; mutually recursive functions</font>
694 A
695
696 &gt; (defun b (x)
697 (if (&gt; x 0)
698 (a (- x 1))
699 (a (+ x 1))))
700 B
701
702 &gt; (a 5)
703 T
704
705 &gt; (defun bar (x) <font color="#008844">; A function with multiple statements</font>
706 (setq x (* x 3)) <font color="#008844">; in its body. It will return the value</font>
707 (setq x (/ x 2)) <font color="#008844">; returned by its final statement</font>
708 (+ x 4))
709 BAR
710
711 &gt; (bar 6)
712 13
713 </pre>
714
715 <p>See <a href="../reference/addition.htm">&nbsp;+&nbsp;</a>,
716 <a href="../reference/subtraction.htm">&nbsp;&minus;&nbsp;</a>,
717 <a href="../reference/multiplication.htm">&nbsp;*&nbsp;</a>,
718 <a href="../reference/division.htm">&nbsp;/&nbsp;</a>,
719 <a href="../reference/number-equal.htm">&nbsp;=&nbsp;</a>,
720 <a href="../reference/number-greaterp.htm">&nbsp;&gt;&nbsp;</a>,
721 <a href="../reference/defun.htm">defun</a>,
722 <a href="../reference/if.htm">&nbsp;if&nbsp;</a>,
723 <a href="../reference/setq.htm">setq</a>. When we defined 'foo', we
724 gave it two arguments, 'x' <nobr>and 'y'</nobr>. Now when we call 'foo', we
725 are required to provide exactly two arguments. The first will become the
726 value of 'x' for the duration of the call to 'foo', and the second will
727 become the value of 'y' for the duration of the call. <nobr>In Lisp</nobr>,
728 most variables are lexically scoped. <nobr>That is</nobr>, if 'foo' calls
729 'bar' and 'bar' tries to reference 'x', then 'bar' will not get 'foo's value
730 <nobr>for x</nobr>.</p>
731
732 <p><div class="box">
733
734 <p>The process of assigning a symbol a value for the duration of some
735 lexical scope is called 'binding'.</p>
736
737 </div></p>
738
739 <p>You can specify optional arguments for your functions. Any argument
740 after the symbol
741 <a href="../reference/lambda-keyword-optional.htm">&amp;optional</a> is
742 optional:</p>
743
744 <pre class="example">
745 &gt; (defun bar (x &amp;optional y)
746 (if y
747 x
748 0))
749 BAR
750
751 &gt; (defun baaz (&amp;optional (x 3) (z 10))
752 (+ x z))
753 BAAZ
754
755 &gt; (bar 5)
756 0
757
758 &gt; (bar 5 t)
759 5
760
761 &gt; (baaz 5)
762 15
763
764 &gt; (baaz 5 6)
765 11
766
767 &gt; (baaz)
768 13
769 </pre>
770
771 <p>See <a href="../reference/addition.htm">&nbsp;+&nbsp;</a>,
772 <a href="../reference/defun.htm">defun</a>,
773 <a href="../reference/if.htm">&nbsp;if&nbsp;</a>.
774 <nobr>It is</nobr> legal to call the function 'bar' with either one or two
775 arguments. <nobr>If it</nobr> is called with one argument, 'x' will be bound
776 to the value of that argument and 'y' will be bound <nobr>to
777 <a href="../reference/nil.htm">NIL</a></nobr>. <nobr>If it</nobr> is called
778 with two arguments, 'x' and 'y' will be bound to the values of the first and
779 second argument, respectively.</p>
780
781 <p>The function 'baaz' has two optional arguments. <nobr>It specifies</nobr> a
782 default value for each of them. <nobr>If the</nobr> caller specifies only
783 one argument, 'z' will be bound <nobr>to 10</nobr> instead of <nobr>to
784 <a href="../reference/nil.htm">NIL</a></nobr>, and if the caller
785 specifies no arguments, 'x' will be bound <nobr>to 3</nobr> and <nobr>'z' to
786 10</nobr>.</p>
787
788 <p>You can make your function accept any number of arguments by ending its
789 argument list with an
790 <a href="../reference/lambda-keyword-rest.htm">&amp;rest</a> parameter.
791 Lisp will collect all arguments not otherwise accounted for into a list and
792 bind the <a href="../reference/lambda-keyword-rest.htm">&amp;rest</a>
793 parameter to that <nobr>list. So:</nobr></p>
794
795 <pre class="example">
796 &gt; (defun foo (x &rest y)
797 y)
798 FOO
799
800 &gt; (foo 3)
801 NIL
802
803 &gt; (foo 4 5 6)
804 (5 6)
805 </pre>
806
807 <p>See <a href="../reference/defun.htm">defun</a>. Finally, you can give
808 your function another kind of optional argument called a
809 <a href="../reference/lambda-keyword-key.htm">&amp;key</a> 'keyword'
810 argument. <nobr>The caller</nobr> can give these arguments in any order,
811 because they're labelled with keywords:</p>
812
813 <pre class="example">
814 &gt; (defun foo (&key x y)
815 (cons x y))
816 FOO
817
818 &gt; (foo :x 5 :y 3)
819 (5 . 3)
820
821 &gt; (foo :y 3 :x 5)
822 (5 . 3)
823
824 &gt; (foo :y 3)
825 (NIL . 3)
826
827 &gt; (foo)
828 (NIL)
829 </pre>
830
831 <p>See <a href="../reference/defun.htm">defun</a>.
832 <nobr>An <a href="../reference/lambda-keyword-key.htm">&amp;key</a></nobr>
833 parameter can have a default <nobr>value too:</nobr></p>
834
835 <pre class="example">
836 &gt; (defun foo (&key (x 5))
837 x)
838 FOO
839
840 &gt; (foo :x 7)
841 7
842
843 &gt; (foo)
844 5
845 </pre>
846
847 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
848
849 <a name="6"></a>
850
851 <hr>
852
853 <h2>6&nbsp; Printing</h2>
854
855 <hr>
856
857 <p>Some functions can cause output. The simplest one is
858 <a href="../reference/print.htm">print</a>, which
859 prints its argument and then <nobr>returns it:</nobr></p>
860
861 <pre class="example">
862 &gt; (print 3)
863 3 <font color="#008844">; screen output</font>
864 3 <font color="#008844">; return value</font>
865 </pre>
866
867 <p>The first 3 above was <a href="../reference/print.htm">print</a>ed, the
868 second was returned.</p>
869
870 <p>If you want more complicated output, you will need to use
871 <a href="../reference/format.htm">format</a>.
872 Here's an example:</p>
873
874 <pre class="example">
875 &gt; (format t "An atom: ~S~%and a list: ~S~%and an integer: ~A~%"
876 nil (list 5) 6)
877 An atom: NIL <font color="#008844">; screen output</font>
878 and a list: (5) <font color="#008844">; screen output</font>
879 and an integer: 6 <font color="#008844">; screen output</font>
880 NIL <font color="#008844">; return value</font>
881 </pre>
882
883 <p>See <a href="../reference/list.htm">list</a>. <nobr>The first</nobr>
884 argument to <a href="../reference/format.htm">format</a> is either
885 <a href="../reference/t.htm">&nbsp;T&nbsp;</a>,
886 <a href="../reference/nil.htm">NIL</a>, or a stream.
887 <nobr><a href="../reference/t.htm">&nbsp;T&nbsp;</a> specifies</nobr> output
888 to the terminal. <nobr><a href="../reference/nil.htm">NIL</a> means</nobr>
889 not to print anything but to return a string containing the output instead.
890 Streams are general places for output <nobr>to go</nobr>. They can specify a
891 file, or the terminal, or a printer device. This tutorial will not describe
892 streams in any further detail.</p>
893
894 <p>The second argument is a formatting template, which is a string
895 optionally containing formatting directives. <nobr>All remaining</nobr>
896 arguments may be referred to by the formatting directives. Lisp will replace
897 the directives with some appropriate characters based on the arguments to
898 which they refer and then print the resulting string.</p>
899
900 <p>The format function always returns <a href="../reference/nil.htm">NIL</a>
901 unless its first argument is <a href="../reference/nil.htm">NIL</a>, in
902 which case it prints nothing and returns a string.</p>
903
904 <p>There are several different directives available:</p>
905
906 <p><table cellpadding="0" cellspacing="0"><tbody>
907 <tr>
908 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
909 <td valign="top">
910 <table cellpadding="0" cellspacing="0" width="100%"><tbody>
911 <tr valign="top">
912 <td class="button"><nobr><code>~S</code></nobr></td>
913 </tr>
914 </tbody></table>
915 </td>
916 <td valign="top"><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
917 <td width="100%">[standard] - accepts any Lisp object and replaces it by
918 the same printed representation which is produced by the
919 <a href="../reference/print.htm">print</a> function.</td>
920 </tr>
921 <tr>
922 <td><nobr><font size="-2">&nbsp;</font></nobr></td>
923 </tr>
924 <tr>
925 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
926 <td valign="top">
927 <table cellpadding="0" cellspacing="0" width="100%"><tbody>
928 <tr valign="top">
929 <td class="button"><nobr><code>~A</code></nobr></td>
930 </tr>
931 </tbody></table>
932 </td>
933 <td valign="top"><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
934 <td width="100%">[aestethic] - tries to '<nobr>pretty-print</nobr>'
935 its argument.</td>
936 </tr>
937 <tr>
938 <td><nobr><font size="-2">&nbsp;</font></nobr></td>
939 </tr>
940 <tr>
941 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
942 <td valign="top">
943 <table cellpadding="0" cellspacing="0" width="100%"><tbody>
944 <tr valign="top">
945 <td class="button"><nobr><code>~%</code></nobr></td>
946 </tr>
947 </tbody></table>
948 </td>
949 <td valign="top"><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
950 <td width="100%">[linebreak] - is always replaced by a linebreak character
951 or character sequence of the underlying operation system.</td>
952 </tr>
953 <tr>
954 <td><nobr><font size="-2">&nbsp;</font></nobr></td>
955 </tr>
956 <tr>
957 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
958 <td valign="top">
959 <table cellpadding="0" cellspacing="0" width="100%"><tbody>
960 <tr valign="top">
961 <td class="button"><nobr><code>~~</code></nobr></td>
962 </tr>
963 </tbody></table>
964 </td>
965 <td valign="top"><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
966 <td width="100%">[tilde] - is replaced by a single '~' character.</td>
967 </tr>
968 </tbody></table></p>
969
970 <p>If the last character in a line in a
971 <a href="../reference/format.htm">format</a> template is a tilde, then
972 the linebreak is ignored and the template continues with the next
973 <nobr>non-whitespace</nobr> character in the next line.</p>
974
975 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
976
977 <a name="7"></a>
978
979 <hr>
980
981 <h2>7&nbsp; Forms and the Top-Level Loop</h2>
982
983 <hr>
984
985 <p>The things which you type to the Lisp interpreter are called 'forms'.
986 <nobr>The Lisp</nobr> interpreter repeatedly
987 <a href="../reference/read.htm">read</a>s a form,
988 <a href="../reference/eval.htm">eval</a>uates it, and
989 <a href="../reference/print.htm">print</a>s the result. This procedure
990 is therefore called the '<nobr>read-eval-print' loop</nobr>, or REPL for
991 short.</p>
992
993 <p>Some forms will cause errors. After an error, Lisp will put you into the
994 debugger so you can try to figure out what caused the error.</p>
995
996 <p>In general, a form is either an <a href="../reference/atom.htm">atom</a>,
997 <nobr>[for example</nobr> a symbol, an integer, or a string] or <nobr>a
998 <a href="../reference/list.htm">list</a></nobr>. <nobr>If the</nobr> form is
999 an <a href="../reference/atom.htm">atom</a>, Lisp evaluates it immediately.
1000 Symbols evaluate to their value, integers and strings evaluate to
1001 themselves. <nobr>If the</nobr> form is a
1002 <a href="../reference/list.htm">list</a></nobr>, Lisp treats its first
1003 element as the name of a function. <nobr>It evaluates</nobr> the remaining
1004 elements recursively, and then calls the function with the values of the
1005 remaining elements as arguments. </p>
1006
1007 <p>For example, if Lisp sees the form:</p>
1008
1009 <pre class="example">
1010 (+ 3 4)
1011 </pre>
1012
1013 <p>then it treats <a href="../reference/addition.htm">&nbsp;+&nbsp;</a> as
1014 the name of a function. <nobr>It then</nobr> evaluates 3 to <nobr>get
1015 3</nobr> and 4 to <nobr>get 4</nobr>, finally it calls <a
1016 href="../reference/addition.htm">&nbsp;+&nbsp;</a> with 3 and 4 as the
1017 arguments. <nobr>The <a href="../reference/addition.htm">&nbsp;+&nbsp;</a>
1018 function</nobr> <nobr>returns 7</nobr>, which Lisp prints.</p>
1019
1020 <p><div class="box">
1021
1022 <p><b>Nyquist:</b> <nobr>A description</nobr> of the debugger can be found
1023 in the <nobr><a href="../manual/xlisp.htm#break-loop">Break Command Loop</a></nobr>
1024 section and a detailed description of the evaluation process can be found in
1025 the <a href="../manual/xlisp.htm#the-evaluator">Evaluator</a> section of the XLISP
1026 manual.</p>
1027
1028 </div></p>
1029
1030 <p>The <nobr>top-level</nobr> loop provides some other conveniences.
1031 <nobr>One particularly</nobr> convenient convenience is the ability to talk
1032 about the results of previously typed forms. Lisp always saves its most
1033 recent three results, it stores them as the values of the symbols
1034 <a href="../manual/xlisp.htm#command-loop">&nbsp;*&nbsp;</a>,
1035 <a href="../manual/xlisp.htm#command-loop">&nbsp;**&nbsp;</a>,
1036 <nobr>and <a href="../manual/xlisp.htm#command-loop">&nbsp;***&nbsp;</a></nobr>.
1037 <nobr>For example:</nobr></p>
1038
1039 <pre class="example">
1040 &gt; 3
1041 3
1042
1043 &gt; 4
1044 4
1045
1046 &gt; 5
1047 5
1048
1049 &gt; ***
1050 3
1051
1052 &gt; ***
1053 4
1054
1055 &gt; ***
1056 5
1057
1058 &gt; **
1059 4
1060
1061 &gt; *
1062 4
1063 </pre>
1064
1065 <p>See <a href="../manual/xlisp.htm#command-loop">&nbsp;*&nbsp;</a>,
1066 <a href="../manual/xlisp.htm#command-loop">&nbsp;**&nbsp;</a>,
1067 <a href="../manual/xlisp.htm#command-loop">&nbsp;***&nbsp;</a>,
1068 <a href="../manual/xlisp.htm#command-loop">&nbsp;+&nbsp;</a>,
1069 <a href="../manual/xlisp.htm#command-loop">&nbsp;++&nbsp;</a>,
1070 <a href="../manual/xlisp.htm#command-loop">&nbsp;+++&nbsp;</a>,
1071 <a href="../manual/xlisp.htm#command-loop">&nbsp;&minus;&nbsp;</a>.</p>
1072
1073 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1074
1075 <a name="8"></a>
1076
1077 <hr>
1078
1079 <h2>8&nbsp; Special Forms</h2>
1080
1081 <hr>
1082
1083 <p>There are a number of special forms which look like function calls but
1084 aren't. These include control constructs such as
1085 <a href="../reference/if.htm">&nbsp;if&nbsp;</a> statements and
1086 <a href="../reference/do.htm">do</a> loops, assignments like
1087 <a href="../reference/setq.htm">setq</a>,
1088 <a href="../reference/setf.htm">setf</a>,
1089 <a href="../reference/push.htm">push</a>, and
1090 <a href="../reference/pop.htm">pop</a>, definitions such as
1091 <a href="../reference/defun.htm">defun</a>, and binding constructs such
1092 <nobr>as <a href="../reference/let.htm">let</a></nobr>. <nobr>Not all</nobr>
1093 of these special forms have been mentioned yet, see below for examples.</p>
1094
1095 <p>One useful special form is the <a href="../reference/quote.htm">quote</a>
1096 form. <nobr>The <a href="../reference/quote.htm">quote</a></nobr> function
1097 prevents its argument from being evaluated. <nobr>For example:</nobr></p>
1098
1099 <pre class="example">
1100 &gt; (setq a 3)
1101 3
1102
1103 &gt; a
1104 3
1105
1106 &gt; (quote a)
1107 A
1108
1109 &gt; 'a <font color="#008844">; 'a is an abbreviation for (quote a)</font>
1110 A
1111 </pre>
1112
1113 <p>Another similar special form is the
1114 <a href="../reference/function.htm">function</a> form, it causes its
1115 argument to be interpreted as a function rather than being evaluated.
1116 <nobr>For example:</nobr></p>
1117
1118 <pre class="example">
1119 &gt; (setq + 3)
1120 3
1121
1122 &gt; +
1123 3
1124
1125 &gt; '+
1126 +
1127
1128 &gt; (function +)
1129 #&lt;Subr-+: #88b44d5e&gt;
1130
1131 &gt; #'+ <font color="#008844">; #'+ is an abbreviation for (function +)</font>
1132 #&lt;Subr-+: #88b44d5e&gt;
1133 </pre>
1134
1135 <p>The <a href="../reference/function.htm">function</a> special form is
1136 useful when you want to pass a function as an argument to another function.
1137 <nobr>See below</nobr> for some examples of functions which take functions
1138 as arguments.</p>
1139
1140 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1141
1142 <a name="9"></a>
1143
1144 <hr>
1145
1146 <h2>9&nbsp; Binding</h2>
1147
1148 <hr>
1149
1150 <p>Binding is lexically scoped assignment. <nobr>It happens</nobr> to the
1151 variables in a function's parameter list whenever the function is called.
1152 <nobr>The formal</nobr> parameters are bound to the actual parameters for
1153 the duration of the function call. <nobr>You can</nobr> bind variables
1154 anywhere in a program with the <a href="../reference/let.htm">let</a>
1155 special form, which looks <nobr>like this:</nobr></p>
1156
1157 <pre class="example">
1158 (let ((<font color="#0000CC">variable-1 value-1</font>)
1159 (<font color="#0000CC">variable-2 value-2</font>)
1160 <font color="#008844">...</font> )
1161 <font color="#0000CC">body</font>)
1162 </pre>
1163
1164 <p>The <a href="../reference/let.htm">let</a> function binds
1165 '<nobr>variable-1</nobr>' to '<nobr>value-1</nobr>',
1166 '<nobr>variable-2</nobr>' to '<nobr>value-2</nobr>', and so forth.
1167 <nobr>Then it</nobr> executes the statements in its body. The body of a
1168 <a href="../reference/let.htm">let</a> follows exactly the same rules that
1169 a function body does. Some examples:</p>
1170
1171 <pre class="example">
1172 &gt; (let ((a 3)) (+ a 1))
1173 4
1174
1175 &gt; (let ((a 2)
1176 (b 3)
1177 (c 0))
1178 (setq c (+ a b))
1179 c)
1180 5
1181
1182 &gt; (setq c 4)
1183 4
1184
1185 &gt; (let ((c 5))
1186 c)
1187 5
1188
1189 &gt; c
1190 4
1191 </pre>
1192
1193 <p>See <a href="../reference/addition.htm">&nbsp;+&nbsp;</a>,
1194 <a href="../reference/let.htm">let</a>,
1195 <a href="../reference/setq.htm">setq</a>. <nobr>Instead of:</nobr></p>
1196
1197 <pre class="example">
1198 (let ((a nil)
1199 (b nil))
1200 <font color="#008844">...</font> )
1201 </pre>
1202
1203 <p>you can write:</p>
1204
1205 <pre class="example">
1206 (let (a b)
1207 <font color="#008844">...</font> )
1208 </pre>
1209
1210 <p>The '<nobr>value-1</nobr>', '<nobr>value-2</nobr>', etc. inside a
1211 <a href="../reference/let.htm">let</a> form cannot reference the variables
1212 '<nobr>variable-1</nobr>', '<nobr>variable-2</nobr>', etc. that the
1213 <a href="../reference/let.htm">let</a> form is binding. <nobr>For
1214 example:</nobr></p>
1215
1216 <pre class="example">
1217 &gt; (let ((x 1)
1218 (y (+ <font color="#AA0000">x</font> 1))) <font color="#008844">; x is still unbound here</font>
1219 y)
1220 <font color="#AA0000">error: unbound variable - x</font>
1221 </pre>
1222
1223 <p>If the symbol 'x' already has a global value, stranger happenings will
1224 result:</p>
1225
1226 <pre class="example">
1227 &gt; (setq x 7)
1228 7
1229
1230 &gt; (let ((x 1)
1231 (y (+ <font color="#AA0000">x</font> 1))) <font color="#008844">; references to the global x</font>
1232 y)
1233 8
1234 </pre>
1235
1236 <p>The <a href="../reference/let-star.htm">let*</a> special form is just
1237 like <a href="../reference/let.htm">let</a> except that it allows values to
1238 reference variables defined earlier in the
1239 <a href="../reference/let-star.htm">let*</a> form.
1240 <nobr>For example:</nobr></p>
1241
1242 <pre class="example">
1243 &gt; (setq x 7)
1244 7
1245
1246 &gt; (let* ((x 1)
1247 (y (+ x 1))) <font color="#008844">; references to x in the line before</font>
1248 y)
1249 2
1250 </pre>
1251
1252 <p>The <a href="../reference/let-star.htm">let*</a> form:</p>
1253
1254 <pre class="example">
1255 (let* ((x a)
1256 (y b))
1257 <font color="#008844">...</font> )
1258 </pre>
1259
1260 <p>is equivalent to the following <a href="../reference/let.htm">let</a>
1261 construct:</p>
1262
1263 <pre class="example">
1264 (let ((x a))
1265 (let ((y b))
1266 <font color="#008844">...</font> ))
1267 </pre>
1268
1269 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1270
1271 <a name="10"></a>
1272
1273 <hr>
1274
1275 <h2>10&nbsp; Dynamic Scoping</h2>
1276
1277 <hr>
1278
1279 <p>The <a href="../reference/let.htm">let</a> and
1280 <a href="../reference/let-star.htm">let*</a> forms provide lexical scoping,
1281 which is what you expect if you're used to programming in C or Pascal.
1282 Dynamic scoping is what you get in BASIC. <nobr>If you</nobr> assign a value
1283 to a dynamically scoped variable, every mention of that variable returns
1284 that value until you assign another value to the same variable.</p>
1285
1286 <p>In Lisp, dynamically scoped variables are called 'special' variables.
1287 <nobr>In Common Lisp</nobr> special variables are declared with the 'defvar'
1288 special form. <nobr>In Nyquist</nobr> there is no 'defvar' form.</p>
1289
1290 <p><div class="box">
1291
1292 <p>Nyquist has no 'dynamic' scoping in the <nobr>Common Lisp</nobr> sense.</p>
1293
1294 </div></p>
1295
1296 <p>In Nyquist every variable assigned with
1297 <a href="../reference/setq.htm">setq</a> or
1298 <a href="../reference/setf.htm">setf</a> at the <nobr>top-level</nobr>,
1299 outside of a function or a <a href="../reference/let.htm">let</a>
1300 binding, is a lexical scoped variable. Here is an example what this means:</p>
1301
1302 <pre class="example">
1303 &gt; (setq *variable* 5) <font color="#008844">; define a global variable</font>
1304 5
1305
1306 &gt; (defun check-variable () <font color="#008844">; define a function in global scope,</font>
1307 *variable*) <font color="#008844">; returning the value of the variable</font>
1308 CHECK-VARIABLE
1309
1310 &gt; (check-variable) <font color="#008844">; the CHECK-VARIABLE function returns</font>
1311 5 <font color="#008844">; the global value of the variable</font>
1312
1313 &gt; (let ((*variable* 10)) <font color="#008844">; create a local binding for the variable</font>
1314 (print (check-variable)) <font color="#008844">; call CHECK-VARIABLE and print the return value</font>
1315 (print *variable*)) <font color="#008844">; print the local value of the variable</font>
1316 5 <font color="#008844">; return value of CHECK-VARIABLE</font>
1317 10 <font color="#008844">; variable value inside of LET</font>
1318 10
1319 </pre>
1320
1321 <p>See <a href="../reference/defun.htm">defun</a>,
1322 <a href="../reference/let.htm">let</a>,
1323 <a href="../reference/print.htm">print</a>,
1324 <a href="../reference/setq.htm">setq</a>. Because the
1325 '<nobr>check-variable</nobr>' function was defined in global
1326 scope and therefore is lexically outside of the
1327 <a href="../reference/let.htm">let</a> form, the
1328 '<nobr>check-variable</nobr>' function returns the variable's global value
1329 <nobr>of 5</nobr>, even if called from inside the
1330 <a href="../reference/let.htm">let</a> form, where the variable has a
1331 <nobr>value of 10</nobr>.</p>
1332
1333 <p><div class="box">
1334
1335 <p><b>Important:</b> In Nyquist there is no way to change the scoping
1336 behaviour of variables, so you must be careful where you define your
1337 variables. With Nyquist it's generally a good idea to prefer local
1338 <a href="../reference/let.htm">let</a> bindings over global variables.</p>
1339
1340 </div></p>
1341
1342 <p>By convention, the name of a global Nyquist variable begins and ends with
1343 a <nobr>star *</nobr> to signal that the variable might behave differently
1344 than the programmer expects.</p>
1345
1346 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1347
1348 <a name="11"></a>
1349
1350 <hr>
1351
1352 <h2>11&nbsp; Arrays</h2>
1353
1354 <hr>
1355
1356 <p>The function
1357 <nobr><a href="../reference/make-array.htm">make-array</a></nobr> makes a
1358 <nobr>1-dimensional</nobr> array.
1359 <nobr>The <a href="../reference/aref.htm">aref</a></nobr> function accesses
1360 its elements. <nobr>All elements</nobr> of an array are initially set
1361 <nobr>to <a href="../reference/nil.htm">NIL</a></nobr>.
1362 <nobr>For example:</nobr></p>
1363
1364 <pre class="example">
1365 &gt; (make-array 4) <font color="#008844">; 1-D array with 4 elements</font>
1366 #(NIL NIL NIL NIL)
1367 </pre>
1368
1369 <p>Array indices always <nobr>start at 0</nobr>. <nobr>See
1370 <a href="#13">below</a></nobr> for how to set the elements of an array.</p>
1371
1372 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1373
1374 <a name="12"></a>
1375
1376 <hr>
1377
1378 <h2>12&nbsp; Strings</h2>
1379
1380 <hr>
1381
1382 <p>A string is a sequence of characters between double quotes. Nyquist
1383 represents a string internally as a <nobr>variable-length</nobr> array of
1384 characters. <nobr>You can</nobr> write a string containing a double quote by
1385 preceding the quote with a backslash. <nobr>A double</nobr> backslash stands
1386 for a single backslash. <nobr>For example:</nobr></p>
1387
1388 <p><table cellpadding="0" cellspacing="0"><tbody>
1389 <tr valign="top">
1390 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
1391 <td class="button"><nobr><code>"abcd"</code></nobr></td>
1392 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
1393 <td width="100%"><nobr>has 4 characters</nobr></td>
1394 </tr>
1395 <tr>
1396 <td height="2px"></td>
1397 </tr>
1398 <tr valign="top">
1399 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
1400 <td class="button"><nobr><code>"\""</code></nobr></td>
1401 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
1402 <td width="100%"><nobr>has 1 character, a quote</nobr></td>
1403 </tr>
1404 <tr>
1405 <td height="2px"></td>
1406 </tr>
1407 <tr valign="top">
1408 <td><nobr><code>&nbsp;&nbsp;</code></nobr></td>
1409 <td class="button"><nobr><code>"\\"</code></nobr></td>
1410 <td><nobr>&nbsp;&nbsp;-&nbsp;</nobr></td>
1411 <td width="100%"><nobr>has 1 character, a backslash</nobr></td>
1412 </tr>
1413 </tbody></table></p>
1414
1415 <p>Here are some functions for dealing with strings:</p>
1416
1417 <pre class="example">
1418 &gt; (strcat "abcd" "efg")
1419 "abcdefg" <font color="#008844">; STRCAT concatenates strings</font>
1420
1421 &gt; (char "abc" 1)
1422 #\b <font color="#008844">; Lisp writes characters preceded by #\</font>
1423
1424 &gt; (subseq "abc" 0 2)
1425 "ab" <font color="#008844">; SUBSEQ extracts substrings</font>
1426 </pre>
1427
1428 <p>See <a href="../reference/char.htm">char</a>,
1429 <a href="../reference/strcat.htm">strcat</a>,
1430 <a href="../reference/subseq.htm">subseq</a>.</p>
1431
1432 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1433
1434 <a name="13"></a>
1435
1436 <hr>
1437
1438 <h2>13&nbsp; Setf</h2>
1439
1440 <hr>
1441
1442 <p>Certain forms in Lisp naturally define a memory location. For example, if
1443 the value of 'x' is a <a href="../reference/list.htm">list</a>, then
1444 <nobr>(<a href="../reference/nth.htm">nth</a> 4 x)</nobr> defines the fifth
1445 element of the <a href="../reference/list.htm">list</a>. <nobr>Or, if</nobr>
1446 the value of 'y' is a <nobr>one-dimensional</nobr>
1447 <a href="../reference/make-array.htm">array</a>,
1448 <nobr>(<a href="../reference/aref.htm">aref</a> y 2)</nobr> defines the
1449 third element of the <a href="../reference/make-array.htm">array</a>.</p>
1450
1451 <p>The <a href="../reference/setf.htm">setf</a> special form uses its first
1452 argument to define a place in memory, evaluates its second argument, and
1453 stores the resulting value in the resulting memory location. <nobr>For
1454 example:</nobr></p>
1455
1456 <pre class="example">
1457 &gt; (setq a (make-array 3))
1458 #(NIL NIL NIL)
1459
1460 &gt; (aref a 1)
1461 NIL
1462
1463 &gt; (setf (aref a 1) 3) <font color="#008844">; store 3 in the second element of a</font>
1464 3
1465
1466 &gt; a
1467 #(NIL 3 NIL)
1468
1469 &gt; (aref a 1) <font color="#008844">; read the second element of a</font>
1470 3
1471
1472 &gt; (setq b (list 1 2 3 4 5))
1473 (1 2 3 4 5)
1474
1475 &gt; (nth 4 b)
1476 5
1477
1478 &gt; (setf (nth 4 b) "five") <font color="#008844">; store "five" in the fifth element of b</font>
1479 "five"
1480
1481 &gt; b
1482 (1 2 3 4 "five")
1483
1484 &gt; (nth 4 b)
1485 "five"
1486 </pre>
1487
1488 <p>The <a href="../reference/setf.htm">setf</a> function is the only way to
1489 set the elements of a list or an array.</p>
1490
1491 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1492
1493 <a name="14"></a>
1494
1495 <hr>
1496
1497 <h2>14&nbsp; Booleans and Conditionals</h2>
1498
1499 <hr>
1500
1501 <p>Lisp uses the <nobr>self-evaluating</nobr> symbol
1502 <a href="../reference/nil.htm">NIL</a> to mean false. Anything other than
1503 <nobr>self-evaluating</nobr> means true. Unless we have a reason not to,
1504 we usually use the <nobr>self-evaluating</nobr> symbol
1505 <a href="../reference/t.htm">&nbsp;T&nbsp;</a> to stand
1506 <nobr>for true</nobr>.</p>
1507
1508 <p>Lisp provides a standard set of logical functions, for example
1509 <a href="../reference/and.htm">and</a>,
1510 <a href="../reference/or.htm">or</a>, and
1511 <a href="../reference/not.htm">not</a>.
1512 <nobr>The <a href="../reference/and.htm">and</a></nobr> and
1513 <a href="../reference/or.htm">or</a> connectives are
1514 <nobr>short-circuiting</nobr>, <a href="../reference/and.htm">and</a>
1515 will not evaluate any arguments to the right of the first one which
1516 evaluates <nobr>to <a href="../reference/nil.htm">NIL</a></nobr>, while
1517 <a href="../reference/or.htm">or</a> will not evaluate any arguments to the
1518 right of the first one which evaluates
1519 <nobr>to <a href="../reference/t.htm">&nbsp;T&nbsp;</a></nobr>.</p>
1520
1521 <p>Lisp also provides several special forms for conditional execution. The
1522 simplest of these <nobr>is
1523 <a href="../reference/if.htm">&nbsp;if&nbsp;</a></nobr>. The first argument
1524 of <a href="../reference/if.htm">&nbsp;if&nbsp;</a> determines whether the
1525 second or third argument will be executed:</p>
1526
1527 <pre class="example">
1528 &gt; (if t 5 6)
1529 5
1530
1531 &gt; (if nil 5 6)
1532 6
1533
1534 &gt; (if 4 5 6)
1535 5
1536 </pre>
1537
1538 <p>If you need to put more than one statement in the 'then' or 'else' clause
1539 of an <a href="../reference/if.htm">&nbsp;if&nbsp;</a></nobr> statement,
1540 you can use the <a href="../reference/progn.htm">progn</a> special form.
1541 <a href="../reference/progn.htm">progn</a> executes each statement in its
1542 body, then returns the value of the <nobr>final one</nobr>:</p>
1543
1544 <pre class="example">
1545 &gt; (setq a 7)
1546 7
1547
1548 &gt; (setq b 0)
1549 0
1550
1551 &gt; (setq c 5)
1552 5
1553
1554 &gt; (if (&gt; a 5)
1555 (progn
1556 (setq a (+ b 7))
1557 (setq b (+ c 8)))
1558 (setq b 4))
1559 13
1560 </pre>
1561
1562 <p>An <a href="../reference/if.htm">&nbsp;if&nbsp;</a> statement which lacks
1563 either a 'then' or an 'else' clause can be written using the
1564 <a href="../reference/when.htm">when</a> or
1565 <a href="../reference/unless.htm">unless</a> special form:</p>
1566
1567 <pre class="example">
1568 &gt; (when t 3)
1569 3
1570
1571 &gt; (when nil 3)
1572 NIL
1573
1574 &gt; (unless t 3)
1575 NIL
1576
1577 &gt; (unless nil 3)
1578 3
1579 </pre>
1580
1581 <p><a href="../reference/when.htm">when</a> and
1582 <a href="../reference/unless.htm">unless</a>, unlike
1583 <a href="../reference/if.htm">&nbsp;if&nbsp;</a>, allow any number of
1584 statements in their bodies:</p>
1585
1586 <pre class="example">
1587 (when x
1588 a
1589 b
1590 c)
1591 </pre>
1592
1593 <p>is equivalent to:</p>
1594
1595 <pre class="example">
1596 (if x
1597 (progn
1598 a
1599 b
1600 c))
1601 </pre>
1602
1603 <p>For example:</p>
1604
1605 <pre class="example">
1606 &gt; (when t
1607 (setq a 5)
1608 (+ a 6))
1609 11
1610 </pre>
1611
1612 <p>More complicated conditionals can be defined using the
1613 <a href="../reference/cond.htm">cond</a> special form.
1614 <nobr>A <a href="../reference/cond.htm">cond</a></nobr> form consists of
1615 the symbol cond followed by a number of
1616 <a href="../reference/cond.htm">cond</a> clauses, each of which is a list.
1617 <nobr>The first</nobr> element of a <a href="../reference/cond.htm">cond</a>
1618 clause is the condition, the remaining elements <nobr>[if any]</nobr> are
1619 the actions:</p>
1620
1621 <pre class="example">
1622 (cond (<font color="#0000CC">condition-1 action-1</font>)
1623 (<font color="#0000CC">condition-2 action-2</font>)
1624 ...
1625 (t <font color="#0000CC">default-action</font>))
1626 </pre>
1627
1628 <p>The <a href="../reference/cond.htm">cond</a> form finds the first clause
1629 whose condition evaluates to true [does not evaluate <nobr>to
1630 <a href="../reference/nil.htm">NIL</a>]</nobr>. <nobr>It then</nobr>
1631 executes the corresponding action and returns the resulting value. None of
1632 the remaining conditions are evaluated, nor are any actions except the one
1633 corresponding to the selected condition. <nobr>For example</nobr>:</p>
1634
1635 <pre class="example">
1636 &gt; (setq a 3)
1637 3
1638
1639 &gt; (cond
1640 ((evenp a) a) <font color="#008844">; if a is even return a</font>
1641 ((&gt; a 7) (/ a 2)) <font color="#008844">; else if a is bigger than 7 return a/2</font>
1642 ((&lt; a 5) (- a 1)) <font color="#008844">; else if a is smaller than 5 return a-1</font>
1643 (t 17)) <font color="#008844">; else return 17</font>
1644 2
1645 </pre>
1646
1647 <p>If the action in the selected <a href="../reference/cond.htm">cond</a>
1648 clause is missing, then <a href="../reference/cond.htm">cond</a> returns
1649 what the condition <nobr>evaluated to:</nobr></p>
1650
1651 <pre class="example">
1652 &gt; (cond ((+ 3 4)))
1653 7
1654 </pre>
1655
1656 <p>The Lisp <a href="../reference/case.htm">case</a> form is like a C
1657 'switch' statement:</p>
1658
1659 <pre class="example">
1660 &gt; (setq x 'b)
1661 B
1662
1663 &gt; (case x
1664 (a 5)
1665 ((d e) 7)
1666 ((b f) 3)
1667 (t 9))
1668 3
1669 </pre>
1670
1671 <p>The <a href="../reference/t.htm">&nbsp;T&nbsp;</a> clause at the end
1672 means that if 'x' is not 'a', '<nobr>d or e</nobr>', or
1673 '<nobr>b or f</nobr>', then the <a href="../reference/case.htm">case</a>
1674 form will <nobr>return 9</nobr>.</p>
1675
1676 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1677
1678 <a name="15"></a>
1679
1680 <hr>
1681
1682 <h2>15&nbsp; Iteration</h2>
1683
1684 <hr>
1685
1686 <p>The simplest iteration construct in Lisp is
1687 <a href="../reference/loop.htm">loop</a>.
1688 <nobr>A <a href="../reference/loop.htm">loop</a></nobr> construct repeatedly
1689 executes its body until it hits a
1690 <a href="../reference/return.htm">return</a> special form.
1691 <nobr>For example</nobr>:</p>
1692
1693 <pre class="example">
1694 &gt; (setq a 4)
1695 4
1696
1697 &gt; (loop
1698 (setq a (+ a 1))
1699 (when (&gt; a 7) (return a)))
1700 8
1701
1702 &gt; (loop
1703 (setq a (- a 1))
1704 (when (&lt; a 3) (return)))
1705 NIL
1706 </pre>
1707
1708 <p>The next simplest is <a href="../reference/dolist.htm">dolist</a>.
1709 <nobr>It binds</nobr> a variable to the elements of a list in order and
1710 stops when it hits the end of <nobr>the list</nobr>:</p>
1711
1712 <pre class="example">
1713 &gt; (dolist (x '(a b c))
1714 (print x))
1715 A
1716 B
1717 C
1718 NIL
1719 </pre>
1720
1721 <p><a href="../reference/dolist.htm">dolist</a> always
1722 <nobr>returns <a href="../reference/nil.htm">NIL</a></nobr>. Note that the
1723 value of 'x' in the above example was
1724 <nobr>never <a href="../reference/nil.htm">NIL</a></nobr>.
1725 <nobr>The <a href="../reference/nil.htm">NIL</a></nobr> below the C was the
1726 value that <a href="../reference/dolist.htm">dolist</a> returned, printed
1727 by the <nobr>read-eval-print</nobr> loop.</p>
1728
1729 <p>The most flexible, but also most complicated iteration form is
1730 <nobr>called <a href="../reference/do.htm">do</a></nobr>.
1731 <nobr>A <a href="../reference/do.htm">do</a></nobr> form looks
1732 <nobr>like this</nobr>:</p>
1733
1734 <pre class="example">
1735 &gt; (do ((x 1 (+ x 1)) <font color="#008844">; variable x, initial value 1, update with (+ x 1)</font>
1736 (y 1 (* y 2))) <font color="#008844">; variable y, initial value 1, update with (* y 2)</font>
1737 ((&gt; x 5) y) <font color="#008844">; terminate if (&gt; x 5), return the value of y</font>
1738 (print y)
1739 (print 'working))
1740 1
1741 WORKING
1742 2
1743 WORKING
1744 4
1745 WORKING
1746 8
1747 WORKING
1748 16
1749 WORKING
1750 32
1751 </pre>
1752
1753 <p>The first part of a <a href="../reference/do.htm">do</a> form specifies
1754 what variables to bind, what their initial values are, and how to update
1755 them. <nobr>The second</nobr> part specifies a termination condition and a
1756 return value. The last part is the body. <nobr>A
1757 <a href="../reference/do.htm">do</a></nobr> form binds its variables to
1758 their initial values like
1759 <nobr>a <a href="../reference/let.htm">let</a></nobr>, then checks the
1760 termination condition. <nobr>As long</nobr> as the condition is false, it
1761 executes the body repeatedly. When the condition becomes true, it
1762 returns the value of the <nobr>return-value</nobr> form.</p>
1763
1764 <p>The <a href="../reference/do-star.htm">do*</a> form is to
1765 <a href="../reference/do.htm">do</a> as
1766 <a href="../reference/let-star.htm">let*</a> is
1767 <nobr>to <a href="../reference/let.htm">let</a></nobr>.</p>
1768
1769 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1770
1771 <a name="16"></a>
1772
1773 <hr>
1774
1775 <h2>16&nbsp; Non-local Exits</h2>
1776
1777 <hr>
1778
1779 <p>The <a href="../reference/return.htm">return</a> special form is an
1780 example of a nonlocal return. Another example is
1781 <nobr><a href="../reference/return-from.htm">return-from</a></nobr>, which
1782 returns a value from the surrounding function:</p>
1783
1784 <pre class="example">
1785 &gt; (defun foo (x)
1786 (return-from foo 3)
1787 x)
1788 FOO
1789
1790 &gt; (foo 17)
1791 3
1792 </pre>
1793
1794 <p>Actually, the <nobr><a
1795 href="../reference/return-from.htm">return-from</a></nobr> form can return
1796 from any named block, it's just that functions are the only blocks which are
1797 named by default. <nobr>You can</nobr> create a named block with the
1798 <a href="../reference/block.htm">block</a> special form:</p>
1799
1800 <pre class="example">
1801 &gt; (block foo
1802 (return-from foo 7)
1803 3)
1804 7
1805 </pre>
1806
1807 <p>The <a href="../reference/return.htm">return</a> special form can return
1808 from any block <nobr>named <a href="../reference/nil.htm">NIL</a></nobr>.
1809 Loops are by default
1810 <nobr>named <a href="../reference/nil.htm">NIL</a></nobr>, but you can
1811 make your own
1812 <nobr><a href="../reference/nil.htm">NIL</a>-named blocks</nobr>:</p>
1813
1814 <pre class="example">
1815 &gt; (block nil
1816 (return 7)
1817 3)
1818 7
1819 </pre>
1820
1821 <p>Another form which causes a nonlocal exit is the
1822 <nobr><a href="../reference/error.htm">error</a> form</nobr>:</p>
1823
1824 <pre class="example">
1825 &gt; (error "This is an error")
1826 <font color="#AA0000">error: This is an error</font>
1827 </pre>
1828
1829 <p>The <a href="../reference/error.htm">error</a> form applies format to its
1830 arguments, then places you in the debugger.</p>
1831
1832 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1833
1834 <a name="17"></a>
1835
1836 <hr>
1837
1838 <h2>17&nbsp; Funcall, Apply, and Mapcar</h2>
1839
1840 <hr>
1841
1842 <p>Earlier I promised to give some functions which take functions as
1843 arguments. Here they are:</p>
1844
1845 <pre class="example">
1846 &gt; (funcall #'+ 3 4)
1847 7
1848
1849 &gt; (apply #'+ 3 4 '(3 4))
1850 14
1851
1852 &gt; (mapcar #'not '(t nil t nil t nil))
1853 (NIL T NIL T NIL T)
1854 </pre>
1855
1856 <p><a href="../reference/funcall.htm">funcall</a> calls its first argument
1857 on its remaining arguments.</p>
1858
1859 <p><a href="../reference/apply.htm">apply</a> is just like
1860 <a href="../reference/funcall.htm">funcall</a>, except that its final
1861 argument should be a list. <nobr>The elements</nobr> of that list are
1862 treated as if they were additional arguments to
1863 <nobr>a <a href="../reference/funcall.htm">funcall</a></nobr>.</p>
1864
1865 <p>The first argument to <a href="../reference/mapcar.htm">mapcar</a> must
1866 be a function of one argument, <a href="../reference/mapcar.htm">mapcar</a>
1867 applies this function to each element of a list and collects the results in
1868 another list.</p>
1869
1870 <p><a href="../reference/funcall.htm">funcall</a> and
1871 <a href="../reference/apply.htm">apply</a> are chiefly useful when their
1872 first argument is a variable. <nobr>For instance</nobr>, a search engine
1873 could take a heuristic function as a parameter and use
1874 <a href="../reference/funcall.htm">funcall</a> or
1875 <a href="../reference/apply.htm">apply</a> to call that function on a
1876 state description. <nobr>The sorting</nobr> functions described later use
1877 <a href="../reference/funcall.htm">funcall</a> to call their comparison
1878 functions.</p>
1879
1880 <p><a href="../reference/mapcar.htm">mapcar</a>, along with nameless
1881 <a href="#18">lambda</a> functions, can replace many loops.</p>
1882
1883 <p><div class="box">
1884
1885 <p><b>Nyquist/XLISP:</b> <nobr>In XLISP</nobr>, a '<nobr>special
1886 form</nobr>' of type FSUBR is not a function. This means that
1887 <a href="../reference/apply.htm">apply</a>,
1888 <a href="../reference/funcall.htm">funcall</a> and
1889 <a href="../reference/mapcar.htm">mapcar</a> only work with functions
1890 of type SUBR [built-in function] or CLOSURE [function defined by
1891 <a href="../reference/defun.htm">defun</a>,
1892 <a href="../reference/flet.htm">flet</a>,
1893 <a href="../reference/labels.htm">labels</a>, or
1894 <a href="../reference/lambda.htm">lambda</a>], but with special forms
1895 of <nobr>type FSUBR</nobr> a 'bad argument type' error is signalled.</p>
1896
1897 </div></p>
1898
1899 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1900
1901 <a name="18"></a>
1902
1903 <hr>
1904
1905 <h2>18&nbsp; Lambda</h2>
1906
1907 <hr>
1908
1909 <p>If you just want to create a temporary function and don't want to
1910 bother giving it a name, <a href="../reference/lambda.htm">lambda</a> is
1911 what you need.</p>
1912
1913 <pre class="example">
1914 &gt; #'(lambda (x)
1915 (+ x 3))
1916 #&lt;Closure: #88b71ece&gt;
1917
1918 &gt; (funcall * 5)
1919 8
1920 </pre>
1921
1922 <p>The combination of <a href="../reference/lambda.htm">lambda</a> and
1923 <a href="../reference/mapcar.htm">mapcar</a> can replace many loops.
1924 <nobr>For example</nobr>, the following two forms are equivalent:</p>
1925
1926 <pre class="example">
1927 &gt; (do ((x '(1 2 3 4 5) (cdr x))
1928 (y nil))
1929 ((null x) (reverse y))
1930 (push (+ (car x) 2) y))
1931 (3 4 5 6 7)
1932
1933 &gt; (mapcar #'(lambda (x) (+ x 2)) '(1 2 3 4 5))
1934 (3 4 5 6 7)
1935 </pre>
1936
1937 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1938
1939 <a name="19"></a>
1940
1941 <hr>
1942
1943 <h2>19&nbsp; Sorting</h2>
1944
1945 <hr>
1946
1947 <p>Lisp provides a primitive for
1948 <nobr>sorting, <a href="../reference/sort.htm">sort</a></nobr>:</p>
1949
1950 <pre class="example">
1951 &gt; (sort '(2 1 5 4 6) #'&lt;)
1952 (1 2 4 5 6)
1953
1954 &gt; (sort '(2 1 5 4 6) #'&gt;)
1955 (6 5 4 2 1)
1956 </pre>
1957
1958 <p>The first argument to <a href="../reference/sort.htm">sort</a> is a list,
1959 the second is a comparison function. <nobr>Be careful</nobr>, because
1960 <a href="../reference/sort.htm">sort</a> is allowed to destroy its argument,
1961 so if the original sequence is important to you, make a copy before sorting
1962 <nobr>the list</nobr>.</p>
1963
1964 <p><div class="box">
1965
1966 <p><b>Bug:</b> In Nyquist 3.03 [November 2010] the XLISP
1967 <a href="../reference/sort.htm">sort</a> function has a bug, so it's better
1968 to store the return value of sort in the original variable <nobr>like
1969 this</nobr>:</p>
1970
1971 <pre class="example">
1972 (setq a '(3 1 4 1 5 9 6 7)) =&gt; (3 1 4 1 5 9 6 7)
1973 (setq a (sort a '&lt;)) =&gt; (1 1 3 4 5 6 7 9)
1974 a =&gt; (1 1 3 4 5 6 7 9)
1975 </pre>
1976
1977 </div></p>
1978
1979 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
1980
1981 <a name="20"></a>
1982
1983 <hr>
1984
1985 <h2>20&nbsp; Equality</h2>
1986
1987 <hr>
1988
1989 <p>Lisp has many different ideas of equality. Numerical equality is
1990 denoted by =. Two symbols are eq if and only if they are identical. Two
1991 copies of the same list are not eq, but they are equal.</p>
1992
1993 <pre class="example">
1994 &gt; (eq 'a 'a)
1995 T
1996
1997 &gt; (eq 'a 'b)
1998 NIL
1999
2000 &gt; (= 3 4)
2001 T
2002
2003 &gt; (eq '(a b c) '(a b c))
2004 NIL
2005
2006 &gt; (equal '(a b c) '(a b c))
2007 T
2008
2009 &gt; (eql 'a 'a)
2010 T
2011
2012 &gt; (eql 3 3)
2013 T
2014 </pre>
2015
2016 <p>The eql predicate is equivalent to eq for symbols and to = for numbers.</p>
2017
2018 <p>The equal predicate is equivalent to eql for symbols and numbers. It is
2019 true for two conses if and only if their cars are equal and their cdrs are
2020 equal.</p>
2021
2022 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
2023
2024 <a name="21"></a>
2025
2026 <hr>
2027
2028 <h2>21&nbsp; Some Useful List Functions</h2>
2029
2030 <hr>
2031
2032 <p>These functions all manipulate lists:</p>
2033
2034 <pre class="example">
2035 &gt; (append '(1 2 3) '(4 5 6)) ;concatenate lists
2036 (1 2 3 4 5 6)
2037
2038 &gt; (reverse '(1 2 3)) ;reverse the elements of a list
2039 (3 2 1)
2040
2041 &gt; (member 'a '(b d a c)) ;set membership -- returns the first tail
2042 (A C) ;whose car is the desired element
2043 </pre>
2044
2045 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
2046
2047 <hr>
2048
2049 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
2050 <a href="../manual/contents.htm">Contents</a> |
2051 <a href="tutorials.htm">Tutorials</a> |
2052 <a href="../examples/examples.htm">Examples</a> |
2053 <a href="../reference/reference-index.htm">Reference</a>
2054
2055 </body></html>