| 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: Structures, Next: Assertions, Prev: Lists, Up: Top |
|---|
| 31 |
|
|---|
| 32 |
Structures |
|---|
| 33 |
********** |
|---|
| 34 |
|
|---|
| 35 |
The Common Lisp "structure" mechanism provides a general way to define |
|---|
| 36 |
data types similar to C's `struct' types. A structure is a Lisp object |
|---|
| 37 |
containing some number of "slots", each of which can hold any Lisp data |
|---|
| 38 |
object. Functions are provided for accessing and setting the slots, |
|---|
| 39 |
creating or copying structure objects, and recognizing objects of a |
|---|
| 40 |
particular structure type. |
|---|
| 41 |
|
|---|
| 42 |
In true Common Lisp, each structure type is a new type distinct from |
|---|
| 43 |
all existing Lisp types. Since the underlying Emacs Lisp system |
|---|
| 44 |
provides no way to create new distinct types, this package implements |
|---|
| 45 |
structures as vectors (or lists upon request) with a special "tag" |
|---|
| 46 |
symbol to identify them. |
|---|
| 47 |
|
|---|
| 48 |
- Special Form: defstruct name slots... |
|---|
| 49 |
The `defstruct' form defines a new structure type called NAME, |
|---|
| 50 |
with the specified SLOTS. (The SLOTS may begin with a string |
|---|
| 51 |
which documents the structure type.) In the simplest case, NAME |
|---|
| 52 |
and each of the SLOTS are symbols. For example, |
|---|
| 53 |
|
|---|
| 54 |
(defstruct person name age sex) |
|---|
| 55 |
|
|---|
| 56 |
defines a struct type called `person' which contains three slots. |
|---|
| 57 |
Given a `person' object P, you can access those slots by calling |
|---|
| 58 |
`(person-name P)', `(person-age P)', and `(person-sex P)'. You |
|---|
| 59 |
can also change these slots by using `setf' on any of these place |
|---|
| 60 |
forms: |
|---|
| 61 |
|
|---|
| 62 |
(incf (person-age birthday-boy)) |
|---|
| 63 |
|
|---|
| 64 |
You can create a new `person' by calling `make-person', which |
|---|
| 65 |
takes keyword arguments `:name', `:age', and `:sex' to specify the |
|---|
| 66 |
initial values of these slots in the new object. (Omitting any of |
|---|
| 67 |
these arguments leaves the corresponding slot "undefined," |
|---|
| 68 |
according to the Common Lisp standard; in Emacs Lisp, such |
|---|
| 69 |
uninitialized slots are filled with `nil'.) |
|---|
| 70 |
|
|---|
| 71 |
Given a `person', `(copy-person P)' makes a new object of the same |
|---|
| 72 |
type whose slots are `eq' to those of P. |
|---|
| 73 |
|
|---|
| 74 |
Given any Lisp object X, `(person-p X)' returns true if X looks |
|---|
| 75 |
like a `person', false otherwise. (Again, in Common Lisp this |
|---|
| 76 |
predicate would be exact; in Emacs Lisp the best it can do is |
|---|
| 77 |
verify that X is a vector of the correct length which starts with |
|---|
| 78 |
the correct tag symbol.) |
|---|
| 79 |
|
|---|
| 80 |
Accessors like `person-name' normally check their arguments |
|---|
| 81 |
(effectively using `person-p') and signal an error if the argument |
|---|
| 82 |
is the wrong type. This check is affected by `(optimize (safety |
|---|
| 83 |
...))' declarations. Safety level 1, the default, uses a somewhat |
|---|
| 84 |
optimized check that will detect all incorrect arguments, but may |
|---|
| 85 |
use an uninformative error message (e.g., "expected a vector" |
|---|
| 86 |
instead of "expected a `person'"). Safety level 0 omits all |
|---|
| 87 |
checks except as provided by the underlying `aref' call; safety |
|---|
| 88 |
levels 2 and 3 do rigorous checking that will always print a |
|---|
| 89 |
descriptive error message for incorrect inputs. *Note |
|---|
| 90 |
Declarations::. |
|---|
| 91 |
|
|---|
| 92 |
(setq dave (make-person :name "Dave" :sex 'male)) |
|---|
| 93 |
=> [cl-struct-person "Dave" nil male] |
|---|
| 94 |
(setq other (copy-person dave)) |
|---|
| 95 |
=> [cl-struct-person "Dave" nil male] |
|---|
| 96 |
(eq dave other) |
|---|
| 97 |
=> nil |
|---|
| 98 |
(eq (person-name dave) (person-name other)) |
|---|
| 99 |
=> t |
|---|
| 100 |
(person-p dave) |
|---|
| 101 |
=> t |
|---|
| 102 |
(person-p [1 2 3 4]) |
|---|
| 103 |
=> nil |
|---|
| 104 |
(person-p "Bogus") |
|---|
| 105 |
=> nil |
|---|
| 106 |
(person-p '[cl-struct-person counterfeit person object]) |
|---|
| 107 |
=> t |
|---|
| 108 |
|
|---|
| 109 |
In general, NAME is either a name symbol or a list of a name |
|---|
| 110 |
symbol followed by any number of "struct options"; each SLOT is |
|---|
| 111 |
either a slot symbol or a list of the form `(SLOT-NAME |
|---|
| 112 |
DEFAULT-VALUE SLOT-OPTIONS...)'. The DEFAULT-VALUE is a Lisp form |
|---|
| 113 |
which is evaluated any time an instance of the structure type is |
|---|
| 114 |
created without specifying that slot's value. |
|---|
| 115 |
|
|---|
| 116 |
Common Lisp defines several slot options, but the only one |
|---|
| 117 |
implemented in this package is `:read-only'. A non-`nil' value |
|---|
| 118 |
for this option means the slot should not be `setf'-able; the |
|---|
| 119 |
slot's value is determined when the object is created and does not |
|---|
| 120 |
change afterward. |
|---|
| 121 |
|
|---|
| 122 |
(defstruct person |
|---|
| 123 |
(name nil :read-only t) |
|---|
| 124 |
age |
|---|
| 125 |
(sex 'unknown)) |
|---|
| 126 |
|
|---|
| 127 |
Any slot options other than `:read-only' are ignored. |
|---|
| 128 |
|
|---|
| 129 |
For obscure historical reasons, structure options take a different |
|---|
| 130 |
form than slot options. A structure option is either a keyword |
|---|
| 131 |
symbol, or a list beginning with a keyword symbol possibly followed |
|---|
| 132 |
by arguments. (By contrast, slot options are key-value pairs not |
|---|
| 133 |
enclosed in lists.) |
|---|
| 134 |
|
|---|
| 135 |
(defstruct (person (:constructor create-person) |
|---|
| 136 |
(:type list) |
|---|
| 137 |
:named) |
|---|
| 138 |
name age sex) |
|---|
| 139 |
|
|---|
| 140 |
The following structure options are recognized. |
|---|
| 141 |
|
|---|
| 142 |
`:conc-name' |
|---|
| 143 |
The argument is a symbol whose print name is used as the |
|---|
| 144 |
prefix for the names of slot accessor functions. The default |
|---|
| 145 |
is the name of the struct type followed by a hyphen. The |
|---|
| 146 |
option `(:conc-name p-)' would change this prefix to `p-'. |
|---|
| 147 |
Specifying `nil' as an argument means no prefix, so that the |
|---|
| 148 |
slot names themselves are used to name the accessor functions. |
|---|
| 149 |
|
|---|
| 150 |
`:constructor' |
|---|
| 151 |
In the simple case, this option takes one argument which is an |
|---|
| 152 |
alternate name to use for the constructor function. The |
|---|
| 153 |
default is `make-NAME', e.g., `make-person'. The above |
|---|
| 154 |
example changes this to `create-person'. Specifying `nil' as |
|---|
| 155 |
an argument means that no standard constructor should be |
|---|
| 156 |
generated at all. |
|---|
| 157 |
|
|---|
| 158 |
In the full form of this option, the constructor name is |
|---|
| 159 |
followed by an arbitrary argument list. *Note Program |
|---|
| 160 |
Structure::, for a description of the format of Common Lisp |
|---|
| 161 |
argument lists. All options, such as `&rest' and `&key', are |
|---|
| 162 |
supported. The argument names should match the slot names; |
|---|
| 163 |
each slot is initialized from the corresponding argument. |
|---|
| 164 |
Slots whose names do not appear in the argument list are |
|---|
| 165 |
initialized based on the DEFAULT-VALUE in their slot |
|---|
| 166 |
descriptor. Also, `&optional' and `&key' arguments which |
|---|
| 167 |
don't specify defaults take their defaults from the slot |
|---|
| 168 |
descriptor. It is legal to include arguments which don't |
|---|
| 169 |
correspond to slot names; these are useful if they are |
|---|
| 170 |
referred to in the defaults for optional, keyword, or `&aux' |
|---|
| 171 |
arguments which _do_ correspond to slots. |
|---|
| 172 |
|
|---|
| 173 |
You can specify any number of full-format `:constructor' |
|---|
| 174 |
options on a structure. The default constructor is still |
|---|
| 175 |
generated as well unless you disable it with a simple-format |
|---|
| 176 |
`:constructor' option. |
|---|
| 177 |
|
|---|
| 178 |
(defstruct |
|---|
| 179 |
(person |
|---|
| 180 |
(:constructor nil) ; no default constructor |
|---|
| 181 |
(:constructor new-person (name sex &optional (age 0))) |
|---|
| 182 |
(:constructor new-hound (&key (name "Rover") |
|---|
| 183 |
(dog-years 0) |
|---|
| 184 |
&aux (age (* 7 dog-years)) |
|---|
| 185 |
(sex 'canine)))) |
|---|
| 186 |
name age sex) |
|---|
| 187 |
|
|---|
| 188 |
The first constructor here takes its arguments positionally |
|---|
| 189 |
rather than by keyword. (In official Common Lisp |
|---|
| 190 |
terminology, constructors that work By Order of Arguments |
|---|
| 191 |
instead of by keyword are called "BOA constructors." No, I'm |
|---|
| 192 |
not making this up.) For example, `(new-person "Jane" |
|---|
| 193 |
'female)' generates a person whose slots are `"Jane"', 0, and |
|---|
| 194 |
`female', respectively. |
|---|
| 195 |
|
|---|
| 196 |
The second constructor takes two keyword arguments, `:name', |
|---|
| 197 |
which initializes the `name' slot and defaults to `"Rover"', |
|---|
| 198 |
and `:dog-years', which does not itself correspond to a slot |
|---|
| 199 |
but which is used to initialize the `age' slot. The `sex' |
|---|
| 200 |
slot is forced to the symbol `canine' with no syntax for |
|---|
| 201 |
overriding it. |
|---|
| 202 |
|
|---|
| 203 |
`:copier' |
|---|
| 204 |
The argument is an alternate name for the copier function for |
|---|
| 205 |
this type. The default is `copy-NAME'. `nil' means not to |
|---|
| 206 |
generate a copier function. (In this implementation, all |
|---|
| 207 |
copier functions are simply synonyms for `copy-sequence'.) |
|---|
| 208 |
|
|---|
| 209 |
`:predicate' |
|---|
| 210 |
The argument is an alternate name for the predicate which |
|---|
| 211 |
recognizes objects of this type. The default is `NAME-p'. |
|---|
| 212 |
`nil' means not to generate a predicate function. (If the |
|---|
| 213 |
`:type' option is used without the `:named' option, no |
|---|
| 214 |
predicate is ever generated.) |
|---|
| 215 |
|
|---|
| 216 |
In true Common Lisp, `typep' is always able to recognize a |
|---|
| 217 |
structure object even if `:predicate' was used. In this |
|---|
| 218 |
package, `typep' simply looks for a function called |
|---|
| 219 |
`TYPENAME-p', so it will work for structure types only if |
|---|
| 220 |
they used the default predicate name. |
|---|
| 221 |
|
|---|
| 222 |
`:include' |
|---|
| 223 |
This option implements a very limited form of C++-style |
|---|
| 224 |
inheritance. The argument is the name of another structure |
|---|
| 225 |
type previously created with `defstruct'. The effect is to |
|---|
| 226 |
cause the new structure type to inherit all of the included |
|---|
| 227 |
structure's slots (plus, of course, any new slots described |
|---|
| 228 |
by this struct's slot descriptors). The new structure is |
|---|
| 229 |
considered a "specialization" of the included one. In fact, |
|---|
| 230 |
the predicate and slot accessors for the included type will |
|---|
| 231 |
also accept objects of the new type. |
|---|
| 232 |
|
|---|
| 233 |
If there are extra arguments to the `:include' option after |
|---|
| 234 |
the included-structure name, these options are treated as |
|---|
| 235 |
replacement slot descriptors for slots in the included |
|---|
| 236 |
structure, possibly with modified default values. Borrowing |
|---|
| 237 |
an example from Steele: |
|---|
| 238 |
|
|---|
| 239 |
(defstruct person name (age 0) sex) |
|---|
| 240 |
=> person |
|---|
| 241 |
(defstruct (astronaut (:include person (age 45))) |
|---|
| 242 |
helmet-size |
|---|
| 243 |
(favorite-beverage 'tang)) |
|---|
| 244 |
=> astronaut |
|---|
| 245 |
|
|---|
| 246 |
(setq joe (make-person :name "Joe")) |
|---|
| 247 |
=> [cl-struct-person "Joe" 0 nil] |
|---|
| 248 |
(setq buzz (make-astronaut :name "Buzz")) |
|---|
| 249 |
=> [cl-struct-astronaut "Buzz" 45 nil nil tang] |
|---|
| 250 |
|
|---|
| 251 |
(list (person-p joe) (person-p buzz)) |
|---|
| 252 |
=> (t t) |
|---|
| 253 |
(list (astronaut-p joe) (astronaut-p buzz)) |
|---|
| 254 |
=> (nil t) |
|---|
| 255 |
|
|---|
| 256 |
(person-name buzz) |
|---|
| 257 |
=> "Buzz" |
|---|
| 258 |
(astronaut-name joe) |
|---|
| 259 |
=> error: "astronaut-name accessing a non-astronaut" |
|---|
| 260 |
|
|---|
| 261 |
Thus, if `astronaut' is a specialization of `person', then |
|---|
| 262 |
every `astronaut' is also a `person' (but not the other way |
|---|
| 263 |
around). Every `astronaut' includes all the slots of a |
|---|
| 264 |
`person', plus extra slots that are specific to astronauts. |
|---|
| 265 |
Operations that work on people (like `person-name') work on |
|---|
| 266 |
astronauts just like other people. |
|---|
| 267 |
|
|---|
| 268 |
`:print-function' |
|---|
| 269 |
In full Common Lisp, this option allows you to specify a |
|---|
| 270 |
function which is called to print an instance of the |
|---|
| 271 |
structure type. The Emacs Lisp system offers no hooks into |
|---|
| 272 |
the Lisp printer which would allow for such a feature, so |
|---|
| 273 |
this package simply ignores `:print-function'. |
|---|
| 274 |
|
|---|
| 275 |
`:type' |
|---|
| 276 |
The argument should be one of the symbols `vector' or `list'. |
|---|
| 277 |
This tells which underlying Lisp data type should be used to |
|---|
| 278 |
implement the new structure type. Vectors are used by |
|---|
| 279 |
default, but `(:type list)' will cause structure objects to |
|---|
| 280 |
be stored as lists instead. |
|---|
| 281 |
|
|---|
| 282 |
The vector representation for structure objects has the |
|---|
| 283 |
advantage that all structure slots can be accessed quickly, |
|---|
| 284 |
although creating vectors is a bit slower in Emacs Lisp. |
|---|
| 285 |
Lists are easier to create, but take a relatively long time |
|---|
| 286 |
accessing the later slots. |
|---|
| 287 |
|
|---|
| 288 |
`:named' |
|---|
| 289 |
This option, which takes no arguments, causes a |
|---|
| 290 |
characteristic "tag" symbol to be stored at the front of the |
|---|
| 291 |
structure object. Using `:type' without also using `:named' |
|---|
| 292 |
will result in a structure type stored as plain vectors or |
|---|
| 293 |
lists with no identifying features. |
|---|
| 294 |
|
|---|
| 295 |
The default, if you don't specify `:type' explicitly, is to |
|---|
| 296 |
use named vectors. Therefore, `:named' is only useful in |
|---|
| 297 |
conjunction with `:type'. |
|---|
| 298 |
|
|---|
| 299 |
(defstruct (person1) name age sex) |
|---|
| 300 |
(defstruct (person2 (:type list) :named) name age sex) |
|---|
| 301 |
(defstruct (person3 (:type list)) name age sex) |
|---|
| 302 |
|
|---|
| 303 |
(setq p1 (make-person1)) |
|---|
| 304 |
=> [cl-struct-person1 nil nil nil] |
|---|
| 305 |
(setq p2 (make-person2)) |
|---|
| 306 |
=> (person2 nil nil nil) |
|---|
| 307 |
(setq p3 (make-person3)) |
|---|
| 308 |
=> (nil nil nil) |
|---|
| 309 |
|
|---|
| 310 |
(person1-p p1) |
|---|
| 311 |
=> t |
|---|
| 312 |
(person2-p p2) |
|---|
| 313 |
=> t |
|---|
| 314 |
(person3-p p3) |
|---|
| 315 |
=> error: function person3-p undefined |
|---|
| 316 |
|
|---|
| 317 |
Since unnamed structures don't have tags, `defstruct' is not |
|---|
| 318 |
able to make a useful predicate for recognizing them. Also, |
|---|
| 319 |
accessors like `person3-name' will be generated but they will |
|---|
| 320 |
not be able to do any type checking. The `person3-name' |
|---|
| 321 |
function, for example, will simply be a synonym for `car' in |
|---|
| 322 |
this case. By contrast, `person2-name' is able to verify |
|---|
| 323 |
that its argument is indeed a `person2' object before |
|---|
| 324 |
proceeding. |
|---|
| 325 |
|
|---|
| 326 |
`:initial-offset' |
|---|
| 327 |
The argument must be a nonnegative integer. It specifies a |
|---|
| 328 |
number of slots to be left "empty" at the front of the |
|---|
| 329 |
structure. If the structure is named, the tag appears at the |
|---|
| 330 |
specified position in the list or vector; otherwise, the first |
|---|
| 331 |
slot appears at that position. Earlier positions are filled |
|---|
| 332 |
with `nil' by the constructors and ignored otherwise. If the |
|---|
| 333 |
type `:include's another type, then `:initial-offset' |
|---|
| 334 |
specifies a number of slots to be skipped between the last |
|---|
| 335 |
slot of the included type and the first new slot. |
|---|
| 336 |
|
|---|
| 337 |
Except as noted, the `defstruct' facility of this package is |
|---|
| 338 |
entirely compatible with that of Common Lisp. |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
File: cl, Node: Assertions, Next: Efficiency Concerns, Prev: Structures, Up: Top |
|---|
| 342 |
|
|---|
| 343 |
Assertions and Errors |
|---|
| 344 |
********************* |
|---|
| 345 |
|
|---|
| 346 |
This section describes two macros that test "assertions", i.e., |
|---|
| 347 |
conditions which must be true if the program is operating correctly. |
|---|
| 348 |
Assertions never add to the behavior of a Lisp program; they simply |
|---|
| 349 |
make "sanity checks" to make sure everything is as it should be. |
|---|
| 350 |
|
|---|
| 351 |
If the optimization property `speed' has been set to 3, and `safety' |
|---|
| 352 |
is less than 3, then the byte-compiler will optimize away the following |
|---|
| 353 |
assertions. Because assertions might be optimized away, it is a bad |
|---|
| 354 |
idea for them to include side-effects. |
|---|
| 355 |
|
|---|
| 356 |
- Special Form: assert test-form [show-args string args...] |
|---|
| 357 |
This form verifies that TEST-FORM is true (i.e., evaluates to a |
|---|
| 358 |
non-`nil' value). If so, it returns `nil'. If the test is not |
|---|
| 359 |
satisfied, `assert' signals an error. |
|---|
| 360 |
|
|---|
| 361 |
A default error message will be supplied which includes TEST-FORM. |
|---|
| 362 |
You can specify a different error message by including a STRING |
|---|
| 363 |
argument plus optional extra arguments. Those arguments are simply |
|---|
| 364 |
passed to `error' to signal the error. |
|---|
| 365 |
|
|---|
| 366 |
If the optional second argument SHOW-ARGS is `t' instead of `nil', |
|---|
| 367 |
then the error message (with or without STRING) will also include |
|---|
| 368 |
all non-constant arguments of the top-level FORM. For example: |
|---|
| 369 |
|
|---|
| 370 |
(assert (> x 10) t "x is too small: %d") |
|---|
| 371 |
|
|---|
| 372 |
This usage of SHOW-ARGS is an extension to Common Lisp. In true |
|---|
| 373 |
Common Lisp, the second argument gives a list of PLACES which can |
|---|
| 374 |
be `setf''d by the user before continuing from the error. Since |
|---|
| 375 |
Emacs Lisp does not support continuable errors, it makes no sense |
|---|
| 376 |
to specify PLACES. |
|---|
| 377 |
|
|---|
| 378 |
- Special Form: check-type form type [string] |
|---|
| 379 |
This form verifies that FORM evaluates to a value of type TYPE. |
|---|
| 380 |
If so, it returns `nil'. If not, `check-type' signals a |
|---|
| 381 |
`wrong-type-argument' error. The default error message lists the |
|---|
| 382 |
erroneous value along with TYPE and FORM themselves. If STRING is |
|---|
| 383 |
specified, it is included in the error message in place of TYPE. |
|---|
| 384 |
For example: |
|---|
| 385 |
|
|---|
| 386 |
(check-type x (integer 1 *) "a positive integer") |
|---|
| 387 |
|
|---|
| 388 |
*Note Type Predicates::, for a description of the type specifiers |
|---|
| 389 |
that may be used for TYPE. |
|---|
| 390 |
|
|---|
| 391 |
Note that in Common Lisp, the first argument to `check-type' must |
|---|
| 392 |
be a PLACE suitable for use by `setf', because `check-type' |
|---|
| 393 |
signals a continuable error that allows the user to modify PLACE. |
|---|
| 394 |
|
|---|
| 395 |
The following error-related macro is also defined: |
|---|
| 396 |
|
|---|
| 397 |
- Special Form: ignore-errors forms... |
|---|
| 398 |
This executes FORMS exactly like a `progn', except that errors are |
|---|
| 399 |
ignored during the FORMS. More precisely, if an error is signaled |
|---|
| 400 |
then `ignore-errors' immediately aborts execution of the FORMS and |
|---|
| 401 |
returns `nil'. If the FORMS complete successfully, `ignore-errors' |
|---|
| 402 |
returns the result of the last FORM. |
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
File: cl, Node: Efficiency Concerns, Next: Common Lisp Compatibility, Prev: Assertions, Up: Top |
|---|
| 406 |
|
|---|
| 407 |
Efficiency Concerns |
|---|
| 408 |
******************* |
|---|
| 409 |
|
|---|
| 410 |
Macros |
|---|
| 411 |
====== |
|---|
| 412 |
|
|---|
| 413 |
Many of the advanced features of this package, such as `defun*', |
|---|
| 414 |
`loop', and `setf', are implemented as Lisp macros. In byte-compiled |
|---|
| 415 |
code, these complex notations will be expanded into equivalent Lisp |
|---|
| 416 |
code which is simple and efficient. For example, the forms |
|---|
| 417 |
|
|---|
| 418 |
(incf i n) |
|---|
| 419 |
(push x (car p)) |
|---|
| 420 |
|
|---|
| 421 |
are expanded at compile-time to the Lisp forms |
|---|
| 422 |
|
|---|
| 423 |
(setq i (+ i n)) |
|---|
| 424 |
(setcar p (cons x (car p))) |
|---|
| 425 |
|
|---|
| 426 |
which are the most efficient ways of doing these respective operations |
|---|
| 427 |
in Lisp. Thus, there is no performance penalty for using the more |
|---|
| 428 |
readable `incf' and `push' forms in your compiled code. |
|---|
| 429 |
|
|---|
| 430 |
_Interpreted_ code, on the other hand, must expand these macros |
|---|
| 431 |
every time they are executed. For this reason it is strongly |
|---|
| 432 |
recommended that code making heavy use of macros be compiled. (The |
|---|
| 433 |
features labeled "Special Form" instead of "Function" in this manual |
|---|
| 434 |
are macros.) A loop using `incf' a hundred times will execute |
|---|
| 435 |
considerably faster if compiled, and will also garbage-collect less |
|---|
| 436 |
because the macro expansion will not have to be generated, used, and |
|---|
| 437 |
thrown away a hundred times. |
|---|
| 438 |
|
|---|
| 439 |
You can find out how a macro expands by using the `cl-prettyexpand' |
|---|
| 440 |
function. |
|---|
| 441 |
|
|---|
| 442 |
- Function: cl-prettyexpand form &optional full |
|---|
| 443 |
This function takes a single Lisp form as an argument and inserts |
|---|
| 444 |
a nicely formatted copy of it in the current buffer (which must be |
|---|
| 445 |
in Lisp mode so that indentation works properly). It also expands |
|---|
| 446 |
all Lisp macros which appear in the form. The easiest way to use |
|---|
| 447 |
this function is to go to the `*scratch*' buffer and type, say, |
|---|
| 448 |
|
|---|
| 449 |
(cl-prettyexpand '(loop for x below 10 collect x)) |
|---|
| 450 |
|
|---|
| 451 |
and type `C-x C-e' immediately after the closing parenthesis; the |
|---|
| 452 |
expansion |
|---|
| 453 |
|
|---|
| 454 |
(block nil |
|---|
| 455 |
(let* ((x 0) |
|---|
| 456 |
(G1004 nil)) |
|---|
| 457 |
(while (< x 10) |
|---|
| 458 |
(setq G1004 (cons x G1004)) |
|---|
| 459 |
(setq x (+ x 1))) |
|---|
| 460 |
(nreverse G1004))) |
|---|
| 461 |
|
|---|
| 462 |
will be inserted into the buffer. (The `block' macro is expanded |
|---|
| 463 |
differently in the interpreter and compiler, so `cl-prettyexpand' |
|---|
| 464 |
just leaves it alone. The temporary variable `G1004' was created |
|---|
| 465 |
by `gensym'.) |
|---|
| 466 |
|
|---|
| 467 |
If the optional argument FULL is true, then _all_ macros are |
|---|
| 468 |
expanded, including `block', `eval-when', and compiler macros. |
|---|
| 469 |
Expansion is done as if FORM were a top-level form in a file being |
|---|
| 470 |
compiled. For example, |
|---|
| 471 |
|
|---|
| 472 |
(cl-prettyexpand '(pushnew 'x list)) |
|---|
| 473 |
-| (setq list (adjoin 'x list)) |
|---|
| 474 |
(cl-prettyexpand '(pushnew 'x list) t) |
|---|
| 475 |
-| (setq list (if (memq 'x list) list (cons 'x list))) |
|---|
| 476 |
(cl-prettyexpand '(caddr (member* 'a list)) t) |
|---|
| 477 |
-| (car (cdr (cdr (memq 'a list)))) |
|---|
| 478 |
|
|---|
| 479 |
Note that `adjoin', `caddr', and `member*' all have built-in |
|---|
| 480 |
compiler macros to optimize them in common cases. |
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
Error Checking |
|---|
| 484 |
============== |
|---|
| 485 |
|
|---|
| 486 |
Common Lisp compliance has in general not been sacrificed for the sake |
|---|
| 487 |
of efficiency. A few exceptions have been made for cases where |
|---|
| 488 |
substantial gains were possible at the expense of marginal |
|---|
| 489 |
incompatibility. |
|---|
| 490 |
|
|---|
| 491 |
The Common Lisp standard (as embodied in Steele's book) uses the |
|---|
| 492 |
phrase "it is an error if" to indicate a situation which is not |
|---|
| 493 |
supposed to arise in complying programs; implementations are strongly |
|---|
| 494 |
encouraged but not required to signal an error in these situations. |
|---|
| 495 |
This package sometimes omits such error checking in the interest of |
|---|
| 496 |
compactness and efficiency. For example, `do' variable specifiers are |
|---|
| 497 |
supposed to be lists of one, two, or three forms; extra forms are |
|---|
| 498 |
ignored by this package rather than signaling a syntax error. The |
|---|
| 499 |
`endp' function is simply a synonym for `null' in this package. |
|---|
| 500 |
Functions taking keyword arguments will accept an odd number of |
|---|
| 501 |
arguments, treating the trailing keyword as if it were followed by the |
|---|
| 502 |
value `nil'. |
|---|
| 503 |
|
|---|
| 504 |
Argument lists (as processed by `defun*' and friends) _are_ checked |
|---|
| 505 |
rigorously except for the minor point just mentioned; in particular, |
|---|
| 506 |
keyword arguments are checked for validity, and `&allow-other-keys' and |
|---|
| 507 |
`:allow-other-keys' are fully implemented. Keyword validity checking |
|---|
| 508 |
is slightly time consuming (though not too bad in byte-compiled code); |
|---|
| 509 |
you can use `&allow-other-keys' to omit this check. Functions defined |
|---|
| 510 |
in this package such as `find' and `member*' do check their keyword |
|---|
| 511 |
arguments for validity. |
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
Optimizing Compiler |
|---|
| 515 |
=================== |
|---|
| 516 |
|
|---|
| 517 |
Use of the optimizing Emacs compiler is highly recommended; many of the |
|---|
| 518 |
Common Lisp macros emit code which can be improved by optimization. In |
|---|
| 519 |
particular, `block's (whether explicit or implicit in constructs like |
|---|
| 520 |
`defun*' and `loop') carry a fair run-time penalty; the optimizing |
|---|
| 521 |
compiler removes `block's which are not actually referenced by `return' |
|---|
| 522 |
or `return-from' inside the block. |
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 |
File: cl, Node: Common Lisp Compatibility, Next: Old CL Compatibility, Prev: Efficiency Concerns, Up: Top |
|---|
| 526 |
|
|---|
| 527 |
Common Lisp Compatibility |
|---|
| 528 |
************************* |
|---|
| 529 |
|
|---|
| 530 |
Following is a list of all known incompatibilities between this package |
|---|
| 531 |
and Common Lisp as documented in Steele (2nd edition). |
|---|
| 532 |
|
|---|
| 533 |
Certain function names, such as `member', `assoc', and `floor', were |
|---|
| 534 |
already taken by (incompatible) Emacs Lisp functions; this package |
|---|
| 535 |
appends `*' to the names of its Common Lisp versions of these functions. |
|---|
| 536 |
|
|---|
| 537 |
The word `defun*' is required instead of `defun' in order to use |
|---|
| 538 |
extended Common Lisp argument lists in a function. Likewise, |
|---|
| 539 |
`defmacro*' and `function*' are versions of those forms which |
|---|
| 540 |
understand full-featured argument lists. The `&whole' keyword does not |
|---|
| 541 |
work in `defmacro' argument lists (except inside recursive argument |
|---|
| 542 |
lists). |
|---|
| 543 |
|
|---|
| 544 |
The `eql' and `equal' predicates do not distinguish between IEEE |
|---|
| 545 |
floating-point plus and minus zero. The `equalp' predicate has several |
|---|
| 546 |
differences with Common Lisp; *note Predicates::. |
|---|
| 547 |
|
|---|
| 548 |
The `setf' mechanism is entirely compatible, except that |
|---|
| 549 |
setf-methods return a list of five values rather than five values |
|---|
| 550 |
directly. Also, the new "`setf' function" concept (typified by `(defun |
|---|
| 551 |
(setf foo) ...)') is not implemented. |
|---|
| 552 |
|
|---|
| 553 |
The `do-all-symbols' form is the same as `do-symbols' with no |
|---|
| 554 |
OBARRAY argument. In Common Lisp, this form would iterate over all |
|---|
| 555 |
symbols in all packages. Since Emacs obarrays are not a first-class |
|---|
| 556 |
package mechanism, there is no way for `do-all-symbols' to locate any |
|---|
| 557 |
but the default obarray. |
|---|
| 558 |
|
|---|
| 559 |
The `loop' macro is complete except that `loop-finish' and type |
|---|
| 560 |
specifiers are unimplemented. |
|---|
| 561 |
|
|---|
| 562 |
The multiple-value return facility treats lists as multiple values, |
|---|
| 563 |
since Emacs Lisp cannot support multiple return values directly. The |
|---|
| 564 |
macros will be compatible with Common Lisp if `values' or `values-list' |
|---|
| 565 |
is always used to return to a `multiple-value-bind' or other |
|---|
| 566 |
multiple-value receiver; if `values' is used without |
|---|
| 567 |
`multiple-value-...' or vice-versa the effect will be different from |
|---|
| 568 |
Common Lisp. |
|---|
| 569 |
|
|---|
| 570 |
Many Common Lisp declarations are ignored, and others match the |
|---|
| 571 |
Common Lisp standard in concept but not in detail. For example, local |
|---|
| 572 |
`special' declarations, which are purely advisory in Emacs Lisp, do not |
|---|
| 573 |
rigorously obey the scoping rules set down in Steele's book. |
|---|
| 574 |
|
|---|
| 575 |
The variable `*gensym-counter*' starts out with a pseudo-random |
|---|
| 576 |
value rather than with zero. This is to cope with the fact that |
|---|
| 577 |
generated symbols become interned when they are written to and loaded |
|---|
| 578 |
back from a file. |
|---|
| 579 |
|
|---|
| 580 |
The `defstruct' facility is compatible, except that structures are |
|---|
| 581 |
of type `:type vector :named' by default rather than some special, |
|---|
| 582 |
distinct type. Also, the `:type' slot option is ignored. |
|---|
| 583 |
|
|---|
| 584 |
The second argument of `check-type' is treated differently. |
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
File: cl, Node: Old CL Compatibility, Next: Porting Common Lisp, Prev: Common Lisp Compatibility, Up: Top |
|---|
| 588 |
|
|---|
| 589 |
Old CL Compatibility |
|---|
| 590 |
******************** |
|---|
| 591 |
|
|---|
| 592 |
Following is a list of all known incompatibilities between this package |
|---|
| 593 |
and the older Quiroz `cl.el' package. |
|---|
| 594 |
|
|---|
| 595 |
This package's emulation of multiple return values in functions is |
|---|
| 596 |
incompatible with that of the older package. That package attempted to |
|---|
| 597 |
come as close as possible to true Common Lisp multiple return values; |
|---|
| 598 |
unfortunately, it could not be 100% reliable and so was prone to |
|---|
| 599 |
occasional surprises if used freely. This package uses a simpler |
|---|
| 600 |
method, namely replacing multiple values with lists of values, which is |
|---|
| 601 |
more predictable though more noticeably different from Common Lisp. |
|---|
| 602 |
|
|---|
| 603 |
The `defkeyword' form and `keywordp' function are not implemented in |
|---|
| 604 |
this package. |
|---|
| 605 |
|
|---|
| 606 |
The `member', `floor', `ceiling', `truncate', `round', `mod', and |
|---|
| 607 |
`rem' functions are suffixed by `*' in this package to avoid collision |
|---|
| 608 |
with existing functions in Emacs. The older package simply redefined |
|---|
| 609 |
these functions, overwriting the built-in meanings and causing serious |
|---|
| 610 |
portability problems. (Some more recent versions of the Quiroz package |
|---|
| 611 |
changed the names to `cl-member', etc.; this package defines the latter |
|---|
| 612 |
names as aliases for `member*', etc.) |
|---|
| 613 |
|
|---|
| 614 |
Certain functions in the old package which were buggy or inconsistent |
|---|
| 615 |
with the Common Lisp standard are incompatible with the conforming |
|---|
| 616 |
versions in this package. For example, `eql' and `member' were |
|---|
| 617 |
synonyms for `eq' and `memq' in that package, `setf' failed to preserve |
|---|
| 618 |
correct order of evaluation of its arguments, etc. |
|---|
| 619 |
|
|---|
| 620 |
Finally, unlike the older package, this package is careful to prefix |
|---|
| 621 |
all of its internal names with `cl-'. Except for a few functions which |
|---|
| 622 |
are explicitly defined as additional features (such as `floatp-safe' |
|---|
| 623 |
and `letf'), this package does not export any non-`cl-' symbols which |
|---|
| 624 |
are not also part of Common Lisp. |
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
The `cl-compat' package |
|---|
| 628 |
======================= |
|---|
| 629 |
|
|---|
| 630 |
The "CL" package includes emulations of some features of the old |
|---|
| 631 |
`cl.el', in the form of a compatibility package `cl-compat'. To use |
|---|
| 632 |
it, put `(require 'cl-compat)' in your program. |
|---|
| 633 |
|
|---|
| 634 |
The old package defined a number of internal routines without `cl-' |
|---|
| 635 |
prefixes or other annotations. Call to these routines may have crept |
|---|
| 636 |
into existing Lisp code. `cl-compat' provides emulations of the |
|---|
| 637 |
following internal routines: `pair-with-newsyms', `zip-lists', |
|---|
| 638 |
`unzip-lists', `reassemble-arglists', `duplicate-symbols-p', |
|---|
| 639 |
`safe-idiv'. |
|---|
| 640 |
|
|---|
| 641 |
Some `setf' forms translated into calls to internal functions that |
|---|
| 642 |
user code might call directly. The functions `setnth', `setnthcdr', |
|---|
| 643 |
and `setelt' fall in this category; they are defined by `cl-compat', |
|---|
| 644 |
but the best fix is to change to use `setf' properly. |
|---|
| 645 |
|
|---|
| 646 |
The `cl-compat' file defines the keyword functions `keywordp', |
|---|
| 647 |
`keyword-of', and `defkeyword', which are not defined by the new "CL" |
|---|
| 648 |
package because the use of keywords as data is discouraged. |
|---|
| 649 |
|
|---|
| 650 |
The `build-klist' mechanism for parsing keyword arguments is |
|---|
| 651 |
emulated by `cl-compat'; the `with-keyword-args' macro is not, however, |
|---|
| 652 |
and in any case it's best to change to use the more natural keyword |
|---|
| 653 |
argument processing offered by `defun*'. |
|---|
| 654 |
|
|---|
| 655 |
Multiple return values are treated differently by the two Common |
|---|
| 656 |
Lisp packages. The old package's method was more compatible with true |
|---|
| 657 |
Common Lisp, though it used heuristics that caused it to report |
|---|
| 658 |
spurious multiple return values in certain cases. The `cl-compat' |
|---|
| 659 |
package defines a set of multiple-value macros that are compatible with |
|---|
| 660 |
the old CL package; again, they are heuristic in nature, but they are |
|---|
| 661 |
guaranteed to work in any case where the old package's macros worked. |
|---|
| 662 |
To avoid name collision with the "official" multiple-value facilities, |
|---|
| 663 |
the ones in `cl-compat' have capitalized names: `Values', |
|---|
| 664 |
`Values-list', `Multiple-value-bind', etc. |
|---|
| 665 |
|
|---|
| 666 |
The functions `cl-floor', `cl-ceiling', `cl-truncate', and |
|---|
| 667 |
`cl-round' are defined by `cl-compat' to use the old-style |
|---|
| 668 |
multiple-value mechanism, just as they did in the old package. The |
|---|
| 669 |
newer `floor*' and friends return their two results in a list rather |
|---|
| 670 |
than as multiple values. Note that older versions of the old package |
|---|
| 671 |
used the unadorned names `floor', `ceiling', etc.; `cl-compat' cannot |
|---|
| 672 |
use these names because they conflict with Emacs built-ins. |
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
File: cl, Node: Porting Common Lisp, Next: Function Index, Prev: Old CL Compatibility, Up: Top |
|---|
| 676 |
|
|---|
| 677 |
Porting Common Lisp |
|---|
| 678 |
******************* |
|---|
| 679 |
|
|---|
| 680 |
This package is meant to be used as an extension to Emacs Lisp, not as |
|---|
| 681 |
an Emacs implementation of true Common Lisp. Some of the remaining |
|---|
| 682 |
differences between Emacs Lisp and Common Lisp make it difficult to |
|---|
| 683 |
port large Common Lisp applications to Emacs. For one, some of the |
|---|
| 684 |
features in this package are not fully compliant with ANSI or Steele; |
|---|
| 685 |
*note Common Lisp Compatibility::. But there are also quite a few |
|---|
| 686 |
features that this package does not provide at all. Here are some |
|---|
| 687 |
major omissions that you will want watch out for when bringing Common |
|---|
| 688 |
Lisp code into Emacs. |
|---|
| 689 |
|
|---|
| 690 |
* Case-insensitivity. Symbols in Common Lisp are case-insensitive |
|---|
| 691 |
by default. Some programs refer to a function or variable as |
|---|
| 692 |
`foo' in one place and `Foo' or `FOO' in another. Emacs Lisp will |
|---|
| 693 |
treat these as three distinct symbols. |
|---|
| 694 |
|
|---|
| 695 |
Some Common Lisp code is written entirely in upper case. While |
|---|
| 696 |
Emacs is happy to let the program's own functions and variables use |
|---|
| 697 |
this convention, calls to Lisp builtins like `if' and `defun' will |
|---|
| 698 |
have to be changed to lower case. |
|---|
| 699 |
|
|---|
| 700 |
* Lexical scoping. In Common Lisp, function arguments and `let' |
|---|
| 701 |
bindings apply only to references physically within their bodies |
|---|
| 702 |
(or within macro expansions in their bodies). Emacs Lisp, by |
|---|
| 703 |
contrast, uses "dynamic scoping" wherein a binding to a variable |
|---|
| 704 |
is visible even inside functions called from the body. |
|---|
| 705 |
|
|---|
| 706 |
Variables in Common Lisp can be made dynamically scoped by |
|---|
| 707 |
declaring them `special' or using `defvar'. In Emacs Lisp it is |
|---|
| 708 |
as if all variables were declared `special'. |
|---|
| 709 |
|
|---|
| 710 |
Often you can use code that was written for lexical scoping even |
|---|
| 711 |
in a dynamically scoped Lisp, but not always. Here is an example |
|---|
| 712 |
of a Common Lisp code fragment that would fail in Emacs Lisp: |
|---|
| 713 |
|
|---|
| 714 |
(defun map-odd-elements (func list) |
|---|
| 715 |
(loop for x in list |
|---|
| 716 |
for flag = t then (not flag) |
|---|
| 717 |
collect (if flag x (funcall func x)))) |
|---|
| 718 |
|
|---|
| 719 |
(defun add-odd-elements (list x) |
|---|
| 720 |
(map-odd-elements (lambda (a) (+ a x))) list) |
|---|
| 721 |
|
|---|
| 722 |
In Common Lisp, the two functions' usages of `x' are completely |
|---|
| 723 |
independent. In Emacs Lisp, the binding to `x' made by |
|---|
| 724 |
`add-odd-elements' will have been hidden by the binding in |
|---|
| 725 |
`map-odd-elements' by the time the `(+ a x)' function is called. |
|---|
| 726 |
|
|---|
| 727 |
(This package avoids such problems in its own mapping functions by |
|---|
| 728 |
using names like `cl-x' instead of `x' internally; as long as you |
|---|
| 729 |
don't use the `cl-' prefix for your own variables no collision can |
|---|
| 730 |
occur.) |
|---|
| 731 |
|
|---|
| 732 |
*Note Lexical Bindings::, for a description of the `lexical-let' |
|---|
| 733 |
form which establishes a Common Lisp-style lexical binding, and |
|---|
| 734 |
some examples of how it differs from Emacs' regular `let'. |
|---|
| 735 |
|
|---|
| 736 |
* Reader macros. Common Lisp includes a second type of macro that |
|---|
| 737 |
works at the level of individual characters. For example, Common |
|---|
| 738 |
Lisp implements the quote notation by a reader macro called `'', |
|---|
| 739 |
whereas Emacs Lisp's parser just treats quote as a special case. |
|---|
| 740 |
Some Lisp packages use reader macros to create special syntaxes |
|---|
| 741 |
for themselves, which the Emacs parser is incapable of reading. |
|---|
| 742 |
|
|---|
| 743 |
The lack of reader macros, incidentally, is the reason behind |
|---|
| 744 |
Emacs Lisp's unusual backquote syntax. Since backquotes are |
|---|
| 745 |
implemented as a Lisp package and not built-in to the Emacs |
|---|
| 746 |
parser, they are forced to use a regular macro named ``' which is |
|---|
| 747 |
used with the standard function/macro call notation. |
|---|
| 748 |
|
|---|
| 749 |
* Other syntactic features. Common Lisp provides a number of |
|---|
| 750 |
notations beginning with `#' that the Emacs Lisp parser won't |
|---|
| 751 |
understand. For example, `#| ... |#' is an alternate comment |
|---|
| 752 |
notation, and `#+lucid (foo)' tells the parser to ignore the |
|---|
| 753 |
`(foo)' except in Lucid Common Lisp. |
|---|
| 754 |
|
|---|
| 755 |
* Packages. In Common Lisp, symbols are divided into "packages". |
|---|
| 756 |
Symbols that are Lisp built-ins are typically stored in one |
|---|
| 757 |
package; symbols that are vendor extensions are put in another, |
|---|
| 758 |
and each application program would have a package for its own |
|---|
| 759 |
symbols. Certain symbols are "exported" by a package and others |
|---|
| 760 |
are internal; certain packages "use" or import the exported symbols |
|---|
| 761 |
of other packages. To access symbols that would not normally be |
|---|
| 762 |
visible due to this importing and exporting, Common Lisp provides |
|---|
| 763 |
a syntax like `package:symbol' or `package::symbol'. |
|---|
| 764 |
|
|---|
| 765 |
Emacs Lisp has a single namespace for all interned symbols, and |
|---|
| 766 |
then uses a naming convention of putting a prefix like `cl-' in |
|---|
| 767 |
front of the name. Some Emacs packages adopt the Common Lisp-like |
|---|
| 768 |
convention of using `cl:' or `cl::' as the prefix. However, the |
|---|
| 769 |
Emacs parser does not understand colons and just treats them as |
|---|
| 770 |
part of the symbol name. Thus, while `mapcar' and `lisp:mapcar' |
|---|
| 771 |
may refer to the same symbol in Common Lisp, they are totally |
|---|
| 772 |
distinct in Emacs Lisp. Common Lisp programs which refer to a |
|---|
| 773 |
symbol by the full name sometimes and the short name other times |
|---|
| 774 |
will not port cleanly to Emacs. |
|---|
| 775 |
|
|---|
| 776 |
Emacs Lisp does have a concept of "obarrays," which are |
|---|
| 777 |
package-like collections of symbols, but this feature is not |
|---|
| 778 |
strong enough to be used as a true package mechanism. |
|---|
| 779 |
|
|---|
| 780 |
* The `format' function is quite different between Common Lisp and |
|---|
| 781 |
Emacs Lisp. It takes an additional "destination" argument before |
|---|
| 782 |
the format string. A destination of `nil' means to format to a |
|---|
| 783 |
string as in Emacs Lisp; a destination of `t' means to write to |
|---|
| 784 |
the terminal (similar to `message' in Emacs). Also, format |
|---|
| 785 |
control strings are utterly different; `~' is used instead of `%' |
|---|
| 786 |
to introduce format codes, and the set of available codes is much |
|---|
| 787 |
richer. There are no notations like `\n' for string literals; |
|---|
| 788 |
instead, `format' is used with the "newline" format code, `~%'. |
|---|
| 789 |
More advanced formatting codes provide such features as paragraph |
|---|
| 790 |
filling, case conversion, and even loops and conditionals. |
|---|
| 791 |
|
|---|
| 792 |
While it would have been possible to implement most of Common Lisp |
|---|
| 793 |
`format' in this package (under the name `format*', of course), it |
|---|
| 794 |
was not deemed worthwhile. It would have required a huge amount |
|---|
| 795 |
of code to implement even a decent subset of `format*', yet the |
|---|
| 796 |
functionality it would provide over Emacs Lisp's `format' would |
|---|
| 797 |
rarely be useful. |
|---|
| 798 |
|
|---|
| 799 |
* Vector constants use square brackets in Emacs Lisp, but `#(a b c)' |
|---|
| 800 |
notation in Common Lisp. To further complicate matters, Emacs has |
|---|
| 801 |
its own `#(' notation for something entirely different--strings |
|---|
| 802 |
with properties. |
|---|
| 803 |
|
|---|
| 804 |
* Characters are distinct from integers in Common Lisp. The |
|---|
| 805 |
notation for character constants is also different: `#\A' instead |
|---|
| 806 |
of `?A'. Also, `string=' and `string-equal' are synonyms in Emacs |
|---|
| 807 |
Lisp whereas the latter is case-insensitive in Common Lisp. |
|---|
| 808 |
|
|---|
| 809 |
* Data types. Some Common Lisp data types do not exist in Emacs |
|---|
| 810 |
Lisp. Rational numbers and complex numbers are not present, nor |
|---|
| 811 |
are large integers (all integers are "fixnums"). All arrays are |
|---|
| 812 |
one-dimensional. There are no readtables or pathnames; streams |
|---|
| 813 |
are a set of existing data types rather than a new data type of |
|---|
| 814 |
their own. Hash tables, random-states, structures, and packages |
|---|
| 815 |
(obarrays) are built from Lisp vectors or lists rather than being |
|---|
| 816 |
distinct types. |
|---|
| 817 |
|
|---|
| 818 |
* The Common Lisp Object System (CLOS) is not implemented, nor is |
|---|
| 819 |
the Common Lisp Condition System. However, the EIEIO package from |
|---|
| 820 |
`ftp://ftp.ultranet.com/pub/zappo' does implement some CLOS |
|---|
| 821 |
functionality. |
|---|
| 822 |
|
|---|
| 823 |
* Common Lisp features that are completely redundant with Emacs Lisp |
|---|
| 824 |
features of a different name generally have not been implemented. |
|---|
| 825 |
For example, Common Lisp writes `defconstant' where Emacs Lisp |
|---|
| 826 |
uses `defconst'. Similarly, `make-list' takes its arguments in |
|---|
| 827 |
different ways in the two Lisps but does exactly the same thing, |
|---|
| 828 |
so this package has not bothered to implement a Common Lisp-style |
|---|
| 829 |
`make-list'. |
|---|
| 830 |
|
|---|
| 831 |
* A few more notable Common Lisp features not included in this |
|---|
| 832 |
package: `compiler-let', `tagbody', `prog', `ldb/dpb', |
|---|
| 833 |
`parse-integer', `cerror'. |
|---|
| 834 |
|
|---|
| 835 |
* Recursion. While recursion works in Emacs Lisp just like it does |
|---|
| 836 |
in Common Lisp, various details of the Emacs Lisp system and |
|---|
| 837 |
compiler make recursion much less efficient than it is in most |
|---|
| 838 |
Lisps. Some schools of thought prefer to use recursion in Lisp |
|---|
| 839 |
over other techniques; they would sum a list of numbers using |
|---|
| 840 |
something like |
|---|
| 841 |
|
|---|
| 842 |
(defun sum-list (list) |
|---|
| 843 |
(if list |
|---|
| 844 |
(+ (car list) (sum-list (cdr list))) |
|---|
| 845 |
0)) |
|---|
| 846 |
|
|---|
| 847 |
where a more iteratively-minded programmer might write one of |
|---|
| 848 |
these forms: |
|---|
| 849 |
|
|---|
| 850 |
(let ((total 0)) (dolist (x my-list) (incf total x)) total) |
|---|
| 851 |
(loop for x in my-list sum x) |
|---|
| 852 |
|
|---|
| 853 |
While this would be mainly a stylistic choice in most Common Lisps, |
|---|
| 854 |
in Emacs Lisp you should be aware that the iterative forms are |
|---|
| 855 |
much faster than recursion. Also, Lisp programmers will want to |
|---|
| 856 |
note that the current Emacs Lisp compiler does not optimize tail |
|---|
| 857 |
recursion. |
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
File: cl, Node: Function Index, Next: Variable Index, Prev: Porting Common Lisp, Up: Top |
|---|
| 861 |
|
|---|
| 862 |
Function Index |
|---|
| 863 |
************** |
|---|
| 864 |
|
|---|
| 865 |
* Menu: |
|---|
| 866 |
|
|---|
| 867 |
* acons: Association Lists. |
|---|
| 868 |
* adjoin: Lists as Sets. |
|---|
| 869 |
* assert: Assertions. |
|---|
| 870 |
* assoc*: Association Lists. |
|---|
| 871 |
* assoc-if: Association Lists. |
|---|
| 872 |
* assoc-if-not: Association Lists. |
|---|
| 873 |
* block: Blocks and Exits. |
|---|
| 874 |
* caddr: List Functions. |
|---|
| 875 |
* callf: Modify Macros. |
|---|
| 876 |
* callf2: Modify Macros. |
|---|
| 877 |
* case: Conditionals. |
|---|
| 878 |
* ceiling*: Numerical Functions. |
|---|
| 879 |
* check-type: Assertions. |
|---|
| 880 |
* cl-float-limits: Implementation Parameters. |
|---|
| 881 |
* cl-prettyexpand: Efficiency Concerns. |
|---|
| 882 |
* coerce: Type Predicates. |
|---|
| 883 |
* compiler-macroexpand: Macros. |
|---|
| 884 |
* concatenate: Sequence Functions. |
|---|
| 885 |
* copy-list: List Functions. |
|---|
| 886 |
* copy-tree: List Functions. |
|---|
| 887 |
* count: Searching Sequences. |
|---|
| 888 |
* count-if: Searching Sequences. |
|---|
| 889 |
* count-if-not: Searching Sequences. |
|---|
| 890 |
* decf: Modify Macros. |
|---|
| 891 |
* declaim: Declarations. |
|---|
| 892 |
* declare: Declarations. |
|---|
| 893 |
* define-compiler-macro: Macros. |
|---|
| 894 |
* define-modify-macro: Customizing Setf. |
|---|
| 895 |
* define-setf-method: Customizing Setf. |
|---|
| 896 |
* defmacro*: Argument Lists. |
|---|
| 897 |
* defsetf: Customizing Setf. |
|---|
| 898 |
* defstruct: Structures. |
|---|
| 899 |
* defsubst*: Argument Lists. |
|---|
| 900 |
* deftype: Type Predicates. |
|---|
| 901 |
* defun*: Argument Lists. |
|---|
| 902 |
* delete*: Sequence Functions. |
|---|
| 903 |
* delete-duplicates: Sequence Functions. |
|---|
| 904 |
* delete-if: Sequence Functions. |
|---|
| 905 |
* delete-if-not: Sequence Functions. |
|---|
| 906 |
* destructuring-bind: Macros. |
|---|
| 907 |
* do: Iteration. |
|---|
| 908 |
* do*: Iteration. |
|---|
| 909 |
* do-all-symbols: Iteration. |
|---|
| 910 |
* do-symbols: Iteration. |
|---|
| 911 |
* dolist: Iteration. |
|---|
| 912 |
* dotimes: Iteration. |
|---|
| 913 |
* ecase: Conditionals. |
|---|
| 914 |
* endp: List Functions. |
|---|
| 915 |
* eql: Equality Predicates. |
|---|
| 916 |
* equalp: Equality Predicates. |
|---|
| 917 |
* etypecase: Conditionals. |
|---|
| 918 |
* eval-when: Time of Evaluation. |
|---|
| 919 |
* eval-when-compile: Time of Evaluation. |
|---|
| 920 |
* evenp: Predicates on Numbers. |
|---|
| 921 |
* every: Mapping over Sequences. |
|---|
| 922 |
* fill: Sequence Functions. |
|---|
| 923 |
* find: Searching Sequences. |
|---|
| 924 |
* find-if: Searching Sequences. |
|---|
| 925 |
* find-if-not: Searching Sequences. |
|---|
| 926 |
* first: List Functions. |
|---|
| 927 |
* flet: Function Bindings. |
|---|
| 928 |
* floatp-safe: Predicates on Numbers. |
|---|
| 929 |
* floor*: Numerical Functions. |
|---|
| 930 |
* function*: Argument Lists. |
|---|
| 931 |
* gcd: Numerical Functions. |
|---|
| 932 |
* gensym: Creating Symbols. |
|---|
| 933 |
* gentemp: Creating Symbols. |
|---|
| 934 |
* get*: Property Lists. |
|---|
| 935 |
* get-setf-method: Customizing Setf. |
|---|
| 936 |
* getf: Property Lists. |
|---|
| 937 |
* ignore-errors: Assertions. |
|---|
| 938 |
* incf: Modify Macros. |
|---|
| 939 |
* intersection: Lists as Sets. |
|---|
| 940 |
* isqrt: Numerical Functions. |
|---|
| 941 |
* labels: Function Bindings. |
|---|
| 942 |
* lcm: Numerical Functions. |
|---|
| 943 |
* ldiff: List Functions. |
|---|
| 944 |
* letf: Modify Macros. |
|---|
| 945 |
* letf*: Modify Macros. |
|---|
| 946 |
* lexical-let: Lexical Bindings. |
|---|
| 947 |
* lexical-let*: Lexical Bindings. |
|---|
| 948 |
* list*: List Functions. |
|---|
| 949 |
* list-length: List Functions. |
|---|
| 950 |
* load-time-value: Time of Evaluation. |
|---|
| 951 |
* locally: Declarations. |
|---|
| 952 |
* loop <1>: Loop Basics. |
|---|
| 953 |
* loop: Iteration. |
|---|
| 954 |
* macrolet: Macro Bindings. |
|---|
| 955 |
* make-random-state: Random Numbers. |
|---|
| 956 |
* map: Mapping over Sequences. |
|---|
| 957 |
* mapc: Mapping over Sequences. |
|---|
| 958 |
* mapcan: Mapping over Sequences. |
|---|
| 959 |
* mapcar*: Mapping over Sequences. |
|---|
| 960 |
* mapcon: Mapping over Sequences. |
|---|
| 961 |
* mapl: Mapping over Sequences. |
|---|
| 962 |
* maplist: Mapping over Sequences. |
|---|
| 963 |
* member*: Lists as Sets. |
|---|
| 964 |
* member-if: &nb |
|---|