Mercurial > emacs
annotate man/cl.texi @ 76519:2ce1d66ecddf
* autorevert.el (auto-revert-tail-handler): Call after-revert-hook.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Sat, 17 Mar 2007 20:38:04 +0000 |
parents | 4ad431d8e164 |
children | 88224dc74061 95d0cdf160ea |
rev | line source |
---|---|
25829 | 1 \input texinfo @c -*-texinfo-*- |
2 @setfilename ../info/cl | |
3 @settitle Common Lisp Extensions | |
4 | |
47737
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
5 @copying |
25829 | 6 This file documents the GNU Emacs Common Lisp emulation package. |
7 | |
75249
4ad431d8e164
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
74889
diff
changeset
|
8 Copyright @copyright{} 1993, 2001, 2002, 2003, 2004, 2005, 2006, 2007 |
4ad431d8e164
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
74889
diff
changeset
|
9 Free Software Foundation, Inc. |
47737
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
10 |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
11 @quotation |
32315 | 12 Permission is granted to copy, distribute and/or modify this document |
65555
69b3598a61c5
Update all manuals to specify GFDL version 1.2.
Romain Francoise <romain@orebokech.com>
parents:
64890
diff
changeset
|
13 under the terms of the GNU Free Documentation License, Version 1.2 or |
32315 | 14 any later version published by the Free Software Foundation; with no |
15 Invariant Sections, with the Front-Cover texts being ``A GNU | |
16 Manual'', and with the Back-Cover Texts as in (a) below. A copy of the | |
17 license is included in the section entitled ``GNU Free Documentation | |
18 License'' in the Emacs manual. | |
19 | |
20 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify | |
21 this GNU Manual, like GNU software. Copies published by the Free | |
22 Software Foundation raise funds for GNU development.'' | |
23 | |
24 This document is part of a collection distributed under the GNU Free | |
25 Documentation License. If you want to distribute this document | |
26 separately from the collection, you can do so by adding a copy of the | |
27 license to the document, as described in section 6 of the license. | |
47737
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
28 @end quotation |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
29 @end copying |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
30 |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
31 @dircategory Emacs |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
32 @direntry |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
33 * CL: (cl). Partial Common Lisp support for Emacs Lisp. |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
34 @end direntry |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
35 |
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
36 @finalout |
25829 | 37 |
38 @titlepage | |
39 @sp 6 | |
40 @center @titlefont{Common Lisp Extensions} | |
41 @sp 4 | |
42 @center For GNU Emacs Lisp | |
43 @sp 1 | |
44 @center Version 2.02 | |
45 @sp 5 | |
46 @center Dave Gillespie | |
47 @center daveg@@synaptics.com | |
48 @page | |
49 @vskip 0pt plus 1filll | |
47737
0a70200bde27
use @copying instead of @ifinfo.
Karl Berry <karl@gnu.org>
parents:
42136
diff
changeset
|
50 @insertcopying |
25829 | 51 @end titlepage |
52 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
53 @node Top, Overview, (dir), (dir) |
55833
1986f67220dd
(Top): Call this chapter `Introduction'.
Richard M. Stallman <rms@gnu.org>
parents:
54379
diff
changeset
|
54 @chapter Introduction |
25829 | 55 |
56 @noindent | |
57 This document describes a set of Emacs Lisp facilities borrowed from | |
58 Common Lisp. All the facilities are described here in detail. While | |
59 this document does not assume any prior knowledge of Common Lisp, it | |
60 does assume a basic familiarity with Emacs Lisp. | |
61 | |
62 @menu | |
63 * Overview:: Installation, usage, etc. | |
64 * Program Structure:: Arglists, `eval-when', `defalias' | |
65 * Predicates:: `typep', `eql', and `equalp' | |
66 * Control Structure:: `setf', `do', `loop', etc. | |
67 * Macros:: Destructuring, `define-compiler-macro' | |
68 * Declarations:: `proclaim', `declare', etc. | |
69 * Symbols:: Property lists, `gensym' | |
70 * Numbers:: Predicates, functions, random numbers | |
71 * Sequences:: Mapping, functions, searching, sorting | |
72 * Lists:: `cadr', `sublis', `member*', `assoc*', etc. | |
73 * Structures:: `defstruct' | |
74 * Assertions:: `check-type', `assert', `ignore-errors'. | |
75 | |
76 * Efficiency Concerns:: Hints and techniques | |
77 * Common Lisp Compatibility:: All known differences with Steele | |
78 * Old CL Compatibility:: All known differences with old cl.el | |
79 * Porting Common Lisp:: Hints for porting Common Lisp code | |
80 | |
81 * Function Index:: | |
82 * Variable Index:: | |
83 @end menu | |
84 | |
85 @node Overview, Program Structure, Top, Top | |
55833
1986f67220dd
(Top): Call this chapter `Introduction'.
Richard M. Stallman <rms@gnu.org>
parents:
54379
diff
changeset
|
86 @ifnottex |
25829 | 87 @chapter Overview |
55833
1986f67220dd
(Top): Call this chapter `Introduction'.
Richard M. Stallman <rms@gnu.org>
parents:
54379
diff
changeset
|
88 @end ifnottex |
25829 | 89 |
90 @noindent | |
91 Common Lisp is a huge language, and Common Lisp systems tend to be | |
92 massive and extremely complex. Emacs Lisp, by contrast, is rather | |
93 minimalist in the choice of Lisp features it offers the programmer. | |
94 As Emacs Lisp programmers have grown in number, and the applications | |
95 they write have grown more ambitious, it has become clear that Emacs | |
96 Lisp could benefit from many of the conveniences of Common Lisp. | |
97 | |
98 The @dfn{CL} package adds a number of Common Lisp functions and | |
99 control structures to Emacs Lisp. While not a 100% complete | |
100 implementation of Common Lisp, @dfn{CL} adds enough functionality | |
101 to make Emacs Lisp programming significantly more convenient. | |
102 | |
40590
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
103 @strong{Please note:} the @dfn{CL} functions are not standard parts of |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
104 the Emacs Lisp name space, so it is legitimate for users to define |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
105 them with other, conflicting meanings. To avoid conflicting with |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
106 those user activities, we have a policy that packages installed in |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
107 Emacs must not load @dfn{CL} at run time. (It is ok for them to load |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
108 @dfn{CL} at compile time only, with @code{eval-when-compile}, and use |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
109 the macros it provides.) If you are writing packages that you plan to |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
110 distribute and invite widespread use for, you might want to observe |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
111 the same rule. |
e9ca70d27e37
Explain when not to use CL.
Richard M. Stallman <rms@gnu.org>
parents:
36931
diff
changeset
|
112 |
25829 | 113 Some Common Lisp features have been omitted from this package |
114 for various reasons: | |
115 | |
116 @itemize @bullet | |
117 @item | |
118 Some features are too complex or bulky relative to their benefit | |
119 to Emacs Lisp programmers. CLOS and Common Lisp streams are fine | |
120 examples of this group. | |
121 | |
122 @item | |
123 Other features cannot be implemented without modification to the | |
124 Emacs Lisp interpreter itself, such as multiple return values, | |
125 lexical scoping, case-insensitive symbols, and complex numbers. | |
126 The @dfn{CL} package generally makes no attempt to emulate these | |
127 features. | |
128 | |
129 @item | |
130 Some features conflict with existing things in Emacs Lisp. For | |
131 example, Emacs' @code{assoc} function is incompatible with the | |
132 Common Lisp @code{assoc}. In such cases, this package usually | |
133 adds the suffix @samp{*} to the function name of the Common | |
134 Lisp version of the function (e.g., @code{assoc*}). | |
135 @end itemize | |
136 | |
137 The package described here was written by Dave Gillespie, | |
138 @file{daveg@@synaptics.com}. It is a total rewrite of the original | |
139 1986 @file{cl.el} package by Cesar Quiroz. Most features of the | |
64455
c0646b78dca7
(Overview): Delete duplicate words.
Juri Linkov <juri@jurta.org>
parents:
64366
diff
changeset
|
140 Quiroz package have been retained; any incompatibilities are |
25829 | 141 noted in the descriptions below. Care has been taken in this |
142 version to ensure that each function is defined efficiently, | |
143 concisely, and with minimal impact on the rest of the Emacs | |
144 environment. | |
145 | |
146 @menu | |
147 * Usage:: How to use the CL package | |
148 * Organization:: The package's five component files | |
149 * Installation:: Compiling and installing CL | |
150 * Naming Conventions:: Notes on CL function names | |
151 @end menu | |
152 | |
153 @node Usage, Organization, Overview, Overview | |
154 @section Usage | |
155 | |
156 @noindent | |
157 Lisp code that uses features from the @dfn{CL} package should | |
158 include at the beginning: | |
159 | |
160 @example | |
161 (require 'cl) | |
162 @end example | |
163 | |
164 @noindent | |
165 If you want to ensure that the new (Gillespie) version of @dfn{CL} | |
166 is the one that is present, add an additional @code{(require 'cl-19)} | |
167 call: | |
168 | |
169 @example | |
170 (require 'cl) | |
171 (require 'cl-19) | |
172 @end example | |
173 | |
174 @noindent | |
175 The second call will fail (with ``@file{cl-19.el} not found'') if | |
176 the old @file{cl.el} package was in use. | |
177 | |
178 It is safe to arrange to load @dfn{CL} at all times, e.g., | |
179 in your @file{.emacs} file. But it's a good idea, for portability, | |
180 to @code{(require 'cl)} in your code even if you do this. | |
181 | |
182 @node Organization, Installation, Usage, Overview | |
183 @section Organization | |
184 | |
185 @noindent | |
186 The Common Lisp package is organized into four files: | |
187 | |
188 @table @file | |
189 @item cl.el | |
190 This is the ``main'' file, which contains basic functions | |
191 and information about the package. This file is relatively | |
192 compact---about 700 lines. | |
193 | |
194 @item cl-extra.el | |
195 This file contains the larger, more complex or unusual functions. | |
196 It is kept separate so that packages which only want to use Common | |
197 Lisp fundamentals like the @code{cadr} function won't need to pay | |
198 the overhead of loading the more advanced functions. | |
199 | |
200 @item cl-seq.el | |
201 This file contains most of the advanced functions for operating | |
202 on sequences or lists, such as @code{delete-if} and @code{assoc*}. | |
203 | |
204 @item cl-macs.el | |
205 This file contains the features of the packages which are macros | |
206 instead of functions. Macros expand when the caller is compiled, | |
207 not when it is run, so the macros generally only need to be | |
208 present when the byte-compiler is running (or when the macros are | |
209 used in uncompiled code such as a @file{.emacs} file). Most of | |
210 the macros of this package are isolated in @file{cl-macs.el} so | |
211 that they won't take up memory unless you are compiling. | |
212 @end table | |
213 | |
214 The file @file{cl.el} includes all necessary @code{autoload} | |
215 commands for the functions and macros in the other three files. | |
216 All you have to do is @code{(require 'cl)}, and @file{cl.el} | |
217 will take care of pulling in the other files when they are | |
218 needed. | |
219 | |
220 There is another file, @file{cl-compat.el}, which defines some | |
221 routines from the older @file{cl.el} package that are no longer | |
222 present in the new package. This includes internal routines | |
223 like @code{setelt} and @code{zip-lists}, deprecated features | |
224 like @code{defkeyword}, and an emulation of the old-style | |
225 multiple-values feature. @xref{Old CL Compatibility}. | |
226 | |
227 @node Installation, Naming Conventions, Organization, Overview | |
228 @section Installation | |
229 | |
230 @noindent | |
231 Installation of the @dfn{CL} package is simple: Just put the | |
232 byte-compiled files @file{cl.elc}, @file{cl-extra.elc}, | |
233 @file{cl-seq.elc}, @file{cl-macs.elc}, and @file{cl-compat.elc} | |
234 into a directory on your @code{load-path}. | |
235 | |
236 There are no special requirements to compile this package: | |
237 The files do not have to be loaded before they are compiled, | |
238 nor do they need to be compiled in any particular order. | |
239 | |
240 You may choose to put the files into your main @file{lisp/} | |
241 directory, replacing the original @file{cl.el} file there. Or, | |
242 you could put them into a directory that comes before @file{lisp/} | |
243 on your @code{load-path} so that the old @file{cl.el} is | |
244 effectively hidden. | |
245 | |
246 Also, format the @file{cl.texinfo} file and put the resulting | |
247 Info files in the @file{info/} directory or another suitable place. | |
248 | |
249 You may instead wish to leave this package's components all in | |
250 their own directory, and then add this directory to your | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
251 @code{load-path} and @code{Info-directory-list}. |
25829 | 252 Add the directory to the front of the list so the old @dfn{CL} |
253 package and its documentation are hidden. | |
254 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
255 @node Naming Conventions, , Installation, Overview |
25829 | 256 @section Naming Conventions |
257 | |
258 @noindent | |
259 Except where noted, all functions defined by this package have the | |
260 same names and calling conventions as their Common Lisp counterparts. | |
261 | |
262 Following is a complete list of functions whose names were changed | |
263 from Common Lisp, usually to avoid conflicts with Emacs. In each | |
264 case, a @samp{*} has been appended to the Common Lisp name to obtain | |
265 the Emacs name: | |
266 | |
267 @example | |
268 defun* defsubst* defmacro* function* | |
269 member* assoc* rassoc* get* | |
270 remove* delete* mapcar* sort* | |
271 floor* ceiling* truncate* round* | |
36931 | 272 mod* rem* random* |
25829 | 273 @end example |
274 | |
275 Internal function and variable names in the package are prefixed | |
276 by @code{cl-}. Here is a complete list of functions @emph{not} | |
277 prefixed by @code{cl-} which were not taken from Common Lisp: | |
278 | |
279 @example | |
28039
c3a446101beb
Remove references to rassoc, delete, expt.
Dave Love <fx@gnu.org>
parents:
27511
diff
changeset
|
280 floatp-safe lexical-let lexical-let* |
25829 | 281 callf callf2 letf letf* |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
282 defsubst* |
25829 | 283 @end example |
284 | |
285 The following simple functions and macros are defined in @file{cl.el}; | |
286 they do not cause other components like @file{cl-extra} to be loaded. | |
287 | |
288 @example | |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
289 eql floatp-safe endp |
25829 | 290 evenp oddp plusp minusp |
35089 | 291 caaar .. cddddr |
25829 | 292 list* ldiff rest first .. tenth |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
293 copy-list subst mapcar* [2] |
25829 | 294 adjoin [3] acons pairlis pop [4] |
295 push [4] pushnew [3,4] incf [4] decf [4] | |
296 proclaim declaim | |
297 @end example | |
298 | |
299 @noindent | |
300 [2] Only for one sequence argument or two list arguments. | |
301 | |
302 @noindent | |
303 [3] Only if @code{:test} is @code{eq}, @code{equal}, or unspecified, | |
304 and @code{:key} is not used. | |
305 | |
306 @noindent | |
307 [4] Only when @var{place} is a plain variable name. | |
308 | |
309 @iftex | |
310 @chapno=4 | |
311 @end iftex | |
312 | |
313 @node Program Structure, Predicates, Overview, Top | |
314 @chapter Program Structure | |
315 | |
316 @noindent | |
317 This section describes features of the @dfn{CL} package which have to | |
318 do with programs as a whole: advanced argument lists for functions, | |
319 and the @code{eval-when} construct. | |
320 | |
321 @menu | |
322 * Argument Lists:: `&key', `&aux', `defun*', `defmacro*'. | |
323 * Time of Evaluation:: The `eval-when' construct. | |
324 @end menu | |
325 | |
326 @iftex | |
327 @secno=1 | |
328 @end iftex | |
329 | |
330 @node Argument Lists, Time of Evaluation, Program Structure, Program Structure | |
331 @section Argument Lists | |
332 | |
333 @noindent | |
334 Emacs Lisp's notation for argument lists of functions is a subset of | |
335 the Common Lisp notation. As well as the familiar @code{&optional} | |
336 and @code{&rest} markers, Common Lisp allows you to specify default | |
337 values for optional arguments, and it provides the additional markers | |
338 @code{&key} and @code{&aux}. | |
339 | |
340 Since argument parsing is built-in to Emacs, there is no way for | |
341 this package to implement Common Lisp argument lists seamlessly. | |
342 Instead, this package defines alternates for several Lisp forms | |
343 which you must use if you need Common Lisp argument lists. | |
344 | |
345 @defspec defun* name arglist body... | |
346 This form is identical to the regular @code{defun} form, except | |
347 that @var{arglist} is allowed to be a full Common Lisp argument | |
348 list. Also, the function body is enclosed in an implicit block | |
349 called @var{name}; @pxref{Blocks and Exits}. | |
350 @end defspec | |
351 | |
352 @defspec defsubst* name arglist body... | |
353 This is just like @code{defun*}, except that the function that | |
354 is defined is automatically proclaimed @code{inline}, i.e., | |
355 calls to it may be expanded into in-line code by the byte compiler. | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
356 This is analogous to the @code{defsubst} form; |
25829 | 357 @code{defsubst*} uses a different method (compiler macros) which |
358 works in all version of Emacs, and also generates somewhat more | |
359 efficient inline expansions. In particular, @code{defsubst*} | |
360 arranges for the processing of keyword arguments, default values, | |
361 etc., to be done at compile-time whenever possible. | |
362 @end defspec | |
363 | |
364 @defspec defmacro* name arglist body... | |
365 This is identical to the regular @code{defmacro} form, | |
366 except that @var{arglist} is allowed to be a full Common Lisp | |
367 argument list. The @code{&environment} keyword is supported as | |
368 described in Steele. The @code{&whole} keyword is supported only | |
369 within destructured lists (see below); top-level @code{&whole} | |
370 cannot be implemented with the current Emacs Lisp interpreter. | |
371 The macro expander body is enclosed in an implicit block called | |
372 @var{name}. | |
373 @end defspec | |
374 | |
375 @defspec function* symbol-or-lambda | |
376 This is identical to the regular @code{function} form, | |
377 except that if the argument is a @code{lambda} form then that | |
378 form may use a full Common Lisp argument list. | |
379 @end defspec | |
380 | |
381 Also, all forms (such as @code{defsetf} and @code{flet}) defined | |
382 in this package that include @var{arglist}s in their syntax allow | |
383 full Common Lisp argument lists. | |
384 | |
385 Note that it is @emph{not} necessary to use @code{defun*} in | |
386 order to have access to most @dfn{CL} features in your function. | |
387 These features are always present; @code{defun*}'s only | |
388 difference from @code{defun} is its more flexible argument | |
389 lists and its implicit block. | |
390 | |
391 The full form of a Common Lisp argument list is | |
392 | |
393 @example | |
394 (@var{var}... | |
395 &optional (@var{var} @var{initform} @var{svar})... | |
396 &rest @var{var} | |
397 &key ((@var{keyword} @var{var}) @var{initform} @var{svar})... | |
398 &aux (@var{var} @var{initform})...) | |
399 @end example | |
400 | |
401 Each of the five argument list sections is optional. The @var{svar}, | |
402 @var{initform}, and @var{keyword} parts are optional; if they are | |
403 omitted, then @samp{(@var{var})} may be written simply @samp{@var{var}}. | |
404 | |
405 The first section consists of zero or more @dfn{required} arguments. | |
406 These arguments must always be specified in a call to the function; | |
407 there is no difference between Emacs Lisp and Common Lisp as far as | |
408 required arguments are concerned. | |
409 | |
410 The second section consists of @dfn{optional} arguments. These | |
411 arguments may be specified in the function call; if they are not, | |
412 @var{initform} specifies the default value used for the argument. | |
413 (No @var{initform} means to use @code{nil} as the default.) The | |
414 @var{initform} is evaluated with the bindings for the preceding | |
415 arguments already established; @code{(a &optional (b (1+ a)))} | |
416 matches one or two arguments, with the second argument defaulting | |
417 to one plus the first argument. If the @var{svar} is specified, | |
418 it is an auxiliary variable which is bound to @code{t} if the optional | |
419 argument was specified, or to @code{nil} if the argument was omitted. | |
420 If you don't use an @var{svar}, then there will be no way for your | |
421 function to tell whether it was called with no argument, or with | |
422 the default value passed explicitly as an argument. | |
423 | |
424 The third section consists of a single @dfn{rest} argument. If | |
425 more arguments were passed to the function than are accounted for | |
426 by the required and optional arguments, those extra arguments are | |
427 collected into a list and bound to the ``rest'' argument variable. | |
428 Common Lisp's @code{&rest} is equivalent to that of Emacs Lisp. | |
429 Common Lisp accepts @code{&body} as a synonym for @code{&rest} in | |
430 macro contexts; this package accepts it all the time. | |
431 | |
432 The fourth section consists of @dfn{keyword} arguments. These | |
433 are optional arguments which are specified by name rather than | |
434 positionally in the argument list. For example, | |
435 | |
436 @example | |
437 (defun* foo (a &optional b &key c d (e 17))) | |
438 @end example | |
439 | |
440 @noindent | |
441 defines a function which may be called with one, two, or more | |
442 arguments. The first two arguments are bound to @code{a} and | |
443 @code{b} in the usual way. The remaining arguments must be | |
444 pairs of the form @code{:c}, @code{:d}, or @code{:e} followed | |
445 by the value to be bound to the corresponding argument variable. | |
446 (Symbols whose names begin with a colon are called @dfn{keywords}, | |
447 and they are self-quoting in the same way as @code{nil} and | |
448 @code{t}.) | |
449 | |
450 For example, the call @code{(foo 1 2 :d 3 :c 4)} sets the five | |
451 arguments to 1, 2, 4, 3, and 17, respectively. If the same keyword | |
452 appears more than once in the function call, the first occurrence | |
453 takes precedence over the later ones. Note that it is not possible | |
454 to specify keyword arguments without specifying the optional | |
455 argument @code{b} as well, since @code{(foo 1 :c 2)} would bind | |
456 @code{b} to the keyword @code{:c}, then signal an error because | |
457 @code{2} is not a valid keyword. | |
458 | |
459 If a @var{keyword} symbol is explicitly specified in the argument | |
460 list as shown in the above diagram, then that keyword will be | |
461 used instead of just the variable name prefixed with a colon. | |
462 You can specify a @var{keyword} symbol which does not begin with | |
463 a colon at all, but such symbols will not be self-quoting; you | |
464 will have to quote them explicitly with an apostrophe in the | |
465 function call. | |
466 | |
467 Ordinarily it is an error to pass an unrecognized keyword to | |
468 a function, e.g., @code{(foo 1 2 :c 3 :goober 4)}. You can ask | |
469 Lisp to ignore unrecognized keywords, either by adding the | |
470 marker @code{&allow-other-keys} after the keyword section | |
471 of the argument list, or by specifying an @code{:allow-other-keys} | |
472 argument in the call whose value is non-@code{nil}. If the | |
473 function uses both @code{&rest} and @code{&key} at the same time, | |
474 the ``rest'' argument is bound to the keyword list as it appears | |
475 in the call. For example: | |
476 | |
477 @smallexample | |
478 (defun* find-thing (thing &rest rest &key need &allow-other-keys) | |
479 (or (apply 'member* thing thing-list :allow-other-keys t rest) | |
480 (if need (error "Thing not found")))) | |
481 @end smallexample | |
482 | |
483 @noindent | |
484 This function takes a @code{:need} keyword argument, but also | |
485 accepts other keyword arguments which are passed on to the | |
486 @code{member*} function. @code{allow-other-keys} is used to | |
487 keep both @code{find-thing} and @code{member*} from complaining | |
488 about each others' keywords in the arguments. | |
489 | |
490 The fifth section of the argument list consists of @dfn{auxiliary | |
491 variables}. These are not really arguments at all, but simply | |
492 variables which are bound to @code{nil} or to the specified | |
493 @var{initforms} during execution of the function. There is no | |
494 difference between the following two functions, except for a | |
495 matter of stylistic taste: | |
496 | |
497 @example | |
498 (defun* foo (a b &aux (c (+ a b)) d) | |
499 @var{body}) | |
500 | |
501 (defun* foo (a b) | |
502 (let ((c (+ a b)) d) | |
503 @var{body})) | |
504 @end example | |
505 | |
506 Argument lists support @dfn{destructuring}. In Common Lisp, | |
507 destructuring is only allowed with @code{defmacro}; this package | |
508 allows it with @code{defun*} and other argument lists as well. | |
509 In destructuring, any argument variable (@var{var} in the above | |
510 diagram) can be replaced by a list of variables, or more generally, | |
511 a recursive argument list. The corresponding argument value must | |
512 be a list whose elements match this recursive argument list. | |
513 For example: | |
514 | |
515 @example | |
516 (defmacro* dolist ((var listform &optional resultform) | |
517 &rest body) | |
518 ...) | |
519 @end example | |
520 | |
521 This says that the first argument of @code{dolist} must be a list | |
522 of two or three items; if there are other arguments as well as this | |
523 list, they are stored in @code{body}. All features allowed in | |
524 regular argument lists are allowed in these recursive argument lists. | |
525 In addition, the clause @samp{&whole @var{var}} is allowed at the | |
526 front of a recursive argument list. It binds @var{var} to the | |
527 whole list being matched; thus @code{(&whole all a b)} matches | |
528 a list of two things, with @code{a} bound to the first thing, | |
529 @code{b} bound to the second thing, and @code{all} bound to the | |
530 list itself. (Common Lisp allows @code{&whole} in top-level | |
531 @code{defmacro} argument lists as well, but Emacs Lisp does not | |
532 support this usage.) | |
533 | |
534 One last feature of destructuring is that the argument list may be | |
535 dotted, so that the argument list @code{(a b . c)} is functionally | |
536 equivalent to @code{(a b &rest c)}. | |
537 | |
538 If the optimization quality @code{safety} is set to 0 | |
539 (@pxref{Declarations}), error checking for wrong number of | |
540 arguments and invalid keyword arguments is disabled. By default, | |
541 argument lists are rigorously checked. | |
542 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
543 @node Time of Evaluation, , Argument Lists, Program Structure |
25829 | 544 @section Time of Evaluation |
545 | |
546 @noindent | |
547 Normally, the byte-compiler does not actually execute the forms in | |
548 a file it compiles. For example, if a file contains @code{(setq foo t)}, | |
549 the act of compiling it will not actually set @code{foo} to @code{t}. | |
550 This is true even if the @code{setq} was a top-level form (i.e., not | |
551 enclosed in a @code{defun} or other form). Sometimes, though, you | |
552 would like to have certain top-level forms evaluated at compile-time. | |
553 For example, the compiler effectively evaluates @code{defmacro} forms | |
554 at compile-time so that later parts of the file can refer to the | |
555 macros that are defined. | |
556 | |
557 @defspec eval-when (situations...) forms... | |
558 This form controls when the body @var{forms} are evaluated. | |
559 The @var{situations} list may contain any set of the symbols | |
560 @code{compile}, @code{load}, and @code{eval} (or their long-winded | |
561 ANSI equivalents, @code{:compile-toplevel}, @code{:load-toplevel}, | |
562 and @code{:execute}). | |
563 | |
564 The @code{eval-when} form is handled differently depending on | |
565 whether or not it is being compiled as a top-level form. | |
566 Specifically, it gets special treatment if it is being compiled | |
567 by a command such as @code{byte-compile-file} which compiles files | |
568 or buffers of code, and it appears either literally at the | |
569 top level of the file or inside a top-level @code{progn}. | |
570 | |
571 For compiled top-level @code{eval-when}s, the body @var{forms} are | |
572 executed at compile-time if @code{compile} is in the @var{situations} | |
573 list, and the @var{forms} are written out to the file (to be executed | |
574 at load-time) if @code{load} is in the @var{situations} list. | |
575 | |
576 For non-compiled-top-level forms, only the @code{eval} situation is | |
577 relevant. (This includes forms executed by the interpreter, forms | |
578 compiled with @code{byte-compile} rather than @code{byte-compile-file}, | |
579 and non-top-level forms.) The @code{eval-when} acts like a | |
580 @code{progn} if @code{eval} is specified, and like @code{nil} | |
581 (ignoring the body @var{forms}) if not. | |
582 | |
583 The rules become more subtle when @code{eval-when}s are nested; | |
584 consult Steele (second edition) for the gruesome details (and | |
585 some gruesome examples). | |
586 | |
587 Some simple examples: | |
588 | |
589 @example | |
590 ;; Top-level forms in foo.el: | |
591 (eval-when (compile) (setq foo1 'bar)) | |
592 (eval-when (load) (setq foo2 'bar)) | |
593 (eval-when (compile load) (setq foo3 'bar)) | |
594 (eval-when (eval) (setq foo4 'bar)) | |
595 (eval-when (eval compile) (setq foo5 'bar)) | |
596 (eval-when (eval load) (setq foo6 'bar)) | |
597 (eval-when (eval compile load) (setq foo7 'bar)) | |
598 @end example | |
599 | |
600 When @file{foo.el} is compiled, these variables will be set during | |
601 the compilation itself: | |
602 | |
603 @example | |
604 foo1 foo3 foo5 foo7 ; `compile' | |
605 @end example | |
606 | |
607 When @file{foo.elc} is loaded, these variables will be set: | |
608 | |
609 @example | |
610 foo2 foo3 foo6 foo7 ; `load' | |
611 @end example | |
612 | |
613 And if @file{foo.el} is loaded uncompiled, these variables will | |
614 be set: | |
615 | |
616 @example | |
617 foo4 foo5 foo6 foo7 ; `eval' | |
618 @end example | |
619 | |
620 If these seven @code{eval-when}s had been, say, inside a @code{defun}, | |
621 then the first three would have been equivalent to @code{nil} and the | |
622 last four would have been equivalent to the corresponding @code{setq}s. | |
623 | |
624 Note that @code{(eval-when (load eval) @dots{})} is equivalent | |
625 to @code{(progn @dots{})} in all contexts. The compiler treats | |
626 certain top-level forms, like @code{defmacro} (sort-of) and | |
627 @code{require}, as if they were wrapped in @code{(eval-when | |
628 (compile load eval) @dots{})}. | |
629 @end defspec | |
630 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
631 Emacs includes two special forms related to @code{eval-when}. |
25829 | 632 One of these, @code{eval-when-compile}, is not quite equivalent to |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
633 any @code{eval-when} construct and is described below. |
25829 | 634 |
635 The other form, @code{(eval-and-compile @dots{})}, is exactly | |
636 equivalent to @samp{(eval-when (compile load eval) @dots{})} and | |
637 so is not itself defined by this package. | |
638 | |
639 @defspec eval-when-compile forms... | |
640 The @var{forms} are evaluated at compile-time; at execution time, | |
641 this form acts like a quoted constant of the resulting value. Used | |
642 at top-level, @code{eval-when-compile} is just like @samp{eval-when | |
643 (compile eval)}. In other contexts, @code{eval-when-compile} | |
644 allows code to be evaluated once at compile-time for efficiency | |
645 or other reasons. | |
646 | |
647 This form is similar to the @samp{#.} syntax of true Common Lisp. | |
648 @end defspec | |
649 | |
650 @defspec load-time-value form | |
651 The @var{form} is evaluated at load-time; at execution time, | |
652 this form acts like a quoted constant of the resulting value. | |
653 | |
654 Early Common Lisp had a @samp{#,} syntax that was similar to | |
655 this, but ANSI Common Lisp replaced it with @code{load-time-value} | |
656 and gave it more well-defined semantics. | |
657 | |
658 In a compiled file, @code{load-time-value} arranges for @var{form} | |
659 to be evaluated when the @file{.elc} file is loaded and then used | |
660 as if it were a quoted constant. In code compiled by | |
661 @code{byte-compile} rather than @code{byte-compile-file}, the | |
662 effect is identical to @code{eval-when-compile}. In uncompiled | |
663 code, both @code{eval-when-compile} and @code{load-time-value} | |
664 act exactly like @code{progn}. | |
665 | |
666 @example | |
667 (defun report () | |
668 (insert "This function was executed on: " | |
669 (current-time-string) | |
670 ", compiled on: " | |
671 (eval-when-compile (current-time-string)) | |
672 ;; or '#.(current-time-string) in real Common Lisp | |
673 ", and loaded on: " | |
674 (load-time-value (current-time-string)))) | |
675 @end example | |
676 | |
677 @noindent | |
678 Byte-compiled, the above defun will result in the following code | |
679 (or its compiled equivalent, of course) in the @file{.elc} file: | |
680 | |
681 @example | |
682 (setq --temp-- (current-time-string)) | |
683 (defun report () | |
684 (insert "This function was executed on: " | |
685 (current-time-string) | |
686 ", compiled on: " | |
687 '"Wed Jun 23 18:33:43 1993" | |
688 ", and loaded on: " | |
689 --temp--)) | |
690 @end example | |
691 @end defspec | |
692 | |
693 @node Predicates, Control Structure, Program Structure, Top | |
694 @chapter Predicates | |
695 | |
696 @noindent | |
697 This section describes functions for testing whether various | |
698 facts are true or false. | |
699 | |
700 @menu | |
701 * Type Predicates:: `typep', `deftype', and `coerce' | |
702 * Equality Predicates:: `eql' and `equalp' | |
703 @end menu | |
704 | |
705 @node Type Predicates, Equality Predicates, Predicates, Predicates | |
706 @section Type Predicates | |
707 | |
708 @noindent | |
709 The @dfn{CL} package defines a version of the Common Lisp @code{typep} | |
710 predicate. | |
711 | |
712 @defun typep object type | |
713 Check if @var{object} is of type @var{type}, where @var{type} is a | |
714 (quoted) type name of the sort used by Common Lisp. For example, | |
715 @code{(typep foo 'integer)} is equivalent to @code{(integerp foo)}. | |
716 @end defun | |
717 | |
718 The @var{type} argument to the above function is either a symbol | |
719 or a list beginning with a symbol. | |
720 | |
721 @itemize @bullet | |
722 @item | |
723 If the type name is a symbol, Emacs appends @samp{-p} to the | |
724 symbol name to form the name of a predicate function for testing | |
725 the type. (Built-in predicates whose names end in @samp{p} rather | |
726 than @samp{-p} are used when appropriate.) | |
727 | |
728 @item | |
729 The type symbol @code{t} stands for the union of all types. | |
730 @code{(typep @var{object} t)} is always true. Likewise, the | |
731 type symbol @code{nil} stands for nothing at all, and | |
732 @code{(typep @var{object} nil)} is always false. | |
733 | |
734 @item | |
735 The type symbol @code{null} represents the symbol @code{nil}. | |
736 Thus @code{(typep @var{object} 'null)} is equivalent to | |
737 @code{(null @var{object})}. | |
738 | |
739 @item | |
64366
c1cb0b064667
(Type Predicates): Document `atom' type.
Eli Zaretskii <eliz@gnu.org>
parents:
61442
diff
changeset
|
740 The type symbol @code{atom} represents all objects that are not cons |
c1cb0b064667
(Type Predicates): Document `atom' type.
Eli Zaretskii <eliz@gnu.org>
parents:
61442
diff
changeset
|
741 cells. Thus @code{(typep @var{object} 'atom)} is equivalent to |
c1cb0b064667
(Type Predicates): Document `atom' type.
Eli Zaretskii <eliz@gnu.org>
parents:
61442
diff
changeset
|
742 @code{(atom @var{object})}. |
c1cb0b064667
(Type Predicates): Document `atom' type.
Eli Zaretskii <eliz@gnu.org>
parents:
61442
diff
changeset
|
743 |
c1cb0b064667
(Type Predicates): Document `atom' type.
Eli Zaretskii <eliz@gnu.org>
parents:
61442
diff
changeset
|
744 @item |
25829 | 745 The type symbol @code{real} is a synonym for @code{number}, and |
746 @code{fixnum} is a synonym for @code{integer}. | |
747 | |
748 @item | |
749 The type symbols @code{character} and @code{string-char} match | |
750 integers in the range from 0 to 255. | |
751 | |
752 @item | |
753 The type symbol @code{float} uses the @code{floatp-safe} predicate | |
754 defined by this package rather than @code{floatp}, so it will work | |
755 correctly even in Emacs versions without floating-point support. | |
756 | |
757 @item | |
758 The type list @code{(integer @var{low} @var{high})} represents all | |
759 integers between @var{low} and @var{high}, inclusive. Either bound | |
760 may be a list of a single integer to specify an exclusive limit, | |
761 or a @code{*} to specify no limit. The type @code{(integer * *)} | |
762 is thus equivalent to @code{integer}. | |
763 | |
764 @item | |
765 Likewise, lists beginning with @code{float}, @code{real}, or | |
766 @code{number} represent numbers of that type falling in a particular | |
767 range. | |
768 | |
769 @item | |
770 Lists beginning with @code{and}, @code{or}, and @code{not} form | |
771 combinations of types. For example, @code{(or integer (float 0 *))} | |
772 represents all objects that are integers or non-negative floats. | |
773 | |
774 @item | |
775 Lists beginning with @code{member} or @code{member*} represent | |
776 objects @code{eql} to any of the following values. For example, | |
777 @code{(member 1 2 3 4)} is equivalent to @code{(integer 1 4)}, | |
778 and @code{(member nil)} is equivalent to @code{null}. | |
779 | |
780 @item | |
781 Lists of the form @code{(satisfies @var{predicate})} represent | |
782 all objects for which @var{predicate} returns true when called | |
783 with that object as an argument. | |
784 @end itemize | |
785 | |
786 The following function and macro (not technically predicates) are | |
787 related to @code{typep}. | |
788 | |
789 @defun coerce object type | |
790 This function attempts to convert @var{object} to the specified | |
791 @var{type}. If @var{object} is already of that type as determined by | |
792 @code{typep}, it is simply returned. Otherwise, certain types of | |
793 conversions will be made: If @var{type} is any sequence type | |
794 (@code{string}, @code{list}, etc.) then @var{object} will be | |
795 converted to that type if possible. If @var{type} is | |
796 @code{character}, then strings of length one and symbols with | |
797 one-character names can be coerced. If @var{type} is @code{float}, | |
798 then integers can be coerced in versions of Emacs that support | |
799 floats. In all other circumstances, @code{coerce} signals an | |
800 error. | |
801 @end defun | |
802 | |
803 @defspec deftype name arglist forms... | |
804 This macro defines a new type called @var{name}. It is similar | |
805 to @code{defmacro} in many ways; when @var{name} is encountered | |
806 as a type name, the body @var{forms} are evaluated and should | |
807 return a type specifier that is equivalent to the type. The | |
808 @var{arglist} is a Common Lisp argument list of the sort accepted | |
809 by @code{defmacro*}. The type specifier @samp{(@var{name} @var{args}...)} | |
810 is expanded by calling the expander with those arguments; the type | |
811 symbol @samp{@var{name}} is expanded by calling the expander with | |
812 no arguments. The @var{arglist} is processed the same as for | |
813 @code{defmacro*} except that optional arguments without explicit | |
814 defaults use @code{*} instead of @code{nil} as the ``default'' | |
815 default. Some examples: | |
816 | |
817 @example | |
818 (deftype null () '(satisfies null)) ; predefined | |
819 (deftype list () '(or null cons)) ; predefined | |
820 (deftype unsigned-byte (&optional bits) | |
821 (list 'integer 0 (if (eq bits '*) bits (1- (lsh 1 bits))))) | |
822 (unsigned-byte 8) @equiv{} (integer 0 255) | |
823 (unsigned-byte) @equiv{} (integer 0 *) | |
824 unsigned-byte @equiv{} (integer 0 *) | |
825 @end example | |
826 | |
827 @noindent | |
828 The last example shows how the Common Lisp @code{unsigned-byte} | |
829 type specifier could be implemented if desired; this package does | |
830 not implement @code{unsigned-byte} by default. | |
831 @end defspec | |
832 | |
833 The @code{typecase} and @code{check-type} macros also use type | |
834 names. @xref{Conditionals}. @xref{Assertions}. The @code{map}, | |
835 @code{concatenate}, and @code{merge} functions take type-name | |
836 arguments to specify the type of sequence to return. @xref{Sequences}. | |
837 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
838 @node Equality Predicates, , Type Predicates, Predicates |
25829 | 839 @section Equality Predicates |
840 | |
841 @noindent | |
842 This package defines two Common Lisp predicates, @code{eql} and | |
843 @code{equalp}. | |
844 | |
845 @defun eql a b | |
846 This function is almost the same as @code{eq}, except that if @var{a} | |
847 and @var{b} are numbers of the same type, it compares them for numeric | |
848 equality (as if by @code{equal} instead of @code{eq}). This makes a | |
849 difference only for versions of Emacs that are compiled with | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
850 floating-point support. Emacs floats are allocated |
25829 | 851 objects just like cons cells, which means that @code{(eq 3.0 3.0)} |
852 will not necessarily be true---if the two @code{3.0}s were allocated | |
853 separately, the pointers will be different even though the numbers are | |
854 the same. But @code{(eql 3.0 3.0)} will always be true. | |
855 | |
856 The types of the arguments must match, so @code{(eql 3 3.0)} is | |
857 still false. | |
858 | |
859 Note that Emacs integers are ``direct'' rather than allocated, which | |
860 basically means @code{(eq 3 3)} will always be true. Thus @code{eq} | |
861 and @code{eql} behave differently only if floating-point numbers are | |
862 involved, and are indistinguishable on Emacs versions that don't | |
863 support floats. | |
864 | |
865 There is a slight inconsistency with Common Lisp in the treatment of | |
866 positive and negative zeros. Some machines, notably those with IEEE | |
867 standard arithmetic, represent @code{+0} and @code{-0} as distinct | |
868 values. Normally this doesn't matter because the standard specifies | |
869 that @code{(= 0.0 -0.0)} should always be true, and this is indeed | |
870 what Emacs Lisp and Common Lisp do. But the Common Lisp standard | |
871 states that @code{(eql 0.0 -0.0)} and @code{(equal 0.0 -0.0)} should | |
872 be false on IEEE-like machines; Emacs Lisp does not do this, and in | |
873 fact the only known way to distinguish between the two zeros in Emacs | |
874 Lisp is to @code{format} them and check for a minus sign. | |
875 @end defun | |
876 | |
877 @defun equalp a b | |
878 This function is a more flexible version of @code{equal}. In | |
879 particular, it compares strings case-insensitively, and it compares | |
880 numbers without regard to type (so that @code{(equalp 3 3.0)} is | |
881 true). Vectors and conses are compared recursively. All other | |
882 objects are compared as if by @code{equal}. | |
883 | |
884 This function differs from Common Lisp @code{equalp} in several | |
885 respects. First, Common Lisp's @code{equalp} also compares | |
886 @emph{characters} case-insensitively, which would be impractical | |
887 in this package since Emacs does not distinguish between integers | |
888 and characters. In keeping with the idea that strings are less | |
889 vector-like in Emacs Lisp, this package's @code{equalp} also will | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
890 not compare strings against vectors of integers. |
25829 | 891 @end defun |
892 | |
893 Also note that the Common Lisp functions @code{member} and @code{assoc} | |
894 use @code{eql} to compare elements, whereas Emacs Lisp follows the | |
895 MacLisp tradition and uses @code{equal} for these two functions. | |
896 In Emacs, use @code{member*} and @code{assoc*} to get functions | |
897 which use @code{eql} for comparisons. | |
898 | |
899 @node Control Structure, Macros, Predicates, Top | |
900 @chapter Control Structure | |
901 | |
902 @noindent | |
903 The features described in the following sections implement | |
904 various advanced control structures, including the powerful | |
905 @code{setf} facility and a number of looping and conditional | |
906 constructs. | |
907 | |
908 @menu | |
909 * Assignment:: The `psetq' form | |
910 * Generalized Variables:: `setf', `incf', `push', etc. | |
911 * Variable Bindings:: `progv', `lexical-let', `flet', `macrolet' | |
912 * Conditionals:: `case', `typecase' | |
913 * Blocks and Exits:: `block', `return', `return-from' | |
27511
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
914 * Iteration:: `do', `dotimes', `dolist', `do-symbols' |
25829 | 915 * Loop Facility:: The Common Lisp `loop' macro |
916 * Multiple Values:: `values', `multiple-value-bind', etc. | |
917 @end menu | |
918 | |
919 @node Assignment, Generalized Variables, Control Structure, Control Structure | |
920 @section Assignment | |
921 | |
922 @noindent | |
923 The @code{psetq} form is just like @code{setq}, except that multiple | |
924 assignments are done in parallel rather than sequentially. | |
925 | |
926 @defspec psetq [symbol form]@dots{} | |
927 This special form (actually a macro) is used to assign to several | |
928 variables simultaneously. Given only one @var{symbol} and @var{form}, | |
929 it has the same effect as @code{setq}. Given several @var{symbol} | |
930 and @var{form} pairs, it evaluates all the @var{form}s in advance | |
931 and then stores the corresponding variables afterwards. | |
932 | |
933 @example | |
934 (setq x 2 y 3) | |
935 (setq x (+ x y) y (* x y)) | |
936 x | |
937 @result{} 5 | |
938 y ; @r{@code{y} was computed after @code{x} was set.} | |
939 @result{} 15 | |
940 (setq x 2 y 3) | |
941 (psetq x (+ x y) y (* x y)) | |
942 x | |
943 @result{} 5 | |
944 y ; @r{@code{y} was computed before @code{x} was set.} | |
945 @result{} 6 | |
946 @end example | |
947 | |
948 The simplest use of @code{psetq} is @code{(psetq x y y x)}, which | |
949 exchanges the values of two variables. (The @code{rotatef} form | |
950 provides an even more convenient way to swap two variables; | |
951 @pxref{Modify Macros}.) | |
952 | |
953 @code{psetq} always returns @code{nil}. | |
954 @end defspec | |
955 | |
956 @node Generalized Variables, Variable Bindings, Assignment, Control Structure | |
957 @section Generalized Variables | |
958 | |
959 @noindent | |
960 A ``generalized variable'' or ``place form'' is one of the many places | |
961 in Lisp memory where values can be stored. The simplest place form is | |
962 a regular Lisp variable. But the cars and cdrs of lists, elements | |
963 of arrays, properties of symbols, and many other locations are also | |
964 places where Lisp values are stored. | |
965 | |
966 The @code{setf} form is like @code{setq}, except that it accepts | |
967 arbitrary place forms on the left side rather than just | |
968 symbols. For example, @code{(setf (car a) b)} sets the car of | |
969 @code{a} to @code{b}, doing the same operation as @code{(setcar a b)} | |
970 but without having to remember two separate functions for setting | |
971 and accessing every type of place. | |
972 | |
973 Generalized variables are analogous to ``lvalues'' in the C | |
974 language, where @samp{x = a[i]} gets an element from an array | |
975 and @samp{a[i] = x} stores an element using the same notation. | |
976 Just as certain forms like @code{a[i]} can be lvalues in C, there | |
977 is a set of forms that can be generalized variables in Lisp. | |
978 | |
979 @menu | |
980 * Basic Setf:: `setf' and place forms | |
981 * Modify Macros:: `incf', `push', `rotatef', `letf', `callf', etc. | |
982 * Customizing Setf:: `define-modify-macro', `defsetf', `define-setf-method' | |
983 @end menu | |
984 | |
985 @node Basic Setf, Modify Macros, Generalized Variables, Generalized Variables | |
986 @subsection Basic Setf | |
987 | |
988 @noindent | |
989 The @code{setf} macro is the most basic way to operate on generalized | |
990 variables. | |
991 | |
992 @defspec setf [place form]@dots{} | |
993 This macro evaluates @var{form} and stores it in @var{place}, which | |
994 must be a valid generalized variable form. If there are several | |
995 @var{place} and @var{form} pairs, the assignments are done sequentially | |
996 just as with @code{setq}. @code{setf} returns the value of the last | |
997 @var{form}. | |
998 | |
999 The following Lisp forms will work as generalized variables, and | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
1000 so may appear in the @var{place} argument of @code{setf}: |
25829 | 1001 |
1002 @itemize @bullet | |
1003 @item | |
1004 A symbol naming a variable. In other words, @code{(setf x y)} is | |
1005 exactly equivalent to @code{(setq x y)}, and @code{setq} itself is | |
1006 strictly speaking redundant now that @code{setf} exists. Many | |
1007 programmers continue to prefer @code{setq} for setting simple | |
1008 variables, though, purely for stylistic or historical reasons. | |
1009 The macro @code{(setf x y)} actually expands to @code{(setq x y)}, | |
1010 so there is no performance penalty for using it in compiled code. | |
1011 | |
1012 @item | |
1013 A call to any of the following Lisp functions: | |
1014 | |
1015 @smallexample | |
1016 car cdr caar .. cddddr | |
1017 nth rest first .. tenth | |
1018 aref elt nthcdr | |
1019 symbol-function symbol-value symbol-plist | |
1020 get get* getf | |
1021 gethash subseq | |
1022 @end smallexample | |
1023 | |
1024 @noindent | |
1025 Note that for @code{nthcdr} and @code{getf}, the list argument | |
1026 of the function must itself be a valid @var{place} form. For | |
1027 example, @code{(setf (nthcdr 0 foo) 7)} will set @code{foo} itself | |
1028 to 7. Note that @code{push} and @code{pop} on an @code{nthcdr} | |
1029 place can be used to insert or delete at any position in a list. | |
1030 The use of @code{nthcdr} as a @var{place} form is an extension | |
1031 to standard Common Lisp. | |
1032 | |
1033 @item | |
1034 The following Emacs-specific functions are also @code{setf}-able. | |
1035 | |
1036 @smallexample | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1037 buffer-file-name marker-position |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1038 buffer-modified-p match-data |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1039 buffer-name mouse-position |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1040 buffer-string overlay-end |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1041 buffer-substring overlay-get |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1042 current-buffer overlay-start |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1043 current-case-table point |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1044 current-column point-marker |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1045 current-global-map point-max |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1046 current-input-mode point-min |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1047 current-local-map process-buffer |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1048 current-window-configuration process-filter |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1049 default-file-modes process-sentinel |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1050 default-value read-mouse-position |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1051 documentation-property screen-height |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1052 extent-data screen-menubar |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1053 extent-end-position screen-width |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1054 extent-start-position selected-window |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1055 face-background selected-screen |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1056 face-background-pixmap selected-frame |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1057 face-font standard-case-table |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1058 face-foreground syntax-table |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1059 face-underline-p window-buffer |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1060 file-modes window-dedicated-p |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1061 frame-height window-display-table |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1062 frame-parameters window-height |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1063 frame-visible-p window-hscroll |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1064 frame-width window-point |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1065 get-register window-start |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1066 getenv window-width |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1067 global-key-binding x-get-cut-buffer |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1068 keymap-parent x-get-cutbuffer |
25829 | 1069 local-key-binding x-get-secondary-selection |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1070 mark x-get-selection |
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
1071 mark-marker |
25829 | 1072 @end smallexample |
1073 | |
1074 Most of these have directly corresponding ``set'' functions, like | |
1075 @code{use-local-map} for @code{current-local-map}, or @code{goto-char} | |
1076 for @code{point}. A few, like @code{point-min}, expand to longer | |
1077 sequences of code when they are @code{setf}'d (@code{(narrow-to-region | |
1078 x (point-max))} in this case). | |
1079 | |
1080 @item | |
1081 A call of the form @code{(substring @var{subplace} @var{n} [@var{m}])}, | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
1082 where @var{subplace} is itself a valid generalized variable whose |
25829 | 1083 current value is a string, and where the value stored is also a |
1084 string. The new string is spliced into the specified part of the | |
1085 destination string. For example: | |
1086 | |
1087 @example | |
1088 (setq a (list "hello" "world")) | |
1089 @result{} ("hello" "world") | |
1090 (cadr a) | |
1091 @result{} "world" | |
1092 (substring (cadr a) 2 4) | |
1093 @result{} "rl" | |
1094 (setf (substring (cadr a) 2 4) "o") | |
1095 @result{} "o" | |
1096 (cadr a) | |
1097 @result{} "wood" | |
1098 a | |
1099 @result{} ("hello" "wood") | |
1100 @end example | |
1101 | |
1102 The generalized variable @code{buffer-substring}, listed above, | |
1103 also works in this way by replacing a portion of the current buffer. | |
1104 | |
1105 @item | |
1106 A call of the form @code{(apply '@var{func} @dots{})} or | |
1107 @code{(apply (function @var{func}) @dots{})}, where @var{func} | |
1108 is a @code{setf}-able function whose store function is ``suitable'' | |
1109 in the sense described in Steele's book; since none of the standard | |
1110 Emacs place functions are suitable in this sense, this feature is | |
1111 only interesting when used with places you define yourself with | |
1112 @code{define-setf-method} or the long form of @code{defsetf}. | |
1113 | |
1114 @item | |
1115 A macro call, in which case the macro is expanded and @code{setf} | |
1116 is applied to the resulting form. | |
1117 | |
1118 @item | |
1119 Any form for which a @code{defsetf} or @code{define-setf-method} | |
1120 has been made. | |
1121 @end itemize | |
1122 | |
1123 Using any forms other than these in the @var{place} argument to | |
1124 @code{setf} will signal an error. | |
1125 | |
1126 The @code{setf} macro takes care to evaluate all subforms in | |
1127 the proper left-to-right order; for example, | |
1128 | |
1129 @example | |
1130 (setf (aref vec (incf i)) i) | |
1131 @end example | |
1132 | |
1133 @noindent | |
1134 looks like it will evaluate @code{(incf i)} exactly once, before the | |
1135 following access to @code{i}; the @code{setf} expander will insert | |
1136 temporary variables as necessary to ensure that it does in fact work | |
1137 this way no matter what setf-method is defined for @code{aref}. | |
1138 (In this case, @code{aset} would be used and no such steps would | |
1139 be necessary since @code{aset} takes its arguments in a convenient | |
1140 order.) | |
1141 | |
1142 However, if the @var{place} form is a macro which explicitly | |
1143 evaluates its arguments in an unusual order, this unusual order | |
1144 will be preserved. Adapting an example from Steele, given | |
1145 | |
1146 @example | |
1147 (defmacro wrong-order (x y) (list 'aref y x)) | |
1148 @end example | |
1149 | |
1150 @noindent | |
1151 the form @code{(setf (wrong-order @var{a} @var{b}) 17)} will | |
1152 evaluate @var{b} first, then @var{a}, just as in an actual call | |
1153 to @code{wrong-order}. | |
1154 @end defspec | |
1155 | |
1156 @node Modify Macros, Customizing Setf, Basic Setf, Generalized Variables | |
1157 @subsection Modify Macros | |
1158 | |
1159 @noindent | |
1160 This package defines a number of other macros besides @code{setf} | |
1161 that operate on generalized variables. Many are interesting and | |
1162 useful even when the @var{place} is just a variable name. | |
1163 | |
1164 @defspec psetf [place form]@dots{} | |
1165 This macro is to @code{setf} what @code{psetq} is to @code{setq}: | |
1166 When several @var{place}s and @var{form}s are involved, the | |
1167 assignments take place in parallel rather than sequentially. | |
1168 Specifically, all subforms are evaluated from left to right, then | |
1169 all the assignments are done (in an undefined order). | |
1170 @end defspec | |
1171 | |
1172 @defspec incf place &optional x | |
1173 This macro increments the number stored in @var{place} by one, or | |
1174 by @var{x} if specified. The incremented value is returned. For | |
1175 example, @code{(incf i)} is equivalent to @code{(setq i (1+ i))}, and | |
1176 @code{(incf (car x) 2)} is equivalent to @code{(setcar x (+ (car x) 2))}. | |
1177 | |
1178 Once again, care is taken to preserve the ``apparent'' order of | |
1179 evaluation. For example, | |
1180 | |
1181 @example | |
1182 (incf (aref vec (incf i))) | |
1183 @end example | |
1184 | |
1185 @noindent | |
1186 appears to increment @code{i} once, then increment the element of | |
1187 @code{vec} addressed by @code{i}; this is indeed exactly what it | |
1188 does, which means the above form is @emph{not} equivalent to the | |
1189 ``obvious'' expansion, | |
1190 | |
1191 @example | |
1192 (setf (aref vec (incf i)) (1+ (aref vec (incf i)))) ; Wrong! | |
1193 @end example | |
1194 | |
1195 @noindent | |
1196 but rather to something more like | |
1197 | |
1198 @example | |
1199 (let ((temp (incf i))) | |
1200 (setf (aref vec temp) (1+ (aref vec temp)))) | |
1201 @end example | |
1202 | |
1203 @noindent | |
1204 Again, all of this is taken care of automatically by @code{incf} and | |
1205 the other generalized-variable macros. | |
1206 | |
1207 As a more Emacs-specific example of @code{incf}, the expression | |
1208 @code{(incf (point) @var{n})} is essentially equivalent to | |
1209 @code{(forward-char @var{n})}. | |
1210 @end defspec | |
1211 | |
1212 @defspec decf place &optional x | |
1213 This macro decrements the number stored in @var{place} by one, or | |
1214 by @var{x} if specified. | |
1215 @end defspec | |
1216 | |
1217 @defspec pop place | |
1218 This macro removes and returns the first element of the list stored | |
1219 in @var{place}. It is analogous to @code{(prog1 (car @var{place}) | |
1220 (setf @var{place} (cdr @var{place})))}, except that it takes care | |
1221 to evaluate all subforms only once. | |
1222 @end defspec | |
1223 | |
1224 @defspec push x place | |
1225 This macro inserts @var{x} at the front of the list stored in | |
1226 @var{place}. It is analogous to @code{(setf @var{place} (cons | |
1227 @var{x} @var{place}))}, except for evaluation of the subforms. | |
1228 @end defspec | |
1229 | |
1230 @defspec pushnew x place @t{&key :test :test-not :key} | |
1231 This macro inserts @var{x} at the front of the list stored in | |
1232 @var{place}, but only if @var{x} was not @code{eql} to any | |
1233 existing element of the list. The optional keyword arguments | |
1234 are interpreted in the same way as for @code{adjoin}. | |
1235 @xref{Lists as Sets}. | |
1236 @end defspec | |
1237 | |
1238 @defspec shiftf place@dots{} newvalue | |
1239 This macro shifts the @var{place}s left by one, shifting in the | |
1240 value of @var{newvalue} (which may be any Lisp expression, not just | |
1241 a generalized variable), and returning the value shifted out of | |
1242 the first @var{place}. Thus, @code{(shiftf @var{a} @var{b} @var{c} | |
1243 @var{d})} is equivalent to | |
1244 | |
1245 @example | |
1246 (prog1 | |
1247 @var{a} | |
1248 (psetf @var{a} @var{b} | |
1249 @var{b} @var{c} | |
1250 @var{c} @var{d})) | |
1251 @end example | |
1252 | |
1253 @noindent | |
1254 except that the subforms of @var{a}, @var{b}, and @var{c} are actually | |
1255 evaluated only once each and in the apparent order. | |
1256 @end defspec | |
1257 | |
1258 @defspec rotatef place@dots{} | |
1259 This macro rotates the @var{place}s left by one in circular fashion. | |
1260 Thus, @code{(rotatef @var{a} @var{b} @var{c} @var{d})} is equivalent to | |
1261 | |
1262 @example | |
1263 (psetf @var{a} @var{b} | |
1264 @var{b} @var{c} | |
1265 @var{c} @var{d} | |
1266 @var{d} @var{a}) | |
1267 @end example | |
1268 | |
1269 @noindent | |
1270 except for the evaluation of subforms. @code{rotatef} always | |
1271 returns @code{nil}. Note that @code{(rotatef @var{a} @var{b})} | |
1272 conveniently exchanges @var{a} and @var{b}. | |
1273 @end defspec | |
1274 | |
1275 The following macros were invented for this package; they have no | |
1276 analogues in Common Lisp. | |
1277 | |
1278 @defspec letf (bindings@dots{}) forms@dots{} | |
1279 This macro is analogous to @code{let}, but for generalized variables | |
1280 rather than just symbols. Each @var{binding} should be of the form | |
1281 @code{(@var{place} @var{value})}; the original contents of the | |
1282 @var{place}s are saved, the @var{value}s are stored in them, and | |
1283 then the body @var{form}s are executed. Afterwards, the @var{places} | |
1284 are set back to their original saved contents. This cleanup happens | |
1285 even if the @var{form}s exit irregularly due to a @code{throw} or an | |
1286 error. | |
1287 | |
1288 For example, | |
1289 | |
1290 @example | |
1291 (letf (((point) (point-min)) | |
1292 (a 17)) | |
1293 ...) | |
1294 @end example | |
1295 | |
1296 @noindent | |
1297 moves ``point'' in the current buffer to the beginning of the buffer, | |
1298 and also binds @code{a} to 17 (as if by a normal @code{let}, since | |
1299 @code{a} is just a regular variable). After the body exits, @code{a} | |
1300 is set back to its original value and point is moved back to its | |
1301 original position. | |
1302 | |
1303 Note that @code{letf} on @code{(point)} is not quite like a | |
1304 @code{save-excursion}, as the latter effectively saves a marker | |
1305 which tracks insertions and deletions in the buffer. Actually, | |
1306 a @code{letf} of @code{(point-marker)} is much closer to this | |
1307 behavior. (@code{point} and @code{point-marker} are equivalent | |
1308 as @code{setf} places; each will accept either an integer or a | |
1309 marker as the stored value.) | |
1310 | |
1311 Since generalized variables look like lists, @code{let}'s shorthand | |
1312 of using @samp{foo} for @samp{(foo nil)} as a @var{binding} would | |
1313 be ambiguous in @code{letf} and is not allowed. | |
1314 | |
1315 However, a @var{binding} specifier may be a one-element list | |
1316 @samp{(@var{place})}, which is similar to @samp{(@var{place} | |
1317 @var{place})}. In other words, the @var{place} is not disturbed | |
1318 on entry to the body, and the only effect of the @code{letf} is | |
1319 to restore the original value of @var{place} afterwards. (The | |
1320 redundant access-and-store suggested by the @code{(@var{place} | |
1321 @var{place})} example does not actually occur.) | |
1322 | |
1323 In most cases, the @var{place} must have a well-defined value on | |
1324 entry to the @code{letf} form. The only exceptions are plain | |
1325 variables and calls to @code{symbol-value} and @code{symbol-function}. | |
1326 If the symbol is not bound on entry, it is simply made unbound by | |
1327 @code{makunbound} or @code{fmakunbound} on exit. | |
1328 @end defspec | |
1329 | |
1330 @defspec letf* (bindings@dots{}) forms@dots{} | |
1331 This macro is to @code{letf} what @code{let*} is to @code{let}: | |
1332 It does the bindings in sequential rather than parallel order. | |
1333 @end defspec | |
1334 | |
1335 @defspec callf @var{function} @var{place} @var{args}@dots{} | |
1336 This is the ``generic'' modify macro. It calls @var{function}, | |
1337 which should be an unquoted function name, macro name, or lambda. | |
1338 It passes @var{place} and @var{args} as arguments, and assigns the | |
1339 result back to @var{place}. For example, @code{(incf @var{place} | |
1340 @var{n})} is the same as @code{(callf + @var{place} @var{n})}. | |
1341 Some more examples: | |
1342 | |
1343 @example | |
1344 (callf abs my-number) | |
1345 (callf concat (buffer-name) "<" (int-to-string n) ">") | |
1346 (callf union happy-people (list joe bob) :test 'same-person) | |
1347 @end example | |
1348 | |
1349 @xref{Customizing Setf}, for @code{define-modify-macro}, a way | |
1350 to create even more concise notations for modify macros. Note | |
1351 again that @code{callf} is an extension to standard Common Lisp. | |
1352 @end defspec | |
1353 | |
1354 @defspec callf2 @var{function} @var{arg1} @var{place} @var{args}@dots{} | |
1355 This macro is like @code{callf}, except that @var{place} is | |
1356 the @emph{second} argument of @var{function} rather than the | |
1357 first. For example, @code{(push @var{x} @var{place})} is | |
1358 equivalent to @code{(callf2 cons @var{x} @var{place})}. | |
1359 @end defspec | |
1360 | |
1361 The @code{callf} and @code{callf2} macros serve as building | |
1362 blocks for other macros like @code{incf}, @code{pushnew}, and | |
1363 @code{define-modify-macro}. The @code{letf} and @code{letf*} | |
1364 macros are used in the processing of symbol macros; | |
1365 @pxref{Macro Bindings}. | |
1366 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
1367 @node Customizing Setf, , Modify Macros, Generalized Variables |
25829 | 1368 @subsection Customizing Setf |
1369 | |
1370 @noindent | |
1371 Common Lisp defines three macros, @code{define-modify-macro}, | |
1372 @code{defsetf}, and @code{define-setf-method}, that allow the | |
1373 user to extend generalized variables in various ways. | |
1374 | |
1375 @defspec define-modify-macro name arglist function [doc-string] | |
1376 This macro defines a ``read-modify-write'' macro similar to | |
1377 @code{incf} and @code{decf}. The macro @var{name} is defined | |
1378 to take a @var{place} argument followed by additional arguments | |
1379 described by @var{arglist}. The call | |
1380 | |
1381 @example | |
1382 (@var{name} @var{place} @var{args}...) | |
1383 @end example | |
1384 | |
1385 @noindent | |
1386 will be expanded to | |
1387 | |
1388 @example | |
1389 (callf @var{func} @var{place} @var{args}...) | |
1390 @end example | |
1391 | |
1392 @noindent | |
1393 which in turn is roughly equivalent to | |
1394 | |
1395 @example | |
1396 (setf @var{place} (@var{func} @var{place} @var{args}...)) | |
1397 @end example | |
1398 | |
1399 For example: | |
1400 | |
1401 @example | |
1402 (define-modify-macro incf (&optional (n 1)) +) | |
1403 (define-modify-macro concatf (&rest args) concat) | |
1404 @end example | |
1405 | |
1406 Note that @code{&key} is not allowed in @var{arglist}, but | |
1407 @code{&rest} is sufficient to pass keywords on to the function. | |
1408 | |
1409 Most of the modify macros defined by Common Lisp do not exactly | |
1410 follow the pattern of @code{define-modify-macro}. For example, | |
1411 @code{push} takes its arguments in the wrong order, and @code{pop} | |
1412 is completely irregular. You can define these macros ``by hand'' | |
1413 using @code{get-setf-method}, or consult the source file | |
1414 @file{cl-macs.el} to see how to use the internal @code{setf} | |
1415 building blocks. | |
1416 @end defspec | |
1417 | |
1418 @defspec defsetf access-fn update-fn | |
1419 This is the simpler of two @code{defsetf} forms. Where | |
1420 @var{access-fn} is the name of a function which accesses a place, | |
1421 this declares @var{update-fn} to be the corresponding store | |
1422 function. From now on, | |
1423 | |
1424 @example | |
1425 (setf (@var{access-fn} @var{arg1} @var{arg2} @var{arg3}) @var{value}) | |
1426 @end example | |
1427 | |
1428 @noindent | |
1429 will be expanded to | |
1430 | |
1431 @example | |
1432 (@var{update-fn} @var{arg1} @var{arg2} @var{arg3} @var{value}) | |
1433 @end example | |
1434 | |
1435 @noindent | |
1436 The @var{update-fn} is required to be either a true function, or | |
1437 a macro which evaluates its arguments in a function-like way. Also, | |
1438 the @var{update-fn} is expected to return @var{value} as its result. | |
1439 Otherwise, the above expansion would not obey the rules for the way | |
1440 @code{setf} is supposed to behave. | |
1441 | |
1442 As a special (non-Common-Lisp) extension, a third argument of @code{t} | |
1443 to @code{defsetf} says that the @code{update-fn}'s return value is | |
1444 not suitable, so that the above @code{setf} should be expanded to | |
1445 something more like | |
1446 | |
1447 @example | |
1448 (let ((temp @var{value})) | |
1449 (@var{update-fn} @var{arg1} @var{arg2} @var{arg3} temp) | |
1450 temp) | |
1451 @end example | |
1452 | |
1453 Some examples of the use of @code{defsetf}, drawn from the standard | |
1454 suite of setf methods, are: | |
1455 | |
1456 @example | |
1457 (defsetf car setcar) | |
1458 (defsetf symbol-value set) | |
1459 (defsetf buffer-name rename-buffer t) | |
1460 @end example | |
1461 @end defspec | |
1462 | |
1463 @defspec defsetf access-fn arglist (store-var) forms@dots{} | |
1464 This is the second, more complex, form of @code{defsetf}. It is | |
1465 rather like @code{defmacro} except for the additional @var{store-var} | |
1466 argument. The @var{forms} should return a Lisp form which stores | |
1467 the value of @var{store-var} into the generalized variable formed | |
1468 by a call to @var{access-fn} with arguments described by @var{arglist}. | |
1469 The @var{forms} may begin with a string which documents the @code{setf} | |
1470 method (analogous to the doc string that appears at the front of a | |
1471 function). | |
1472 | |
1473 For example, the simple form of @code{defsetf} is shorthand for | |
1474 | |
1475 @example | |
1476 (defsetf @var{access-fn} (&rest args) (store) | |
1477 (append '(@var{update-fn}) args (list store))) | |
1478 @end example | |
1479 | |
1480 The Lisp form that is returned can access the arguments from | |
1481 @var{arglist} and @var{store-var} in an unrestricted fashion; | |
1482 macros like @code{setf} and @code{incf} which invoke this | |
1483 setf-method will insert temporary variables as needed to make | |
1484 sure the apparent order of evaluation is preserved. | |
1485 | |
1486 Another example drawn from the standard package: | |
1487 | |
1488 @example | |
1489 (defsetf nth (n x) (store) | |
1490 (list 'setcar (list 'nthcdr n x) store)) | |
1491 @end example | |
1492 @end defspec | |
1493 | |
1494 @defspec define-setf-method access-fn arglist forms@dots{} | |
1495 This is the most general way to create new place forms. When | |
1496 a @code{setf} to @var{access-fn} with arguments described by | |
1497 @var{arglist} is expanded, the @var{forms} are evaluated and | |
1498 must return a list of five items: | |
1499 | |
1500 @enumerate | |
1501 @item | |
1502 A list of @dfn{temporary variables}. | |
1503 | |
1504 @item | |
1505 A list of @dfn{value forms} corresponding to the temporary variables | |
1506 above. The temporary variables will be bound to these value forms | |
1507 as the first step of any operation on the generalized variable. | |
1508 | |
1509 @item | |
1510 A list of exactly one @dfn{store variable} (generally obtained | |
1511 from a call to @code{gensym}). | |
1512 | |
1513 @item | |
1514 A Lisp form which stores the contents of the store variable into | |
1515 the generalized variable, assuming the temporaries have been | |
1516 bound as described above. | |
1517 | |
1518 @item | |
1519 A Lisp form which accesses the contents of the generalized variable, | |
1520 assuming the temporaries have been bound. | |
1521 @end enumerate | |
1522 | |
1523 This is exactly like the Common Lisp macro of the same name, | |
1524 except that the method returns a list of five values rather | |
1525 than the five values themselves, since Emacs Lisp does not | |
1526 support Common Lisp's notion of multiple return values. | |
1527 | |
1528 Once again, the @var{forms} may begin with a documentation string. | |
1529 | |
1530 A setf-method should be maximally conservative with regard to | |
1531 temporary variables. In the setf-methods generated by | |
1532 @code{defsetf}, the second return value is simply the list of | |
1533 arguments in the place form, and the first return value is a | |
1534 list of a corresponding number of temporary variables generated | |
1535 by @code{gensym}. Macros like @code{setf} and @code{incf} which | |
1536 use this setf-method will optimize away most temporaries that | |
1537 turn out to be unnecessary, so there is little reason for the | |
1538 setf-method itself to optimize. | |
1539 @end defspec | |
1540 | |
1541 @defun get-setf-method place &optional env | |
1542 This function returns the setf-method for @var{place}, by | |
1543 invoking the definition previously recorded by @code{defsetf} | |
1544 or @code{define-setf-method}. The result is a list of five | |
1545 values as described above. You can use this function to build | |
1546 your own @code{incf}-like modify macros. (Actually, it is | |
1547 better to use the internal functions @code{cl-setf-do-modify} | |
1548 and @code{cl-setf-do-store}, which are a bit easier to use and | |
1549 which also do a number of optimizations; consult the source | |
1550 code for the @code{incf} function for a simple example.) | |
1551 | |
1552 The argument @var{env} specifies the ``environment'' to be | |
1553 passed on to @code{macroexpand} if @code{get-setf-method} should | |
1554 need to expand a macro in @var{place}. It should come from | |
1555 an @code{&environment} argument to the macro or setf-method | |
1556 that called @code{get-setf-method}. | |
1557 | |
1558 See also the source code for the setf-methods for @code{apply} | |
1559 and @code{substring}, each of which works by calling | |
1560 @code{get-setf-method} on a simpler case, then massaging | |
1561 the result in various ways. | |
1562 @end defun | |
1563 | |
1564 Modern Common Lisp defines a second, independent way to specify | |
1565 the @code{setf} behavior of a function, namely ``@code{setf} | |
1566 functions'' whose names are lists @code{(setf @var{name})} | |
1567 rather than symbols. For example, @code{(defun (setf foo) @dots{})} | |
1568 defines the function that is used when @code{setf} is applied to | |
1569 @code{foo}. This package does not currently support @code{setf} | |
1570 functions. In particular, it is a compile-time error to use | |
1571 @code{setf} on a form which has not already been @code{defsetf}'d | |
1572 or otherwise declared; in newer Common Lisps, this would not be | |
1573 an error since the function @code{(setf @var{func})} might be | |
1574 defined later. | |
1575 | |
1576 @iftex | |
1577 @secno=4 | |
1578 @end iftex | |
1579 | |
1580 @node Variable Bindings, Conditionals, Generalized Variables, Control Structure | |
1581 @section Variable Bindings | |
1582 | |
1583 @noindent | |
1584 These Lisp forms make bindings to variables and function names, | |
1585 analogous to Lisp's built-in @code{let} form. | |
1586 | |
1587 @xref{Modify Macros}, for the @code{letf} and @code{letf*} forms which | |
1588 are also related to variable bindings. | |
1589 | |
1590 @menu | |
1591 * Dynamic Bindings:: The `progv' form | |
1592 * Lexical Bindings:: `lexical-let' and lexical closures | |
1593 * Function Bindings:: `flet' and `labels' | |
1594 * Macro Bindings:: `macrolet' and `symbol-macrolet' | |
1595 @end menu | |
1596 | |
1597 @node Dynamic Bindings, Lexical Bindings, Variable Bindings, Variable Bindings | |
1598 @subsection Dynamic Bindings | |
1599 | |
1600 @noindent | |
1601 The standard @code{let} form binds variables whose names are known | |
1602 at compile-time. The @code{progv} form provides an easy way to | |
1603 bind variables whose names are computed at run-time. | |
1604 | |
1605 @defspec progv symbols values forms@dots{} | |
1606 This form establishes @code{let}-style variable bindings on a | |
1607 set of variables computed at run-time. The expressions | |
1608 @var{symbols} and @var{values} are evaluated, and must return lists | |
1609 of symbols and values, respectively. The symbols are bound to the | |
1610 corresponding values for the duration of the body @var{form}s. | |
1611 If @var{values} is shorter than @var{symbols}, the last few symbols | |
1612 are made unbound (as if by @code{makunbound}) inside the body. | |
1613 If @var{symbols} is shorter than @var{values}, the excess values | |
1614 are ignored. | |
1615 @end defspec | |
1616 | |
1617 @node Lexical Bindings, Function Bindings, Dynamic Bindings, Variable Bindings | |
1618 @subsection Lexical Bindings | |
1619 | |
1620 @noindent | |
1621 The @dfn{CL} package defines the following macro which | |
1622 more closely follows the Common Lisp @code{let} form: | |
1623 | |
1624 @defspec lexical-let (bindings@dots{}) forms@dots{} | |
1625 This form is exactly like @code{let} except that the bindings it | |
1626 establishes are purely lexical. Lexical bindings are similar to | |
1627 local variables in a language like C: Only the code physically | |
1628 within the body of the @code{lexical-let} (after macro expansion) | |
1629 may refer to the bound variables. | |
1630 | |
1631 @example | |
1632 (setq a 5) | |
1633 (defun foo (b) (+ a b)) | |
1634 (let ((a 2)) (foo a)) | |
1635 @result{} 4 | |
1636 (lexical-let ((a 2)) (foo a)) | |
1637 @result{} 7 | |
1638 @end example | |
1639 | |
1640 @noindent | |
1641 In this example, a regular @code{let} binding of @code{a} actually | |
1642 makes a temporary change to the global variable @code{a}, so @code{foo} | |
1643 is able to see the binding of @code{a} to 2. But @code{lexical-let} | |
1644 actually creates a distinct local variable @code{a} for use within its | |
1645 body, without any effect on the global variable of the same name. | |
1646 | |
1647 The most important use of lexical bindings is to create @dfn{closures}. | |
1648 A closure is a function object that refers to an outside lexical | |
1649 variable. For example: | |
1650 | |
1651 @example | |
1652 (defun make-adder (n) | |
1653 (lexical-let ((n n)) | |
1654 (function (lambda (m) (+ n m))))) | |
1655 (setq add17 (make-adder 17)) | |
1656 (funcall add17 4) | |
1657 @result{} 21 | |
1658 @end example | |
1659 | |
1660 @noindent | |
1661 The call @code{(make-adder 17)} returns a function object which adds | |
1662 17 to its argument. If @code{let} had been used instead of | |
1663 @code{lexical-let}, the function object would have referred to the | |
1664 global @code{n}, which would have been bound to 17 only during the | |
1665 call to @code{make-adder} itself. | |
1666 | |
1667 @example | |
1668 (defun make-counter () | |
1669 (lexical-let ((n 0)) | |
1670 (function* (lambda (&optional (m 1)) (incf n m))))) | |
1671 (setq count-1 (make-counter)) | |
1672 (funcall count-1 3) | |
1673 @result{} 3 | |
1674 (funcall count-1 14) | |
1675 @result{} 17 | |
1676 (setq count-2 (make-counter)) | |
1677 (funcall count-2 5) | |
1678 @result{} 5 | |
1679 (funcall count-1 2) | |
1680 @result{} 19 | |
1681 (funcall count-2) | |
1682 @result{} 6 | |
1683 @end example | |
1684 | |
1685 @noindent | |
1686 Here we see that each call to @code{make-counter} creates a distinct | |
1687 local variable @code{n}, which serves as a private counter for the | |
1688 function object that is returned. | |
1689 | |
1690 Closed-over lexical variables persist until the last reference to | |
1691 them goes away, just like all other Lisp objects. For example, | |
1692 @code{count-2} refers to a function object which refers to an | |
1693 instance of the variable @code{n}; this is the only reference | |
1694 to that variable, so after @code{(setq count-2 nil)} the garbage | |
1695 collector would be able to delete this instance of @code{n}. | |
1696 Of course, if a @code{lexical-let} does not actually create any | |
1697 closures, then the lexical variables are free as soon as the | |
1698 @code{lexical-let} returns. | |
1699 | |
1700 Many closures are used only during the extent of the bindings they | |
1701 refer to; these are known as ``downward funargs'' in Lisp parlance. | |
1702 When a closure is used in this way, regular Emacs Lisp dynamic | |
1703 bindings suffice and will be more efficient than @code{lexical-let} | |
1704 closures: | |
1705 | |
1706 @example | |
1707 (defun add-to-list (x list) | |
28344
54fda0e8528a
Weed out redundant uses of `function'
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28039
diff
changeset
|
1708 (mapcar (lambda (y) (+ x y))) list) |
25829 | 1709 (add-to-list 7 '(1 2 5)) |
1710 @result{} (8 9 12) | |
1711 @end example | |
1712 | |
1713 @noindent | |
1714 Since this lambda is only used while @code{x} is still bound, | |
1715 it is not necessary to make a true closure out of it. | |
1716 | |
1717 You can use @code{defun} or @code{flet} inside a @code{lexical-let} | |
1718 to create a named closure. If several closures are created in the | |
1719 body of a single @code{lexical-let}, they all close over the same | |
1720 instance of the lexical variable. | |
1721 | |
1722 The @code{lexical-let} form is an extension to Common Lisp. In | |
1723 true Common Lisp, all bindings are lexical unless declared otherwise. | |
1724 @end defspec | |
1725 | |
1726 @defspec lexical-let* (bindings@dots{}) forms@dots{} | |
1727 This form is just like @code{lexical-let}, except that the bindings | |
1728 are made sequentially in the manner of @code{let*}. | |
1729 @end defspec | |
1730 | |
1731 @node Function Bindings, Macro Bindings, Lexical Bindings, Variable Bindings | |
1732 @subsection Function Bindings | |
1733 | |
1734 @noindent | |
1735 These forms make @code{let}-like bindings to functions instead | |
1736 of variables. | |
1737 | |
1738 @defspec flet (bindings@dots{}) forms@dots{} | |
1739 This form establishes @code{let}-style bindings on the function | |
1740 cells of symbols rather than on the value cells. Each @var{binding} | |
1741 must be a list of the form @samp{(@var{name} @var{arglist} | |
1742 @var{forms}@dots{})}, which defines a function exactly as if | |
1743 it were a @code{defun*} form. The function @var{name} is defined | |
1744 accordingly for the duration of the body of the @code{flet}; then | |
1745 the old function definition, or lack thereof, is restored. | |
1746 | |
1747 While @code{flet} in Common Lisp establishes a lexical binding of | |
1748 @var{name}, Emacs Lisp @code{flet} makes a dynamic binding. The | |
1749 result is that @code{flet} affects indirect calls to a function as | |
1750 well as calls directly inside the @code{flet} form itself. | |
1751 | |
1752 You can use @code{flet} to disable or modify the behavior of a | |
1753 function in a temporary fashion. This will even work on Emacs | |
1754 primitives, although note that some calls to primitive functions | |
1755 internal to Emacs are made without going through the symbol's | |
1756 function cell, and so will not be affected by @code{flet}. For | |
1757 example, | |
1758 | |
1759 @example | |
1760 (flet ((message (&rest args) (push args saved-msgs))) | |
1761 (do-something)) | |
1762 @end example | |
1763 | |
1764 This code attempts to replace the built-in function @code{message} | |
1765 with a function that simply saves the messages in a list rather | |
1766 than displaying them. The original definition of @code{message} | |
1767 will be restored after @code{do-something} exits. This code will | |
1768 work fine on messages generated by other Lisp code, but messages | |
1769 generated directly inside Emacs will not be caught since they make | |
1770 direct C-language calls to the message routines rather than going | |
1771 through the Lisp @code{message} function. | |
1772 | |
1773 Functions defined by @code{flet} may use the full Common Lisp | |
1774 argument notation supported by @code{defun*}; also, the function | |
1775 body is enclosed in an implicit block as if by @code{defun*}. | |
1776 @xref{Program Structure}. | |
1777 @end defspec | |
1778 | |
1779 @defspec labels (bindings@dots{}) forms@dots{} | |
1780 The @code{labels} form is like @code{flet}, except that it | |
1781 makes lexical bindings of the function names rather than | |
1782 dynamic bindings. (In true Common Lisp, both @code{flet} and | |
1783 @code{labels} make lexical bindings of slightly different sorts; | |
1784 since Emacs Lisp is dynamically bound by default, it seemed | |
1785 more appropriate for @code{flet} also to use dynamic binding. | |
1786 The @code{labels} form, with its lexical binding, is fully | |
1787 compatible with Common Lisp.) | |
1788 | |
1789 Lexical scoping means that all references to the named | |
1790 functions must appear physically within the body of the | |
1791 @code{labels} form. References may appear both in the body | |
1792 @var{forms} of @code{labels} itself, and in the bodies of | |
1793 the functions themselves. Thus, @code{labels} can define | |
1794 local recursive functions, or mutually-recursive sets of | |
1795 functions. | |
1796 | |
1797 A ``reference'' to a function name is either a call to that | |
1798 function, or a use of its name quoted by @code{quote} or | |
1799 @code{function} to be passed on to, say, @code{mapcar}. | |
1800 @end defspec | |
1801 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
1802 @node Macro Bindings, , Function Bindings, Variable Bindings |
25829 | 1803 @subsection Macro Bindings |
1804 | |
1805 @noindent | |
1806 These forms create local macros and ``symbol macros.'' | |
1807 | |
1808 @defspec macrolet (bindings@dots{}) forms@dots{} | |
1809 This form is analogous to @code{flet}, but for macros instead of | |
1810 functions. Each @var{binding} is a list of the same form as the | |
1811 arguments to @code{defmacro*} (i.e., a macro name, argument list, | |
1812 and macro-expander forms). The macro is defined accordingly for | |
1813 use within the body of the @code{macrolet}. | |
1814 | |
1815 Because of the nature of macros, @code{macrolet} is lexically | |
1816 scoped even in Emacs Lisp: The @code{macrolet} binding will | |
1817 affect only calls that appear physically within the body | |
1818 @var{forms}, possibly after expansion of other macros in the | |
1819 body. | |
1820 @end defspec | |
1821 | |
1822 @defspec symbol-macrolet (bindings@dots{}) forms@dots{} | |
1823 This form creates @dfn{symbol macros}, which are macros that look | |
1824 like variable references rather than function calls. Each | |
1825 @var{binding} is a list @samp{(@var{var} @var{expansion})}; | |
1826 any reference to @var{var} within the body @var{forms} is | |
1827 replaced by @var{expansion}. | |
1828 | |
1829 @example | |
1830 (setq bar '(5 . 9)) | |
1831 (symbol-macrolet ((foo (car bar))) | |
1832 (incf foo)) | |
1833 bar | |
1834 @result{} (6 . 9) | |
1835 @end example | |
1836 | |
1837 A @code{setq} of a symbol macro is treated the same as a @code{setf}. | |
1838 I.e., @code{(setq foo 4)} in the above would be equivalent to | |
1839 @code{(setf foo 4)}, which in turn expands to @code{(setf (car bar) 4)}. | |
1840 | |
1841 Likewise, a @code{let} or @code{let*} binding a symbol macro is | |
1842 treated like a @code{letf} or @code{letf*}. This differs from true | |
1843 Common Lisp, where the rules of lexical scoping cause a @code{let} | |
1844 binding to shadow a @code{symbol-macrolet} binding. In this package, | |
1845 only @code{lexical-let} and @code{lexical-let*} will shadow a symbol | |
1846 macro. | |
1847 | |
1848 There is no analogue of @code{defmacro} for symbol macros; all symbol | |
1849 macros are local. A typical use of @code{symbol-macrolet} is in the | |
1850 expansion of another macro: | |
1851 | |
1852 @example | |
1853 (defmacro* my-dolist ((x list) &rest body) | |
1854 (let ((var (gensym))) | |
1855 (list 'loop 'for var 'on list 'do | |
1856 (list* 'symbol-macrolet (list (list x (list 'car var))) | |
1857 body)))) | |
1858 | |
1859 (setq mylist '(1 2 3 4)) | |
1860 (my-dolist (x mylist) (incf x)) | |
1861 mylist | |
1862 @result{} (2 3 4 5) | |
1863 @end example | |
1864 | |
1865 @noindent | |
1866 In this example, the @code{my-dolist} macro is similar to @code{dolist} | |
27511
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
1867 (@pxref{Iteration}) except that the variable @code{x} becomes a true |
25829 | 1868 reference onto the elements of the list. The @code{my-dolist} call |
1869 shown here expands to | |
1870 | |
1871 @example | |
1872 (loop for G1234 on mylist do | |
1873 (symbol-macrolet ((x (car G1234))) | |
1874 (incf x))) | |
1875 @end example | |
1876 | |
1877 @noindent | |
1878 which in turn expands to | |
1879 | |
1880 @example | |
1881 (loop for G1234 on mylist do (incf (car G1234))) | |
1882 @end example | |
1883 | |
1884 @xref{Loop Facility}, for a description of the @code{loop} macro. | |
1885 This package defines a nonstandard @code{in-ref} loop clause that | |
1886 works much like @code{my-dolist}. | |
1887 @end defspec | |
1888 | |
1889 @node Conditionals, Blocks and Exits, Variable Bindings, Control Structure | |
1890 @section Conditionals | |
1891 | |
1892 @noindent | |
1893 These conditional forms augment Emacs Lisp's simple @code{if}, | |
1894 @code{and}, @code{or}, and @code{cond} forms. | |
1895 | |
1896 @defspec case keyform clause@dots{} | |
1897 This macro evaluates @var{keyform}, then compares it with the key | |
1898 values listed in the various @var{clause}s. Whichever clause matches | |
1899 the key is executed; comparison is done by @code{eql}. If no clause | |
1900 matches, the @code{case} form returns @code{nil}. The clauses are | |
1901 of the form | |
1902 | |
1903 @example | |
1904 (@var{keylist} @var{body-forms}@dots{}) | |
1905 @end example | |
1906 | |
1907 @noindent | |
1908 where @var{keylist} is a list of key values. If there is exactly | |
1909 one value, and it is not a cons cell or the symbol @code{nil} or | |
1910 @code{t}, then it can be used by itself as a @var{keylist} without | |
1911 being enclosed in a list. All key values in the @code{case} form | |
1912 must be distinct. The final clauses may use @code{t} in place of | |
1913 a @var{keylist} to indicate a default clause that should be taken | |
1914 if none of the other clauses match. (The symbol @code{otherwise} | |
1915 is also recognized in place of @code{t}. To make a clause that | |
1916 matches the actual symbol @code{t}, @code{nil}, or @code{otherwise}, | |
1917 enclose the symbol in a list.) | |
1918 | |
1919 For example, this expression reads a keystroke, then does one of | |
1920 four things depending on whether it is an @samp{a}, a @samp{b}, | |
1921 a @key{RET} or @kbd{C-j}, or anything else. | |
1922 | |
1923 @example | |
1924 (case (read-char) | |
1925 (?a (do-a-thing)) | |
1926 (?b (do-b-thing)) | |
1927 ((?\r ?\n) (do-ret-thing)) | |
1928 (t (do-other-thing))) | |
1929 @end example | |
1930 @end defspec | |
1931 | |
1932 @defspec ecase keyform clause@dots{} | |
1933 This macro is just like @code{case}, except that if the key does | |
1934 not match any of the clauses, an error is signaled rather than | |
1935 simply returning @code{nil}. | |
1936 @end defspec | |
1937 | |
1938 @defspec typecase keyform clause@dots{} | |
1939 This macro is a version of @code{case} that checks for types | |
1940 rather than values. Each @var{clause} is of the form | |
1941 @samp{(@var{type} @var{body}...)}. @xref{Type Predicates}, | |
1942 for a description of type specifiers. For example, | |
1943 | |
1944 @example | |
1945 (typecase x | |
1946 (integer (munch-integer x)) | |
1947 (float (munch-float x)) | |
1948 (string (munch-integer (string-to-int x))) | |
1949 (t (munch-anything x))) | |
1950 @end example | |
1951 | |
1952 The type specifier @code{t} matches any type of object; the word | |
1953 @code{otherwise} is also allowed. To make one clause match any of | |
1954 several types, use an @code{(or ...)} type specifier. | |
1955 @end defspec | |
1956 | |
1957 @defspec etypecase keyform clause@dots{} | |
1958 This macro is just like @code{typecase}, except that if the key does | |
1959 not match any of the clauses, an error is signaled rather than | |
1960 simply returning @code{nil}. | |
1961 @end defspec | |
1962 | |
1963 @node Blocks and Exits, Iteration, Conditionals, Control Structure | |
1964 @section Blocks and Exits | |
1965 | |
1966 @noindent | |
1967 Common Lisp @dfn{blocks} provide a non-local exit mechanism very | |
1968 similar to @code{catch} and @code{throw}, but lexically rather than | |
1969 dynamically scoped. This package actually implements @code{block} | |
1970 in terms of @code{catch}; however, the lexical scoping allows the | |
1971 optimizing byte-compiler to omit the costly @code{catch} step if the | |
1972 body of the block does not actually @code{return-from} the block. | |
1973 | |
1974 @defspec block name forms@dots{} | |
1975 The @var{forms} are evaluated as if by a @code{progn}. However, | |
1976 if any of the @var{forms} execute @code{(return-from @var{name})}, | |
1977 they will jump out and return directly from the @code{block} form. | |
1978 The @code{block} returns the result of the last @var{form} unless | |
1979 a @code{return-from} occurs. | |
1980 | |
1981 The @code{block}/@code{return-from} mechanism is quite similar to | |
1982 the @code{catch}/@code{throw} mechanism. The main differences are | |
1983 that block @var{name}s are unevaluated symbols, rather than forms | |
1984 (such as quoted symbols) which evaluate to a tag at run-time; and | |
1985 also that blocks are lexically scoped whereas @code{catch}/@code{throw} | |
1986 are dynamically scoped. This means that functions called from the | |
1987 body of a @code{catch} can also @code{throw} to the @code{catch}, | |
1988 but the @code{return-from} referring to a block name must appear | |
1989 physically within the @var{forms} that make up the body of the block. | |
1990 They may not appear within other called functions, although they may | |
1991 appear within macro expansions or @code{lambda}s in the body. Block | |
1992 names and @code{catch} names form independent name-spaces. | |
1993 | |
1994 In true Common Lisp, @code{defun} and @code{defmacro} surround | |
1995 the function or expander bodies with implicit blocks with the | |
1996 same name as the function or macro. This does not occur in Emacs | |
1997 Lisp, but this package provides @code{defun*} and @code{defmacro*} | |
1998 forms which do create the implicit block. | |
1999 | |
2000 The Common Lisp looping constructs defined by this package, | |
27511
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2001 such as @code{loop} and @code{dolist}, also create implicit blocks |
25829 | 2002 just as in Common Lisp. |
2003 | |
2004 Because they are implemented in terms of Emacs Lisp @code{catch} | |
2005 and @code{throw}, blocks have the same overhead as actual | |
2006 @code{catch} constructs (roughly two function calls). However, | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47737
diff
changeset
|
2007 the optimizing byte compiler will optimize away the @code{catch} |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2008 if the block does |
25829 | 2009 not in fact contain any @code{return} or @code{return-from} calls |
2010 that jump to it. This means that @code{do} loops and @code{defun*} | |
2011 functions which don't use @code{return} don't pay the overhead to | |
2012 support it. | |
2013 @end defspec | |
2014 | |
2015 @defspec return-from name [result] | |
2016 This macro returns from the block named @var{name}, which must be | |
2017 an (unevaluated) symbol. If a @var{result} form is specified, it | |
2018 is evaluated to produce the result returned from the @code{block}. | |
2019 Otherwise, @code{nil} is returned. | |
2020 @end defspec | |
2021 | |
2022 @defspec return [result] | |
2023 This macro is exactly like @code{(return-from nil @var{result})}. | |
27511
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2024 Common Lisp loops like @code{do} and @code{dolist} implicitly enclose |
25829 | 2025 themselves in @code{nil} blocks. |
2026 @end defspec | |
2027 | |
2028 @node Iteration, Loop Facility, Blocks and Exits, Control Structure | |
2029 @section Iteration | |
2030 | |
2031 @noindent | |
2032 The macros described here provide more sophisticated, high-level | |
2033 looping constructs to complement Emacs Lisp's basic @code{while} | |
2034 loop. | |
2035 | |
2036 @defspec loop forms@dots{} | |
2037 The @dfn{CL} package supports both the simple, old-style meaning of | |
2038 @code{loop} and the extremely powerful and flexible feature known as | |
2039 the @dfn{Loop Facility} or @dfn{Loop Macro}. This more advanced | |
2040 facility is discussed in the following section; @pxref{Loop Facility}. | |
2041 The simple form of @code{loop} is described here. | |
2042 | |
2043 If @code{loop} is followed by zero or more Lisp expressions, | |
2044 then @code{(loop @var{exprs}@dots{})} simply creates an infinite | |
2045 loop executing the expressions over and over. The loop is | |
2046 enclosed in an implicit @code{nil} block. Thus, | |
2047 | |
2048 @example | |
2049 (loop (foo) (if (no-more) (return 72)) (bar)) | |
2050 @end example | |
2051 | |
2052 @noindent | |
2053 is exactly equivalent to | |
2054 | |
2055 @example | |
2056 (block nil (while t (foo) (if (no-more) (return 72)) (bar))) | |
2057 @end example | |
2058 | |
2059 If any of the expressions are plain symbols, the loop is instead | |
2060 interpreted as a Loop Macro specification as described later. | |
2061 (This is not a restriction in practice, since a plain symbol | |
2062 in the above notation would simply access and throw away the | |
2063 value of a variable.) | |
2064 @end defspec | |
2065 | |
2066 @defspec do (spec@dots{}) (end-test [result@dots{}]) forms@dots{} | |
2067 This macro creates a general iterative loop. Each @var{spec} is | |
2068 of the form | |
2069 | |
2070 @example | |
2071 (@var{var} [@var{init} [@var{step}]]) | |
2072 @end example | |
2073 | |
2074 The loop works as follows: First, each @var{var} is bound to the | |
2075 associated @var{init} value as if by a @code{let} form. Then, in | |
2076 each iteration of the loop, the @var{end-test} is evaluated; if | |
2077 true, the loop is finished. Otherwise, the body @var{forms} are | |
2078 evaluated, then each @var{var} is set to the associated @var{step} | |
2079 expression (as if by a @code{psetq} form) and the next iteration | |
2080 begins. Once the @var{end-test} becomes true, the @var{result} | |
2081 forms are evaluated (with the @var{var}s still bound to their | |
2082 values) to produce the result returned by @code{do}. | |
2083 | |
2084 The entire @code{do} loop is enclosed in an implicit @code{nil} | |
2085 block, so that you can use @code{(return)} to break out of the | |
2086 loop at any time. | |
2087 | |
2088 If there are no @var{result} forms, the loop returns @code{nil}. | |
2089 If a given @var{var} has no @var{step} form, it is bound to its | |
2090 @var{init} value but not otherwise modified during the @code{do} | |
2091 loop (unless the code explicitly modifies it); this case is just | |
2092 a shorthand for putting a @code{(let ((@var{var} @var{init})) @dots{})} | |
2093 around the loop. If @var{init} is also omitted it defaults to | |
2094 @code{nil}, and in this case a plain @samp{@var{var}} can be used | |
2095 in place of @samp{(@var{var})}, again following the analogy with | |
2096 @code{let}. | |
2097 | |
2098 This example (from Steele) illustrates a loop which applies the | |
2099 function @code{f} to successive pairs of values from the lists | |
2100 @code{foo} and @code{bar}; it is equivalent to the call | |
2101 @code{(mapcar* 'f foo bar)}. Note that this loop has no body | |
2102 @var{forms} at all, performing all its work as side effects of | |
2103 the rest of the loop. | |
2104 | |
2105 @example | |
2106 (do ((x foo (cdr x)) | |
2107 (y bar (cdr y)) | |
2108 (z nil (cons (f (car x) (car y)) z))) | |
2109 ((or (null x) (null y)) | |
2110 (nreverse z))) | |
2111 @end example | |
2112 @end defspec | |
2113 | |
2114 @defspec do* (spec@dots{}) (end-test [result@dots{}]) forms@dots{} | |
2115 This is to @code{do} what @code{let*} is to @code{let}. In | |
2116 particular, the initial values are bound as if by @code{let*} | |
2117 rather than @code{let}, and the steps are assigned as if by | |
2118 @code{setq} rather than @code{psetq}. | |
2119 | |
2120 Here is another way to write the above loop: | |
2121 | |
2122 @example | |
2123 (do* ((xp foo (cdr xp)) | |
2124 (yp bar (cdr yp)) | |
2125 (x (car xp) (car xp)) | |
2126 (y (car yp) (car yp)) | |
2127 z) | |
2128 ((or (null xp) (null yp)) | |
2129 (nreverse z)) | |
2130 (push (f x y) z)) | |
2131 @end example | |
2132 @end defspec | |
2133 | |
27511
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2134 @defspec dolist (var list [result]) forms@dots{} |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2135 This is a more specialized loop which iterates across the elements |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2136 of a list. @var{list} should evaluate to a list; the body @var{forms} |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2137 are executed with @var{var} bound to each element of the list in |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2138 turn. Finally, the @var{result} form (or @code{nil}) is evaluated |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2139 with @var{var} bound to @code{nil} to produce the result returned by |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2140 the loop. Unlike with Emacs's built in @code{dolist}, the loop is |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2141 surrounded by an implicit @code{nil} block. |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2142 @end defspec |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2143 |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2144 @defspec dotimes (var count [result]) forms@dots{} |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2145 This is a more specialized loop which iterates a specified number |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2146 of times. The body is executed with @var{var} bound to the integers |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2147 from zero (inclusive) to @var{count} (exclusive), in turn. Then |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2148 the @code{result} form is evaluated with @var{var} bound to the total |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2149 number of iterations that were done (i.e., @code{(max 0 @var{count})}) |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2150 to get the return value for the loop form. Unlike with Emacs's built in |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2151 @code{dolist}, the loop is surrounded by an implicit @code{nil} block. |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2152 @end defspec |
ead47a92d9d0
Replace dotimes, dolist, with comment.
Dave Love <fx@gnu.org>
parents:
27487
diff
changeset
|
2153 |
25829 | 2154 @defspec do-symbols (var [obarray [result]]) forms@dots{} |
2155 This loop iterates over all interned symbols. If @var{obarray} | |
2156 is specified and is not @code{nil}, it loops over all symbols in | |
2157 that obarray. For each symbol, the body @var{forms} are evaluated | |
2158 with @var{var} bound to that symbol. The symbols are visited in | |
2159 an unspecified order. Afterward the @var{result} form, if any, | |
2160 is evaluated (with @var{var} bound to @code{nil}) to get the return | |
2161 value. The loop is surrounded by an implicit @code{nil} block. | |
2162 @end defspec | |
2163 | |
2164 @defspec do-all-symbols (var [result]) forms@dots{} | |
2165 This is identical to @code{do-symbols} except that the @var{obarray} | |
2166 argument is omitted; it always iterates over the default obarray. | |
2167 @end defspec | |
2168 | |
2169 @xref{Mapping over Sequences}, for some more functions for | |
2170 iterating over vectors or lists. | |
2171 | |
2172 @node Loop Facility, Multiple Values, Iteration, Control Structure | |
2173 @section Loop Facility | |
2174 | |
2175 @noindent | |
2176 A common complaint with Lisp's traditional looping constructs is | |
2177 that they are either too simple and limited, such as Common Lisp's | |
2178 @code{dotimes} or Emacs Lisp's @code{while}, or too unreadable and | |
2179 obscure, like Common Lisp's @code{do} loop. | |
2180 | |
2181 To remedy this, recent versions of Common Lisp have added a new | |
2182 construct called the ``Loop Facility'' or ``@code{loop} macro,'' | |
2183 with an easy-to-use but very powerful and expressive syntax. | |
2184 | |
2185 @menu | |
2186 * Loop Basics:: `loop' macro, basic clause structure | |
2187 * Loop Examples:: Working examples of `loop' macro | |
2188 * For Clauses:: Clauses introduced by `for' or `as' | |
2189 * Iteration Clauses:: `repeat', `while', `thereis', etc. | |
2190 * Accumulation Clauses:: `collect', `sum', `maximize', etc. | |
2191 * Other Clauses:: `with', `if', `initially', `finally' | |
2192 @end menu | |
2193 | |
2194 @node Loop Basics, Loop Examples, Loop Facility, Loop Facility | |
2195 @subsection Loop Basics | |
2196 | |
2197 @noindent | |
2198 The @code{loop} macro essentially creates a mini-language within | |
2199 Lisp that is specially tailored for describing loops. While this | |
2200 language is a little strange-looking by the standards of regular Lisp, | |
2201 it turns out to be very easy to learn and well-suited to its purpose. | |
2202 | |
2203 Since @code{loop} is a macro, all parsing of the loop language | |
2204 takes place at byte-compile time; compiled @code{loop}s are just | |
2205 as efficient as the equivalent @code{while} loops written longhand. | |
2206 | |
2207 @defspec loop clauses@dots{} | |
2208 A loop construct consists of a series of @var{clause}s, each | |
2209 introduced by a symbol like @code{for} or @code{do}. Clauses | |
2210 are simply strung together in the argument list of @code{loop}, | |
2211 with minimal extra parentheses. The various types of clauses | |
2212 specify initializations, such as the binding of temporary | |
2213 variables, actions to be taken in the loop, stepping actions, | |
2214 and final cleanup. | |
2215 | |
2216 Common Lisp specifies a certain general order of clauses in a | |
2217 loop: | |
2218 | |
2219 @example | |
2220 (loop @var{name-clause} | |
2221 @var{var-clauses}@dots{} | |
2222 @var{action-clauses}@dots{}) | |
2223 @end example | |
2224 | |
2225 The @var{name-clause} optionally gives a name to the implicit | |
2226 block that surrounds the loop. By default, the implicit block | |
2227 is named @code{nil}. The @var{var-clauses} specify what | |
2228 variables should be bound during the loop, and how they should | |
2229 be modified or iterated throughout the course of the loop. The | |
2230 @var{action-clauses} are things to be done during the loop, such | |
2231 as computing, collecting, and returning values. | |
2232 | |
2233 The Emacs version of the @code{loop} macro is less restrictive about | |
2234 the order of clauses, but things will behave most predictably if | |
2235 you put the variable-binding clauses @code{with}, @code{for}, and | |
2236 @code{repeat} before the action clauses. As in Common Lisp, | |
2237 @code{initially} and @code{finally} clauses can go anywhere. | |
2238 | |
2239 Loops generally return @code{nil} by default, but you can cause | |
2240 them to return a value by using an accumulation clause like | |
2241 @code{collect}, an end-test clause like @code{always}, or an | |
2242 explicit @code{return} clause to jump out of the implicit block. | |
2243 (Because the loop body is enclosed in an implicit block, you can | |
2244 also use regular Lisp @code{return} or @code{return-from} to | |
2245 break out of the loop.) | |
2246 @end defspec | |
2247 | |
2248 The following sections give some examples of the Loop Macro in | |
2249 action, and describe the particular loop clauses in great detail. | |
2250 Consult the second edition of Steele's @dfn{Common Lisp, the Language}, | |
2251 for additional discussion and examples of the @code{loop} macro. | |
2252 | |
2253 @node Loop Examples, For Clauses, Loop Basics, Loop Facility | |
2254 @subsection Loop Examples | |
2255 | |
2256 @noindent | |
2257 Before listing the full set of clauses that are allowed, let's | |
2258 look at a few example loops just to get a feel for the @code{loop} | |
2259 language. | |
2260 | |
2261 @example | |
2262 (loop for buf in (buffer-list) | |
2263 collect (buffer-file-name buf)) | |
2264 @end example | |
2265 | |
2266 @noindent | |
2267 This loop iterates over all Emacs buffers, using the list | |
2268 returned by @code{buffer-list}. For each buffer @code{buf}, | |
2269 it calls @code{buffer-file-name} and collects the results into | |
2270 a list, which is then returned from the @code{loop} construct. | |
2271 The result is a list of the file names of all the buffers in | |
2272 Emacs' memory. The words @code{for}, @code{in}, and @code{collect} | |
2273 are reserved words in the @code{loop} language. | |
2274 | |
2275 @example | |
2276 (loop repeat 20 do (insert "Yowsa\n")) | |
2277 @end example | |
2278 | |
2279 @noindent | |
2280 This loop inserts the phrase ``Yowsa'' twenty times in the | |
2281 current buffer. | |
2282 | |
2283 @example | |
2284 (loop until (eobp) do (munch-line) (forward-line 1)) | |
2285 @end example | |
2286 | |
2287 @noindent | |
2288 This loop calls @code{munch-line} on every line until the end | |
2289 of the buffer. If point is already at the end of the buffer, | |
2290 the loop exits immediately. | |
2291 | |
2292 @example | |
2293 (loop do (munch-line) until (eobp) do (forward-line 1)) | |
2294 @end example | |
2295 | |
2296 @noindent | |
2297 This loop is similar to the above one, except that @code{munch-line} | |
2298 is always called at least once. | |
2299 | |
2300 @example | |
2301 (loop for x from 1 to 100 | |
2302 for y = (* x x) | |
2303 until (>= y 729) | |
2304 finally return (list x (= y 729))) | |
2305 @end example | |
2306 | |
2307 @noindent | |
2308 This more complicated loop searches for a number @code{x} whose | |
2309 square is 729. For safety's sake it only examines @code{x} | |
2310 values up to 100; dropping the phrase @samp{to 100} would | |
2311 cause the loop to count upwards with no limit. The second | |
2312 @code{for} clause defines @code{y} to be the square of @code{x} | |
2313 within the loop; the expression after the @code{=} sign is | |
2314 reevaluated each time through the loop. The @code{until} | |
2315 clause gives a condition for terminating the loop, and the | |
2316 @code{finally} clause says what to do when the loop finishes. | |
2317 (This particular example was written less concisely than it | |
2318 could have been, just for the sake of illustration.) | |
2319 | |
2320 Note that even though this loop contains three clauses (two | |
2321 @code{for}s and an @code{until}) that would have been enough to | |
2322 define loops all by themselves, it still creates a single loop | |
2323 rather than some sort of triple-nested loop. You must explicitly | |
2324 nest your @code{loop} constructs if you want nested loops. | |
2325 | |
2326 @node For Clauses, Iteration Clauses, Loop Examples, Loop Facility | |
2327 @subsection For Clauses | |
2328 | |
2329 @noindent | |
2330 Most loops are governed by one or more @code{for} clauses. | |
2331 A @code{for} clause simultaneously describes variables to be | |
2332 bound, how those variables are to be stepped during the loop, | |
2333 and usually an end condition based on those variables. | |
2334 | |
2335 The word @code{as} is a synonym for the word @code{for}. This | |
2336 word is followed by a variable name, then a word like @code{from} | |
2337 or @code{across} that describes the kind of iteration desired. | |
2338 In Common Lisp, the phrase @code{being the} sometimes precedes | |
2339 the type of iteration; in this package both @code{being} and | |
2340 @code{the} are optional. The word @code{each} is a synonym | |
2341 for @code{the}, and the word that follows it may be singular | |
2342 or plural: @samp{for x being the elements of y} or | |
2343 @samp{for x being each element of y}. Which form you use | |
2344 is purely a matter of style. | |
2345 | |
2346 The variable is bound around the loop as if by @code{let}: | |
2347 | |
2348 @example | |
2349 (setq i 'happy) | |
2350 (loop for i from 1 to 10 do (do-something-with i)) | |
2351 i | |
2352 @result{} happy | |
2353 @end example | |
2354 | |
2355 @table @code | |
2356 @item for @var{var} from @var{expr1} to @var{expr2} by @var{expr3} | |
2357 This type of @code{for} clause creates a counting loop. Each of | |
2358 the three sub-terms is optional, though there must be at least one | |
2359 term so that the clause is marked as a counting clause. | |
2360 | |
2361 The three expressions are the starting value, the ending value, and | |
2362 the step value, respectively, of the variable. The loop counts | |
2363 upwards by default (@var{expr3} must be positive), from @var{expr1} | |
2364 to @var{expr2} inclusively. If you omit the @code{from} term, the | |
2365 loop counts from zero; if you omit the @code{to} term, the loop | |
2366 counts forever without stopping (unless stopped by some other | |
2367 loop clause, of course); if you omit the @code{by} term, the loop | |
2368 counts in steps of one. | |
2369 | |
2370 You can replace the word @code{from} with @code{upfrom} or | |
2371 @code{downfrom} to indicate the direction of the loop. Likewise, | |
2372 you can replace @code{to} with @code{upto} or @code{downto}. | |
2373 For example, @samp{for x from 5 downto 1} executes five times | |
2374 with @code{x} taking on the integers from 5 down to 1 in turn. | |
2375 Also, you can replace @code{to} with @code{below} or @code{above}, | |
2376 which are like @code{upto} and @code{downto} respectively except | |
2377 that they are exclusive rather than inclusive limits: | |
2378 | |
2379 @example | |
2380 (loop for x to 10 collect x) | |
2381 @result{} (0 1 2 3 4 5 6 7 8 9 10) | |
2382 (loop for x below 10 collect x) | |
2383 @result{} (0 1 2 3 4 5 6 7 8 9) | |
2384 @end example | |
2385 | |
2386 The @code{by} value is always positive, even for downward-counting | |
2387 loops. Some sort of @code{from} value is required for downward | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
2388 loops; @samp{for x downto 5} is not a valid loop clause all by |
25829 | 2389 itself. |
2390 | |
2391 @item for @var{var} in @var{list} by @var{function} | |
2392 This clause iterates @var{var} over all the elements of @var{list}, | |
2393 in turn. If you specify the @code{by} term, then @var{function} | |
2394 is used to traverse the list instead of @code{cdr}; it must be a | |
2395 function taking one argument. For example: | |
2396 | |
2397 @example | |
2398 (loop for x in '(1 2 3 4 5 6) collect (* x x)) | |
2399 @result{} (1 4 9 16 25 36) | |
2400 (loop for x in '(1 2 3 4 5 6) by 'cddr collect (* x x)) | |
2401 @result{} (1 9 25) | |
2402 @end example | |
2403 | |
2404 @item for @var{var} on @var{list} by @var{function} | |
2405 This clause iterates @var{var} over all the cons cells of @var{list}. | |
2406 | |
2407 @example | |
2408 (loop for x on '(1 2 3 4) collect x) | |
2409 @result{} ((1 2 3 4) (2 3 4) (3 4) (4)) | |
2410 @end example | |
2411 | |
2412 With @code{by}, there is no real reason that the @code{on} expression | |
2413 must be a list. For example: | |
2414 | |
2415 @example | |
2416 (loop for x on first-animal by 'next-animal collect x) | |
2417 @end example | |
2418 | |
2419 @noindent | |
2420 where @code{(next-animal x)} takes an ``animal'' @var{x} and returns | |
2421 the next in the (assumed) sequence of animals, or @code{nil} if | |
2422 @var{x} was the last animal in the sequence. | |
2423 | |
2424 @item for @var{var} in-ref @var{list} by @var{function} | |
2425 This is like a regular @code{in} clause, but @var{var} becomes | |
2426 a @code{setf}-able ``reference'' onto the elements of the list | |
2427 rather than just a temporary variable. For example, | |
2428 | |
2429 @example | |
2430 (loop for x in-ref my-list do (incf x)) | |
2431 @end example | |
2432 | |
2433 @noindent | |
2434 increments every element of @code{my-list} in place. This clause | |
2435 is an extension to standard Common Lisp. | |
2436 | |
2437 @item for @var{var} across @var{array} | |
2438 This clause iterates @var{var} over all the elements of @var{array}, | |
2439 which may be a vector or a string. | |
2440 | |
2441 @example | |
2442 (loop for x across "aeiou" | |
2443 do (use-vowel (char-to-string x))) | |
2444 @end example | |
2445 | |
2446 @item for @var{var} across-ref @var{array} | |
2447 This clause iterates over an array, with @var{var} a @code{setf}-able | |
2448 reference onto the elements; see @code{in-ref} above. | |
2449 | |
2450 @item for @var{var} being the elements of @var{sequence} | |
2451 This clause iterates over the elements of @var{sequence}, which may | |
2452 be a list, vector, or string. Since the type must be determined | |
2453 at run-time, this is somewhat less efficient than @code{in} or | |
2454 @code{across}. The clause may be followed by the additional term | |
2455 @samp{using (index @var{var2})} to cause @var{var2} to be bound to | |
2456 the successive indices (starting at 0) of the elements. | |
2457 | |
2458 This clause type is taken from older versions of the @code{loop} macro, | |
2459 and is not present in modern Common Lisp. The @samp{using (sequence ...)} | |
2460 term of the older macros is not supported. | |
2461 | |
2462 @item for @var{var} being the elements of-ref @var{sequence} | |
2463 This clause iterates over a sequence, with @var{var} a @code{setf}-able | |
2464 reference onto the elements; see @code{in-ref} above. | |
2465 | |
2466 @item for @var{var} being the symbols [of @var{obarray}] | |
2467 This clause iterates over symbols, either over all interned symbols | |
2468 or over all symbols in @var{obarray}. The loop is executed with | |
2469 @var{var} bound to each symbol in turn. The symbols are visited in | |
2470 an unspecified order. | |
2471 | |
2472 As an example, | |
2473 | |
2474 @example | |
2475 (loop for sym being the symbols | |
2476 when (fboundp sym) | |
2477 when (string-match "^map" (symbol-name sym)) | |
2478 collect sym) | |
2479 @end example | |
2480 | |
2481 @noindent | |
2482 returns a list of all the functions whose names begin with @samp{map}. | |
2483 | |
2484 The Common Lisp words @code{external-symbols} and @code{present-symbols} | |
2485 are also recognized but are equivalent to @code{symbols} in Emacs Lisp. | |
2486 | |
2487 Due to a minor implementation restriction, it will not work to have | |
2488 more than one @code{for} clause iterating over symbols, hash tables, | |
2489 keymaps, overlays, or intervals in a given @code{loop}. Fortunately, | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
2490 it would rarely if ever be useful to do so. It @emph{is} valid to mix |
25829 | 2491 one of these types of clauses with other clauses like @code{for ... to} |
2492 or @code{while}. | |
2493 | |
2494 @item for @var{var} being the hash-keys of @var{hash-table} | |
2495 This clause iterates over the entries in @var{hash-table}. For each | |
2496 hash table entry, @var{var} is bound to the entry's key. If you write | |
2497 @samp{the hash-values} instead, @var{var} is bound to the values | |
2498 of the entries. The clause may be followed by the additional | |
2499 term @samp{using (hash-values @var{var2})} (where @code{hash-values} | |
2500 is the opposite word of the word following @code{the}) to cause | |
2501 @var{var} and @var{var2} to be bound to the two parts of each | |
2502 hash table entry. | |
2503 | |
2504 @item for @var{var} being the key-codes of @var{keymap} | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2505 This clause iterates over the entries in @var{keymap}. |
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2506 The iteration does not enter nested keymaps or inherited (parent) keymaps. |
25829 | 2507 You can use @samp{the key-bindings} to access the commands bound to |
2508 the keys rather than the key codes, and you can add a @code{using} | |
2509 clause to access both the codes and the bindings together. | |
2510 | |
2511 @item for @var{var} being the key-seqs of @var{keymap} | |
2512 This clause iterates over all key sequences defined by @var{keymap} | |
2513 and its nested keymaps, where @var{var} takes on values which are | |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
2514 vectors. The strings or vectors |
25829 | 2515 are reused for each iteration, so you must copy them if you wish to keep |
2516 them permanently. You can add a @samp{using (key-bindings ...)} | |
2517 clause to get the command bindings as well. | |
2518 | |
2519 @item for @var{var} being the overlays [of @var{buffer}] @dots{} | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2520 This clause iterates over the ``overlays'' of a buffer |
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2521 (the clause @code{extents} is synonymous |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
2522 with @code{overlays}). If the @code{of} term is omitted, the current |
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
2523 buffer is used. |
25829 | 2524 This clause also accepts optional @samp{from @var{pos}} and |
2525 @samp{to @var{pos}} terms, limiting the clause to overlays which | |
2526 overlap the specified region. | |
2527 | |
2528 @item for @var{var} being the intervals [of @var{buffer}] @dots{} | |
2529 This clause iterates over all intervals of a buffer with constant | |
2530 text properties. The variable @var{var} will be bound to conses | |
2531 of start and end positions, where one start position is always equal | |
2532 to the previous end position. The clause allows @code{of}, | |
2533 @code{from}, @code{to}, and @code{property} terms, where the latter | |
2534 term restricts the search to just the specified property. The | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2535 @code{of} term may specify either a buffer or a string. |
25829 | 2536 |
2537 @item for @var{var} being the frames | |
2538 This clause iterates over all frames, i.e., X window system windows | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2539 open on Emacs files. The |
25829 | 2540 clause @code{screens} is a synonym for @code{frames}. The frames |
2541 are visited in @code{next-frame} order starting from | |
2542 @code{selected-frame}. | |
2543 | |
2544 @item for @var{var} being the windows [of @var{frame}] | |
2545 This clause iterates over the windows (in the Emacs sense) of | |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
2546 the current frame, or of the specified @var{frame}. |
25829 | 2547 |
2548 @item for @var{var} being the buffers | |
2549 This clause iterates over all buffers in Emacs. It is equivalent | |
2550 to @samp{for @var{var} in (buffer-list)}. | |
2551 | |
2552 @item for @var{var} = @var{expr1} then @var{expr2} | |
2553 This clause does a general iteration. The first time through | |
2554 the loop, @var{var} will be bound to @var{expr1}. On the second | |
2555 and successive iterations it will be set by evaluating @var{expr2} | |
2556 (which may refer to the old value of @var{var}). For example, | |
2557 these two loops are effectively the same: | |
2558 | |
2559 @example | |
2560 (loop for x on my-list by 'cddr do ...) | |
2561 (loop for x = my-list then (cddr x) while x do ...) | |
2562 @end example | |
2563 | |
2564 Note that this type of @code{for} clause does not imply any sort | |
2565 of terminating condition; the above example combines it with a | |
2566 @code{while} clause to tell when to end the loop. | |
2567 | |
2568 If you omit the @code{then} term, @var{expr1} is used both for | |
2569 the initial setting and for successive settings: | |
2570 | |
2571 @example | |
2572 (loop for x = (random) when (> x 0) return x) | |
2573 @end example | |
2574 | |
2575 @noindent | |
2576 This loop keeps taking random numbers from the @code{(random)} | |
2577 function until it gets a positive one, which it then returns. | |
2578 @end table | |
2579 | |
2580 If you include several @code{for} clauses in a row, they are | |
2581 treated sequentially (as if by @code{let*} and @code{setq}). | |
2582 You can instead use the word @code{and} to link the clauses, | |
2583 in which case they are processed in parallel (as if by @code{let} | |
2584 and @code{psetq}). | |
2585 | |
2586 @example | |
2587 (loop for x below 5 for y = nil then x collect (list x y)) | |
2588 @result{} ((0 nil) (1 1) (2 2) (3 3) (4 4)) | |
2589 (loop for x below 5 and y = nil then x collect (list x y)) | |
2590 @result{} ((0 nil) (1 0) (2 1) (3 2) (4 3)) | |
2591 @end example | |
2592 | |
2593 @noindent | |
2594 In the first loop, @code{y} is set based on the value of @code{x} | |
2595 that was just set by the previous clause; in the second loop, | |
2596 @code{x} and @code{y} are set simultaneously so @code{y} is set | |
2597 based on the value of @code{x} left over from the previous time | |
2598 through the loop. | |
2599 | |
2600 Another feature of the @code{loop} macro is @dfn{destructuring}, | |
2601 similar in concept to the destructuring provided by @code{defmacro}. | |
2602 The @var{var} part of any @code{for} clause can be given as a list | |
2603 of variables instead of a single variable. The values produced | |
2604 during loop execution must be lists; the values in the lists are | |
2605 stored in the corresponding variables. | |
2606 | |
2607 @example | |
2608 (loop for (x y) in '((2 3) (4 5) (6 7)) collect (+ x y)) | |
2609 @result{} (5 9 13) | |
2610 @end example | |
2611 | |
2612 In loop destructuring, if there are more values than variables | |
2613 the trailing values are ignored, and if there are more variables | |
2614 than values the trailing variables get the value @code{nil}. | |
2615 If @code{nil} is used as a variable name, the corresponding | |
2616 values are ignored. Destructuring may be nested, and dotted | |
2617 lists of variables like @code{(x . y)} are allowed. | |
2618 | |
2619 @node Iteration Clauses, Accumulation Clauses, For Clauses, Loop Facility | |
2620 @subsection Iteration Clauses | |
2621 | |
2622 @noindent | |
2623 Aside from @code{for} clauses, there are several other loop clauses | |
2624 that control the way the loop operates. They might be used by | |
2625 themselves, or in conjunction with one or more @code{for} clauses. | |
2626 | |
2627 @table @code | |
2628 @item repeat @var{integer} | |
2629 This clause simply counts up to the specified number using an | |
2630 internal temporary variable. The loops | |
2631 | |
2632 @example | |
2633 (loop repeat n do ...) | |
2634 (loop for temp to n do ...) | |
2635 @end example | |
2636 | |
2637 @noindent | |
2638 are identical except that the second one forces you to choose | |
2639 a name for a variable you aren't actually going to use. | |
2640 | |
2641 @item while @var{condition} | |
2642 This clause stops the loop when the specified condition (any Lisp | |
2643 expression) becomes @code{nil}. For example, the following two | |
2644 loops are equivalent, except for the implicit @code{nil} block | |
2645 that surrounds the second one: | |
2646 | |
2647 @example | |
2648 (while @var{cond} @var{forms}@dots{}) | |
2649 (loop while @var{cond} do @var{forms}@dots{}) | |
2650 @end example | |
2651 | |
2652 @item until @var{condition} | |
2653 This clause stops the loop when the specified condition is true, | |
2654 i.e., non-@code{nil}. | |
2655 | |
2656 @item always @var{condition} | |
2657 This clause stops the loop when the specified condition is @code{nil}. | |
2658 Unlike @code{while}, it stops the loop using @code{return nil} so that | |
2659 the @code{finally} clauses are not executed. If all the conditions | |
2660 were non-@code{nil}, the loop returns @code{t}: | |
2661 | |
2662 @example | |
2663 (if (loop for size in size-list always (> size 10)) | |
2664 (some-big-sizes) | |
2665 (no-big-sizes)) | |
2666 @end example | |
2667 | |
2668 @item never @var{condition} | |
2669 This clause is like @code{always}, except that the loop returns | |
2670 @code{t} if any conditions were false, or @code{nil} otherwise. | |
2671 | |
2672 @item thereis @var{condition} | |
2673 This clause stops the loop when the specified form is non-@code{nil}; | |
2674 in this case, it returns that non-@code{nil} value. If all the | |
2675 values were @code{nil}, the loop returns @code{nil}. | |
2676 @end table | |
2677 | |
2678 @node Accumulation Clauses, Other Clauses, Iteration Clauses, Loop Facility | |
2679 @subsection Accumulation Clauses | |
2680 | |
2681 @noindent | |
2682 These clauses cause the loop to accumulate information about the | |
2683 specified Lisp @var{form}. The accumulated result is returned | |
2684 from the loop unless overridden, say, by a @code{return} clause. | |
2685 | |
2686 @table @code | |
2687 @item collect @var{form} | |
2688 This clause collects the values of @var{form} into a list. Several | |
2689 examples of @code{collect} appear elsewhere in this manual. | |
2690 | |
2691 The word @code{collecting} is a synonym for @code{collect}, and | |
2692 likewise for the other accumulation clauses. | |
2693 | |
2694 @item append @var{form} | |
2695 This clause collects lists of values into a result list using | |
2696 @code{append}. | |
2697 | |
2698 @item nconc @var{form} | |
2699 This clause collects lists of values into a result list by | |
2700 destructively modifying the lists rather than copying them. | |
2701 | |
2702 @item concat @var{form} | |
2703 This clause concatenates the values of the specified @var{form} | |
2704 into a string. (It and the following clause are extensions to | |
2705 standard Common Lisp.) | |
2706 | |
2707 @item vconcat @var{form} | |
2708 This clause concatenates the values of the specified @var{form} | |
2709 into a vector. | |
2710 | |
2711 @item count @var{form} | |
2712 This clause counts the number of times the specified @var{form} | |
2713 evaluates to a non-@code{nil} value. | |
2714 | |
2715 @item sum @var{form} | |
2716 This clause accumulates the sum of the values of the specified | |
2717 @var{form}, which must evaluate to a number. | |
2718 | |
2719 @item maximize @var{form} | |
2720 This clause accumulates the maximum value of the specified @var{form}, | |
2721 which must evaluate to a number. The return value is undefined if | |
2722 @code{maximize} is executed zero times. | |
2723 | |
2724 @item minimize @var{form} | |
2725 This clause accumulates the minimum value of the specified @var{form}. | |
2726 @end table | |
2727 | |
2728 Accumulation clauses can be followed by @samp{into @var{var}} to | |
2729 cause the data to be collected into variable @var{var} (which is | |
2730 automatically @code{let}-bound during the loop) rather than an | |
2731 unnamed temporary variable. Also, @code{into} accumulations do | |
2732 not automatically imply a return value. The loop must use some | |
2733 explicit mechanism, such as @code{finally return}, to return | |
2734 the accumulated result. | |
2735 | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
2736 It is valid for several accumulation clauses of the same type to |
25829 | 2737 accumulate into the same place. From Steele: |
2738 | |
2739 @example | |
2740 (loop for name in '(fred sue alice joe june) | |
2741 for kids in '((bob ken) () () (kris sunshine) ()) | |
2742 collect name | |
2743 append kids) | |
2744 @result{} (fred bob ken sue alice joe kris sunshine june) | |
2745 @end example | |
2746 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2747 @node Other Clauses, , Accumulation Clauses, Loop Facility |
25829 | 2748 @subsection Other Clauses |
2749 | |
2750 @noindent | |
2751 This section describes the remaining loop clauses. | |
2752 | |
2753 @table @code | |
2754 @item with @var{var} = @var{value} | |
2755 This clause binds a variable to a value around the loop, but | |
2756 otherwise leaves the variable alone during the loop. The following | |
2757 loops are basically equivalent: | |
2758 | |
2759 @example | |
2760 (loop with x = 17 do ...) | |
2761 (let ((x 17)) (loop do ...)) | |
2762 (loop for x = 17 then x do ...) | |
2763 @end example | |
2764 | |
2765 Naturally, the variable @var{var} might be used for some purpose | |
2766 in the rest of the loop. For example: | |
2767 | |
2768 @example | |
2769 (loop for x in my-list with res = nil do (push x res) | |
2770 finally return res) | |
2771 @end example | |
2772 | |
2773 This loop inserts the elements of @code{my-list} at the front of | |
2774 a new list being accumulated in @code{res}, then returns the | |
2775 list @code{res} at the end of the loop. The effect is similar | |
2776 to that of a @code{collect} clause, but the list gets reversed | |
2777 by virtue of the fact that elements are being pushed onto the | |
2778 front of @code{res} rather than the end. | |
2779 | |
2780 If you omit the @code{=} term, the variable is initialized to | |
2781 @code{nil}. (Thus the @samp{= nil} in the above example is | |
2782 unnecessary.) | |
2783 | |
2784 Bindings made by @code{with} are sequential by default, as if | |
2785 by @code{let*}. Just like @code{for} clauses, @code{with} clauses | |
2786 can be linked with @code{and} to cause the bindings to be made by | |
2787 @code{let} instead. | |
2788 | |
2789 @item if @var{condition} @var{clause} | |
2790 This clause executes the following loop clause only if the specified | |
2791 condition is true. The following @var{clause} should be an accumulation, | |
2792 @code{do}, @code{return}, @code{if}, or @code{unless} clause. | |
2793 Several clauses may be linked by separating them with @code{and}. | |
2794 These clauses may be followed by @code{else} and a clause or clauses | |
2795 to execute if the condition was false. The whole construct may | |
2796 optionally be followed by the word @code{end} (which may be used to | |
2797 disambiguate an @code{else} or @code{and} in a nested @code{if}). | |
2798 | |
2799 The actual non-@code{nil} value of the condition form is available | |
2800 by the name @code{it} in the ``then'' part. For example: | |
2801 | |
2802 @example | |
2803 (setq funny-numbers '(6 13 -1)) | |
2804 @result{} (6 13 -1) | |
2805 (loop for x below 10 | |
2806 if (oddp x) | |
2807 collect x into odds | |
2808 and if (memq x funny-numbers) return (cdr it) end | |
2809 else | |
2810 collect x into evens | |
2811 finally return (vector odds evens)) | |
2812 @result{} [(1 3 5 7 9) (0 2 4 6 8)] | |
2813 (setq funny-numbers '(6 7 13 -1)) | |
2814 @result{} (6 7 13 -1) | |
2815 (loop <@r{same thing again}>) | |
2816 @result{} (13 -1) | |
2817 @end example | |
2818 | |
2819 Note the use of @code{and} to put two clauses into the ``then'' | |
2820 part, one of which is itself an @code{if} clause. Note also that | |
2821 @code{end}, while normally optional, was necessary here to make | |
2822 it clear that the @code{else} refers to the outermost @code{if} | |
2823 clause. In the first case, the loop returns a vector of lists | |
2824 of the odd and even values of @var{x}. In the second case, the | |
2825 odd number 7 is one of the @code{funny-numbers} so the loop | |
2826 returns early; the actual returned value is based on the result | |
2827 of the @code{memq} call. | |
2828 | |
2829 @item when @var{condition} @var{clause} | |
2830 This clause is just a synonym for @code{if}. | |
2831 | |
2832 @item unless @var{condition} @var{clause} | |
2833 The @code{unless} clause is just like @code{if} except that the | |
2834 sense of the condition is reversed. | |
2835 | |
2836 @item named @var{name} | |
2837 This clause gives a name other than @code{nil} to the implicit | |
2838 block surrounding the loop. The @var{name} is the symbol to be | |
2839 used as the block name. | |
2840 | |
2841 @item initially [do] @var{forms}... | |
2842 This keyword introduces one or more Lisp forms which will be | |
2843 executed before the loop itself begins (but after any variables | |
2844 requested by @code{for} or @code{with} have been bound to their | |
2845 initial values). @code{initially} clauses can appear anywhere; | |
2846 if there are several, they are executed in the order they appear | |
2847 in the loop. The keyword @code{do} is optional. | |
2848 | |
2849 @item finally [do] @var{forms}... | |
2850 This introduces Lisp forms which will be executed after the loop | |
2851 finishes (say, on request of a @code{for} or @code{while}). | |
2852 @code{initially} and @code{finally} clauses may appear anywhere | |
2853 in the loop construct, but they are executed (in the specified | |
2854 order) at the beginning or end, respectively, of the loop. | |
2855 | |
2856 @item finally return @var{form} | |
2857 This says that @var{form} should be executed after the loop | |
2858 is done to obtain a return value. (Without this, or some other | |
2859 clause like @code{collect} or @code{return}, the loop will simply | |
2860 return @code{nil}.) Variables bound by @code{for}, @code{with}, | |
2861 or @code{into} will still contain their final values when @var{form} | |
2862 is executed. | |
2863 | |
2864 @item do @var{forms}... | |
2865 The word @code{do} may be followed by any number of Lisp expressions | |
2866 which are executed as an implicit @code{progn} in the body of the | |
2867 loop. Many of the examples in this section illustrate the use of | |
2868 @code{do}. | |
2869 | |
2870 @item return @var{form} | |
2871 This clause causes the loop to return immediately. The following | |
2872 Lisp form is evaluated to give the return value of the @code{loop} | |
2873 form. The @code{finally} clauses, if any, are not executed. | |
2874 Of course, @code{return} is generally used inside an @code{if} or | |
2875 @code{unless}, as its use in a top-level loop clause would mean | |
2876 the loop would never get to ``loop'' more than once. | |
2877 | |
2878 The clause @samp{return @var{form}} is equivalent to | |
2879 @samp{do (return @var{form})} (or @code{return-from} if the loop | |
2880 was named). The @code{return} clause is implemented a bit more | |
2881 efficiently, though. | |
2882 @end table | |
2883 | |
2884 While there is no high-level way to add user extensions to @code{loop} | |
2885 (comparable to @code{defsetf} for @code{setf}, say), this package | |
2886 does offer two properties called @code{cl-loop-handler} and | |
2887 @code{cl-loop-for-handler} which are functions to be called when | |
2888 a given symbol is encountered as a top-level loop clause or | |
2889 @code{for} clause, respectively. Consult the source code in | |
2890 file @file{cl-macs.el} for details. | |
2891 | |
2892 This package's @code{loop} macro is compatible with that of Common | |
2893 Lisp, except that a few features are not implemented: @code{loop-finish} | |
2894 and data-type specifiers. Naturally, the @code{for} clauses which | |
2895 iterate over keymaps, overlays, intervals, frames, windows, and | |
2896 buffers are Emacs-specific extensions. | |
2897 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
2898 @node Multiple Values, , Loop Facility, Control Structure |
25829 | 2899 @section Multiple Values |
2900 | |
2901 @noindent | |
2902 Common Lisp functions can return zero or more results. Emacs Lisp | |
2903 functions, by contrast, always return exactly one result. This | |
2904 package makes no attempt to emulate Common Lisp multiple return | |
2905 values; Emacs versions of Common Lisp functions that return more | |
2906 than one value either return just the first value (as in | |
2907 @code{compiler-macroexpand}) or return a list of values (as in | |
2908 @code{get-setf-method}). This package @emph{does} define placeholders | |
2909 for the Common Lisp functions that work with multiple values, but | |
2910 in Emacs Lisp these functions simply operate on lists instead. | |
2911 The @code{values} form, for example, is a synonym for @code{list} | |
2912 in Emacs. | |
2913 | |
2914 @defspec multiple-value-bind (var@dots{}) values-form forms@dots{} | |
2915 This form evaluates @var{values-form}, which must return a list of | |
2916 values. It then binds the @var{var}s to these respective values, | |
2917 as if by @code{let}, and then executes the body @var{forms}. | |
2918 If there are more @var{var}s than values, the extra @var{var}s | |
2919 are bound to @code{nil}. If there are fewer @var{var}s than | |
2920 values, the excess values are ignored. | |
2921 @end defspec | |
2922 | |
2923 @defspec multiple-value-setq (var@dots{}) form | |
2924 This form evaluates @var{form}, which must return a list of values. | |
2925 It then sets the @var{var}s to these respective values, as if by | |
2926 @code{setq}. Extra @var{var}s or values are treated the same as | |
2927 in @code{multiple-value-bind}. | |
2928 @end defspec | |
2929 | |
2930 The older Quiroz package attempted a more faithful (but still | |
2931 imperfect) emulation of Common Lisp multiple values. The old | |
2932 method ``usually'' simulated true multiple values quite well, | |
2933 but under certain circumstances would leave spurious return | |
2934 values in memory where a later, unrelated @code{multiple-value-bind} | |
2935 form would see them. | |
2936 | |
2937 Since a perfect emulation is not feasible in Emacs Lisp, this | |
2938 package opts to keep it as simple and predictable as possible. | |
2939 | |
2940 @node Macros, Declarations, Control Structure, Top | |
2941 @chapter Macros | |
2942 | |
2943 @noindent | |
2944 This package implements the various Common Lisp features of | |
2945 @code{defmacro}, such as destructuring, @code{&environment}, | |
2946 and @code{&body}. Top-level @code{&whole} is not implemented | |
2947 for @code{defmacro} due to technical difficulties. | |
2948 @xref{Argument Lists}. | |
2949 | |
2950 Destructuring is made available to the user by way of the | |
2951 following macro: | |
2952 | |
2953 @defspec destructuring-bind arglist expr forms@dots{} | |
2954 This macro expands to code which executes @var{forms}, with | |
2955 the variables in @var{arglist} bound to the list of values | |
2956 returned by @var{expr}. The @var{arglist} can include all | |
2957 the features allowed for @code{defmacro} argument lists, | |
2958 including destructuring. (The @code{&environment} keyword | |
2959 is not allowed.) The macro expansion will signal an error | |
2960 if @var{expr} returns a list of the wrong number of arguments | |
2961 or with incorrect keyword arguments. | |
2962 @end defspec | |
2963 | |
2964 This package also includes the Common Lisp @code{define-compiler-macro} | |
2965 facility, which allows you to define compile-time expansions and | |
2966 optimizations for your functions. | |
2967 | |
2968 @defspec define-compiler-macro name arglist forms@dots{} | |
2969 This form is similar to @code{defmacro}, except that it only expands | |
2970 calls to @var{name} at compile-time; calls processed by the Lisp | |
2971 interpreter are not expanded, nor are they expanded by the | |
2972 @code{macroexpand} function. | |
2973 | |
2974 The argument list may begin with a @code{&whole} keyword and a | |
2975 variable. This variable is bound to the macro-call form itself, | |
2976 i.e., to a list of the form @samp{(@var{name} @var{args}@dots{})}. | |
2977 If the macro expander returns this form unchanged, then the | |
2978 compiler treats it as a normal function call. This allows | |
2979 compiler macros to work as optimizers for special cases of a | |
2980 function, leaving complicated cases alone. | |
2981 | |
2982 For example, here is a simplified version of a definition that | |
2983 appears as a standard part of this package: | |
2984 | |
2985 @example | |
2986 (define-compiler-macro member* (&whole form a list &rest keys) | |
2987 (if (and (null keys) | |
2988 (eq (car-safe a) 'quote) | |
2989 (not (floatp-safe (cadr a)))) | |
2990 (list 'memq a list) | |
2991 form)) | |
2992 @end example | |
2993 | |
2994 @noindent | |
2995 This definition causes @code{(member* @var{a} @var{list})} to change | |
2996 to a call to the faster @code{memq} in the common case where @var{a} | |
2997 is a non-floating-point constant; if @var{a} is anything else, or | |
2998 if there are any keyword arguments in the call, then the original | |
2999 @code{member*} call is left intact. (The actual compiler macro | |
3000 for @code{member*} optimizes a number of other cases, including | |
3001 common @code{:test} predicates.) | |
3002 @end defspec | |
3003 | |
3004 @defun compiler-macroexpand form | |
3005 This function is analogous to @code{macroexpand}, except that it | |
3006 expands compiler macros rather than regular macros. It returns | |
3007 @var{form} unchanged if it is not a call to a function for which | |
3008 a compiler macro has been defined, or if that compiler macro | |
3009 decided to punt by returning its @code{&whole} argument. Like | |
3010 @code{macroexpand}, it expands repeatedly until it reaches a form | |
3011 for which no further expansion is possible. | |
3012 @end defun | |
3013 | |
3014 @xref{Macro Bindings}, for descriptions of the @code{macrolet} | |
3015 and @code{symbol-macrolet} forms for making ``local'' macro | |
3016 definitions. | |
3017 | |
3018 @node Declarations, Symbols, Macros, Top | |
3019 @chapter Declarations | |
3020 | |
3021 @noindent | |
3022 Common Lisp includes a complex and powerful ``declaration'' | |
3023 mechanism that allows you to give the compiler special hints | |
3024 about the types of data that will be stored in particular variables, | |
3025 and about the ways those variables and functions will be used. This | |
3026 package defines versions of all the Common Lisp declaration forms: | |
3027 @code{declare}, @code{locally}, @code{proclaim}, @code{declaim}, | |
3028 and @code{the}. | |
3029 | |
3030 Most of the Common Lisp declarations are not currently useful in | |
3031 Emacs Lisp, as the byte-code system provides little opportunity | |
3032 to benefit from type information, and @code{special} declarations | |
3033 are redundant in a fully dynamically-scoped Lisp. A few | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3034 declarations are meaningful when the optimizing byte |
25829 | 3035 compiler is being used, however. Under the earlier non-optimizing |
3036 compiler, these declarations will effectively be ignored. | |
3037 | |
3038 @defun proclaim decl-spec | |
3039 This function records a ``global'' declaration specified by | |
3040 @var{decl-spec}. Since @code{proclaim} is a function, @var{decl-spec} | |
3041 is evaluated and thus should normally be quoted. | |
3042 @end defun | |
3043 | |
3044 @defspec declaim decl-specs@dots{} | |
3045 This macro is like @code{proclaim}, except that it takes any number | |
3046 of @var{decl-spec} arguments, and the arguments are unevaluated and | |
3047 unquoted. The @code{declaim} macro also puts an @code{(eval-when | |
3048 (compile load eval) ...)} around the declarations so that they will | |
3049 be registered at compile-time as well as at run-time. (This is vital, | |
3050 since normally the declarations are meant to influence the way the | |
3051 compiler treats the rest of the file that contains the @code{declaim} | |
3052 form.) | |
3053 @end defspec | |
3054 | |
3055 @defspec declare decl-specs@dots{} | |
3056 This macro is used to make declarations within functions and other | |
3057 code. Common Lisp allows declarations in various locations, generally | |
3058 at the beginning of any of the many ``implicit @code{progn}s'' | |
3059 throughout Lisp syntax, such as function bodies, @code{let} bodies, | |
3060 etc. Currently the only declaration understood by @code{declare} | |
3061 is @code{special}. | |
3062 @end defspec | |
3063 | |
3064 @defspec locally declarations@dots{} forms@dots{} | |
3065 In this package, @code{locally} is no different from @code{progn}. | |
3066 @end defspec | |
3067 | |
3068 @defspec the type form | |
3069 Type information provided by @code{the} is ignored in this package; | |
3070 in other words, @code{(the @var{type} @var{form})} is equivalent | |
3071 to @var{form}. Future versions of the optimizing byte-compiler may | |
3072 make use of this information. | |
3073 | |
3074 For example, @code{mapcar} can map over both lists and arrays. It is | |
3075 hard for the compiler to expand @code{mapcar} into an in-line loop | |
3076 unless it knows whether the sequence will be a list or an array ahead | |
3077 of time. With @code{(mapcar 'car (the vector foo))}, a future | |
3078 compiler would have enough information to expand the loop in-line. | |
3079 For now, Emacs Lisp will treat the above code as exactly equivalent | |
3080 to @code{(mapcar 'car foo)}. | |
3081 @end defspec | |
3082 | |
3083 Each @var{decl-spec} in a @code{proclaim}, @code{declaim}, or | |
3084 @code{declare} should be a list beginning with a symbol that says | |
3085 what kind of declaration it is. This package currently understands | |
3086 @code{special}, @code{inline}, @code{notinline}, @code{optimize}, | |
3087 and @code{warn} declarations. (The @code{warn} declaration is an | |
3088 extension of standard Common Lisp.) Other Common Lisp declarations, | |
3089 such as @code{type} and @code{ftype}, are silently ignored. | |
3090 | |
3091 @table @code | |
3092 @item special | |
3093 Since all variables in Emacs Lisp are ``special'' (in the Common | |
3094 Lisp sense), @code{special} declarations are only advisory. They | |
3095 simply tell the optimizing byte compiler that the specified | |
3096 variables are intentionally being referred to without being | |
3097 bound in the body of the function. The compiler normally emits | |
3098 warnings for such references, since they could be typographical | |
3099 errors for references to local variables. | |
3100 | |
3101 The declaration @code{(declare (special @var{var1} @var{var2}))} is | |
3102 equivalent to @code{(defvar @var{var1}) (defvar @var{var2})} in the | |
3103 optimizing compiler, or to nothing at all in older compilers (which | |
3104 do not warn for non-local references). | |
3105 | |
3106 In top-level contexts, it is generally better to write | |
3107 @code{(defvar @var{var})} than @code{(declaim (special @var{var}))}, | |
3108 since @code{defvar} makes your intentions clearer. But the older | |
3109 byte compilers can not handle @code{defvar}s appearing inside of | |
3110 functions, while @code{(declare (special @var{var}))} takes care | |
3111 to work correctly with all compilers. | |
3112 | |
3113 @item inline | |
3114 The @code{inline} @var{decl-spec} lists one or more functions | |
3115 whose bodies should be expanded ``in-line'' into calling functions | |
3116 whenever the compiler is able to arrange for it. For example, | |
3117 the Common Lisp function @code{cadr} is declared @code{inline} | |
3118 by this package so that the form @code{(cadr @var{x})} will | |
3119 expand directly into @code{(car (cdr @var{x}))} when it is called | |
3120 in user functions, for a savings of one (relatively expensive) | |
3121 function call. | |
3122 | |
3123 The following declarations are all equivalent. Note that the | |
3124 @code{defsubst} form is a convenient way to define a function | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3125 and declare it inline all at once. |
25829 | 3126 |
3127 @example | |
3128 (declaim (inline foo bar)) | |
3129 (eval-when (compile load eval) (proclaim '(inline foo bar))) | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3130 (defsubst foo (...) ...) ; instead of defun |
25829 | 3131 @end example |
3132 | |
52704 | 3133 @strong{Please note:} this declaration remains in effect after the |
25829 | 3134 containing source file is done. It is correct to use it to |
3135 request that a function you have defined should be inlined, | |
3136 but it is impolite to use it to request inlining of an external | |
3137 function. | |
3138 | |
3139 In Common Lisp, it is possible to use @code{(declare (inline @dots{}))} | |
3140 before a particular call to a function to cause just that call to | |
3141 be inlined; the current byte compilers provide no way to implement | |
3142 this, so @code{(declare (inline @dots{}))} is currently ignored by | |
3143 this package. | |
3144 | |
3145 @item notinline | |
3146 The @code{notinline} declaration lists functions which should | |
3147 not be inlined after all; it cancels a previous @code{inline} | |
3148 declaration. | |
3149 | |
3150 @item optimize | |
3151 This declaration controls how much optimization is performed by | |
3152 the compiler. Naturally, it is ignored by the earlier non-optimizing | |
3153 compilers. | |
3154 | |
3155 The word @code{optimize} is followed by any number of lists like | |
3156 @code{(speed 3)} or @code{(safety 2)}. Common Lisp defines several | |
3157 optimization ``qualities''; this package ignores all but @code{speed} | |
3158 and @code{safety}. The value of a quality should be an integer from | |
3159 0 to 3, with 0 meaning ``unimportant'' and 3 meaning ``very important.'' | |
3160 The default level for both qualities is 1. | |
3161 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3162 In this package, with the optimizing compiler, the |
25829 | 3163 @code{speed} quality is tied to the @code{byte-compile-optimize} |
3164 flag, which is set to @code{nil} for @code{(speed 0)} and to | |
3165 @code{t} for higher settings; and the @code{safety} quality is | |
3166 tied to the @code{byte-compile-delete-errors} flag, which is | |
3167 set to @code{t} for @code{(safety 3)} and to @code{nil} for all | |
3168 lower settings. (The latter flag controls whether the compiler | |
3169 is allowed to optimize out code whose only side-effect could | |
3170 be to signal an error, e.g., rewriting @code{(progn foo bar)} to | |
3171 @code{bar} when it is not known whether @code{foo} will be bound | |
3172 at run-time.) | |
3173 | |
3174 Note that even compiling with @code{(safety 0)}, the Emacs | |
3175 byte-code system provides sufficient checking to prevent real | |
3176 harm from being done. For example, barring serious bugs in | |
3177 Emacs itself, Emacs will not crash with a segmentation fault | |
3178 just because of an error in a fully-optimized Lisp program. | |
3179 | |
3180 The @code{optimize} declaration is normally used in a top-level | |
3181 @code{proclaim} or @code{declaim} in a file; Common Lisp allows | |
3182 it to be used with @code{declare} to set the level of optimization | |
3183 locally for a given form, but this will not work correctly with the | |
3184 current version of the optimizing compiler. (The @code{declare} | |
3185 will set the new optimization level, but that level will not | |
3186 automatically be unset after the enclosing form is done.) | |
3187 | |
3188 @item warn | |
3189 This declaration controls what sorts of warnings are generated | |
3190 by the byte compiler. Again, only the optimizing compiler | |
3191 generates warnings. The word @code{warn} is followed by any | |
3192 number of ``warning qualities,'' similar in form to optimization | |
3193 qualities. The currently supported warning types are | |
3194 @code{redefine}, @code{callargs}, @code{unresolved}, and | |
3195 @code{free-vars}; in the current system, a value of 0 will | |
3196 disable these warnings and any higher value will enable them. | |
3197 See the documentation for the optimizing byte compiler for details. | |
3198 @end table | |
3199 | |
3200 @node Symbols, Numbers, Declarations, Top | |
3201 @chapter Symbols | |
3202 | |
3203 @noindent | |
3204 This package defines several symbol-related features that were | |
3205 missing from Emacs Lisp. | |
3206 | |
3207 @menu | |
3208 * Property Lists:: `get*', `remprop', `getf', `remf' | |
3209 * Creating Symbols:: `gensym', `gentemp' | |
3210 @end menu | |
3211 | |
3212 @node Property Lists, Creating Symbols, Symbols, Symbols | |
3213 @section Property Lists | |
3214 | |
3215 @noindent | |
3216 These functions augment the standard Emacs Lisp functions @code{get} | |
3217 and @code{put} for operating on properties attached to symbols. | |
3218 There are also functions for working with property lists as | |
3219 first-class data structures not attached to particular symbols. | |
3220 | |
3221 @defun get* symbol property &optional default | |
3222 This function is like @code{get}, except that if the property is | |
3223 not found, the @var{default} argument provides the return value. | |
3224 (The Emacs Lisp @code{get} function always uses @code{nil} as | |
3225 the default; this package's @code{get*} is equivalent to Common | |
3226 Lisp's @code{get}.) | |
3227 | |
3228 The @code{get*} function is @code{setf}-able; when used in this | |
3229 fashion, the @var{default} argument is allowed but ignored. | |
3230 @end defun | |
3231 | |
3232 @defun remprop symbol property | |
3233 This function removes the entry for @var{property} from the property | |
3234 list of @var{symbol}. It returns a true value if the property was | |
3235 indeed found and removed, or @code{nil} if there was no such property. | |
3236 (This function was probably omitted from Emacs originally because, | |
3237 since @code{get} did not allow a @var{default}, it was very difficult | |
3238 to distinguish between a missing property and a property whose value | |
3239 was @code{nil}; thus, setting a property to @code{nil} was close | |
3240 enough to @code{remprop} for most purposes.) | |
3241 @end defun | |
3242 | |
3243 @defun getf place property &optional default | |
3244 This function scans the list @var{place} as if it were a property | |
3245 list, i.e., a list of alternating property names and values. If | |
3246 an even-numbered element of @var{place} is found which is @code{eq} | |
3247 to @var{property}, the following odd-numbered element is returned. | |
3248 Otherwise, @var{default} is returned (or @code{nil} if no default | |
3249 is given). | |
3250 | |
3251 In particular, | |
3252 | |
3253 @example | |
3254 (get sym prop) @equiv{} (getf (symbol-plist sym) prop) | |
3255 @end example | |
3256 | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
3257 It is valid to use @code{getf} as a @code{setf} place, in which case |
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
3258 its @var{place} argument must itself be a valid @code{setf} place. |
25829 | 3259 The @var{default} argument, if any, is ignored in this context. |
3260 The effect is to change (via @code{setcar}) the value cell in the | |
3261 list that corresponds to @var{property}, or to cons a new property-value | |
3262 pair onto the list if the property is not yet present. | |
3263 | |
3264 @example | |
3265 (put sym prop val) @equiv{} (setf (getf (symbol-plist sym) prop) val) | |
3266 @end example | |
3267 | |
3268 The @code{get} and @code{get*} functions are also @code{setf}-able. | |
3269 The fact that @code{default} is ignored can sometimes be useful: | |
3270 | |
3271 @example | |
3272 (incf (get* 'foo 'usage-count 0)) | |
3273 @end example | |
3274 | |
3275 Here, symbol @code{foo}'s @code{usage-count} property is incremented | |
3276 if it exists, or set to 1 (an incremented 0) otherwise. | |
3277 | |
3278 When not used as a @code{setf} form, @code{getf} is just a regular | |
3279 function and its @var{place} argument can actually be any Lisp | |
3280 expression. | |
3281 @end defun | |
3282 | |
3283 @defspec remf place property | |
3284 This macro removes the property-value pair for @var{property} from | |
3285 the property list stored at @var{place}, which is any @code{setf}-able | |
3286 place expression. It returns true if the property was found. Note | |
3287 that if @var{property} happens to be first on the list, this will | |
3288 effectively do a @code{(setf @var{place} (cddr @var{place}))}, | |
3289 whereas if it occurs later, this simply uses @code{setcdr} to splice | |
3290 out the property and value cells. | |
3291 @end defspec | |
3292 | |
3293 @iftex | |
3294 @secno=2 | |
3295 @end iftex | |
3296 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3297 @node Creating Symbols, , Property Lists, Symbols |
25829 | 3298 @section Creating Symbols |
3299 | |
3300 @noindent | |
3301 These functions create unique symbols, typically for use as | |
3302 temporary variables. | |
3303 | |
3304 @defun gensym &optional x | |
3305 This function creates a new, uninterned symbol (using @code{make-symbol}) | |
3306 with a unique name. (The name of an uninterned symbol is relevant | |
3307 only if the symbol is printed.) By default, the name is generated | |
3308 from an increasing sequence of numbers, @samp{G1000}, @samp{G1001}, | |
3309 @samp{G1002}, etc. If the optional argument @var{x} is a string, that | |
3310 string is used as a prefix instead of @samp{G}. Uninterned symbols | |
3311 are used in macro expansions for temporary variables, to ensure that | |
3312 their names will not conflict with ``real'' variables in the user's | |
3313 code. | |
3314 @end defun | |
3315 | |
3316 @defvar *gensym-counter* | |
3317 This variable holds the counter used to generate @code{gensym} names. | |
3318 It is incremented after each use by @code{gensym}. In Common Lisp | |
3319 this is initialized with 0, but this package initializes it with a | |
3320 random (time-dependent) value to avoid trouble when two files that | |
3321 each used @code{gensym} in their compilation are loaded together. | |
3322 (Uninterned symbols become interned when the compiler writes them | |
3323 out to a file and the Emacs loader loads them, so their names have to | |
3324 be treated a bit more carefully than in Common Lisp where uninterned | |
3325 symbols remain uninterned after loading.) | |
3326 @end defvar | |
3327 | |
3328 @defun gentemp &optional x | |
3329 This function is like @code{gensym}, except that it produces a new | |
3330 @emph{interned} symbol. If the symbol that is generated already | |
3331 exists, the function keeps incrementing the counter and trying | |
3332 again until a new symbol is generated. | |
3333 @end defun | |
3334 | |
3335 The Quiroz @file{cl.el} package also defined a @code{defkeyword} | |
3336 form for creating self-quoting keyword symbols. This package | |
3337 automatically creates all keywords that are called for by | |
3338 @code{&key} argument specifiers, and discourages the use of | |
3339 keywords as data unrelated to keyword arguments, so the | |
3340 @code{defkeyword} form has been discontinued. | |
3341 | |
3342 @iftex | |
3343 @chapno=11 | |
3344 @end iftex | |
3345 | |
3346 @node Numbers, Sequences, Symbols, Top | |
3347 @chapter Numbers | |
3348 | |
3349 @noindent | |
3350 This section defines a few simple Common Lisp operations on numbers | |
3351 which were left out of Emacs Lisp. | |
3352 | |
3353 @menu | |
3354 * Predicates on Numbers:: `plusp', `oddp', `floatp-safe', etc. | |
28039
c3a446101beb
Remove references to rassoc, delete, expt.
Dave Love <fx@gnu.org>
parents:
27511
diff
changeset
|
3355 * Numerical Functions:: `abs', `floor*', etc. |
25829 | 3356 * Random Numbers:: `random*', `make-random-state' |
42136
5b63c25571a8
Remove most-positive-fixnum, most-negative-fixnum.
Dave Love <fx@gnu.org>
parents:
40590
diff
changeset
|
3357 * Implementation Parameters:: `most-positive-float' |
25829 | 3358 @end menu |
3359 | |
3360 @iftex | |
3361 @secno=1 | |
3362 @end iftex | |
3363 | |
3364 @node Predicates on Numbers, Numerical Functions, Numbers, Numbers | |
3365 @section Predicates on Numbers | |
3366 | |
3367 @noindent | |
3368 These functions return @code{t} if the specified condition is | |
3369 true of the numerical argument, or @code{nil} otherwise. | |
3370 | |
3371 @defun plusp number | |
3372 This predicate tests whether @var{number} is positive. It is an | |
3373 error if the argument is not a number. | |
3374 @end defun | |
3375 | |
3376 @defun minusp number | |
3377 This predicate tests whether @var{number} is negative. It is an | |
3378 error if the argument is not a number. | |
3379 @end defun | |
3380 | |
3381 @defun oddp integer | |
3382 This predicate tests whether @var{integer} is odd. It is an | |
3383 error if the argument is not an integer. | |
3384 @end defun | |
3385 | |
3386 @defun evenp integer | |
3387 This predicate tests whether @var{integer} is even. It is an | |
3388 error if the argument is not an integer. | |
3389 @end defun | |
3390 | |
3391 @defun floatp-safe object | |
3392 This predicate tests whether @var{object} is a floating-point | |
3393 number. On systems that support floating-point, this is equivalent | |
3394 to @code{floatp}. On other systems, this always returns @code{nil}. | |
3395 @end defun | |
3396 | |
3397 @iftex | |
3398 @secno=3 | |
3399 @end iftex | |
3400 | |
3401 @node Numerical Functions, Random Numbers, Predicates on Numbers, Numbers | |
3402 @section Numerical Functions | |
3403 | |
3404 @noindent | |
3405 These functions perform various arithmetic operations on numbers. | |
3406 | |
3407 @defun gcd &rest integers | |
3408 This function returns the Greatest Common Divisor of the arguments. | |
3409 For one argument, it returns the absolute value of that argument. | |
3410 For zero arguments, it returns zero. | |
3411 @end defun | |
3412 | |
3413 @defun lcm &rest integers | |
3414 This function returns the Least Common Multiple of the arguments. | |
3415 For one argument, it returns the absolute value of that argument. | |
3416 For zero arguments, it returns one. | |
3417 @end defun | |
3418 | |
3419 @defun isqrt integer | |
3420 This function computes the ``integer square root'' of its integer | |
3421 argument, i.e., the greatest integer less than or equal to the true | |
3422 square root of the argument. | |
3423 @end defun | |
3424 | |
3425 @defun floor* number &optional divisor | |
3426 This function implements the Common Lisp @code{floor} function. | |
3427 It is called @code{floor*} to avoid name conflicts with the | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3428 simpler @code{floor} function built-in to Emacs. |
25829 | 3429 |
3430 With one argument, @code{floor*} returns a list of two numbers: | |
3431 The argument rounded down (toward minus infinity) to an integer, | |
3432 and the ``remainder'' which would have to be added back to the | |
3433 first return value to yield the argument again. If the argument | |
3434 is an integer @var{x}, the result is always the list @code{(@var{x} 0)}. | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3435 If the argument is a floating-point number, the first |
25829 | 3436 result is a Lisp integer and the second is a Lisp float between |
3437 0 (inclusive) and 1 (exclusive). | |
3438 | |
3439 With two arguments, @code{floor*} divides @var{number} by | |
3440 @var{divisor}, and returns the floor of the quotient and the | |
3441 corresponding remainder as a list of two numbers. If | |
3442 @code{(floor* @var{x} @var{y})} returns @code{(@var{q} @var{r})}, | |
3443 then @code{@var{q}*@var{y} + @var{r} = @var{x}}, with @var{r} | |
3444 between 0 (inclusive) and @var{r} (exclusive). Also, note | |
3445 that @code{(floor* @var{x})} is exactly equivalent to | |
3446 @code{(floor* @var{x} 1)}. | |
3447 | |
3448 This function is entirely compatible with Common Lisp's @code{floor} | |
3449 function, except that it returns the two results in a list since | |
3450 Emacs Lisp does not support multiple-valued functions. | |
3451 @end defun | |
3452 | |
3453 @defun ceiling* number &optional divisor | |
3454 This function implements the Common Lisp @code{ceiling} function, | |
3455 which is analogous to @code{floor} except that it rounds the | |
3456 argument or quotient of the arguments up toward plus infinity. | |
3457 The remainder will be between 0 and minus @var{r}. | |
3458 @end defun | |
3459 | |
3460 @defun truncate* number &optional divisor | |
3461 This function implements the Common Lisp @code{truncate} function, | |
3462 which is analogous to @code{floor} except that it rounds the | |
3463 argument or quotient of the arguments toward zero. Thus it is | |
3464 equivalent to @code{floor*} if the argument or quotient is | |
3465 positive, or to @code{ceiling*} otherwise. The remainder has | |
3466 the same sign as @var{number}. | |
3467 @end defun | |
3468 | |
3469 @defun round* number &optional divisor | |
3470 This function implements the Common Lisp @code{round} function, | |
3471 which is analogous to @code{floor} except that it rounds the | |
3472 argument or quotient of the arguments to the nearest integer. | |
3473 In the case of a tie (the argument or quotient is exactly | |
3474 halfway between two integers), it rounds to the even integer. | |
3475 @end defun | |
3476 | |
3477 @defun mod* number divisor | |
3478 This function returns the same value as the second return value | |
3479 of @code{floor}. | |
3480 @end defun | |
3481 | |
3482 @defun rem* number divisor | |
3483 This function returns the same value as the second return value | |
3484 of @code{truncate}. | |
3485 @end defun | |
3486 | |
3487 These definitions are compatible with those in the Quiroz | |
3488 @file{cl.el} package, except that this package appends @samp{*} | |
3489 to certain function names to avoid conflicts with existing | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3490 Emacs functions, and that the mechanism for returning |
25829 | 3491 multiple values is different. |
3492 | |
3493 @iftex | |
3494 @secno=8 | |
3495 @end iftex | |
3496 | |
3497 @node Random Numbers, Implementation Parameters, Numerical Functions, Numbers | |
3498 @section Random Numbers | |
3499 | |
3500 @noindent | |
3501 This package also provides an implementation of the Common Lisp | |
3502 random number generator. It uses its own additive-congruential | |
3503 algorithm, which is much more likely to give statistically clean | |
3504 random numbers than the simple generators supplied by many | |
3505 operating systems. | |
3506 | |
3507 @defun random* number &optional state | |
3508 This function returns a random nonnegative number less than | |
3509 @var{number}, and of the same type (either integer or floating-point). | |
3510 The @var{state} argument should be a @code{random-state} object | |
3511 which holds the state of the random number generator. The | |
3512 function modifies this state object as a side effect. If | |
3513 @var{state} is omitted, it defaults to the variable | |
3514 @code{*random-state*}, which contains a pre-initialized | |
3515 @code{random-state} object. | |
3516 @end defun | |
3517 | |
3518 @defvar *random-state* | |
3519 This variable contains the system ``default'' @code{random-state} | |
3520 object, used for calls to @code{random*} that do not specify an | |
3521 alternative state object. Since any number of programs in the | |
3522 Emacs process may be accessing @code{*random-state*} in interleaved | |
3523 fashion, the sequence generated from this variable will be | |
3524 irreproducible for all intents and purposes. | |
3525 @end defvar | |
3526 | |
3527 @defun make-random-state &optional state | |
3528 This function creates or copies a @code{random-state} object. | |
3529 If @var{state} is omitted or @code{nil}, it returns a new copy of | |
3530 @code{*random-state*}. This is a copy in the sense that future | |
3531 sequences of calls to @code{(random* @var{n})} and | |
3532 @code{(random* @var{n} @var{s})} (where @var{s} is the new | |
3533 random-state object) will return identical sequences of random | |
3534 numbers. | |
3535 | |
3536 If @var{state} is a @code{random-state} object, this function | |
3537 returns a copy of that object. If @var{state} is @code{t}, this | |
3538 function returns a new @code{random-state} object seeded from the | |
3539 date and time. As an extension to Common Lisp, @var{state} may also | |
3540 be an integer in which case the new object is seeded from that | |
3541 integer; each different integer seed will result in a completely | |
3542 different sequence of random numbers. | |
3543 | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
3544 It is valid to print a @code{random-state} object to a buffer or |
25829 | 3545 file and later read it back with @code{read}. If a program wishes |
3546 to use a sequence of pseudo-random numbers which can be reproduced | |
3547 later for debugging, it can call @code{(make-random-state t)} to | |
3548 get a new sequence, then print this sequence to a file. When the | |
3549 program is later rerun, it can read the original run's random-state | |
3550 from the file. | |
3551 @end defun | |
3552 | |
3553 @defun random-state-p object | |
3554 This predicate returns @code{t} if @var{object} is a | |
3555 @code{random-state} object, or @code{nil} otherwise. | |
3556 @end defun | |
3557 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3558 @node Implementation Parameters, , Random Numbers, Numbers |
25829 | 3559 @section Implementation Parameters |
3560 | |
3561 @noindent | |
3562 This package defines several useful constants having to with numbers. | |
3563 | |
3564 The following parameters have to do with floating-point numbers. | |
3565 This package determines their values by exercising the computer's | |
3566 floating-point arithmetic in various ways. Because this operation | |
3567 might be slow, the code for initializing them is kept in a separate | |
3568 function that must be called before the parameters can be used. | |
3569 | |
3570 @defun cl-float-limits | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3571 This function makes sure that the Common Lisp floating-point parameters |
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3572 like @code{most-positive-float} have been initialized. Until it is |
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3573 called, these parameters will be @code{nil}. If this version of Emacs |
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3574 does not support floats, the parameters will remain @code{nil}. If the |
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3575 parameters have already been initialized, the function returns |
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3576 immediately. |
25829 | 3577 |
3578 The algorithm makes assumptions that will be valid for most modern | |
3579 machines, but will fail if the machine's arithmetic is extremely | |
3580 unusual, e.g., decimal. | |
3581 @end defun | |
3582 | |
3583 Since true Common Lisp supports up to four different floating-point | |
3584 precisions, it has families of constants like | |
3585 @code{most-positive-single-float}, @code{most-positive-double-float}, | |
3586 @code{most-positive-long-float}, and so on. Emacs has only one | |
3587 floating-point precision, so this package omits the precision word | |
3588 from the constants' names. | |
3589 | |
3590 @defvar most-positive-float | |
3591 This constant equals the largest value a Lisp float can hold. | |
3592 For those systems whose arithmetic supports infinities, this is | |
3593 the largest @emph{finite} value. For IEEE machines, the value | |
3594 is approximately @code{1.79e+308}. | |
3595 @end defvar | |
3596 | |
3597 @defvar most-negative-float | |
3598 This constant equals the most-negative value a Lisp float can hold. | |
3599 (It is assumed to be equal to @code{(- most-positive-float)}.) | |
3600 @end defvar | |
3601 | |
3602 @defvar least-positive-float | |
3603 This constant equals the smallest Lisp float value greater than zero. | |
3604 For IEEE machines, it is about @code{4.94e-324} if denormals are | |
3605 supported or @code{2.22e-308} if not. | |
3606 @end defvar | |
3607 | |
3608 @defvar least-positive-normalized-float | |
3609 This constant equals the smallest @emph{normalized} Lisp float greater | |
3610 than zero, i.e., the smallest value for which IEEE denormalization | |
3611 will not result in a loss of precision. For IEEE machines, this | |
3612 value is about @code{2.22e-308}. For machines that do not support | |
3613 the concept of denormalization and gradual underflow, this constant | |
3614 will always equal @code{least-positive-float}. | |
3615 @end defvar | |
3616 | |
3617 @defvar least-negative-float | |
3618 This constant is the negative counterpart of @code{least-positive-float}. | |
3619 @end defvar | |
3620 | |
3621 @defvar least-negative-normalized-float | |
3622 This constant is the negative counterpart of | |
3623 @code{least-positive-normalized-float}. | |
3624 @end defvar | |
3625 | |
3626 @defvar float-epsilon | |
3627 This constant is the smallest positive Lisp float that can be added | |
3628 to 1.0 to produce a distinct value. Adding a smaller number to 1.0 | |
3629 will yield 1.0 again due to roundoff. For IEEE machines, epsilon | |
3630 is about @code{2.22e-16}. | |
3631 @end defvar | |
3632 | |
3633 @defvar float-negative-epsilon | |
3634 This is the smallest positive value that can be subtracted from | |
3635 1.0 to produce a distinct value. For IEEE machines, it is about | |
3636 @code{1.11e-16}. | |
3637 @end defvar | |
3638 | |
3639 @iftex | |
3640 @chapno=13 | |
3641 @end iftex | |
3642 | |
3643 @node Sequences, Lists, Numbers, Top | |
3644 @chapter Sequences | |
3645 | |
3646 @noindent | |
3647 Common Lisp defines a number of functions that operate on | |
3648 @dfn{sequences}, which are either lists, strings, or vectors. | |
3649 Emacs Lisp includes a few of these, notably @code{elt} and | |
3650 @code{length}; this package defines most of the rest. | |
3651 | |
3652 @menu | |
3653 * Sequence Basics:: Arguments shared by all sequence functions | |
3654 * Mapping over Sequences:: `mapcar*', `mapcan', `map', `every', etc. | |
3655 * Sequence Functions:: `subseq', `remove*', `substitute', etc. | |
3656 * Searching Sequences:: `find', `position', `count', `search', etc. | |
3657 * Sorting Sequences:: `sort*', `stable-sort', `merge' | |
3658 @end menu | |
3659 | |
3660 @node Sequence Basics, Mapping over Sequences, Sequences, Sequences | |
3661 @section Sequence Basics | |
3662 | |
3663 @noindent | |
3664 Many of the sequence functions take keyword arguments; @pxref{Argument | |
3665 Lists}. All keyword arguments are optional and, if specified, | |
3666 may appear in any order. | |
3667 | |
3668 The @code{:key} argument should be passed either @code{nil}, or a | |
3669 function of one argument. This key function is used as a filter | |
3670 through which the elements of the sequence are seen; for example, | |
3671 @code{(find x y :key 'car)} is similar to @code{(assoc* x y)}: | |
3672 It searches for an element of the list whose @code{car} equals | |
3673 @code{x}, rather than for an element which equals @code{x} itself. | |
3674 If @code{:key} is omitted or @code{nil}, the filter is effectively | |
3675 the identity function. | |
3676 | |
3677 The @code{:test} and @code{:test-not} arguments should be either | |
3678 @code{nil}, or functions of two arguments. The test function is | |
3679 used to compare two sequence elements, or to compare a search value | |
3680 with sequence elements. (The two values are passed to the test | |
3681 function in the same order as the original sequence function | |
3682 arguments from which they are derived, or, if they both come from | |
3683 the same sequence, in the same order as they appear in that sequence.) | |
3684 The @code{:test} argument specifies a function which must return | |
3685 true (non-@code{nil}) to indicate a match; instead, you may use | |
3686 @code{:test-not} to give a function which returns @emph{false} to | |
3687 indicate a match. The default test function is @code{:test 'eql}. | |
3688 | |
3689 Many functions which take @var{item} and @code{:test} or @code{:test-not} | |
3690 arguments also come in @code{-if} and @code{-if-not} varieties, | |
3691 where a @var{predicate} function is passed instead of @var{item}, | |
3692 and sequence elements match if the predicate returns true on them | |
3693 (or false in the case of @code{-if-not}). For example: | |
3694 | |
3695 @example | |
3696 (remove* 0 seq :test '=) @equiv{} (remove-if 'zerop seq) | |
3697 @end example | |
3698 | |
3699 @noindent | |
3700 to remove all zeros from sequence @code{seq}. | |
3701 | |
3702 Some operations can work on a subsequence of the argument sequence; | |
3703 these function take @code{:start} and @code{:end} arguments which | |
3704 default to zero and the length of the sequence, respectively. | |
3705 Only elements between @var{start} (inclusive) and @var{end} | |
3706 (exclusive) are affected by the operation. The @var{end} argument | |
3707 may be passed @code{nil} to signify the length of the sequence; | |
3708 otherwise, both @var{start} and @var{end} must be integers, with | |
3709 @code{0 <= @var{start} <= @var{end} <= (length @var{seq})}. | |
3710 If the function takes two sequence arguments, the limits are | |
3711 defined by keywords @code{:start1} and @code{:end1} for the first, | |
3712 and @code{:start2} and @code{:end2} for the second. | |
3713 | |
3714 A few functions accept a @code{:from-end} argument, which, if | |
3715 non-@code{nil}, causes the operation to go from right-to-left | |
3716 through the sequence instead of left-to-right, and a @code{:count} | |
3717 argument, which specifies an integer maximum number of elements | |
3718 to be removed or otherwise processed. | |
3719 | |
3720 The sequence functions make no guarantees about the order in | |
3721 which the @code{:test}, @code{:test-not}, and @code{:key} functions | |
3722 are called on various elements. Therefore, it is a bad idea to depend | |
3723 on side effects of these functions. For example, @code{:from-end} | |
3724 may cause the sequence to be scanned actually in reverse, or it may | |
3725 be scanned forwards but computing a result ``as if'' it were scanned | |
3726 backwards. (Some functions, like @code{mapcar*} and @code{every}, | |
3727 @emph{do} specify exactly the order in which the function is called | |
3728 so side effects are perfectly acceptable in those cases.) | |
3729 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
3730 Strings may contain ``text properties'' as well |
25829 | 3731 as character data. Except as noted, it is undefined whether or |
3732 not text properties are preserved by sequence functions. For | |
3733 example, @code{(remove* ?A @var{str})} may or may not preserve | |
3734 the properties of the characters copied from @var{str} into the | |
3735 result. | |
3736 | |
3737 @node Mapping over Sequences, Sequence Functions, Sequence Basics, Sequences | |
3738 @section Mapping over Sequences | |
3739 | |
3740 @noindent | |
3741 These functions ``map'' the function you specify over the elements | |
3742 of lists or arrays. They are all variations on the theme of the | |
3743 built-in function @code{mapcar}. | |
3744 | |
3745 @defun mapcar* function seq &rest more-seqs | |
3746 This function calls @var{function} on successive parallel sets of | |
3747 elements from its argument sequences. Given a single @var{seq} | |
3748 argument it is equivalent to @code{mapcar}; given @var{n} sequences, | |
3749 it calls the function with the first elements of each of the sequences | |
3750 as the @var{n} arguments to yield the first element of the result | |
3751 list, then with the second elements, and so on. The mapping stops as | |
3752 soon as the shortest sequence runs out. The argument sequences may | |
3753 be any mixture of lists, strings, and vectors; the return sequence | |
3754 is always a list. | |
3755 | |
3756 Common Lisp's @code{mapcar} accepts multiple arguments but works | |
3757 only on lists; Emacs Lisp's @code{mapcar} accepts a single sequence | |
3758 argument. This package's @code{mapcar*} works as a compatible | |
3759 superset of both. | |
3760 @end defun | |
3761 | |
3762 @defun map result-type function seq &rest more-seqs | |
3763 This function maps @var{function} over the argument sequences, | |
3764 just like @code{mapcar*}, but it returns a sequence of type | |
3765 @var{result-type} rather than a list. @var{result-type} must | |
3766 be one of the following symbols: @code{vector}, @code{string}, | |
3767 @code{list} (in which case the effect is the same as for | |
3768 @code{mapcar*}), or @code{nil} (in which case the results are | |
3769 thrown away and @code{map} returns @code{nil}). | |
3770 @end defun | |
3771 | |
3772 @defun maplist function list &rest more-lists | |
3773 This function calls @var{function} on each of its argument lists, | |
3774 then on the @code{cdr}s of those lists, and so on, until the | |
3775 shortest list runs out. The results are returned in the form | |
3776 of a list. Thus, @code{maplist} is like @code{mapcar*} except | |
3777 that it passes in the list pointers themselves rather than the | |
3778 @code{car}s of the advancing pointers. | |
3779 @end defun | |
3780 | |
3781 @defun mapc function seq &rest more-seqs | |
36931 | 3782 This function is like @code{mapcar*}, except that the values returned |
3783 by @var{function} are ignored and thrown away rather than being | |
3784 collected into a list. The return value of @code{mapc} is @var{seq}, | |
3785 the first sequence. This function is more general than the Emacs | |
3786 primitive @code{mapc}. | |
25829 | 3787 @end defun |
3788 | |
3789 @defun mapl function list &rest more-lists | |
3790 This function is like @code{maplist}, except that it throws away | |
3791 the values returned by @var{function}. | |
3792 @end defun | |
3793 | |
3794 @defun mapcan function seq &rest more-seqs | |
3795 This function is like @code{mapcar*}, except that it concatenates | |
3796 the return values (which must be lists) using @code{nconc}, | |
3797 rather than simply collecting them into a list. | |
3798 @end defun | |
3799 | |
3800 @defun mapcon function list &rest more-lists | |
3801 This function is like @code{maplist}, except that it concatenates | |
3802 the return values using @code{nconc}. | |
3803 @end defun | |
3804 | |
3805 @defun some predicate seq &rest more-seqs | |
3806 This function calls @var{predicate} on each element of @var{seq} | |
3807 in turn; if @var{predicate} returns a non-@code{nil} value, | |
3808 @code{some} returns that value, otherwise it returns @code{nil}. | |
3809 Given several sequence arguments, it steps through the sequences | |
3810 in parallel until the shortest one runs out, just as in | |
3811 @code{mapcar*}. You can rely on the left-to-right order in which | |
3812 the elements are visited, and on the fact that mapping stops | |
3813 immediately as soon as @var{predicate} returns non-@code{nil}. | |
3814 @end defun | |
3815 | |
3816 @defun every predicate seq &rest more-seqs | |
3817 This function calls @var{predicate} on each element of the sequence(s) | |
3818 in turn; it returns @code{nil} as soon as @var{predicate} returns | |
3819 @code{nil} for any element, or @code{t} if the predicate was true | |
3820 for all elements. | |
3821 @end defun | |
3822 | |
3823 @defun notany predicate seq &rest more-seqs | |
3824 This function calls @var{predicate} on each element of the sequence(s) | |
3825 in turn; it returns @code{nil} as soon as @var{predicate} returns | |
3826 a non-@code{nil} value for any element, or @code{t} if the predicate | |
3827 was @code{nil} for all elements. | |
3828 @end defun | |
3829 | |
3830 @defun notevery predicate seq &rest more-seqs | |
3831 This function calls @var{predicate} on each element of the sequence(s) | |
3832 in turn; it returns a non-@code{nil} value as soon as @var{predicate} | |
3833 returns @code{nil} for any element, or @code{t} if the predicate was | |
3834 true for all elements. | |
3835 @end defun | |
3836 | |
3837 @defun reduce function seq @t{&key :from-end :start :end :initial-value :key} | |
3838 This function combines the elements of @var{seq} using an associative | |
3839 binary operation. Suppose @var{function} is @code{*} and @var{seq} is | |
3840 the list @code{(2 3 4 5)}. The first two elements of the list are | |
3841 combined with @code{(* 2 3) = 6}; this is combined with the next | |
3842 element, @code{(* 6 4) = 24}, and that is combined with the final | |
3843 element: @code{(* 24 5) = 120}. Note that the @code{*} function happens | |
3844 to be self-reducing, so that @code{(* 2 3 4 5)} has the same effect as | |
3845 an explicit call to @code{reduce}. | |
3846 | |
3847 If @code{:from-end} is true, the reduction is right-associative instead | |
3848 of left-associative: | |
3849 | |
3850 @example | |
3851 (reduce '- '(1 2 3 4)) | |
3852 @equiv{} (- (- (- 1 2) 3) 4) @result{} -8 | |
3853 (reduce '- '(1 2 3 4) :from-end t) | |
3854 @equiv{} (- 1 (- 2 (- 3 4))) @result{} -2 | |
3855 @end example | |
3856 | |
3857 If @code{:key} is specified, it is a function of one argument which | |
3858 is called on each of the sequence elements in turn. | |
3859 | |
3860 If @code{:initial-value} is specified, it is effectively added to the | |
3861 front (or rear in the case of @code{:from-end}) of the sequence. | |
3862 The @code{:key} function is @emph{not} applied to the initial value. | |
3863 | |
3864 If the sequence, including the initial value, has exactly one element | |
3865 then that element is returned without ever calling @var{function}. | |
3866 If the sequence is empty (and there is no initial value), then | |
3867 @var{function} is called with no arguments to obtain the return value. | |
3868 @end defun | |
3869 | |
3870 All of these mapping operations can be expressed conveniently in | |
3871 terms of the @code{loop} macro. In compiled code, @code{loop} will | |
3872 be faster since it generates the loop as in-line code with no | |
3873 function calls. | |
3874 | |
3875 @node Sequence Functions, Searching Sequences, Mapping over Sequences, Sequences | |
3876 @section Sequence Functions | |
3877 | |
3878 @noindent | |
3879 This section describes a number of Common Lisp functions for | |
3880 operating on sequences. | |
3881 | |
3882 @defun subseq sequence start &optional end | |
3883 This function returns a given subsequence of the argument | |
3884 @var{sequence}, which may be a list, string, or vector. | |
3885 The indices @var{start} and @var{end} must be in range, and | |
3886 @var{start} must be no greater than @var{end}. If @var{end} | |
3887 is omitted, it defaults to the length of the sequence. The | |
3888 return value is always a copy; it does not share structure | |
3889 with @var{sequence}. | |
3890 | |
3891 As an extension to Common Lisp, @var{start} and/or @var{end} | |
3892 may be negative, in which case they represent a distance back | |
3893 from the end of the sequence. This is for compatibility with | |
3894 Emacs' @code{substring} function. Note that @code{subseq} is | |
3895 the @emph{only} sequence function that allows negative | |
3896 @var{start} and @var{end}. | |
3897 | |
3898 You can use @code{setf} on a @code{subseq} form to replace a | |
3899 specified range of elements with elements from another sequence. | |
3900 The replacement is done as if by @code{replace}, described below. | |
3901 @end defun | |
3902 | |
3903 @defun concatenate result-type &rest seqs | |
3904 This function concatenates the argument sequences together to | |
3905 form a result sequence of type @var{result-type}, one of the | |
3906 symbols @code{vector}, @code{string}, or @code{list}. The | |
3907 arguments are always copied, even in cases such as | |
3908 @code{(concatenate 'list '(1 2 3))} where the result is | |
3909 identical to an argument. | |
3910 @end defun | |
3911 | |
3912 @defun fill seq item @t{&key :start :end} | |
3913 This function fills the elements of the sequence (or the specified | |
3914 part of the sequence) with the value @var{item}. | |
3915 @end defun | |
3916 | |
3917 @defun replace seq1 seq2 @t{&key :start1 :end1 :start2 :end2} | |
3918 This function copies part of @var{seq2} into part of @var{seq1}. | |
3919 The sequence @var{seq1} is not stretched or resized; the amount | |
3920 of data copied is simply the shorter of the source and destination | |
3921 (sub)sequences. The function returns @var{seq1}. | |
3922 | |
3923 If @var{seq1} and @var{seq2} are @code{eq}, then the replacement | |
3924 will work correctly even if the regions indicated by the start | |
3925 and end arguments overlap. However, if @var{seq1} and @var{seq2} | |
3926 are lists which share storage but are not @code{eq}, and the | |
3927 start and end arguments specify overlapping regions, the effect | |
3928 is undefined. | |
3929 @end defun | |
3930 | |
3931 @defun remove* item seq @t{&key :test :test-not :key :count :start :end :from-end} | |
3932 This returns a copy of @var{seq} with all elements matching | |
3933 @var{item} removed. The result may share storage with or be | |
3934 @code{eq} to @var{seq} in some circumstances, but the original | |
3935 @var{seq} will not be modified. The @code{:test}, @code{:test-not}, | |
3936 and @code{:key} arguments define the matching test that is used; | |
3937 by default, elements @code{eql} to @var{item} are removed. The | |
3938 @code{:count} argument specifies the maximum number of matching | |
3939 elements that can be removed (only the leftmost @var{count} matches | |
3940 are removed). The @code{:start} and @code{:end} arguments specify | |
3941 a region in @var{seq} in which elements will be removed; elements | |
3942 outside that region are not matched or removed. The @code{:from-end} | |
3943 argument, if true, says that elements should be deleted from the | |
3944 end of the sequence rather than the beginning (this matters only | |
3945 if @var{count} was also specified). | |
3946 @end defun | |
3947 | |
3948 @defun delete* item seq @t{&key :test :test-not :key :count :start :end :from-end} | |
3949 This deletes all elements of @var{seq} which match @var{item}. | |
3950 It is a destructive operation. Since Emacs Lisp does not support | |
3951 stretchable strings or vectors, this is the same as @code{remove*} | |
3952 for those sequence types. On lists, @code{remove*} will copy the | |
3953 list if necessary to preserve the original list, whereas | |
3954 @code{delete*} will splice out parts of the argument list. | |
3955 Compare @code{append} and @code{nconc}, which are analogous | |
3956 non-destructive and destructive list operations in Emacs Lisp. | |
3957 @end defun | |
3958 | |
3959 @findex remove-if | |
3960 @findex remove-if-not | |
3961 @findex delete-if | |
3962 @findex delete-if-not | |
3963 The predicate-oriented functions @code{remove-if}, @code{remove-if-not}, | |
3964 @code{delete-if}, and @code{delete-if-not} are defined similarly. | |
3965 | |
3966 @defun remove-duplicates seq @t{&key :test :test-not :key :start :end :from-end} | |
3967 This function returns a copy of @var{seq} with duplicate elements | |
3968 removed. Specifically, if two elements from the sequence match | |
3969 according to the @code{:test}, @code{:test-not}, and @code{:key} | |
3970 arguments, only the rightmost one is retained. If @code{:from-end} | |
3971 is true, the leftmost one is retained instead. If @code{:start} or | |
3972 @code{:end} is specified, only elements within that subsequence are | |
3973 examined or removed. | |
3974 @end defun | |
3975 | |
3976 @defun delete-duplicates seq @t{&key :test :test-not :key :start :end :from-end} | |
3977 This function deletes duplicate elements from @var{seq}. It is | |
3978 a destructive version of @code{remove-duplicates}. | |
3979 @end defun | |
3980 | |
3981 @defun substitute new old seq @t{&key :test :test-not :key :count :start :end :from-end} | |
3982 This function returns a copy of @var{seq}, with all elements | |
3983 matching @var{old} replaced with @var{new}. The @code{:count}, | |
3984 @code{:start}, @code{:end}, and @code{:from-end} arguments may be | |
3985 used to limit the number of substitutions made. | |
3986 @end defun | |
3987 | |
3988 @defun nsubstitute new old seq @t{&key :test :test-not :key :count :start :end :from-end} | |
3989 This is a destructive version of @code{substitute}; it performs | |
3990 the substitution using @code{setcar} or @code{aset} rather than | |
3991 by returning a changed copy of the sequence. | |
3992 @end defun | |
3993 | |
3994 @findex substitute-if | |
3995 @findex substitute-if-not | |
3996 @findex nsubstitute-if | |
3997 @findex nsubstitute-if-not | |
3998 The @code{substitute-if}, @code{substitute-if-not}, @code{nsubstitute-if}, | |
3999 and @code{nsubstitute-if-not} functions are defined similarly. For | |
4000 these, a @var{predicate} is given in place of the @var{old} argument. | |
4001 | |
4002 @node Searching Sequences, Sorting Sequences, Sequence Functions, Sequences | |
4003 @section Searching Sequences | |
4004 | |
4005 @noindent | |
4006 These functions search for elements or subsequences in a sequence. | |
4007 (See also @code{member*} and @code{assoc*}; @pxref{Lists}.) | |
4008 | |
4009 @defun find item seq @t{&key :test :test-not :key :start :end :from-end} | |
4010 This function searches @var{seq} for an element matching @var{item}. | |
4011 If it finds a match, it returns the matching element. Otherwise, | |
4012 it returns @code{nil}. It returns the leftmost match, unless | |
4013 @code{:from-end} is true, in which case it returns the rightmost | |
4014 match. The @code{:start} and @code{:end} arguments may be used to | |
4015 limit the range of elements that are searched. | |
4016 @end defun | |
4017 | |
4018 @defun position item seq @t{&key :test :test-not :key :start :end :from-end} | |
4019 This function is like @code{find}, except that it returns the | |
4020 integer position in the sequence of the matching item rather than | |
4021 the item itself. The position is relative to the start of the | |
4022 sequence as a whole, even if @code{:start} is non-zero. The function | |
4023 returns @code{nil} if no matching element was found. | |
4024 @end defun | |
4025 | |
4026 @defun count item seq @t{&key :test :test-not :key :start :end} | |
4027 This function returns the number of elements of @var{seq} which | |
4028 match @var{item}. The result is always a nonnegative integer. | |
4029 @end defun | |
4030 | |
4031 @findex find-if | |
4032 @findex find-if-not | |
4033 @findex position-if | |
4034 @findex position-if-not | |
4035 @findex count-if | |
4036 @findex count-if-not | |
4037 The @code{find-if}, @code{find-if-not}, @code{position-if}, | |
4038 @code{position-if-not}, @code{count-if}, and @code{count-if-not} | |
4039 functions are defined similarly. | |
4040 | |
4041 @defun mismatch seq1 seq2 @t{&key :test :test-not :key :start1 :end1 :start2 :end2 :from-end} | |
4042 This function compares the specified parts of @var{seq1} and | |
4043 @var{seq2}. If they are the same length and the corresponding | |
4044 elements match (according to @code{:test}, @code{:test-not}, | |
4045 and @code{:key}), the function returns @code{nil}. If there is | |
4046 a mismatch, the function returns the index (relative to @var{seq1}) | |
4047 of the first mismatching element. This will be the leftmost pair of | |
4048 elements which do not match, or the position at which the shorter of | |
4049 the two otherwise-matching sequences runs out. | |
4050 | |
4051 If @code{:from-end} is true, then the elements are compared from right | |
4052 to left starting at @code{(1- @var{end1})} and @code{(1- @var{end2})}. | |
4053 If the sequences differ, then one plus the index of the rightmost | |
4054 difference (relative to @var{seq1}) is returned. | |
4055 | |
4056 An interesting example is @code{(mismatch str1 str2 :key 'upcase)}, | |
4057 which compares two strings case-insensitively. | |
4058 @end defun | |
4059 | |
4060 @defun search seq1 seq2 @t{&key :test :test-not :key :from-end :start1 :end1 :start2 :end2} | |
4061 This function searches @var{seq2} for a subsequence that matches | |
4062 @var{seq1} (or part of it specified by @code{:start1} and | |
4063 @code{:end1}.) Only matches which fall entirely within the region | |
4064 defined by @code{:start2} and @code{:end2} will be considered. | |
4065 The return value is the index of the leftmost element of the | |
4066 leftmost match, relative to the start of @var{seq2}, or @code{nil} | |
4067 if no matches were found. If @code{:from-end} is true, the | |
4068 function finds the @emph{rightmost} matching subsequence. | |
4069 @end defun | |
4070 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
4071 @node Sorting Sequences, , Searching Sequences, Sequences |
25829 | 4072 @section Sorting Sequences |
4073 | |
4074 @defun sort* seq predicate @t{&key :key} | |
4075 This function sorts @var{seq} into increasing order as determined | |
4076 by using @var{predicate} to compare pairs of elements. @var{predicate} | |
4077 should return true (non-@code{nil}) if and only if its first argument | |
4078 is less than (not equal to) its second argument. For example, | |
4079 @code{<} and @code{string-lessp} are suitable predicate functions | |
4080 for sorting numbers and strings, respectively; @code{>} would sort | |
4081 numbers into decreasing rather than increasing order. | |
4082 | |
4083 This function differs from Emacs' built-in @code{sort} in that it | |
4084 can operate on any type of sequence, not just lists. Also, it | |
4085 accepts a @code{:key} argument which is used to preprocess data | |
4086 fed to the @var{predicate} function. For example, | |
4087 | |
4088 @example | |
74831 | 4089 (setq data (sort* data 'string-lessp :key 'downcase)) |
25829 | 4090 @end example |
4091 | |
4092 @noindent | |
4093 sorts @var{data}, a sequence of strings, into increasing alphabetical | |
4094 order without regard to case. A @code{:key} function of @code{car} | |
74889
c18ed26cc2cc
(Sorting Sequences): In sort*, add a little cautionary note about the key
Juanma Barranquero <lekktu@gmail.com>
parents:
74831
diff
changeset
|
4095 would be useful for sorting association lists. It should only be a |
c18ed26cc2cc
(Sorting Sequences): In sort*, add a little cautionary note about the key
Juanma Barranquero <lekktu@gmail.com>
parents:
74831
diff
changeset
|
4096 simple accessor though, it's used heavily in the current |
c18ed26cc2cc
(Sorting Sequences): In sort*, add a little cautionary note about the key
Juanma Barranquero <lekktu@gmail.com>
parents:
74831
diff
changeset
|
4097 implementation. |
25829 | 4098 |
4099 The @code{sort*} function is destructive; it sorts lists by actually | |
4100 rearranging the @code{cdr} pointers in suitable fashion. | |
4101 @end defun | |
4102 | |
4103 @defun stable-sort seq predicate @t{&key :key} | |
4104 This function sorts @var{seq} @dfn{stably}, meaning two elements | |
4105 which are equal in terms of @var{predicate} are guaranteed not to | |
4106 be rearranged out of their original order by the sort. | |
4107 | |
4108 In practice, @code{sort*} and @code{stable-sort} are equivalent | |
4109 in Emacs Lisp because the underlying @code{sort} function is | |
4110 stable by default. However, this package reserves the right to | |
4111 use non-stable methods for @code{sort*} in the future. | |
4112 @end defun | |
4113 | |
4114 @defun merge type seq1 seq2 predicate @t{&key :key} | |
4115 This function merges two sequences @var{seq1} and @var{seq2} by | |
4116 interleaving their elements. The result sequence, of type @var{type} | |
4117 (in the sense of @code{concatenate}), has length equal to the sum | |
4118 of the lengths of the two input sequences. The sequences may be | |
4119 modified destructively. Order of elements within @var{seq1} and | |
4120 @var{seq2} is preserved in the interleaving; elements of the two | |
4121 sequences are compared by @var{predicate} (in the sense of | |
4122 @code{sort}) and the lesser element goes first in the result. | |
4123 When elements are equal, those from @var{seq1} precede those from | |
4124 @var{seq2} in the result. Thus, if @var{seq1} and @var{seq2} are | |
4125 both sorted according to @var{predicate}, then the result will be | |
4126 a merged sequence which is (stably) sorted according to | |
4127 @var{predicate}. | |
4128 @end defun | |
4129 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
4130 @node Lists, Structures, Sequences, Top |
25829 | 4131 @chapter Lists |
4132 | |
4133 @noindent | |
4134 The functions described here operate on lists. | |
4135 | |
4136 @menu | |
36931 | 4137 * List Functions:: `caddr', `first', `list*', etc. |
25829 | 4138 * Substitution of Expressions:: `subst', `sublis', etc. |
4139 * Lists as Sets:: `member*', `adjoin', `union', etc. | |
4140 * Association Lists:: `assoc*', `rassoc*', `acons', `pairlis' | |
4141 @end menu | |
4142 | |
4143 @node List Functions, Substitution of Expressions, Lists, Lists | |
4144 @section List Functions | |
4145 | |
4146 @noindent | |
4147 This section describes a number of simple operations on lists, | |
4148 i.e., chains of cons cells. | |
4149 | |
4150 @defun caddr x | |
4151 This function is equivalent to @code{(car (cdr (cdr @var{x})))}. | |
4152 Likewise, this package defines all 28 @code{c@var{xxx}r} functions | |
4153 where @var{xxx} is up to four @samp{a}s and/or @samp{d}s. | |
4154 All of these functions are @code{setf}-able, and calls to them | |
4155 are expanded inline by the byte-compiler for maximum efficiency. | |
4156 @end defun | |
4157 | |
4158 @defun first x | |
4159 This function is a synonym for @code{(car @var{x})}. Likewise, | |
4160 the functions @code{second}, @code{third}, @dots{}, through | |
4161 @code{tenth} return the given element of the list @var{x}. | |
4162 @end defun | |
4163 | |
4164 @defun rest x | |
4165 This function is a synonym for @code{(cdr @var{x})}. | |
4166 @end defun | |
4167 | |
4168 @defun endp x | |
4169 Common Lisp defines this function to act like @code{null}, but | |
4170 signaling an error if @code{x} is neither a @code{nil} nor a | |
4171 cons cell. This package simply defines @code{endp} as a synonym | |
4172 for @code{null}. | |
4173 @end defun | |
4174 | |
4175 @defun list-length x | |
4176 This function returns the length of list @var{x}, exactly like | |
4177 @code{(length @var{x})}, except that if @var{x} is a circular | |
4178 list (where the cdr-chain forms a loop rather than terminating | |
4179 with @code{nil}), this function returns @code{nil}. (The regular | |
4180 @code{length} function would get stuck if given a circular list.) | |
4181 @end defun | |
4182 | |
4183 @defun list* arg &rest others | |
4184 This function constructs a list of its arguments. The final | |
4185 argument becomes the @code{cdr} of the last cell constructed. | |
4186 Thus, @code{(list* @var{a} @var{b} @var{c})} is equivalent to | |
4187 @code{(cons @var{a} (cons @var{b} @var{c}))}, and | |
4188 @code{(list* @var{a} @var{b} nil)} is equivalent to | |
4189 @code{(list @var{a} @var{b})}. | |
4190 | |
4191 (Note that this function really is called @code{list*} in Common | |
4192 Lisp; it is not a name invented for this package like @code{member*} | |
4193 or @code{defun*}.) | |
4194 @end defun | |
4195 | |
4196 @defun ldiff list sublist | |
4197 If @var{sublist} is a sublist of @var{list}, i.e., is @code{eq} to | |
4198 one of the cons cells of @var{list}, then this function returns | |
4199 a copy of the part of @var{list} up to but not including | |
4200 @var{sublist}. For example, @code{(ldiff x (cddr x))} returns | |
4201 the first two elements of the list @code{x}. The result is a | |
4202 copy; the original @var{list} is not modified. If @var{sublist} | |
4203 is not a sublist of @var{list}, a copy of the entire @var{list} | |
4204 is returned. | |
4205 @end defun | |
4206 | |
4207 @defun copy-list list | |
4208 This function returns a copy of the list @var{list}. It copies | |
4209 dotted lists like @code{(1 2 . 3)} correctly. | |
4210 @end defun | |
4211 | |
4212 @defun copy-tree x &optional vecp | |
4213 This function returns a copy of the tree of cons cells @var{x}. | |
4214 Unlike @code{copy-sequence} (and its alias @code{copy-list}), | |
4215 which copies only along the @code{cdr} direction, this function | |
4216 copies (recursively) along both the @code{car} and the @code{cdr} | |
4217 directions. If @var{x} is not a cons cell, the function simply | |
4218 returns @var{x} unchanged. If the optional @var{vecp} argument | |
4219 is true, this function copies vectors (recursively) as well as | |
4220 cons cells. | |
4221 @end defun | |
4222 | |
4223 @defun tree-equal x y @t{&key :test :test-not :key} | |
4224 This function compares two trees of cons cells. If @var{x} and | |
4225 @var{y} are both cons cells, their @code{car}s and @code{cdr}s are | |
4226 compared recursively. If neither @var{x} nor @var{y} is a cons | |
4227 cell, they are compared by @code{eql}, or according to the | |
4228 specified test. The @code{:key} function, if specified, is | |
4229 applied to the elements of both trees. @xref{Sequences}. | |
4230 @end defun | |
4231 | |
4232 @iftex | |
4233 @secno=3 | |
4234 @end iftex | |
4235 | |
4236 @node Substitution of Expressions, Lists as Sets, List Functions, Lists | |
4237 @section Substitution of Expressions | |
4238 | |
4239 @noindent | |
4240 These functions substitute elements throughout a tree of cons | |
4241 cells. (@xref{Sequence Functions}, for the @code{substitute} | |
4242 function, which works on just the top-level elements of a list.) | |
4243 | |
4244 @defun subst new old tree @t{&key :test :test-not :key} | |
4245 This function substitutes occurrences of @var{old} with @var{new} | |
4246 in @var{tree}, a tree of cons cells. It returns a substituted | |
4247 tree, which will be a copy except that it may share storage with | |
4248 the argument @var{tree} in parts where no substitutions occurred. | |
4249 The original @var{tree} is not modified. This function recurses | |
4250 on, and compares against @var{old}, both @code{car}s and @code{cdr}s | |
4251 of the component cons cells. If @var{old} is itself a cons cell, | |
4252 then matching cells in the tree are substituted as usual without | |
4253 recursively substituting in that cell. Comparisons with @var{old} | |
4254 are done according to the specified test (@code{eql} by default). | |
4255 The @code{:key} function is applied to the elements of the tree | |
4256 but not to @var{old}. | |
4257 @end defun | |
4258 | |
4259 @defun nsubst new old tree @t{&key :test :test-not :key} | |
4260 This function is like @code{subst}, except that it works by | |
4261 destructive modification (by @code{setcar} or @code{setcdr}) | |
4262 rather than copying. | |
4263 @end defun | |
4264 | |
4265 @findex subst-if | |
4266 @findex subst-if-not | |
4267 @findex nsubst-if | |
4268 @findex nsubst-if-not | |
4269 The @code{subst-if}, @code{subst-if-not}, @code{nsubst-if}, and | |
4270 @code{nsubst-if-not} functions are defined similarly. | |
4271 | |
4272 @defun sublis alist tree @t{&key :test :test-not :key} | |
4273 This function is like @code{subst}, except that it takes an | |
4274 association list @var{alist} of @var{old}-@var{new} pairs. | |
4275 Each element of the tree (after applying the @code{:key} | |
4276 function, if any), is compared with the @code{car}s of | |
4277 @var{alist}; if it matches, it is replaced by the corresponding | |
4278 @code{cdr}. | |
4279 @end defun | |
4280 | |
4281 @defun nsublis alist tree @t{&key :test :test-not :key} | |
4282 This is a destructive version of @code{sublis}. | |
4283 @end defun | |
4284 | |
4285 @node Lists as Sets, Association Lists, Substitution of Expressions, Lists | |
4286 @section Lists as Sets | |
4287 | |
4288 @noindent | |
4289 These functions perform operations on lists which represent sets | |
4290 of elements. | |
4291 | |
4292 @defun member* item list @t{&key :test :test-not :key} | |
4293 This function searches @var{list} for an element matching @var{item}. | |
4294 If a match is found, it returns the cons cell whose @code{car} was | |
4295 the matching element. Otherwise, it returns @code{nil}. Elements | |
4296 are compared by @code{eql} by default; you can use the @code{:test}, | |
4297 @code{:test-not}, and @code{:key} arguments to modify this behavior. | |
4298 @xref{Sequences}. | |
4299 | |
4300 Note that this function's name is suffixed by @samp{*} to avoid | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
4301 the incompatible @code{member} function defined in Emacs. |
25829 | 4302 (That function uses @code{equal} for comparisons; it is equivalent |
4303 to @code{(member* @var{item} @var{list} :test 'equal)}.) | |
4304 @end defun | |
4305 | |
4306 @findex member-if | |
4307 @findex member-if-not | |
4308 The @code{member-if} and @code{member-if-not} functions | |
4309 analogously search for elements which satisfy a given predicate. | |
4310 | |
4311 @defun tailp sublist list | |
4312 This function returns @code{t} if @var{sublist} is a sublist of | |
4313 @var{list}, i.e., if @var{sublist} is @code{eql} to @var{list} or to | |
4314 any of its @code{cdr}s. | |
4315 @end defun | |
4316 | |
4317 @defun adjoin item list @t{&key :test :test-not :key} | |
4318 This function conses @var{item} onto the front of @var{list}, | |
4319 like @code{(cons @var{item} @var{list})}, but only if @var{item} | |
4320 is not already present on the list (as determined by @code{member*}). | |
4321 If a @code{:key} argument is specified, it is applied to | |
4322 @var{item} as well as to the elements of @var{list} during | |
4323 the search, on the reasoning that @var{item} is ``about'' to | |
4324 become part of the list. | |
4325 @end defun | |
4326 | |
4327 @defun union list1 list2 @t{&key :test :test-not :key} | |
4328 This function combines two lists which represent sets of items, | |
4329 returning a list that represents the union of those two sets. | |
4330 The result list will contain all items which appear in @var{list1} | |
4331 or @var{list2}, and no others. If an item appears in both | |
4332 @var{list1} and @var{list2} it will be copied only once. If | |
4333 an item is duplicated in @var{list1} or @var{list2}, it is | |
4334 undefined whether or not that duplication will survive in the | |
4335 result list. The order of elements in the result list is also | |
4336 undefined. | |
4337 @end defun | |
4338 | |
4339 @defun nunion list1 list2 @t{&key :test :test-not :key} | |
4340 This is a destructive version of @code{union}; rather than copying, | |
4341 it tries to reuse the storage of the argument lists if possible. | |
4342 @end defun | |
4343 | |
4344 @defun intersection list1 list2 @t{&key :test :test-not :key} | |
4345 This function computes the intersection of the sets represented | |
4346 by @var{list1} and @var{list2}. It returns the list of items | |
4347 which appear in both @var{list1} and @var{list2}. | |
4348 @end defun | |
4349 | |
4350 @defun nintersection list1 list2 @t{&key :test :test-not :key} | |
4351 This is a destructive version of @code{intersection}. It | |
4352 tries to reuse storage of @var{list1} rather than copying. | |
4353 It does @emph{not} reuse the storage of @var{list2}. | |
4354 @end defun | |
4355 | |
4356 @defun set-difference list1 list2 @t{&key :test :test-not :key} | |
4357 This function computes the ``set difference'' of @var{list1} | |
4358 and @var{list2}, i.e., the set of elements that appear in | |
4359 @var{list1} but @emph{not} in @var{list2}. | |
4360 @end defun | |
4361 | |
4362 @defun nset-difference list1 list2 @t{&key :test :test-not :key} | |
4363 This is a destructive @code{set-difference}, which will try | |
4364 to reuse @var{list1} if possible. | |
4365 @end defun | |
4366 | |
4367 @defun set-exclusive-or list1 list2 @t{&key :test :test-not :key} | |
4368 This function computes the ``set exclusive or'' of @var{list1} | |
4369 and @var{list2}, i.e., the set of elements that appear in | |
4370 exactly one of @var{list1} and @var{list2}. | |
4371 @end defun | |
4372 | |
4373 @defun nset-exclusive-or list1 list2 @t{&key :test :test-not :key} | |
4374 This is a destructive @code{set-exclusive-or}, which will try | |
4375 to reuse @var{list1} and @var{list2} if possible. | |
4376 @end defun | |
4377 | |
4378 @defun subsetp list1 list2 @t{&key :test :test-not :key} | |
4379 This function checks whether @var{list1} represents a subset | |
4380 of @var{list2}, i.e., whether every element of @var{list1} | |
4381 also appears in @var{list2}. | |
4382 @end defun | |
4383 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
4384 @node Association Lists, , Lists as Sets, Lists |
25829 | 4385 @section Association Lists |
4386 | |
4387 @noindent | |
4388 An @dfn{association list} is a list representing a mapping from | |
4389 one set of values to another; any list whose elements are cons | |
4390 cells is an association list. | |
4391 | |
4392 @defun assoc* item a-list @t{&key :test :test-not :key} | |
4393 This function searches the association list @var{a-list} for an | |
4394 element whose @code{car} matches (in the sense of @code{:test}, | |
4395 @code{:test-not}, and @code{:key}, or by comparison with @code{eql}) | |
4396 a given @var{item}. It returns the matching element, if any, | |
4397 otherwise @code{nil}. It ignores elements of @var{a-list} which | |
4398 are not cons cells. (This corresponds to the behavior of | |
4399 @code{assq} and @code{assoc} in Emacs Lisp; Common Lisp's | |
4400 @code{assoc} ignores @code{nil}s but considers any other non-cons | |
4401 elements of @var{a-list} to be an error.) | |
4402 @end defun | |
4403 | |
4404 @defun rassoc* item a-list @t{&key :test :test-not :key} | |
4405 This function searches for an element whose @code{cdr} matches | |
4406 @var{item}. If @var{a-list} represents a mapping, this applies | |
4407 the inverse of the mapping to @var{item}. | |
4408 @end defun | |
4409 | |
4410 @findex assoc-if | |
4411 @findex assoc-if-not | |
4412 @findex rassoc-if | |
4413 @findex rassoc-if-not | |
4414 The @code{assoc-if}, @code{assoc-if-not}, @code{rassoc-if}, | |
4415 and @code{rassoc-if-not} functions are defined similarly. | |
4416 | |
4417 Two simple functions for constructing association lists are: | |
4418 | |
4419 @defun acons key value alist | |
4420 This is equivalent to @code{(cons (cons @var{key} @var{value}) @var{alist})}. | |
4421 @end defun | |
4422 | |
4423 @defun pairlis keys values &optional alist | |
4424 This is equivalent to @code{(nconc (mapcar* 'cons @var{keys} @var{values}) | |
4425 @var{alist})}. | |
4426 @end defun | |
4427 | |
4428 @iftex | |
4429 @chapno=18 | |
4430 @end iftex | |
4431 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
4432 @node Structures, Assertions, Lists, Top |
25829 | 4433 @chapter Structures |
4434 | |
4435 @noindent | |
4436 The Common Lisp @dfn{structure} mechanism provides a general way | |
4437 to define data types similar to C's @code{struct} types. A | |
4438 structure is a Lisp object containing some number of @dfn{slots}, | |
4439 each of which can hold any Lisp data object. Functions are | |
4440 provided for accessing and setting the slots, creating or copying | |
4441 structure objects, and recognizing objects of a particular structure | |
4442 type. | |
4443 | |
4444 In true Common Lisp, each structure type is a new type distinct | |
4445 from all existing Lisp types. Since the underlying Emacs Lisp | |
4446 system provides no way to create new distinct types, this package | |
4447 implements structures as vectors (or lists upon request) with a | |
4448 special ``tag'' symbol to identify them. | |
4449 | |
4450 @defspec defstruct name slots@dots{} | |
4451 The @code{defstruct} form defines a new structure type called | |
4452 @var{name}, with the specified @var{slots}. (The @var{slots} | |
4453 may begin with a string which documents the structure type.) | |
4454 In the simplest case, @var{name} and each of the @var{slots} | |
4455 are symbols. For example, | |
4456 | |
4457 @example | |
4458 (defstruct person name age sex) | |
4459 @end example | |
4460 | |
4461 @noindent | |
4462 defines a struct type called @code{person} which contains three | |
4463 slots. Given a @code{person} object @var{p}, you can access those | |
4464 slots by calling @code{(person-name @var{p})}, @code{(person-age @var{p})}, | |
4465 and @code{(person-sex @var{p})}. You can also change these slots by | |
4466 using @code{setf} on any of these place forms: | |
4467 | |
4468 @example | |
4469 (incf (person-age birthday-boy)) | |
4470 @end example | |
4471 | |
4472 You can create a new @code{person} by calling @code{make-person}, | |
4473 which takes keyword arguments @code{:name}, @code{:age}, and | |
4474 @code{:sex} to specify the initial values of these slots in the | |
4475 new object. (Omitting any of these arguments leaves the corresponding | |
4476 slot ``undefined,'' according to the Common Lisp standard; in Emacs | |
4477 Lisp, such uninitialized slots are filled with @code{nil}.) | |
4478 | |
4479 Given a @code{person}, @code{(copy-person @var{p})} makes a new | |
4480 object of the same type whose slots are @code{eq} to those of @var{p}. | |
4481 | |
4482 Given any Lisp object @var{x}, @code{(person-p @var{x})} returns | |
4483 true if @var{x} looks like a @code{person}, false otherwise. (Again, | |
4484 in Common Lisp this predicate would be exact; in Emacs Lisp the | |
4485 best it can do is verify that @var{x} is a vector of the correct | |
4486 length which starts with the correct tag symbol.) | |
4487 | |
4488 Accessors like @code{person-name} normally check their arguments | |
4489 (effectively using @code{person-p}) and signal an error if the | |
4490 argument is the wrong type. This check is affected by | |
4491 @code{(optimize (safety @dots{}))} declarations. Safety level 1, | |
4492 the default, uses a somewhat optimized check that will detect all | |
4493 incorrect arguments, but may use an uninformative error message | |
4494 (e.g., ``expected a vector'' instead of ``expected a @code{person}''). | |
4495 Safety level 0 omits all checks except as provided by the underlying | |
4496 @code{aref} call; safety levels 2 and 3 do rigorous checking that will | |
4497 always print a descriptive error message for incorrect inputs. | |
4498 @xref{Declarations}. | |
4499 | |
4500 @example | |
4501 (setq dave (make-person :name "Dave" :sex 'male)) | |
4502 @result{} [cl-struct-person "Dave" nil male] | |
4503 (setq other (copy-person dave)) | |
4504 @result{} [cl-struct-person "Dave" nil male] | |
4505 (eq dave other) | |
4506 @result{} nil | |
4507 (eq (person-name dave) (person-name other)) | |
4508 @result{} t | |
4509 (person-p dave) | |
4510 @result{} t | |
4511 (person-p [1 2 3 4]) | |
4512 @result{} nil | |
4513 (person-p "Bogus") | |
4514 @result{} nil | |
4515 (person-p '[cl-struct-person counterfeit person object]) | |
4516 @result{} t | |
4517 @end example | |
4518 | |
4519 In general, @var{name} is either a name symbol or a list of a name | |
4520 symbol followed by any number of @dfn{struct options}; each @var{slot} | |
4521 is either a slot symbol or a list of the form @samp{(@var{slot-name} | |
4522 @var{default-value} @var{slot-options}@dots{})}. The @var{default-value} | |
4523 is a Lisp form which is evaluated any time an instance of the | |
4524 structure type is created without specifying that slot's value. | |
4525 | |
4526 Common Lisp defines several slot options, but the only one | |
4527 implemented in this package is @code{:read-only}. A non-@code{nil} | |
4528 value for this option means the slot should not be @code{setf}-able; | |
4529 the slot's value is determined when the object is created and does | |
4530 not change afterward. | |
4531 | |
4532 @example | |
4533 (defstruct person | |
4534 (name nil :read-only t) | |
4535 age | |
4536 (sex 'unknown)) | |
4537 @end example | |
4538 | |
4539 Any slot options other than @code{:read-only} are ignored. | |
4540 | |
4541 For obscure historical reasons, structure options take a different | |
4542 form than slot options. A structure option is either a keyword | |
4543 symbol, or a list beginning with a keyword symbol possibly followed | |
4544 by arguments. (By contrast, slot options are key-value pairs not | |
4545 enclosed in lists.) | |
4546 | |
4547 @example | |
4548 (defstruct (person (:constructor create-person) | |
4549 (:type list) | |
4550 :named) | |
4551 name age sex) | |
4552 @end example | |
4553 | |
4554 The following structure options are recognized. | |
4555 | |
4556 @table @code | |
4557 @iftex | |
4558 @itemmax=0 in | |
4559 @advance@leftskip-.5@tableindent | |
4560 @end iftex | |
4561 @item :conc-name | |
4562 The argument is a symbol whose print name is used as the prefix for | |
4563 the names of slot accessor functions. The default is the name of | |
4564 the struct type followed by a hyphen. The option @code{(:conc-name p-)} | |
4565 would change this prefix to @code{p-}. Specifying @code{nil} as an | |
4566 argument means no prefix, so that the slot names themselves are used | |
4567 to name the accessor functions. | |
4568 | |
4569 @item :constructor | |
4570 In the simple case, this option takes one argument which is an | |
4571 alternate name to use for the constructor function. The default | |
4572 is @code{make-@var{name}}, e.g., @code{make-person}. The above | |
4573 example changes this to @code{create-person}. Specifying @code{nil} | |
4574 as an argument means that no standard constructor should be | |
4575 generated at all. | |
4576 | |
4577 In the full form of this option, the constructor name is followed | |
4578 by an arbitrary argument list. @xref{Program Structure}, for a | |
4579 description of the format of Common Lisp argument lists. All | |
4580 options, such as @code{&rest} and @code{&key}, are supported. | |
4581 The argument names should match the slot names; each slot is | |
4582 initialized from the corresponding argument. Slots whose names | |
4583 do not appear in the argument list are initialized based on the | |
4584 @var{default-value} in their slot descriptor. Also, @code{&optional} | |
4585 and @code{&key} arguments which don't specify defaults take their | |
60927
bae786986f2e
* calc.texi, cl.texi, gnus.texi, idlwave.texi, reftex.texi: Replace
Werner LEMBERG <wl@gnu.org>
parents:
55833
diff
changeset
|
4586 defaults from the slot descriptor. It is valid to include arguments |
25829 | 4587 which don't correspond to slot names; these are useful if they are |
4588 referred to in the defaults for optional, keyword, or @code{&aux} | |
4589 arguments which @emph{do} correspond to slots. | |
4590 | |
4591 You can specify any number of full-format @code{:constructor} | |
4592 options on a structure. The default constructor is still generated | |
4593 as well unless you disable it with a simple-format @code{:constructor} | |
4594 option. | |
4595 | |
4596 @example | |
4597 (defstruct | |
4598 (person | |
4599 (:constructor nil) ; no default constructor | |
4600 (:constructor new-person (name sex &optional (age 0))) | |
4601 (:constructor new-hound (&key (name "Rover") | |
4602 (dog-years 0) | |
4603 &aux (age (* 7 dog-years)) | |
4604 (sex 'canine)))) | |
4605 name age sex) | |
4606 @end example | |
4607 | |
4608 The first constructor here takes its arguments positionally rather | |
4609 than by keyword. (In official Common Lisp terminology, constructors | |
4610 that work By Order of Arguments instead of by keyword are called | |
4611 ``BOA constructors.'' No, I'm not making this up.) For example, | |
4612 @code{(new-person "Jane" 'female)} generates a person whose slots | |
4613 are @code{"Jane"}, 0, and @code{female}, respectively. | |
4614 | |
4615 The second constructor takes two keyword arguments, @code{:name}, | |
4616 which initializes the @code{name} slot and defaults to @code{"Rover"}, | |
4617 and @code{:dog-years}, which does not itself correspond to a slot | |
4618 but which is used to initialize the @code{age} slot. The @code{sex} | |
4619 slot is forced to the symbol @code{canine} with no syntax for | |
4620 overriding it. | |
4621 | |
4622 @item :copier | |
4623 The argument is an alternate name for the copier function for | |
4624 this type. The default is @code{copy-@var{name}}. @code{nil} | |
4625 means not to generate a copier function. (In this implementation, | |
4626 all copier functions are simply synonyms for @code{copy-sequence}.) | |
4627 | |
4628 @item :predicate | |
4629 The argument is an alternate name for the predicate which recognizes | |
4630 objects of this type. The default is @code{@var{name}-p}. @code{nil} | |
4631 means not to generate a predicate function. (If the @code{:type} | |
4632 option is used without the @code{:named} option, no predicate is | |
4633 ever generated.) | |
4634 | |
4635 In true Common Lisp, @code{typep} is always able to recognize a | |
4636 structure object even if @code{:predicate} was used. In this | |
4637 package, @code{typep} simply looks for a function called | |
4638 @code{@var{typename}-p}, so it will work for structure types | |
4639 only if they used the default predicate name. | |
4640 | |
4641 @item :include | |
4642 This option implements a very limited form of C++-style inheritance. | |
4643 The argument is the name of another structure type previously | |
4644 created with @code{defstruct}. The effect is to cause the new | |
4645 structure type to inherit all of the included structure's slots | |
4646 (plus, of course, any new slots described by this struct's slot | |
4647 descriptors). The new structure is considered a ``specialization'' | |
4648 of the included one. In fact, the predicate and slot accessors | |
4649 for the included type will also accept objects of the new type. | |
4650 | |
4651 If there are extra arguments to the @code{:include} option after | |
4652 the included-structure name, these options are treated as replacement | |
4653 slot descriptors for slots in the included structure, possibly with | |
4654 modified default values. Borrowing an example from Steele: | |
4655 | |
4656 @example | |
4657 (defstruct person name (age 0) sex) | |
4658 @result{} person | |
4659 (defstruct (astronaut (:include person (age 45))) | |
4660 helmet-size | |
4661 (favorite-beverage 'tang)) | |
4662 @result{} astronaut | |
4663 | |
4664 (setq joe (make-person :name "Joe")) | |
4665 @result{} [cl-struct-person "Joe" 0 nil] | |
4666 (setq buzz (make-astronaut :name "Buzz")) | |
4667 @result{} [cl-struct-astronaut "Buzz" 45 nil nil tang] | |
4668 | |
4669 (list (person-p joe) (person-p buzz)) | |
4670 @result{} (t t) | |
4671 (list (astronaut-p joe) (astronaut-p buzz)) | |
4672 @result{} (nil t) | |
4673 | |
4674 (person-name buzz) | |
4675 @result{} "Buzz" | |
4676 (astronaut-name joe) | |
4677 @result{} error: "astronaut-name accessing a non-astronaut" | |
4678 @end example | |
4679 | |
4680 Thus, if @code{astronaut} is a specialization of @code{person}, | |
4681 then every @code{astronaut} is also a @code{person} (but not the | |
4682 other way around). Every @code{astronaut} includes all the slots | |
4683 of a @code{person}, plus extra slots that are specific to | |
4684 astronauts. Operations that work on people (like @code{person-name}) | |
4685 work on astronauts just like other people. | |
4686 | |
4687 @item :print-function | |
4688 In full Common Lisp, this option allows you to specify a function | |
4689 which is called to print an instance of the structure type. The | |
4690 Emacs Lisp system offers no hooks into the Lisp printer which would | |
4691 allow for such a feature, so this package simply ignores | |
4692 @code{:print-function}. | |
4693 | |
4694 @item :type | |
4695 The argument should be one of the symbols @code{vector} or @code{list}. | |
4696 This tells which underlying Lisp data type should be used to implement | |
4697 the new structure type. Vectors are used by default, but | |
4698 @code{(:type list)} will cause structure objects to be stored as | |
4699 lists instead. | |
4700 | |
4701 The vector representation for structure objects has the advantage | |
4702 that all structure slots can be accessed quickly, although creating | |
4703 vectors is a bit slower in Emacs Lisp. Lists are easier to create, | |
4704 but take a relatively long time accessing the later slots. | |
4705 | |
4706 @item :named | |
4707 This option, which takes no arguments, causes a characteristic ``tag'' | |
4708 symbol to be stored at the front of the structure object. Using | |
4709 @code{:type} without also using @code{:named} will result in a | |
4710 structure type stored as plain vectors or lists with no identifying | |
4711 features. | |
4712 | |
4713 The default, if you don't specify @code{:type} explicitly, is to | |
4714 use named vectors. Therefore, @code{:named} is only useful in | |
4715 conjunction with @code{:type}. | |
4716 | |
4717 @example | |
4718 (defstruct (person1) name age sex) | |
4719 (defstruct (person2 (:type list) :named) name age sex) | |
4720 (defstruct (person3 (:type list)) name age sex) | |
4721 | |
4722 (setq p1 (make-person1)) | |
4723 @result{} [cl-struct-person1 nil nil nil] | |
4724 (setq p2 (make-person2)) | |
4725 @result{} (person2 nil nil nil) | |
4726 (setq p3 (make-person3)) | |
4727 @result{} (nil nil nil) | |
4728 | |
4729 (person1-p p1) | |
4730 @result{} t | |
4731 (person2-p p2) | |
4732 @result{} t | |
4733 (person3-p p3) | |
4734 @result{} error: function person3-p undefined | |
4735 @end example | |
4736 | |
4737 Since unnamed structures don't have tags, @code{defstruct} is not | |
4738 able to make a useful predicate for recognizing them. Also, | |
4739 accessors like @code{person3-name} will be generated but they | |
4740 will not be able to do any type checking. The @code{person3-name} | |
4741 function, for example, will simply be a synonym for @code{car} in | |
4742 this case. By contrast, @code{person2-name} is able to verify | |
4743 that its argument is indeed a @code{person2} object before | |
4744 proceeding. | |
4745 | |
4746 @item :initial-offset | |
4747 The argument must be a nonnegative integer. It specifies a | |
4748 number of slots to be left ``empty'' at the front of the | |
4749 structure. If the structure is named, the tag appears at the | |
4750 specified position in the list or vector; otherwise, the first | |
4751 slot appears at that position. Earlier positions are filled | |
4752 with @code{nil} by the constructors and ignored otherwise. If | |
4753 the type @code{:include}s another type, then @code{:initial-offset} | |
4754 specifies a number of slots to be skipped between the last slot | |
4755 of the included type and the first new slot. | |
4756 @end table | |
4757 @end defspec | |
4758 | |
4759 Except as noted, the @code{defstruct} facility of this package is | |
4760 entirely compatible with that of Common Lisp. | |
4761 | |
4762 @iftex | |
4763 @chapno=23 | |
4764 @end iftex | |
4765 | |
4766 @node Assertions, Efficiency Concerns, Structures, Top | |
4767 @chapter Assertions and Errors | |
4768 | |
4769 @noindent | |
4770 This section describes two macros that test @dfn{assertions}, i.e., | |
4771 conditions which must be true if the program is operating correctly. | |
4772 Assertions never add to the behavior of a Lisp program; they simply | |
4773 make ``sanity checks'' to make sure everything is as it should be. | |
4774 | |
4775 If the optimization property @code{speed} has been set to 3, and | |
4776 @code{safety} is less than 3, then the byte-compiler will optimize | |
4777 away the following assertions. Because assertions might be optimized | |
4778 away, it is a bad idea for them to include side-effects. | |
4779 | |
4780 @defspec assert test-form [show-args string args@dots{}] | |
4781 This form verifies that @var{test-form} is true (i.e., evaluates to | |
4782 a non-@code{nil} value). If so, it returns @code{nil}. If the test | |
4783 is not satisfied, @code{assert} signals an error. | |
4784 | |
4785 A default error message will be supplied which includes @var{test-form}. | |
4786 You can specify a different error message by including a @var{string} | |
4787 argument plus optional extra arguments. Those arguments are simply | |
4788 passed to @code{error} to signal the error. | |
4789 | |
4790 If the optional second argument @var{show-args} is @code{t} instead | |
4791 of @code{nil}, then the error message (with or without @var{string}) | |
4792 will also include all non-constant arguments of the top-level | |
4793 @var{form}. For example: | |
4794 | |
4795 @example | |
4796 (assert (> x 10) t "x is too small: %d") | |
4797 @end example | |
4798 | |
4799 This usage of @var{show-args} is an extension to Common Lisp. In | |
4800 true Common Lisp, the second argument gives a list of @var{places} | |
4801 which can be @code{setf}'d by the user before continuing from the | |
4802 error. Since Emacs Lisp does not support continuable errors, it | |
4803 makes no sense to specify @var{places}. | |
4804 @end defspec | |
4805 | |
4806 @defspec check-type form type [string] | |
4807 This form verifies that @var{form} evaluates to a value of type | |
4808 @var{type}. If so, it returns @code{nil}. If not, @code{check-type} | |
4809 signals a @code{wrong-type-argument} error. The default error message | |
4810 lists the erroneous value along with @var{type} and @var{form} | |
4811 themselves. If @var{string} is specified, it is included in the | |
4812 error message in place of @var{type}. For example: | |
4813 | |
4814 @example | |
4815 (check-type x (integer 1 *) "a positive integer") | |
4816 @end example | |
4817 | |
4818 @xref{Type Predicates}, for a description of the type specifiers | |
4819 that may be used for @var{type}. | |
4820 | |
4821 Note that in Common Lisp, the first argument to @code{check-type} | |
4822 must be a @var{place} suitable for use by @code{setf}, because | |
4823 @code{check-type} signals a continuable error that allows the | |
4824 user to modify @var{place}. | |
4825 @end defspec | |
4826 | |
4827 The following error-related macro is also defined: | |
4828 | |
4829 @defspec ignore-errors forms@dots{} | |
4830 This executes @var{forms} exactly like a @code{progn}, except that | |
4831 errors are ignored during the @var{forms}. More precisely, if | |
4832 an error is signaled then @code{ignore-errors} immediately | |
4833 aborts execution of the @var{forms} and returns @code{nil}. | |
4834 If the @var{forms} complete successfully, @code{ignore-errors} | |
4835 returns the result of the last @var{form}. | |
4836 @end defspec | |
4837 | |
4838 @node Efficiency Concerns, Common Lisp Compatibility, Assertions, Top | |
4839 @appendix Efficiency Concerns | |
4840 | |
4841 @appendixsec Macros | |
4842 | |
4843 @noindent | |
4844 Many of the advanced features of this package, such as @code{defun*}, | |
4845 @code{loop}, and @code{setf}, are implemented as Lisp macros. In | |
4846 byte-compiled code, these complex notations will be expanded into | |
4847 equivalent Lisp code which is simple and efficient. For example, | |
4848 the forms | |
4849 | |
4850 @example | |
4851 (incf i n) | |
4852 (push x (car p)) | |
4853 @end example | |
4854 | |
4855 @noindent | |
4856 are expanded at compile-time to the Lisp forms | |
4857 | |
4858 @example | |
4859 (setq i (+ i n)) | |
4860 (setcar p (cons x (car p))) | |
4861 @end example | |
4862 | |
4863 @noindent | |
4864 which are the most efficient ways of doing these respective operations | |
4865 in Lisp. Thus, there is no performance penalty for using the more | |
4866 readable @code{incf} and @code{push} forms in your compiled code. | |
4867 | |
4868 @emph{Interpreted} code, on the other hand, must expand these macros | |
4869 every time they are executed. For this reason it is strongly | |
4870 recommended that code making heavy use of macros be compiled. | |
4871 (The features labeled ``Special Form'' instead of ``Function'' in | |
4872 this manual are macros.) A loop using @code{incf} a hundred times | |
4873 will execute considerably faster if compiled, and will also | |
4874 garbage-collect less because the macro expansion will not have | |
4875 to be generated, used, and thrown away a hundred times. | |
4876 | |
4877 You can find out how a macro expands by using the | |
4878 @code{cl-prettyexpand} function. | |
4879 | |
4880 @defun cl-prettyexpand form &optional full | |
4881 This function takes a single Lisp form as an argument and inserts | |
4882 a nicely formatted copy of it in the current buffer (which must be | |
4883 in Lisp mode so that indentation works properly). It also expands | |
4884 all Lisp macros which appear in the form. The easiest way to use | |
4885 this function is to go to the @code{*scratch*} buffer and type, say, | |
4886 | |
4887 @example | |
4888 (cl-prettyexpand '(loop for x below 10 collect x)) | |
4889 @end example | |
4890 | |
4891 @noindent | |
4892 and type @kbd{C-x C-e} immediately after the closing parenthesis; | |
4893 the expansion | |
4894 | |
4895 @example | |
4896 (block nil | |
4897 (let* ((x 0) | |
4898 (G1004 nil)) | |
4899 (while (< x 10) | |
4900 (setq G1004 (cons x G1004)) | |
4901 (setq x (+ x 1))) | |
4902 (nreverse G1004))) | |
4903 @end example | |
4904 | |
4905 @noindent | |
4906 will be inserted into the buffer. (The @code{block} macro is | |
4907 expanded differently in the interpreter and compiler, so | |
4908 @code{cl-prettyexpand} just leaves it alone. The temporary | |
4909 variable @code{G1004} was created by @code{gensym}.) | |
4910 | |
4911 If the optional argument @var{full} is true, then @emph{all} | |
4912 macros are expanded, including @code{block}, @code{eval-when}, | |
4913 and compiler macros. Expansion is done as if @var{form} were | |
4914 a top-level form in a file being compiled. For example, | |
4915 | |
4916 @example | |
4917 (cl-prettyexpand '(pushnew 'x list)) | |
4918 @print{} (setq list (adjoin 'x list)) | |
4919 (cl-prettyexpand '(pushnew 'x list) t) | |
4920 @print{} (setq list (if (memq 'x list) list (cons 'x list))) | |
4921 (cl-prettyexpand '(caddr (member* 'a list)) t) | |
4922 @print{} (car (cdr (cdr (memq 'a list)))) | |
4923 @end example | |
4924 | |
4925 Note that @code{adjoin}, @code{caddr}, and @code{member*} all | |
4926 have built-in compiler macros to optimize them in common cases. | |
4927 @end defun | |
4928 | |
4929 @ifinfo | |
4930 @example | |
4931 | |
4932 @end example | |
4933 @end ifinfo | |
4934 @appendixsec Error Checking | |
4935 | |
4936 @noindent | |
4937 Common Lisp compliance has in general not been sacrificed for the | |
4938 sake of efficiency. A few exceptions have been made for cases | |
4939 where substantial gains were possible at the expense of marginal | |
33295
d66d9f108b83
Remove obsolete warnings about use of memq for keyword args.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32315
diff
changeset
|
4940 incompatibility. |
25829 | 4941 |
4942 The Common Lisp standard (as embodied in Steele's book) uses the | |
4943 phrase ``it is an error if'' to indicate a situation which is not | |
4944 supposed to arise in complying programs; implementations are strongly | |
4945 encouraged but not required to signal an error in these situations. | |
4946 This package sometimes omits such error checking in the interest of | |
4947 compactness and efficiency. For example, @code{do} variable | |
4948 specifiers are supposed to be lists of one, two, or three forms; | |
4949 extra forms are ignored by this package rather than signaling a | |
4950 syntax error. The @code{endp} function is simply a synonym for | |
4951 @code{null} in this package. Functions taking keyword arguments | |
4952 will accept an odd number of arguments, treating the trailing | |
4953 keyword as if it were followed by the value @code{nil}. | |
4954 | |
4955 Argument lists (as processed by @code{defun*} and friends) | |
4956 @emph{are} checked rigorously except for the minor point just | |
4957 mentioned; in particular, keyword arguments are checked for | |
4958 validity, and @code{&allow-other-keys} and @code{:allow-other-keys} | |
4959 are fully implemented. Keyword validity checking is slightly | |
4960 time consuming (though not too bad in byte-compiled code); | |
4961 you can use @code{&allow-other-keys} to omit this check. Functions | |
4962 defined in this package such as @code{find} and @code{member*} | |
4963 do check their keyword arguments for validity. | |
4964 | |
4965 @ifinfo | |
4966 @example | |
4967 | |
4968 @end example | |
4969 @end ifinfo | |
4970 @appendixsec Optimizing Compiler | |
4971 | |
4972 @noindent | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
4973 Use of the optimizing Emacs compiler is highly recommended; many of the Common |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
4974 Lisp macros emit |
25829 | 4975 code which can be improved by optimization. In particular, |
4976 @code{block}s (whether explicit or implicit in constructs like | |
4977 @code{defun*} and @code{loop}) carry a fair run-time penalty; the | |
4978 optimizing compiler removes @code{block}s which are not actually | |
4979 referenced by @code{return} or @code{return-from} inside the block. | |
4980 | |
4981 @node Common Lisp Compatibility, Old CL Compatibility, Efficiency Concerns, Top | |
4982 @appendix Common Lisp Compatibility | |
4983 | |
4984 @noindent | |
4985 Following is a list of all known incompatibilities between this | |
4986 package and Common Lisp as documented in Steele (2nd edition). | |
4987 | |
4988 Certain function names, such as @code{member}, @code{assoc}, and | |
4989 @code{floor}, were already taken by (incompatible) Emacs Lisp | |
4990 functions; this package appends @samp{*} to the names of its | |
4991 Common Lisp versions of these functions. | |
4992 | |
4993 The word @code{defun*} is required instead of @code{defun} in order | |
4994 to use extended Common Lisp argument lists in a function. Likewise, | |
4995 @code{defmacro*} and @code{function*} are versions of those forms | |
4996 which understand full-featured argument lists. The @code{&whole} | |
4997 keyword does not work in @code{defmacro} argument lists (except | |
4998 inside recursive argument lists). | |
4999 | |
5000 The @code{eql} and @code{equal} predicates do not distinguish | |
5001 between IEEE floating-point plus and minus zero. The @code{equalp} | |
5002 predicate has several differences with Common Lisp; @pxref{Predicates}. | |
5003 | |
5004 The @code{setf} mechanism is entirely compatible, except that | |
5005 setf-methods return a list of five values rather than five | |
5006 values directly. Also, the new ``@code{setf} function'' concept | |
5007 (typified by @code{(defun (setf foo) @dots{})}) is not implemented. | |
5008 | |
5009 The @code{do-all-symbols} form is the same as @code{do-symbols} | |
5010 with no @var{obarray} argument. In Common Lisp, this form would | |
5011 iterate over all symbols in all packages. Since Emacs obarrays | |
5012 are not a first-class package mechanism, there is no way for | |
5013 @code{do-all-symbols} to locate any but the default obarray. | |
5014 | |
5015 The @code{loop} macro is complete except that @code{loop-finish} | |
5016 and type specifiers are unimplemented. | |
5017 | |
5018 The multiple-value return facility treats lists as multiple | |
5019 values, since Emacs Lisp cannot support multiple return values | |
5020 directly. The macros will be compatible with Common Lisp if | |
5021 @code{values} or @code{values-list} is always used to return to | |
5022 a @code{multiple-value-bind} or other multiple-value receiver; | |
5023 if @code{values} is used without @code{multiple-value-@dots{}} | |
5024 or vice-versa the effect will be different from Common Lisp. | |
5025 | |
5026 Many Common Lisp declarations are ignored, and others match | |
5027 the Common Lisp standard in concept but not in detail. For | |
5028 example, local @code{special} declarations, which are purely | |
5029 advisory in Emacs Lisp, do not rigorously obey the scoping rules | |
5030 set down in Steele's book. | |
5031 | |
5032 The variable @code{*gensym-counter*} starts out with a pseudo-random | |
5033 value rather than with zero. This is to cope with the fact that | |
5034 generated symbols become interned when they are written to and | |
5035 loaded back from a file. | |
5036 | |
5037 The @code{defstruct} facility is compatible, except that structures | |
5038 are of type @code{:type vector :named} by default rather than some | |
5039 special, distinct type. Also, the @code{:type} slot option is ignored. | |
5040 | |
5041 The second argument of @code{check-type} is treated differently. | |
5042 | |
5043 @node Old CL Compatibility, Porting Common Lisp, Common Lisp Compatibility, Top | |
5044 @appendix Old CL Compatibility | |
5045 | |
5046 @noindent | |
5047 Following is a list of all known incompatibilities between this package | |
5048 and the older Quiroz @file{cl.el} package. | |
5049 | |
5050 This package's emulation of multiple return values in functions is | |
5051 incompatible with that of the older package. That package attempted | |
5052 to come as close as possible to true Common Lisp multiple return | |
5053 values; unfortunately, it could not be 100% reliable and so was prone | |
5054 to occasional surprises if used freely. This package uses a simpler | |
5055 method, namely replacing multiple values with lists of values, which | |
5056 is more predictable though more noticeably different from Common Lisp. | |
5057 | |
5058 The @code{defkeyword} form and @code{keywordp} function are not | |
5059 implemented in this package. | |
5060 | |
5061 The @code{member}, @code{floor}, @code{ceiling}, @code{truncate}, | |
5062 @code{round}, @code{mod}, and @code{rem} functions are suffixed | |
5063 by @samp{*} in this package to avoid collision with existing | |
27225
ee242fddb1ab
Remove various old stuff and references thereto.
Dave Love <fx@gnu.org>
parents:
25829
diff
changeset
|
5064 functions in Emacs. The older package simply |
25829 | 5065 redefined these functions, overwriting the built-in meanings and |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
5066 causing serious portability problems. (Some more |
25829 | 5067 recent versions of the Quiroz package changed the names to |
5068 @code{cl-member}, etc.; this package defines the latter names as | |
5069 aliases for @code{member*}, etc.) | |
5070 | |
5071 Certain functions in the old package which were buggy or inconsistent | |
5072 with the Common Lisp standard are incompatible with the conforming | |
5073 versions in this package. For example, @code{eql} and @code{member} | |
5074 were synonyms for @code{eq} and @code{memq} in that package, @code{setf} | |
5075 failed to preserve correct order of evaluation of its arguments, etc. | |
5076 | |
5077 Finally, unlike the older package, this package is careful to | |
5078 prefix all of its internal names with @code{cl-}. Except for a | |
5079 few functions which are explicitly defined as additional features | |
5080 (such as @code{floatp-safe} and @code{letf}), this package does not | |
5081 export any non-@samp{cl-} symbols which are not also part of Common | |
5082 Lisp. | |
5083 | |
5084 @ifinfo | |
5085 @example | |
5086 | |
5087 @end example | |
5088 @end ifinfo | |
5089 @appendixsec The @code{cl-compat} package | |
5090 | |
5091 @noindent | |
5092 The @dfn{CL} package includes emulations of some features of the | |
5093 old @file{cl.el}, in the form of a compatibility package | |
5094 @code{cl-compat}. To use it, put @code{(require 'cl-compat)} in | |
5095 your program. | |
5096 | |
5097 The old package defined a number of internal routines without | |
5098 @code{cl-} prefixes or other annotations. Call to these routines | |
5099 may have crept into existing Lisp code. @code{cl-compat} | |
5100 provides emulations of the following internal routines: | |
5101 @code{pair-with-newsyms}, @code{zip-lists}, @code{unzip-lists}, | |
5102 @code{reassemble-arglists}, @code{duplicate-symbols-p}, | |
5103 @code{safe-idiv}. | |
5104 | |
5105 Some @code{setf} forms translated into calls to internal | |
5106 functions that user code might call directly. The functions | |
5107 @code{setnth}, @code{setnthcdr}, and @code{setelt} fall in | |
5108 this category; they are defined by @code{cl-compat}, but the | |
5109 best fix is to change to use @code{setf} properly. | |
5110 | |
5111 The @code{cl-compat} file defines the keyword functions | |
5112 @code{keywordp}, @code{keyword-of}, and @code{defkeyword}, | |
5113 which are not defined by the new @dfn{CL} package because the | |
5114 use of keywords as data is discouraged. | |
5115 | |
5116 The @code{build-klist} mechanism for parsing keyword arguments | |
5117 is emulated by @code{cl-compat}; the @code{with-keyword-args} | |
5118 macro is not, however, and in any case it's best to change to | |
5119 use the more natural keyword argument processing offered by | |
5120 @code{defun*}. | |
5121 | |
5122 Multiple return values are treated differently by the two | |
5123 Common Lisp packages. The old package's method was more | |
5124 compatible with true Common Lisp, though it used heuristics | |
5125 that caused it to report spurious multiple return values in | |
5126 certain cases. The @code{cl-compat} package defines a set | |
5127 of multiple-value macros that are compatible with the old | |
5128 CL package; again, they are heuristic in nature, but they | |
5129 are guaranteed to work in any case where the old package's | |
5130 macros worked. To avoid name collision with the ``official'' | |
5131 multiple-value facilities, the ones in @code{cl-compat} have | |
5132 capitalized names: @code{Values}, @code{Values-list}, | |
5133 @code{Multiple-value-bind}, etc. | |
5134 | |
5135 The functions @code{cl-floor}, @code{cl-ceiling}, @code{cl-truncate}, | |
5136 and @code{cl-round} are defined by @code{cl-compat} to use the | |
5137 old-style multiple-value mechanism, just as they did in the old | |
5138 package. The newer @code{floor*} and friends return their two | |
5139 results in a list rather than as multiple values. Note that | |
5140 older versions of the old package used the unadorned names | |
5141 @code{floor}, @code{ceiling}, etc.; @code{cl-compat} cannot use | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
5142 these names because they conflict with Emacs built-ins. |
25829 | 5143 |
5144 @node Porting Common Lisp, Function Index, Old CL Compatibility, Top | |
5145 @appendix Porting Common Lisp | |
5146 | |
5147 @noindent | |
5148 This package is meant to be used as an extension to Emacs Lisp, | |
5149 not as an Emacs implementation of true Common Lisp. Some of the | |
5150 remaining differences between Emacs Lisp and Common Lisp make it | |
5151 difficult to port large Common Lisp applications to Emacs. For | |
5152 one, some of the features in this package are not fully compliant | |
5153 with ANSI or Steele; @pxref{Common Lisp Compatibility}. But there | |
5154 are also quite a few features that this package does not provide | |
61442
0129c2b09b4a
(Porting Common Lisp): Fix typo.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
60927
diff
changeset
|
5155 at all. Here are some major omissions that you will want to watch out |
25829 | 5156 for when bringing Common Lisp code into Emacs. |
5157 | |
5158 @itemize @bullet | |
5159 @item | |
5160 Case-insensitivity. Symbols in Common Lisp are case-insensitive | |
5161 by default. Some programs refer to a function or variable as | |
5162 @code{foo} in one place and @code{Foo} or @code{FOO} in another. | |
5163 Emacs Lisp will treat these as three distinct symbols. | |
5164 | |
5165 Some Common Lisp code is written entirely in upper case. While Emacs | |
5166 is happy to let the program's own functions and variables use | |
5167 this convention, calls to Lisp builtins like @code{if} and | |
5168 @code{defun} will have to be changed to lower case. | |
5169 | |
5170 @item | |
5171 Lexical scoping. In Common Lisp, function arguments and @code{let} | |
5172 bindings apply only to references physically within their bodies | |
5173 (or within macro expansions in their bodies). Emacs Lisp, by | |
5174 contrast, uses @dfn{dynamic scoping} wherein a binding to a | |
5175 variable is visible even inside functions called from the body. | |
5176 | |
5177 Variables in Common Lisp can be made dynamically scoped by | |
5178 declaring them @code{special} or using @code{defvar}. In Emacs | |
5179 Lisp it is as if all variables were declared @code{special}. | |
5180 | |
5181 Often you can use code that was written for lexical scoping | |
5182 even in a dynamically scoped Lisp, but not always. Here is | |
5183 an example of a Common Lisp code fragment that would fail in | |
5184 Emacs Lisp: | |
5185 | |
5186 @example | |
5187 (defun map-odd-elements (func list) | |
5188 (loop for x in list | |
5189 for flag = t then (not flag) | |
5190 collect (if flag x (funcall func x)))) | |
5191 | |
5192 (defun add-odd-elements (list x) | |
28344
54fda0e8528a
Weed out redundant uses of `function'
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28039
diff
changeset
|
5193 (map-odd-elements (lambda (a) (+ a x))) list) |
25829 | 5194 @end example |
5195 | |
5196 @noindent | |
5197 In Common Lisp, the two functions' usages of @code{x} are completely | |
5198 independent. In Emacs Lisp, the binding to @code{x} made by | |
5199 @code{add-odd-elements} will have been hidden by the binding | |
5200 in @code{map-odd-elements} by the time the @code{(+ a x)} function | |
5201 is called. | |
5202 | |
5203 (This package avoids such problems in its own mapping functions | |
5204 by using names like @code{cl-x} instead of @code{x} internally; | |
5205 as long as you don't use the @code{cl-} prefix for your own | |
5206 variables no collision can occur.) | |
5207 | |
5208 @xref{Lexical Bindings}, for a description of the @code{lexical-let} | |
5209 form which establishes a Common Lisp-style lexical binding, and some | |
5210 examples of how it differs from Emacs' regular @code{let}. | |
5211 | |
5212 @item | |
5213 Reader macros. Common Lisp includes a second type of macro that | |
5214 works at the level of individual characters. For example, Common | |
5215 Lisp implements the quote notation by a reader macro called @code{'}, | |
5216 whereas Emacs Lisp's parser just treats quote as a special case. | |
5217 Some Lisp packages use reader macros to create special syntaxes | |
5218 for themselves, which the Emacs parser is incapable of reading. | |
5219 | |
5220 The lack of reader macros, incidentally, is the reason behind | |
5221 Emacs Lisp's unusual backquote syntax. Since backquotes are | |
5222 implemented as a Lisp package and not built-in to the Emacs | |
5223 parser, they are forced to use a regular macro named @code{`} | |
5224 which is used with the standard function/macro call notation. | |
5225 | |
5226 @item | |
5227 Other syntactic features. Common Lisp provides a number of | |
5228 notations beginning with @code{#} that the Emacs Lisp parser | |
5229 won't understand. For example, @samp{#| ... |#} is an | |
5230 alternate comment notation, and @samp{#+lucid (foo)} tells | |
5231 the parser to ignore the @code{(foo)} except in Lucid Common | |
5232 Lisp. | |
5233 | |
5234 @item | |
5235 Packages. In Common Lisp, symbols are divided into @dfn{packages}. | |
5236 Symbols that are Lisp built-ins are typically stored in one package; | |
5237 symbols that are vendor extensions are put in another, and each | |
5238 application program would have a package for its own symbols. | |
5239 Certain symbols are ``exported'' by a package and others are | |
5240 internal; certain packages ``use'' or import the exported symbols | |
5241 of other packages. To access symbols that would not normally be | |
5242 visible due to this importing and exporting, Common Lisp provides | |
5243 a syntax like @code{package:symbol} or @code{package::symbol}. | |
5244 | |
5245 Emacs Lisp has a single namespace for all interned symbols, and | |
5246 then uses a naming convention of putting a prefix like @code{cl-} | |
5247 in front of the name. Some Emacs packages adopt the Common Lisp-like | |
5248 convention of using @code{cl:} or @code{cl::} as the prefix. | |
5249 However, the Emacs parser does not understand colons and just | |
5250 treats them as part of the symbol name. Thus, while @code{mapcar} | |
5251 and @code{lisp:mapcar} may refer to the same symbol in Common | |
5252 Lisp, they are totally distinct in Emacs Lisp. Common Lisp | |
5253 programs which refer to a symbol by the full name sometimes | |
5254 and the short name other times will not port cleanly to Emacs. | |
5255 | |
5256 Emacs Lisp does have a concept of ``obarrays,'' which are | |
5257 package-like collections of symbols, but this feature is not | |
5258 strong enough to be used as a true package mechanism. | |
5259 | |
5260 @item | |
5261 The @code{format} function is quite different between Common | |
5262 Lisp and Emacs Lisp. It takes an additional ``destination'' | |
5263 argument before the format string. A destination of @code{nil} | |
5264 means to format to a string as in Emacs Lisp; a destination | |
5265 of @code{t} means to write to the terminal (similar to | |
5266 @code{message} in Emacs). Also, format control strings are | |
5267 utterly different; @code{~} is used instead of @code{%} to | |
5268 introduce format codes, and the set of available codes is | |
5269 much richer. There are no notations like @code{\n} for | |
5270 string literals; instead, @code{format} is used with the | |
5271 ``newline'' format code, @code{~%}. More advanced formatting | |
5272 codes provide such features as paragraph filling, case | |
5273 conversion, and even loops and conditionals. | |
5274 | |
5275 While it would have been possible to implement most of Common | |
5276 Lisp @code{format} in this package (under the name @code{format*}, | |
5277 of course), it was not deemed worthwhile. It would have required | |
5278 a huge amount of code to implement even a decent subset of | |
5279 @code{format*}, yet the functionality it would provide over | |
5280 Emacs Lisp's @code{format} would rarely be useful. | |
5281 | |
5282 @item | |
5283 Vector constants use square brackets in Emacs Lisp, but | |
5284 @code{#(a b c)} notation in Common Lisp. To further complicate | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
5285 matters, Emacs has its own @code{#(} notation for |
25829 | 5286 something entirely different---strings with properties. |
5287 | |
5288 @item | |
5289 Characters are distinct from integers in Common Lisp. The | |
5290 notation for character constants is also different: @code{#\A} | |
5291 instead of @code{?A}. Also, @code{string=} and @code{string-equal} | |
5292 are synonyms in Emacs Lisp whereas the latter is case-insensitive | |
5293 in Common Lisp. | |
5294 | |
5295 @item | |
5296 Data types. Some Common Lisp data types do not exist in Emacs | |
5297 Lisp. Rational numbers and complex numbers are not present, | |
5298 nor are large integers (all integers are ``fixnums''). All | |
5299 arrays are one-dimensional. There are no readtables or pathnames; | |
5300 streams are a set of existing data types rather than a new data | |
5301 type of their own. Hash tables, random-states, structures, and | |
5302 packages (obarrays) are built from Lisp vectors or lists rather | |
5303 than being distinct types. | |
5304 | |
5305 @item | |
5306 The Common Lisp Object System (CLOS) is not implemented, | |
5307 nor is the Common Lisp Condition System. However, the EIEIO package | |
5308 from @uref{ftp://ftp.ultranet.com/pub/zappo} does implement some | |
5309 CLOS functionality. | |
5310 | |
5311 @item | |
5312 Common Lisp features that are completely redundant with Emacs | |
5313 Lisp features of a different name generally have not been | |
5314 implemented. For example, Common Lisp writes @code{defconstant} | |
5315 where Emacs Lisp uses @code{defconst}. Similarly, @code{make-list} | |
5316 takes its arguments in different ways in the two Lisps but does | |
5317 exactly the same thing, so this package has not bothered to | |
5318 implement a Common Lisp-style @code{make-list}. | |
5319 | |
5320 @item | |
5321 A few more notable Common Lisp features not included in this | |
5322 package: @code{compiler-let}, @code{tagbody}, @code{prog}, | |
5323 @code{ldb/dpb}, @code{parse-integer}, @code{cerror}. | |
5324 | |
5325 @item | |
5326 Recursion. While recursion works in Emacs Lisp just like it | |
5327 does in Common Lisp, various details of the Emacs Lisp system | |
5328 and compiler make recursion much less efficient than it is in | |
5329 most Lisps. Some schools of thought prefer to use recursion | |
5330 in Lisp over other techniques; they would sum a list of | |
5331 numbers using something like | |
5332 | |
5333 @example | |
5334 (defun sum-list (list) | |
5335 (if list | |
5336 (+ (car list) (sum-list (cdr list))) | |
5337 0)) | |
5338 @end example | |
5339 | |
5340 @noindent | |
5341 where a more iteratively-minded programmer might write one of | |
5342 these forms: | |
5343 | |
5344 @example | |
5345 (let ((total 0)) (dolist (x my-list) (incf total x)) total) | |
5346 (loop for x in my-list sum x) | |
5347 @end example | |
5348 | |
5349 While this would be mainly a stylistic choice in most Common Lisps, | |
5350 in Emacs Lisp you should be aware that the iterative forms are | |
5351 much faster than recursion. Also, Lisp programmers will want to | |
5352 note that the current Emacs Lisp compiler does not optimize tail | |
5353 recursion. | |
5354 @end itemize | |
5355 | |
5356 @node Function Index, Variable Index, Porting Common Lisp, Top | |
5357 @unnumbered Function Index | |
5358 | |
5359 @printindex fn | |
5360 | |
31572
384603523f41
Remove obsolescences, especially hash tables and refs to Emacs 19.
Dave Love <fx@gnu.org>
parents:
30009
diff
changeset
|
5361 @node Variable Index, , Function Index, Top |
25829 | 5362 @unnumbered Variable Index |
5363 | |
5364 @printindex vr | |
5365 | |
29713 | 5366 @setchapternewpage odd |
25829 | 5367 @contents |
5368 @bye | |
52401 | 5369 |
5370 @ignore | |
5371 arch-tag: b61e7200-3bfa-4a70-a9d3-095e152696f8 | |
5372 @end ignore |