| 1 |
This is ../info/cl, produced by makeinfo version 4.2 from cl.texi. |
|---|
| 2 |
|
|---|
| 3 |
INFO-DIR-SECTION Emacs |
|---|
| 4 |
START-INFO-DIR-ENTRY |
|---|
| 5 |
* CL: (cl). Partial Common Lisp support for Emacs Lisp. |
|---|
| 6 |
END-INFO-DIR-ENTRY |
|---|
| 7 |
|
|---|
| 8 |
This file documents the GNU Emacs Common Lisp emulation package. |
|---|
| 9 |
|
|---|
| 10 |
Copyright (C) 1993 Free Software Foundation, Inc. |
|---|
| 11 |
|
|---|
| 12 |
Permission is granted to copy, distribute and/or modify this document |
|---|
| 13 |
under the terms of the GNU Free Documentation License, Version 1.1 or |
|---|
| 14 |
any later version published by the Free Software Foundation; with no |
|---|
| 15 |
Invariant Sections, with the Front-Cover texts being "A GNU Manual", |
|---|
| 16 |
and with the Back-Cover Texts as in (a) below. A copy of the license |
|---|
| 17 |
is included in the section entitled "GNU Free Documentation License" in |
|---|
| 18 |
the Emacs manual. |
|---|
| 19 |
|
|---|
| 20 |
(a) The FSF's Back-Cover Text is: "You have freedom to copy and |
|---|
| 21 |
modify 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. |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
File: cl, Node: For Clauses, Next: Iteration Clauses, Prev: Loop Examples, Up: Loop Facility |
|---|
| 31 |
|
|---|
| 32 |
For Clauses |
|---|
| 33 |
----------- |
|---|
| 34 |
|
|---|
| 35 |
Most loops are governed by one or more `for' clauses. A `for' clause |
|---|
| 36 |
simultaneously describes variables to be bound, how those variables are |
|---|
| 37 |
to be stepped during the loop, and usually an end condition based on |
|---|
| 38 |
those variables. |
|---|
| 39 |
|
|---|
| 40 |
The word `as' is a synonym for the word `for'. This word is |
|---|
| 41 |
followed by a variable name, then a word like `from' or `across' that |
|---|
| 42 |
describes the kind of iteration desired. In Common Lisp, the phrase |
|---|
| 43 |
`being the' sometimes precedes the type of iteration; in this package |
|---|
| 44 |
both `being' and `the' are optional. The word `each' is a synonym for |
|---|
| 45 |
`the', and the word that follows it may be singular or plural: `for x |
|---|
| 46 |
being the elements of y' or `for x being each element of y'. Which |
|---|
| 47 |
form you use is purely a matter of style. |
|---|
| 48 |
|
|---|
| 49 |
The variable is bound around the loop as if by `let': |
|---|
| 50 |
|
|---|
| 51 |
(setq i 'happy) |
|---|
| 52 |
(loop for i from 1 to 10 do (do-something-with i)) |
|---|
| 53 |
i |
|---|
| 54 |
=> happy |
|---|
| 55 |
|
|---|
| 56 |
`for VAR from EXPR1 to EXPR2 by EXPR3' |
|---|
| 57 |
This type of `for' clause creates a counting loop. Each of the |
|---|
| 58 |
three sub-terms is optional, though there must be at least one |
|---|
| 59 |
term so that the clause is marked as a counting clause. |
|---|
| 60 |
|
|---|
| 61 |
The three expressions are the starting value, the ending value, and |
|---|
| 62 |
the step value, respectively, of the variable. The loop counts |
|---|
| 63 |
upwards by default (EXPR3 must be positive), from EXPR1 to EXPR2 |
|---|
| 64 |
inclusively. If you omit the `from' term, the loop counts from |
|---|
| 65 |
zero; if you omit the `to' term, the loop counts forever without |
|---|
| 66 |
stopping (unless stopped by some other loop clause, of course); if |
|---|
| 67 |
you omit the `by' term, the loop counts in steps of one. |
|---|
| 68 |
|
|---|
| 69 |
You can replace the word `from' with `upfrom' or `downfrom' to |
|---|
| 70 |
indicate the direction of the loop. Likewise, you can replace |
|---|
| 71 |
`to' with `upto' or `downto'. For example, `for x from 5 downto |
|---|
| 72 |
1' executes five times with `x' taking on the integers from 5 down |
|---|
| 73 |
to 1 in turn. Also, you can replace `to' with `below' or `above', |
|---|
| 74 |
which are like `upto' and `downto' respectively except that they |
|---|
| 75 |
are exclusive rather than inclusive limits: |
|---|
| 76 |
|
|---|
| 77 |
(loop for x to 10 collect x) |
|---|
| 78 |
=> (0 1 2 3 4 5 6 7 8 9 10) |
|---|
| 79 |
(loop for x below 10 collect x) |
|---|
| 80 |
=> (0 1 2 3 4 5 6 7 8 9) |
|---|
| 81 |
|
|---|
| 82 |
The `by' value is always positive, even for downward-counting |
|---|
| 83 |
loops. Some sort of `from' value is required for downward loops; |
|---|
| 84 |
`for x downto 5' is not a legal loop clause all by itself. |
|---|
| 85 |
|
|---|
| 86 |
`for VAR in LIST by FUNCTION' |
|---|
| 87 |
This clause iterates VAR over all the elements of LIST, in turn. |
|---|
| 88 |
If you specify the `by' term, then FUNCTION is used to traverse |
|---|
| 89 |
the list instead of `cdr'; it must be a function taking one |
|---|
| 90 |
argument. For example: |
|---|
| 91 |
|
|---|
| 92 |
(loop for x in '(1 2 3 4 5 6) collect (* x x)) |
|---|
| 93 |
=> (1 4 9 16 25 36) |
|---|
| 94 |
(loop for x in '(1 2 3 4 5 6) by 'cddr collect (* x x)) |
|---|
| 95 |
=> (1 9 25) |
|---|
| 96 |
|
|---|
| 97 |
`for VAR on LIST by FUNCTION' |
|---|
| 98 |
This clause iterates VAR over all the cons cells of LIST. |
|---|
| 99 |
|
|---|
| 100 |
(loop for x on '(1 2 3 4) collect x) |
|---|
| 101 |
=> ((1 2 3 4) (2 3 4) (3 4) (4)) |
|---|
| 102 |
|
|---|
| 103 |
With `by', there is no real reason that the `on' expression must |
|---|
| 104 |
be a list. For example: |
|---|
| 105 |
|
|---|
| 106 |
(loop for x on first-animal by 'next-animal collect x) |
|---|
| 107 |
|
|---|
| 108 |
where `(next-animal x)' takes an "animal" X and returns the next |
|---|
| 109 |
in the (assumed) sequence of animals, or `nil' if X was the last |
|---|
| 110 |
animal in the sequence. |
|---|
| 111 |
|
|---|
| 112 |
`for VAR in-ref LIST by FUNCTION' |
|---|
| 113 |
This is like a regular `in' clause, but VAR becomes a `setf'-able |
|---|
| 114 |
"reference" onto the elements of the list rather than just a |
|---|
| 115 |
temporary variable. For example, |
|---|
| 116 |
|
|---|
| 117 |
(loop for x in-ref my-list do (incf x)) |
|---|
| 118 |
|
|---|
| 119 |
increments every element of `my-list' in place. This clause is an |
|---|
| 120 |
extension to standard Common Lisp. |
|---|
| 121 |
|
|---|
| 122 |
`for VAR across ARRAY' |
|---|
| 123 |
This clause iterates VAR over all the elements of ARRAY, which may |
|---|
| 124 |
be a vector or a string. |
|---|
| 125 |
|
|---|
| 126 |
(loop for x across "aeiou" |
|---|
| 127 |
do (use-vowel (char-to-string x))) |
|---|
| 128 |
|
|---|
| 129 |
`for VAR across-ref ARRAY' |
|---|
| 130 |
This clause iterates over an array, with VAR a `setf'-able |
|---|
| 131 |
reference onto the elements; see `in-ref' above. |
|---|
| 132 |
|
|---|
| 133 |
`for VAR being the elements of SEQUENCE' |
|---|
| 134 |
This clause iterates over the elements of SEQUENCE, which may be a |
|---|
| 135 |
list, vector, or string. Since the type must be determined at |
|---|
| 136 |
run-time, this is somewhat less efficient than `in' or `across'. |
|---|
| 137 |
The clause may be followed by the additional term `using (index |
|---|
| 138 |
VAR2)' to cause VAR2 to be bound to the successive indices |
|---|
| 139 |
(starting at 0) of the elements. |
|---|
| 140 |
|
|---|
| 141 |
This clause type is taken from older versions of the `loop' macro, |
|---|
| 142 |
and is not present in modern Common Lisp. The `using (sequence |
|---|
| 143 |
...)' term of the older macros is not supported. |
|---|
| 144 |
|
|---|
| 145 |
`for VAR being the elements of-ref SEQUENCE' |
|---|
| 146 |
This clause iterates over a sequence, with VAR a `setf'-able |
|---|
| 147 |
reference onto the elements; see `in-ref' above. |
|---|
| 148 |
|
|---|
| 149 |
`for VAR being the symbols [of OBARRAY]' |
|---|
| 150 |
This clause iterates over symbols, either over all interned symbols |
|---|
| 151 |
or over all symbols in OBARRAY. The loop is executed with VAR |
|---|
| 152 |
bound to each symbol in turn. The symbols are visited in an |
|---|
| 153 |
unspecified order. |
|---|
| 154 |
|
|---|
| 155 |
As an example, |
|---|
| 156 |
|
|---|
| 157 |
(loop for sym being the symbols |
|---|
| 158 |
when (fboundp sym) |
|---|
| 159 |
when (string-match "^map" (symbol-name sym)) |
|---|
| 160 |
collect sym) |
|---|
| 161 |
|
|---|
| 162 |
returns a list of all the functions whose names begin with `map'. |
|---|
| 163 |
|
|---|
| 164 |
The Common Lisp words `external-symbols' and `present-symbols' are |
|---|
| 165 |
also recognized but are equivalent to `symbols' in Emacs Lisp. |
|---|
| 166 |
|
|---|
| 167 |
Due to a minor implementation restriction, it will not work to have |
|---|
| 168 |
more than one `for' clause iterating over symbols, hash tables, |
|---|
| 169 |
keymaps, overlays, or intervals in a given `loop'. Fortunately, |
|---|
| 170 |
it would rarely if ever be useful to do so. It _is_ legal to mix |
|---|
| 171 |
one of these types of clauses with other clauses like `for ... to' |
|---|
| 172 |
or `while'. |
|---|
| 173 |
|
|---|
| 174 |
`for VAR being the hash-keys of HASH-TABLE' |
|---|
| 175 |
This clause iterates over the entries in HASH-TABLE. For each |
|---|
| 176 |
hash table entry, VAR is bound to the entry's key. If you write |
|---|
| 177 |
`the hash-values' instead, VAR is bound to the values of the |
|---|
| 178 |
entries. The clause may be followed by the additional term `using |
|---|
| 179 |
(hash-values VAR2)' (where `hash-values' is the opposite word of |
|---|
| 180 |
the word following `the') to cause VAR and VAR2 to be bound to the |
|---|
| 181 |
two parts of each hash table entry. |
|---|
| 182 |
|
|---|
| 183 |
`for VAR being the key-codes of KEYMAP' |
|---|
| 184 |
This clause iterates over the entries in KEYMAP. The iteration |
|---|
| 185 |
does not enter nested keymaps or inherited (parent) keymaps. You |
|---|
| 186 |
can use `the key-bindings' to access the commands bound to the |
|---|
| 187 |
keys rather than the key codes, and you can add a `using' clause |
|---|
| 188 |
to access both the codes and the bindings together. |
|---|
| 189 |
|
|---|
| 190 |
`for VAR being the key-seqs of KEYMAP' |
|---|
| 191 |
This clause iterates over all key sequences defined by KEYMAP and |
|---|
| 192 |
its nested keymaps, where VAR takes on values which are vectors. |
|---|
| 193 |
The strings or vectors are reused for each iteration, so you must |
|---|
| 194 |
copy them if you wish to keep them permanently. You can add a |
|---|
| 195 |
`using (key-bindings ...)' clause to get the command bindings as |
|---|
| 196 |
well. |
|---|
| 197 |
|
|---|
| 198 |
`for VAR being the overlays [of BUFFER] ...' |
|---|
| 199 |
This clause iterates over the "overlays" of a buffer (the clause |
|---|
| 200 |
`extents' is synonymous with `overlays'). If the `of' term is |
|---|
| 201 |
omitted, the current buffer is used. This clause also accepts |
|---|
| 202 |
optional `from POS' and `to POS' terms, limiting the clause to |
|---|
| 203 |
overlays which overlap the specified region. |
|---|
| 204 |
|
|---|
| 205 |
`for VAR being the intervals [of BUFFER] ...' |
|---|
| 206 |
This clause iterates over all intervals of a buffer with constant |
|---|
| 207 |
text properties. The variable VAR will be bound to conses of |
|---|
| 208 |
start and end positions, where one start position is always equal |
|---|
| 209 |
to the previous end position. The clause allows `of', `from', |
|---|
| 210 |
`to', and `property' terms, where the latter term restricts the |
|---|
| 211 |
search to just the specified property. The `of' term may specify |
|---|
| 212 |
either a buffer or a string. |
|---|
| 213 |
|
|---|
| 214 |
`for VAR being the frames' |
|---|
| 215 |
This clause iterates over all frames, i.e., X window system windows |
|---|
| 216 |
open on Emacs files. The clause `screens' is a synonym for |
|---|
| 217 |
`frames'. The frames are visited in `next-frame' order starting |
|---|
| 218 |
from `selected-frame'. |
|---|
| 219 |
|
|---|
| 220 |
`for VAR being the windows [of FRAME]' |
|---|
| 221 |
This clause iterates over the windows (in the Emacs sense) of the |
|---|
| 222 |
current frame, or of the specified FRAME. |
|---|
| 223 |
|
|---|
| 224 |
`for VAR being the buffers' |
|---|
| 225 |
This clause iterates over all buffers in Emacs. It is equivalent |
|---|
| 226 |
to `for VAR in (buffer-list)'. |
|---|
| 227 |
|
|---|
| 228 |
`for VAR = EXPR1 then EXPR2' |
|---|
| 229 |
This clause does a general iteration. The first time through the |
|---|
| 230 |
loop, VAR will be bound to EXPR1. On the second and successive |
|---|
| 231 |
iterations it will be set by evaluating EXPR2 (which may refer to |
|---|
| 232 |
the old value of VAR). For example, these two loops are |
|---|
| 233 |
effectively the same: |
|---|
| 234 |
|
|---|
| 235 |
(loop for x on my-list by 'cddr do ...) |
|---|
| 236 |
(loop for x = my-list then (cddr x) while x do ...) |
|---|
| 237 |
|
|---|
| 238 |
Note that this type of `for' clause does not imply any sort of |
|---|
| 239 |
terminating condition; the above example combines it with a |
|---|
| 240 |
`while' clause to tell when to end the loop. |
|---|
| 241 |
|
|---|
| 242 |
If you omit the `then' term, EXPR1 is used both for the initial |
|---|
| 243 |
setting and for successive settings: |
|---|
| 244 |
|
|---|
| 245 |
(loop for x = (random) when (> x 0) return x) |
|---|
| 246 |
|
|---|
| 247 |
This loop keeps taking random numbers from the `(random)' function |
|---|
| 248 |
until it gets a positive one, which it then returns. |
|---|
| 249 |
|
|---|
| 250 |
If you include several `for' clauses in a row, they are treated |
|---|
| 251 |
sequentially (as if by `let*' and `setq'). You can instead use the |
|---|
| 252 |
word `and' to link the clauses, in which case they are processed in |
|---|
| 253 |
parallel (as if by `let' and `psetq'). |
|---|
| 254 |
|
|---|
| 255 |
(loop for x below 5 for y = nil then x collect (list x y)) |
|---|
| 256 |
=> ((0 nil) (1 1) (2 2) (3 3) (4 4)) |
|---|
| 257 |
(loop for x below 5 and y = nil then x collect (list x y)) |
|---|
| 258 |
=> ((0 nil) (1 0) (2 1) (3 2) (4 3)) |
|---|
| 259 |
|
|---|
| 260 |
In the first loop, `y' is set based on the value of `x' that was just |
|---|
| 261 |
set by the previous clause; in the second loop, `x' and `y' are set |
|---|
| 262 |
simultaneously so `y' is set based on the value of `x' left over from |
|---|
| 263 |
the previous time through the loop. |
|---|
| 264 |
|
|---|
| 265 |
Another feature of the `loop' macro is "destructuring", similar in |
|---|
| 266 |
concept to the destructuring provided by `defmacro'. The VAR part of |
|---|
| 267 |
any `for' clause can be given as a list of variables instead of a |
|---|
| 268 |
single variable. The values produced during loop execution must be |
|---|
| 269 |
lists; the values in the lists are stored in the corresponding |
|---|
| 270 |
variables. |
|---|
| 271 |
|
|---|
| 272 |
(loop for (x y) in '((2 3) (4 5) (6 7)) collect (+ x y)) |
|---|
| 273 |
=> (5 9 13) |
|---|
| 274 |
|
|---|
| 275 |
In loop destructuring, if there are more values than variables the |
|---|
| 276 |
trailing values are ignored, and if there are more variables than |
|---|
| 277 |
values the trailing variables get the value `nil'. If `nil' is used as |
|---|
| 278 |
a variable name, the corresponding values are ignored. Destructuring |
|---|
| 279 |
may be nested, and dotted lists of variables like `(x . y)' are allowed. |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
File: cl, Node: Iteration Clauses, Next: Accumulation Clauses, Prev: For Clauses, Up: Loop Facility |
|---|
| 283 |
|
|---|
| 284 |
Iteration Clauses |
|---|
| 285 |
----------------- |
|---|
| 286 |
|
|---|
| 287 |
Aside from `for' clauses, there are several other loop clauses that |
|---|
| 288 |
control the way the loop operates. They might be used by themselves, |
|---|
| 289 |
or in conjunction with one or more `for' clauses. |
|---|
| 290 |
|
|---|
| 291 |
`repeat INTEGER' |
|---|
| 292 |
This clause simply counts up to the specified number using an |
|---|
| 293 |
internal temporary variable. The loops |
|---|
| 294 |
|
|---|
| 295 |
(loop repeat n do ...) |
|---|
| 296 |
(loop for temp to n do ...) |
|---|
| 297 |
|
|---|
| 298 |
are identical except that the second one forces you to choose a |
|---|
| 299 |
name for a variable you aren't actually going to use. |
|---|
| 300 |
|
|---|
| 301 |
`while CONDITION' |
|---|
| 302 |
This clause stops the loop when the specified condition (any Lisp |
|---|
| 303 |
expression) becomes `nil'. For example, the following two loops |
|---|
| 304 |
are equivalent, except for the implicit `nil' block that surrounds |
|---|
| 305 |
the second one: |
|---|
| 306 |
|
|---|
| 307 |
(while COND FORMS...) |
|---|
| 308 |
(loop while COND do FORMS...) |
|---|
| 309 |
|
|---|
| 310 |
`until CONDITION' |
|---|
| 311 |
This clause stops the loop when the specified condition is true, |
|---|
| 312 |
i.e., non-`nil'. |
|---|
| 313 |
|
|---|
| 314 |
`always CONDITION' |
|---|
| 315 |
This clause stops the loop when the specified condition is `nil'. |
|---|
| 316 |
Unlike `while', it stops the loop using `return nil' so that the |
|---|
| 317 |
`finally' clauses are not executed. If all the conditions were |
|---|
| 318 |
non-`nil', the loop returns `t': |
|---|
| 319 |
|
|---|
| 320 |
(if (loop for size in size-list always (> size 10)) |
|---|
| 321 |
(some-big-sizes) |
|---|
| 322 |
(no-big-sizes)) |
|---|
| 323 |
|
|---|
| 324 |
`never CONDITION' |
|---|
| 325 |
This clause is like `always', except that the loop returns `t' if |
|---|
| 326 |
any conditions were false, or `nil' otherwise. |
|---|
| 327 |
|
|---|
| 328 |
`thereis CONDITION' |
|---|
| 329 |
This clause stops the loop when the specified form is non-`nil'; |
|---|
| 330 |
in this case, it returns that non-`nil' value. If all the values |
|---|
| 331 |
were `nil', the loop returns `nil'. |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
File: cl, Node: Accumulation Clauses, Next: Other Clauses, Prev: Iteration Clauses, Up: Loop Facility |
|---|
| 335 |
|
|---|
| 336 |
Accumulation Clauses |
|---|
| 337 |
-------------------- |
|---|
| 338 |
|
|---|
| 339 |
These clauses cause the loop to accumulate information about the |
|---|
| 340 |
specified Lisp FORM. The accumulated result is returned from the loop |
|---|
| 341 |
unless overridden, say, by a `return' clause. |
|---|
| 342 |
|
|---|
| 343 |
`collect FORM' |
|---|
| 344 |
This clause collects the values of FORM into a list. Several |
|---|
| 345 |
examples of `collect' appear elsewhere in this manual. |
|---|
| 346 |
|
|---|
| 347 |
The word `collecting' is a synonym for `collect', and likewise for |
|---|
| 348 |
the other accumulation clauses. |
|---|
| 349 |
|
|---|
| 350 |
`append FORM' |
|---|
| 351 |
This clause collects lists of values into a result list using |
|---|
| 352 |
`append'. |
|---|
| 353 |
|
|---|
| 354 |
`nconc FORM' |
|---|
| 355 |
This clause collects lists of values into a result list by |
|---|
| 356 |
destructively modifying the lists rather than copying them. |
|---|
| 357 |
|
|---|
| 358 |
`concat FORM' |
|---|
| 359 |
This clause concatenates the values of the specified FORM into a |
|---|
| 360 |
string. (It and the following clause are extensions to standard |
|---|
| 361 |
Common Lisp.) |
|---|
| 362 |
|
|---|
| 363 |
`vconcat FORM' |
|---|
| 364 |
This clause concatenates the values of the specified FORM into a |
|---|
| 365 |
vector. |
|---|
| 366 |
|
|---|
| 367 |
`count FORM' |
|---|
| 368 |
This clause counts the number of times the specified FORM |
|---|
| 369 |
evaluates to a non-`nil' value. |
|---|
| 370 |
|
|---|
| 371 |
`sum FORM' |
|---|
| 372 |
This clause accumulates the sum of the values of the specified |
|---|
| 373 |
FORM, which must evaluate to a number. |
|---|
| 374 |
|
|---|
| 375 |
`maximize FORM' |
|---|
| 376 |
This clause accumulates the maximum value of the specified FORM, |
|---|
| 377 |
which must evaluate to a number. The return value is undefined if |
|---|
| 378 |
`maximize' is executed zero times. |
|---|
| 379 |
|
|---|
| 380 |
`minimize FORM' |
|---|
| 381 |
This clause accumulates the minimum value of the specified FORM. |
|---|
| 382 |
|
|---|
| 383 |
Accumulation clauses can be followed by `into VAR' to cause the data |
|---|
| 384 |
to be collected into variable VAR (which is automatically `let'-bound |
|---|
| 385 |
during the loop) rather than an unnamed temporary variable. Also, |
|---|
| 386 |
`into' accumulations do not automatically imply a return value. The |
|---|
| 387 |
loop must use some explicit mechanism, such as `finally return', to |
|---|
| 388 |
return the accumulated result. |
|---|
| 389 |
|
|---|
| 390 |
It is legal for several accumulation clauses of the same type to |
|---|
| 391 |
accumulate into the same place. From Steele: |
|---|
| 392 |
|
|---|
| 393 |
(loop for name in '(fred sue alice joe june) |
|---|
| 394 |
for kids in '((bob ken) () () (kris sunshine) ()) |
|---|
| 395 |
collect name |
|---|
| 396 |
append kids) |
|---|
| 397 |
=> (fred bob ken sue alice joe kris sunshine june) |
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
File: cl, Node: Other Clauses, Prev: Accumulation Clauses, Up: Loop Facility |
|---|
| 401 |
|
|---|
| 402 |
Other Clauses |
|---|
| 403 |
------------- |
|---|
| 404 |
|
|---|
| 405 |
This section describes the remaining loop clauses. |
|---|
| 406 |
|
|---|
| 407 |
`with VAR = VALUE' |
|---|
| 408 |
This clause binds a variable to a value around the loop, but |
|---|
| 409 |
otherwise leaves the variable alone during the loop. The following |
|---|
| 410 |
loops are basically equivalent: |
|---|
| 411 |
|
|---|
| 412 |
(loop with x = 17 do ...) |
|---|
| 413 |
(let ((x 17)) (loop do ...)) |
|---|
| 414 |
(loop for x = 17 then x do ...) |
|---|
| 415 |
|
|---|
| 416 |
Naturally, the variable VAR might be used for some purpose in the |
|---|
| 417 |
rest of the loop. For example: |
|---|
| 418 |
|
|---|
| 419 |
(loop for x in my-list with res = nil do (push x res) |
|---|
| 420 |
finally return res) |
|---|
| 421 |
|
|---|
| 422 |
This loop inserts the elements of `my-list' at the front of a new |
|---|
| 423 |
list being accumulated in `res', then returns the list `res' at |
|---|
| 424 |
the end of the loop. The effect is similar to that of a `collect' |
|---|
| 425 |
clause, but the list gets reversed by virtue of the fact that |
|---|
| 426 |
elements are being pushed onto the front of `res' rather than the |
|---|
| 427 |
end. |
|---|
| 428 |
|
|---|
| 429 |
If you omit the `=' term, the variable is initialized to `nil'. |
|---|
| 430 |
(Thus the `= nil' in the above example is unnecessary.) |
|---|
| 431 |
|
|---|
| 432 |
Bindings made by `with' are sequential by default, as if by |
|---|
| 433 |
`let*'. Just like `for' clauses, `with' clauses can be linked |
|---|
| 434 |
with `and' to cause the bindings to be made by `let' instead. |
|---|
| 435 |
|
|---|
| 436 |
`if CONDITION CLAUSE' |
|---|
| 437 |
This clause executes the following loop clause only if the |
|---|
| 438 |
specified condition is true. The following CLAUSE should be an |
|---|
| 439 |
accumulation, `do', `return', `if', or `unless' clause. Several |
|---|
| 440 |
clauses may be linked by separating them with `and'. These |
|---|
| 441 |
clauses may be followed by `else' and a clause or clauses to |
|---|
| 442 |
execute if the condition was false. The whole construct may |
|---|
| 443 |
optionally be followed by the word `end' (which may be used to |
|---|
| 444 |
disambiguate an `else' or `and' in a nested `if'). |
|---|
| 445 |
|
|---|
| 446 |
The actual non-`nil' value of the condition form is available by |
|---|
| 447 |
the name `it' in the "then" part. For example: |
|---|
| 448 |
|
|---|
| 449 |
(setq funny-numbers '(6 13 -1)) |
|---|
| 450 |
=> (6 13 -1) |
|---|
| 451 |
(loop for x below 10 |
|---|
| 452 |
if (oddp x) |
|---|
| 453 |
collect x into odds |
|---|
| 454 |
and if (memq x funny-numbers) return (cdr it) end |
|---|
| 455 |
else |
|---|
| 456 |
collect x into evens |
|---|
| 457 |
finally return (vector odds evens)) |
|---|
| 458 |
=> [(1 3 5 7 9) (0 2 4 6 8)] |
|---|
| 459 |
(setq funny-numbers '(6 7 13 -1)) |
|---|
| 460 |
=> (6 7 13 -1) |
|---|
| 461 |
(loop <same thing again>) |
|---|
| 462 |
=> (13 -1) |
|---|
| 463 |
|
|---|
| 464 |
Note the use of `and' to put two clauses into the "then" part, one |
|---|
| 465 |
of which is itself an `if' clause. Note also that `end', while |
|---|
| 466 |
normally optional, was necessary here to make it clear that the |
|---|
| 467 |
`else' refers to the outermost `if' clause. In the first case, |
|---|
| 468 |
the loop returns a vector of lists of the odd and even values of |
|---|
| 469 |
X. In the second case, the odd number 7 is one of the |
|---|
| 470 |
`funny-numbers' so the loop returns early; the actual returned |
|---|
| 471 |
value is based on the result of the `memq' call. |
|---|
| 472 |
|
|---|
| 473 |
`when CONDITION CLAUSE' |
|---|
| 474 |
This clause is just a synonym for `if'. |
|---|
| 475 |
|
|---|
| 476 |
`unless CONDITION CLAUSE' |
|---|
| 477 |
The `unless' clause is just like `if' except that the sense of the |
|---|
| 478 |
condition is reversed. |
|---|
| 479 |
|
|---|
| 480 |
`named NAME' |
|---|
| 481 |
This clause gives a name other than `nil' to the implicit block |
|---|
| 482 |
surrounding the loop. The NAME is the symbol to be used as the |
|---|
| 483 |
block name. |
|---|
| 484 |
|
|---|
| 485 |
`initially [do] FORMS...' |
|---|
| 486 |
This keyword introduces one or more Lisp forms which will be |
|---|
| 487 |
executed before the loop itself begins (but after any variables |
|---|
| 488 |
requested by `for' or `with' have been bound to their initial |
|---|
| 489 |
values). `initially' clauses can appear anywhere; if there are |
|---|
| 490 |
several, they are executed in the order they appear in the loop. |
|---|
| 491 |
The keyword `do' is optional. |
|---|
| 492 |
|
|---|
| 493 |
`finally [do] FORMS...' |
|---|
| 494 |
This introduces Lisp forms which will be executed after the loop |
|---|
| 495 |
finishes (say, on request of a `for' or `while'). `initially' and |
|---|
| 496 |
`finally' clauses may appear anywhere in the loop construct, but |
|---|
| 497 |
they are executed (in the specified order) at the beginning or |
|---|
| 498 |
end, respectively, of the loop. |
|---|
| 499 |
|
|---|
| 500 |
`finally return FORM' |
|---|
| 501 |
This says that FORM should be executed after the loop is done to |
|---|
| 502 |
obtain a return value. (Without this, or some other clause like |
|---|
| 503 |
`collect' or `return', the loop will simply return `nil'.) |
|---|
| 504 |
Variables bound by `for', `with', or `into' will still contain |
|---|
| 505 |
their final values when FORM is executed. |
|---|
| 506 |
|
|---|
| 507 |
`do FORMS...' |
|---|
| 508 |
The word `do' may be followed by any number of Lisp expressions |
|---|
| 509 |
which are executed as an implicit `progn' in the body of the loop. |
|---|
| 510 |
Many of the examples in this section illustrate the use of `do'. |
|---|
| 511 |
|
|---|
| 512 |
`return FORM' |
|---|
| 513 |
This clause causes the loop to return immediately. The following |
|---|
| 514 |
Lisp form is evaluated to give the return value of the `loop' |
|---|
| 515 |
form. The `finally' clauses, if any, are not executed. Of |
|---|
| 516 |
course, `return' is generally used inside an `if' or `unless', as |
|---|
| 517 |
its use in a top-level loop clause would mean the loop would never |
|---|
| 518 |
get to "loop" more than once. |
|---|
| 519 |
|
|---|
| 520 |
The clause `return FORM' is equivalent to `do (return FORM)' (or |
|---|
| 521 |
`return-from' if the loop was named). The `return' clause is |
|---|
| 522 |
implemented a bit more efficiently, though. |
|---|
| 523 |
|
|---|
| 524 |
While there is no high-level way to add user extensions to `loop' |
|---|
| 525 |
(comparable to `defsetf' for `setf', say), this package does offer two |
|---|
| 526 |
properties called `cl-loop-handler' and `cl-loop-for-handler' which are |
|---|
| 527 |
functions to be called when a given symbol is encountered as a |
|---|
| 528 |
top-level loop clause or `for' clause, respectively. Consult the |
|---|
| 529 |
source code in file `cl-macs.el' for details. |
|---|
| 530 |
|
|---|
| 531 |
This package's `loop' macro is compatible with that of Common Lisp, |
|---|
| 532 |
except that a few features are not implemented: `loop-finish' and |
|---|
| 533 |
data-type specifiers. Naturally, the `for' clauses which iterate over |
|---|
| 534 |
keymaps, overlays, intervals, frames, windows, and buffers are |
|---|
| 535 |
Emacs-specific extensions. |
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
File: cl, Node: Multiple Values, Prev: Loop Facility, Up: Control Structure |
|---|
| 539 |
|
|---|
| 540 |
Multiple Values |
|---|
| 541 |
=============== |
|---|
| 542 |
|
|---|
| 543 |
Common Lisp functions can return zero or more results. Emacs Lisp |
|---|
| 544 |
functions, by contrast, always return exactly one result. This package |
|---|
| 545 |
makes no attempt to emulate Common Lisp multiple return values; Emacs |
|---|
| 546 |
versions of Common Lisp functions that return more than one value |
|---|
| 547 |
either return just the first value (as in `compiler-macroexpand') or |
|---|
| 548 |
return a list of values (as in `get-setf-method'). This package _does_ |
|---|
| 549 |
define placeholders for the Common Lisp functions that work with |
|---|
| 550 |
multiple values, but in Emacs Lisp these functions simply operate on |
|---|
| 551 |
lists instead. The `values' form, for example, is a synonym for `list' |
|---|
| 552 |
in Emacs. |
|---|
| 553 |
|
|---|
| 554 |
- Special Form: multiple-value-bind (var...) values-form forms... |
|---|
| 555 |
This form evaluates VALUES-FORM, which must return a list of |
|---|
| 556 |
values. It then binds the VARs to these respective values, as if |
|---|
| 557 |
by `let', and then executes the body FORMS. If there are more |
|---|
| 558 |
VARs than values, the extra VARs are bound to `nil'. If there are |
|---|
| 559 |
fewer VARs than values, the excess values are ignored. |
|---|
| 560 |
|
|---|
| 561 |
- Special Form: multiple-value-setq (var...) form |
|---|
| 562 |
This form evaluates FORM, which must return a list of values. It |
|---|
| 563 |
then sets the VARs to these respective values, as if by `setq'. |
|---|
| 564 |
Extra VARs or values are treated the same as in |
|---|
| 565 |
`multiple-value-bind'. |
|---|
| 566 |
|
|---|
| 567 |
The older Quiroz package attempted a more faithful (but still |
|---|
| 568 |
imperfect) emulation of Common Lisp multiple values. The old method |
|---|
| 569 |
"usually" simulated true multiple values quite well, but under certain |
|---|
| 570 |
circumstances would leave spurious return values in memory where a |
|---|
| 571 |
later, unrelated `multiple-value-bind' form would see them. |
|---|
| 572 |
|
|---|
| 573 |
Since a perfect emulation is not feasible in Emacs Lisp, this |
|---|
| 574 |
package opts to keep it as simple and predictable as possible. |
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
File: cl, Node: Macros, Next: Declarations, Prev: Control Structure, Up: Top |
|---|
| 578 |
|
|---|
| 579 |
Macros |
|---|
| 580 |
****** |
|---|
| 581 |
|
|---|
| 582 |
This package implements the various Common Lisp features of `defmacro', |
|---|
| 583 |
such as destructuring, `&environment', and `&body'. Top-level `&whole' |
|---|
| 584 |
is not implemented for `defmacro' due to technical difficulties. *Note |
|---|
| 585 |
Argument Lists::. |
|---|
| 586 |
|
|---|
| 587 |
Destructuring is made available to the user by way of the following |
|---|
| 588 |
macro: |
|---|
| 589 |
|
|---|
| 590 |
- Special Form: destructuring-bind arglist expr forms... |
|---|
| 591 |
This macro expands to code which executes FORMS, with the |
|---|
| 592 |
variables in ARGLIST bound to the list of values returned by EXPR. |
|---|
| 593 |
The ARGLIST can include all the features allowed for `defmacro' |
|---|
| 594 |
argument lists, including destructuring. (The `&environment' |
|---|
| 595 |
keyword is not allowed.) The macro expansion will signal an error |
|---|
| 596 |
if EXPR returns a list of the wrong number of arguments or with |
|---|
| 597 |
incorrect keyword arguments. |
|---|
| 598 |
|
|---|
| 599 |
This package also includes the Common Lisp `define-compiler-macro' |
|---|
| 600 |
facility, which allows you to define compile-time expansions and |
|---|
| 601 |
optimizations for your functions. |
|---|
| 602 |
|
|---|
| 603 |
- Special Form: define-compiler-macro name arglist forms... |
|---|
| 604 |
This form is similar to `defmacro', except that it only expands |
|---|
| 605 |
calls to NAME at compile-time; calls processed by the Lisp |
|---|
| 606 |
interpreter are not expanded, nor are they expanded by the |
|---|
| 607 |
`macroexpand' function. |
|---|
| 608 |
|
|---|
| 609 |
The argument list may begin with a `&whole' keyword and a |
|---|
| 610 |
variable. This variable is bound to the macro-call form itself, |
|---|
| 611 |
i.e., to a list of the form `(NAME ARGS...)'. If the macro |
|---|
| 612 |
expander returns this form unchanged, then the compiler treats it |
|---|
| 613 |
as a normal function call. This allows compiler macros to work as |
|---|
| 614 |
optimizers for special cases of a function, leaving complicated |
|---|
| 615 |
cases alone. |
|---|
| 616 |
|
|---|
| 617 |
For example, here is a simplified version of a definition that |
|---|
| 618 |
appears as a standard part of this package: |
|---|
| 619 |
|
|---|
| 620 |
(define-compiler-macro member* (&whole form a list &rest keys) |
|---|
| 621 |
(if (and (null keys) |
|---|
| 622 |
(eq (car-safe a) 'quote) |
|---|
| 623 |
(not (floatp-safe (cadr a)))) |
|---|
| 624 |
(list 'memq a list) |
|---|
| 625 |
form)) |
|---|
| 626 |
|
|---|
| 627 |
This definition causes `(member* A LIST)' to change to a call to |
|---|
| 628 |
the faster `memq' in the common case where A is a |
|---|
| 629 |
non-floating-point constant; if A is anything else, or if there |
|---|
| 630 |
are any keyword arguments in the call, then the original `member*' |
|---|
| 631 |
call is left intact. (The actual compiler macro for `member*' |
|---|
| 632 |
optimizes a number of other cases, including common `:test' |
|---|
| 633 |
predicates.) |
|---|
| 634 |
|
|---|
| 635 |
- Function: compiler-macroexpand form |
|---|
| 636 |
This function is analogous to `macroexpand', except that it |
|---|
| 637 |
expands compiler macros rather than regular macros. It returns |
|---|
| 638 |
FORM unchanged if it is not a call to a function for which a |
|---|
| 639 |
compiler macro has been defined, or if that compiler macro decided |
|---|
| 640 |
to punt by returning its `&whole' argument. Like `macroexpand', |
|---|
| 641 |
it expands repeatedly until it reaches a form for which no further |
|---|
| 642 |
expansion is possible. |
|---|
| 643 |
|
|---|
| 644 |
*Note Macro Bindings::, for descriptions of the `macrolet' and |
|---|
| 645 |
`symbol-macrolet' forms for making "local" macro definitions. |
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
File: cl, Node: Declarations, Next: Symbols, Prev: Macros, Up: Top |
|---|
| 649 |
|
|---|
| 650 |
Declarations |
|---|
| 651 |
************ |
|---|
| 652 |
|
|---|
| 653 |
Common Lisp includes a complex and powerful "declaration" mechanism |
|---|
| 654 |
that allows you to give the compiler special hints about the types of |
|---|
| 655 |
data that will be stored in particular variables, and about the ways |
|---|
| 656 |
those variables and functions will be used. This package defines |
|---|
| 657 |
versions of all the Common Lisp declaration forms: `declare', |
|---|
| 658 |
`locally', `proclaim', `declaim', and `the'. |
|---|
| 659 |
|
|---|
| 660 |
Most of the Common Lisp declarations are not currently useful in |
|---|
| 661 |
Emacs Lisp, as the byte-code system provides little opportunity to |
|---|
| 662 |
benefit from type information, and `special' declarations are redundant |
|---|
| 663 |
in a fully dynamically-scoped Lisp. A few declarations are meaningful |
|---|
| 664 |
when the optimizing byte compiler is being used, however. Under the |
|---|
| 665 |
earlier non-optimizing compiler, these declarations will effectively be |
|---|
| 666 |
ignored. |
|---|
| 667 |
|
|---|
| 668 |
- Function: proclaim decl-spec |
|---|
| 669 |
This function records a "global" declaration specified by |
|---|
| 670 |
DECL-SPEC. Since `proclaim' is a function, DECL-SPEC is evaluated |
|---|
| 671 |
and thus should normally be quoted. |
|---|
| 672 |
|
|---|
| 673 |
- Special Form: declaim decl-specs... |
|---|
| 674 |
This macro is like `proclaim', except that it takes any number of |
|---|
| 675 |
DECL-SPEC arguments, and the arguments are unevaluated and |
|---|
| 676 |
unquoted. The `declaim' macro also puts an `(eval-when (compile |
|---|
| 677 |
load eval) ...)' around the declarations so that they will be |
|---|
| 678 |
registered at compile-time as well as at run-time. (This is vital, |
|---|
| 679 |
since normally the declarations are meant to influence the way the |
|---|
| 680 |
compiler treats the rest of the file that contains the `declaim' |
|---|
| 681 |
form.) |
|---|
| 682 |
|
|---|
| 683 |
- Special Form: declare decl-specs... |
|---|
| 684 |
This macro is used to make declarations within functions and other |
|---|
| 685 |
code. Common Lisp allows declarations in various locations, |
|---|
| 686 |
generally at the beginning of any of the many "implicit `progn's" |
|---|
| 687 |
throughout Lisp syntax, such as function bodies, `let' bodies, |
|---|
| 688 |
etc. Currently the only declaration understood by `declare' is |
|---|
| 689 |
`special'. |
|---|
| 690 |
|
|---|
| 691 |
- Special Form: locally declarations... forms... |
|---|
| 692 |
In this package, `locally' is no different from `progn'. |
|---|
| 693 |
|
|---|
| 694 |
- Special Form: the type form |
|---|
| 695 |
Type information provided by `the' is ignored in this package; in |
|---|
| 696 |
other words, `(the TYPE FORM)' is equivalent to FORM. Future |
|---|
| 697 |
versions of the optimizing byte-compiler may make use of this |
|---|
| 698 |
information. |
|---|
| 699 |
|
|---|
| 700 |
For example, `mapcar' can map over both lists and arrays. It is |
|---|
| 701 |
hard for the compiler to expand `mapcar' into an in-line loop |
|---|
| 702 |
unless it knows whether the sequence will be a list or an array |
|---|
| 703 |
ahead of time. With `(mapcar 'car (the vector foo))', a future |
|---|
| 704 |
compiler would have enough information to expand the loop in-line. |
|---|
| 705 |
For now, Emacs Lisp will treat the above code as exactly equivalent |
|---|
| 706 |
to `(mapcar 'car foo)'. |
|---|
| 707 |
|
|---|
| 708 |
Each DECL-SPEC in a `proclaim', `declaim', or `declare' should be a |
|---|
| 709 |
list beginning with a symbol that says what kind of declaration it is. |
|---|
| 710 |
This package currently understands `special', `inline', `notinline', |
|---|
| 711 |
`optimize', and `warn' declarations. (The `warn' declaration is an |
|---|
| 712 |
extension of standard Common Lisp.) Other Common Lisp declarations, |
|---|
| 713 |
such as `type' and `ftype', are silently ignored. |
|---|
| 714 |
|
|---|
| 715 |
`special' |
|---|
| 716 |
Since all variables in Emacs Lisp are "special" (in the Common |
|---|
| 717 |
Lisp sense), `special' declarations are only advisory. They |
|---|
| 718 |
simply tell the optimizing byte compiler that the specified |
|---|
| 719 |
variables are intentionally being referred to without being bound |
|---|
| 720 |
in the body of the function. The compiler normally emits warnings |
|---|
| 721 |
for such references, since they could be typographical errors for |
|---|
| 722 |
references to local variables. |
|---|
| 723 |
|
|---|
| 724 |
The declaration `(declare (special VAR1 VAR2))' is equivalent to |
|---|
| 725 |
`(defvar VAR1) (defvar VAR2)' in the optimizing compiler, or to |
|---|
| 726 |
nothing at all in older compilers (which do not warn for non-local |
|---|
| 727 |
references). |
|---|
| 728 |
|
|---|
| 729 |
In top-level contexts, it is generally better to write `(defvar |
|---|
| 730 |
VAR)' than `(declaim (special VAR))', since `defvar' makes your |
|---|
| 731 |
intentions clearer. But the older byte compilers can not handle |
|---|
| 732 |
`defvar's appearing inside of functions, while `(declare (special |
|---|
| 733 |
VAR))' takes care to work correctly with all compilers. |
|---|
| 734 |
|
|---|
| 735 |
`inline' |
|---|
| 736 |
The `inline' DECL-SPEC lists one or more functions whose bodies |
|---|
| 737 |
should be expanded "in-line" into calling functions whenever the |
|---|
| 738 |
compiler is able to arrange for it. For example, the Common Lisp |
|---|
| 739 |
function `cadr' is declared `inline' by this package so that the |
|---|
| 740 |
form `(cadr X)' will expand directly into `(car (cdr X))' when it |
|---|
| 741 |
is called in user functions, for a savings of one (relatively |
|---|
| 742 |
expensive) function call. |
|---|
| 743 |
|
|---|
| 744 |
The following declarations are all equivalent. Note that the |
|---|
| 745 |
`defsubst' form is a convenient way to define a function and |
|---|
| 746 |
declare it inline all at once. |
|---|
| 747 |
|
|---|
| 748 |
(declaim (inline foo bar)) |
|---|
| 749 |
(eval-when (compile load eval) (proclaim '(inline foo bar))) |
|---|
| 750 |
(defsubst foo (...) ...) ; instead of defun |
|---|
| 751 |
|
|---|
| 752 |
*Note:* This declaration remains in effect after the containing |
|---|
| 753 |
source file is done. It is correct to use it to request that a |
|---|
| 754 |
function you have defined should be inlined, but it is impolite to |
|---|
| 755 |
use it to request inlining of an external function. |
|---|
| 756 |
|
|---|
| 757 |
In Common Lisp, it is possible to use `(declare (inline ...))' |
|---|
| 758 |
before a particular call to a function to cause just that call to |
|---|
| 759 |
be inlined; the current byte compilers provide no way to implement |
|---|
| 760 |
this, so `(declare (inline ...))' is currently ignored by this |
|---|
| 761 |
package. |
|---|
| 762 |
|
|---|
| 763 |
`notinline' |
|---|
| 764 |
The `notinline' declaration lists functions which should not be |
|---|
| 765 |
inlined after all; it cancels a previous `inline' declaration. |
|---|
| 766 |
|
|---|
| 767 |
`optimize' |
|---|
| 768 |
This declaration controls how much optimization is performed by |
|---|
| 769 |
the compiler. Naturally, it is ignored by the earlier |
|---|
| 770 |
non-optimizing compilers. |
|---|
| 771 |
|
|---|
| 772 |
The word `optimize' is followed by any number of lists like |
|---|
| 773 |
`(speed 3)' or `(safety 2)'. Common Lisp defines several |
|---|
| 774 |
optimization "qualities"; this package ignores all but `speed' and |
|---|
| 775 |
`safety'. The value of a quality should be an integer from 0 to |
|---|
| 776 |
3, with 0 meaning "unimportant" and 3 meaning "very important." |
|---|
| 777 |
The default level for both qualities is 1. |
|---|
| 778 |
|
|---|
| 779 |
In this package, with the optimizing compiler, the `speed' quality |
|---|
| 780 |
is tied to the `byte-compile-optimize' flag, which is set to `nil' |
|---|
| 781 |
for `(speed 0)' and to `t' for higher settings; and the `safety' |
|---|
| 782 |
quality is tied to the `byte-compile-delete-errors' flag, which is |
|---|
| 783 |
set to `t' for `(safety 3)' and to `nil' for all lower settings. |
|---|
| 784 |
(The latter flag controls whether the compiler is allowed to |
|---|
| 785 |
optimize out code whose only side-effect could be to signal an |
|---|
| 786 |
error, e.g., rewriting `(progn foo bar)' to `bar' when it is not |
|---|
| 787 |
known whether `foo' will be bound at run-time.) |
|---|
| 788 |
|
|---|
| 789 |
Note that even compiling with `(safety 0)', the Emacs byte-code |
|---|
| 790 |
system provides sufficient checking to prevent real harm from |
|---|
| 791 |
being done. For example, barring serious bugs in Emacs itself, |
|---|
| 792 |
Emacs will not crash with a segmentation fault just because of an |
|---|
| 793 |
error in a fully-optimized Lisp program. |
|---|
| 794 |
|
|---|
| 795 |
The `optimize' declaration is normally used in a top-level |
|---|
| 796 |
`proclaim' or `declaim' in a file; Common Lisp allows it to be |
|---|
| 797 |
used with `declare' to set the level of optimization locally for a |
|---|
| 798 |
given form, but this will not work correctly with the current |
|---|
| 799 |
version of the optimizing compiler. (The `declare' will set the |
|---|
| 800 |
new optimization level, but that level will not automatically be |
|---|
| 801 |
unset after the enclosing form is done.) |
|---|
| 802 |
|
|---|
| 803 |
`warn' |
|---|
| 804 |
This declaration controls what sorts of warnings are generated by |
|---|
| 805 |
the byte compiler. Again, only the optimizing compiler generates |
|---|
| 806 |
warnings. The word `warn' is followed by any number of "warning |
|---|
| 807 |
qualities," similar in form to optimization qualities. The |
|---|
| 808 |
currently supported warning types are `redefine', `callargs', |
|---|
| 809 |
`unresolved', and `free-vars'; in the current system, a value of 0 |
|---|
| 810 |
will disable these warnings and any higher value will enable them. |
|---|
| 811 |
See the documentation for the optimizing byte compiler for details. |
|---|
| 812 |
|
|---|
| 813 |
|
|---|
| 814 |
File: cl, Node: Symbols, Next: Numbers, Prev: Declarations, Up: Top |
|---|
| 815 |
|
|---|
| 816 |
Symbols |
|---|
| 817 |
******* |
|---|
| 818 |
|
|---|
| 819 |
This package defines several symbol-related features that were missing |
|---|
| 820 |
from Emacs Lisp. |
|---|
| 821 |
|
|---|
| 822 |
* Menu: |
|---|
| 823 |
|
|---|
| 824 |
* Property Lists:: `get*', `remprop', `getf', `remf' |
|---|
| 825 |
* Creating Symbols:: `gensym', `gentemp' |
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 |
File: cl, Node: Property Lists, Next: Creating Symbols, Prev: Symbols, Up: Symbols |
|---|
| 829 |
|
|---|
| 830 |
Property Lists |
|---|
| 831 |
============== |
|---|
| 832 |
|
|---|
| 833 |
These functions augment the standard Emacs Lisp functions `get' and |
|---|
| 834 |
`put' for operating on properties attached to symbols. There are also |
|---|
| 835 |
functions for working with property lists as first-class data |
|---|
| 836 |
structures not attached to particular symbols. |
|---|
| 837 |
|
|---|
| 838 |
- Function: get* symbol property &optional default |
|---|
| 839 |
This function is like `get', except that if the property is not |
|---|
| 840 |
found, the DEFAULT argument provides the return value. (The Emacs |
|---|
| 841 |
Lisp `get' function always uses `nil' as the default; this |
|---|
| 842 |
package's `get*' is equivalent to Common Lisp's `get'.) |
|---|
| 843 |
|
|---|
| 844 |
The `get*' function is `setf'-able; when used in this fashion, the |
|---|
| 845 |
DEFAULT argument is allowed but ignored. |
|---|
| 846 |
|
|---|
| 847 |
- Function: remprop symbol property |
|---|
| 848 |
This function removes the entry for PROPERTY from the property |
|---|
| 849 |
list of SYMBOL. It returns a true value if the property was |
|---|
| 850 |
indeed found and removed, or `nil' if there was no such property. |
|---|
| 851 |
(This function was probably omitted from Emacs originally because, |
|---|
| 852 |
since `get' did not allow a DEFAULT, it was very difficult to |
|---|
| 853 |
distinguish between a missing property and a property whose value |
|---|
| 854 |
was `nil'; thus, setting a property to `nil' was close enough to |
|---|
| 855 |
`remprop' for most purposes.) |
|---|
| 856 |
|
|---|
| 857 |
- Function: getf place property &optional default |
|---|
| 858 |
This function scans the list PLACE as if it were a property list, |
|---|
| 859 |
i.e., a list of alternating property names and values. If an |
|---|
| 860 |
even-numbered element of PLACE is found which is `eq' to PROPERTY, |
|---|
| 861 |
the following odd-numbered element is returned. Otherwise, |
|---|
| 862 |
DEFAULT is returned (or `nil' if no default is given). |
|---|
| 863 |
|
|---|
| 864 |
In particular, |
|---|
| 865 |
|
|---|
| 866 |
(get sym prop) == (getf (symbol-plist sym) prop) |
|---|
| 867 |
|
|---|
| 868 |
It is legal to use `getf' as a `setf' place, in which case its |
|---|
| 869 |
PLACE argument must itself be a legal `setf' place. The DEFAULT |
|---|
| 870 |
argument, if any, is ignored in this context. The effect is to |
|---|
| 871 |
change (via `setcar') the value cell in the list that corresponds |
|---|
| 872 |
to PROPERTY, or to cons a new property-value pair onto the list if |
|---|
| 873 |
the property is not yet present. |
|---|
| 874 |
|
|---|
| 875 |
(put sym prop val) == (setf (getf (symbol-plist sym) prop) val) |
|---|
| 876 |
|
|---|
| 877 |
The `get' and `get*' functions are also `setf'-able. The fact |
|---|
| 878 |
that `default' is ignored can sometimes be useful: |
|---|
| 879 |
|
|---|
| 880 |
(incf (get* 'foo 'usage-count 0)) |
|---|
| 881 |
|
|---|
| 882 |
Here, symbol `foo''s `usage-count' property is incremented if it |
|---|
| 883 |
exists, or set to 1 (an incremented 0) otherwise. |
|---|
| 884 |
|
|---|
| 885 |
When not used as a `setf' form, `getf' is just a regular function |
|---|
| 886 |
and its PLACE argument can actually be any Lisp expression. |
|---|
| 887 |
|
|---|
| 888 |
- Special Form: remf place property |
|---|
| 889 |
This macro removes the property-value pair for PROPERTY from the |
|---|
| 890 |
property list stored at PLACE, which is any `setf'-able place |
|---|
| 891 |
expression. It returns true if the property was found. Note that |
|---|
| 892 |
if PROPERTY happens to be first on the list, this will effectively |
|---|
| 893 |
do a `(setf PLACE (cddr PLACE))', whereas if it occurs later, this |
|---|
| 894 |
simply uses `setcdr' to splice out the property and value cells. |
|---|
| 895 |
|
|---|
| 896 |
|
|---|
| 897 |
File: cl, Node: Creating Symbols, Prev: Property Lists, Up: Symbols |
|---|
| 898 |
|
|---|
| 899 |
Creating Symbols |
|---|
| 900 |
================ |
|---|
| 901 |
|
|---|
| 902 |
These functions create unique symbols, typically for use as temporary |
|---|
| 903 |
variables. |
|---|
| 904 |
|
|---|
| 905 |
- Function: gensym &optional x |
|---|
| 906 |
This function creates a new, uninterned symbol (using |
|---|
| 907 |
`make-symbol') with a unique name. (The name of an uninterned |
|---|
| 908 |
symbol is relevant only if the symbol is printed.) By default, |
|---|
| 909 |
the name is generated from an increasing sequence of numbers, |
|---|
| 910 |
`G1000', `G1001', `G1002', etc. If the optional argument X is a |
|---|
| 911 |
string, that string is used as a prefix instead of `G'. |
|---|
| 912 |
Uninterned symbols are used in macro expansions for temporary |
|---|
| 913 |
variables, to ensure that their names will not conflict with |
|---|
| 914 |
"real" variables in the user's code. |
|---|
| 915 |
|
|---|
| 916 |
- Variable: *gensym-counter* |
|---|
| 917 |
This variable holds the counter used to generate `gensym' names. |
|---|
| 918 |
It is incremented after each use by `gensym'. In Common Lisp this |
|---|
| 919 |
is initialized with 0, but this package initializes it with a |
|---|
| 920 |
random (time-dependent) value to avoid trouble when two files that |
|---|
| 921 |
each used `gensym' in their compilation are loaded together. |
|---|
| 922 |
(Uninterned symbols become interned when the compiler writes them |
|---|
| 923 |
out to a file and the Emacs loader loads them, so their names have |
|---|
| 924 |
to be treated a bit more carefully than in Common Lisp where |
|---|
| 925 |
uninterned symbols remain uninterned after loading.) |
|---|
| 926 |
|
|---|
| 927 |
- Function: gentemp &optional x |
|---|
| 928 |
This function is like `gensym', except that it produces a new |
|---|
| 929 |
_interned_ symbol. If the symbol that is generated already |
|---|
| 930 |
exists, the function keeps incrementing the counter and trying |
|---|
| 931 |
again until a new symbol is generated. |
|---|
| 932 |
|
|---|
| 933 |
The Quiroz `cl.el' package also defined a `defkeyword' form for |
|---|
| 934 |
creating self-quoting keyword symbols. This package automatically |
|---|
| 935 |
creates all keywords that are called for by `&key' argument specifiers, |
|---|
| 936 |
and discourages the use of keywords as data unrelated to keyword |
|---|
| 937 |
arguments, so the `defkeyword' form has been discontinued. |
|---|
| 938 |
|
|---|
| 939 |
|
|---|
| 940 |
File: cl, Node: Numbers, Next: Sequences, Prev: Symbols, Up: Top |
|---|
| 941 |
|
|---|
| 942 |
Numbers |
|---|
| 943 |
******* |
|---|
| 944 |
|
|---|
| 945 |
This section defines a few simple Common Lisp operations on numbers |
|---|
| 946 |
which were left out of Emacs Lisp. |
|---|
| 947 |
|
|---|
| 948 |
* Menu: |
|---|
| 949 |
|
|---|
| 950 |
* Predicates on Numbers:: `plusp', `oddp', `floatp-safe', etc. |
|---|
| 951 |
* Numerical Functions:: `abs', `floor*', etc. |
|---|
| 952 |
* Random Numbers:: `random*', `make-random-state' |
|---|
| 953 |
* Implementation Parameters:: `most-positive-fixnum', `most-positive-float' |
|---|
| 954 |
|
|---|
| 955 |
|
|---|
| 956 |
File: cl, Node: Predicates on Numbers, Next: Numerical Functions, Prev: Numbers, Up: Numbers |
|---|
| 957 |
|
|---|
| 958 |
Predicates on Numbers |
|---|
| 959 |
===================== |
|---|
| 960 |
|
|---|
| 961 |
These functions return `t' if the specified condition is true of the |
|---|
| 962 |
numerical argument, or `nil' otherwise. |
|---|
| 963 |
|
|---|
| 964 |
- Function: plusp number |
|---|
| 965 |
This predicate tests whether NUMBER is positive. It is an error |
|---|
| 966 |
if the argument is not a number. |
|---|
| 967 |
|
|---|
| 968 |
- Function: minusp number |
|---|
| 969 |
This predicate tests whether NUMBER is negative. It is an error |
|---|
| 970 |
if the argument is not a number. |
|---|
| 971 |
|
|---|
| 972 |
- Function: oddp integer |
|---|
| 973 |
This predicate tests whether INTEGER is odd. It is an error if |
|---|
| 974 |
the argument is not an integer. |
|---|
| 975 |
|
|---|
| 976 |
- Function: evenp integer |
|---|
| 977 |
This predicate tests whether INTEGER is even. It is an error if |
|---|
| 978 |
the argument is not an integer. |
|---|
| 979 |
|
|---|
| 980 |
- Function: floatp-safe object |
|---|
| 981 |
This predicate tests whether OBJECT is a floating-point number. |
|---|
| 982 |
On systems that support floating-point, this is equivalent to |
|---|
| 983 |
`floatp'. On other systems, this always returns `nil'. |
|---|
| 984 |
|
|---|
| 985 |
|
|---|
| 986 |
File: cl, Node: Numerical Functions, Next: Random Numbers, Prev: Predicates on Numbers, Up: Numbers |
|---|
| 987 |
|
|---|
| 988 |
Numerical Functions |
|---|
| 989 |
=================== |
|---|
| 990 |
|
|---|
| 991 |
These functions perform various arithmetic operations on numbers. |
|---|
| 992 |
|
|---|
| 993 |
- Function: gcd &rest integers |
|---|
| 994 |
This function returns the Greatest Common Divisor of the arguments. |
|---|
| 995 |
For one argument, it returns the absolute value of that argument. |
|---|
| 996 |
For zero arguments, it returns zero. |
|---|
| 997 |
|
|---|
| 998 |
- Function: lcm &rest integers |
|---|
| 999 |
This function returns the Least Common Multiple of the arguments. |
|---|
| 1000 |
For one argument, it returns the absolute value of that argument. |
|---|
| 1001 |
For zero arguments, it returns one. |
|---|
| 1002 |
|
|---|
| 1003 |
- Function: isqrt integer |
|---|
| 1004 |
This function computes the "integer square root" of its integer |
|---|
| 1005 |
argument, i.e., the greatest integer less than or equal to the true |
|---|
| 1006 |
square root of the argument. |
|---|
| 1007 |
|
|---|
| 1008 |
- Function: floor* number &optional divisor |
|---|
| 1009 |
This function implements the Common Lisp `floor' function. It is |
|---|
| 1010 |
called `floor*' to avoid name conflicts with the simpler `floor' |
|---|
| 1011 |
function built-in to Emacs. |
|---|
| 1012 |
|
|---|
| 1013 |
With one argument, `floor*' returns a list of two numbers: The |
|---|
| 1014 |
argument rounded down (toward minus infinity) to an integer, and |
|---|
| 1015 |
the "remainder" which would have to be added back to the first |
|---|
| 1016 |
return value to yield the argument again. If the argument is an |
|---|
| 1017 |
integer X, the result is always the list `(X 0)'. If the argument |
|---|
| 1018 |
is a floating-point number, the first result is a Lisp integer and |
|---|
| 1019 |
the second is a Lisp float between 0 (inclusive) and 1 (exclusive). |
|---|
| 1020 |
|
|---|
| 1021 |
With two arguments, `floor*' divides NUMBER by DIVISOR, and |
|---|
| 1022 |
returns the floor of the quotient and the corresponding remainder |
|---|
| 1023 |
as a list of two numbers. If `(floor* X Y)' returns `(Q R)', then |
|---|
| 1024 |
`Q*Y + R = X', with R between 0 (inclusive) and R (exclusive). |
|---|
| 1025 |
Also, note that `(floor* X)' is exactly equivalent to `(floor* X |
|---|
| 1026 |
1)'. |
|---|
| 1027 |
|
|---|
| 1028 |
This function is entirely compatible with Common Lisp's `floor' |
|---|
| 1029 |
function, except that it returns the two results in a list since |
|---|
| 1030 |
Emacs Lisp does not support multiple-valued functions. |
|---|
| 1031 |
|
|---|
| 1032 |
- Function: ceiling* number &optional divisor |
|---|
| 1033 |
This function implements the Common Lisp `ceiling' function, which |
|---|
| 1034 |
is analogous to `floor' except that it rounds the argument or |
|---|
| 1035 |
quotient of the arguments up toward plus infinity. The remainder |
|---|
| 1036 |
will be between 0 and minus R. |
|---|
| 1037 |
|
|---|
| 1038 |
- Function: truncate* number &optional divisor |
|---|
| 1039 |
This function implements the Common Lisp `truncate' function, |
|---|
| 1040 |
which is analogous to `floor' except that it rounds the argument |
|---|
| 1041 |
or quotient of the arguments toward zero. Thus it is equivalent |
|---|
| 1042 |
to `floor*' if the argument or quotient is positive, or to |
|---|
| 1043 |
`ceiling*' otherwise. The remainder has the same sign as NUMBER. |
|---|
| 1044 |
|
|---|
| 1045 |
- Function: round* number &optional divisor |
|---|
| 1046 |
This function implements the Common Lisp `round' function, which |
|---|
| 1047 |
is analogous to `floor' except that it rounds the argument or |
|---|
| 1048 |
quotient of the arguments to the nearest integer. In the case of |
|---|
| 1049 |
a tie (the argument or quotient is exactly halfway between two |
|---|
| 1050 |
integers), it rounds to the even integer. |
|---|
| 1051 |
|
|---|
| 1052 |
- Function: mod* number divisor |
|---|
| 1053 |
This function returns the same value as the second return value of |
|---|
| 1054 |
`floor'. |
|---|
| 1055 |
|
|---|
| 1056 |
- Function: rem* number divisor |
|---|
| 1057 |
This function returns the same value as the second return value of |
|---|
| 1058 |
`truncate'. |
|---|
| 1059 |
|
|---|
| 1060 |
These definitions are compatible with those in the Quiroz `cl.el' |
|---|
| 1061 |
package, except that this package appends `*' to certain function names |
|---|
| 1062 |
to avoid conflicts with existing Emacs functions, and that the |
|---|
| 1063 |
mechanism for returning multiple values is different. |
|---|
| 1064 |
|
|---|
| 1065 |
|
|---|
| 1066 |
File: cl, Node: Random Numbers, Next: Implementation Parameters, Prev: Numerical Functions, Up: Numbers |
|---|
| 1067 |
|
|---|
| 1068 |
Random Numbers |
|---|
| 1069 |
============== |
|---|
| 1070 |
|
|---|
| 1071 |
This package also provides an implementation of the Common Lisp random |
|---|
| 1072 |
number generator. It uses its own additive-congruential algorithm, |
|---|
| 1073 |
which is much more likely to give statistically clean random numbers |
|---|
| 1074 |
than the simple generators supplied by many operating systems. |
|---|
| 1075 |
|
|---|
| 1076 |
- Function: random* number &optional state |
|---|
| 1077 |
This function returns a random nonnegative number less than |
<