Mercurial > emacs
annotate lisp/=cl.el @ 1382:9b210c7d6c23
* rmail.el (rmail-mode): Make this autoload; we might find a file
whose first line local variables want to put it in RMAIL mode;
that ought to work.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sun, 11 Oct 1992 06:35:04 +0000 |
parents | 2619b7a9c11e |
children | 6b409871cc4a |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; cl.el --- Common-Lisp extensions for GNU Emacs Lisp. |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
3 ;; Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
4 |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
5 ;; Author: Cesar Quiroz <quiroz@cs.rochester.edu> |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
6 ;; Keywords: extensions |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
7 |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
8 (defvar cl-version "2.0 beta 29 October 1989") |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
9 |
188 | 10 ;; This file is part of GNU Emacs. |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
14 ;; accepts responsibility to anyone for the consequences of using it | |
15 ;; or for whether it serves any particular purpose or works at all, | |
16 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
17 ;; License for full details. | |
18 | |
19 ;; Everyone is granted permission to copy, modify and redistribute | |
20 ;; GNU Emacs, but only under the conditions described in the | |
21 ;; GNU Emacs General Public License. A copy of this license is | |
22 ;; supposed to have been given to you along with GNU Emacs so you | |
23 ;; can know your rights and responsibilities. It should be in a | |
24 ;; file named COPYING. Among other things, the copyright notice | |
25 ;; and this notice must be preserved on all copies. | |
26 | |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
27 ;;; Commentary: |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
28 |
188 | 29 ;;;; These are extensions to Emacs Lisp that provide some form of |
30 ;;;; Common Lisp compatibility, beyond what is already built-in | |
31 ;;;; in Emacs Lisp. | |
32 ;;;; | |
33 ;;;; When developing them, I had the code spread among several files. | |
34 ;;;; This file 'cl.el' is a concatenation of those original files, | |
35 ;;;; minus some declarations that became redundant. The marks between | |
36 ;;;; the original files can be found easily, as they are lines that | |
37 ;;;; begin with four semicolons (as this does). The names of the | |
38 ;;;; original parts follow the four semicolons in uppercase, those | |
39 ;;;; names are GLOBAL, SYMBOLS, LISTS, SEQUENCES, CONDITIONALS, | |
40 ;;;; ITERATIONS, MULTIPLE VALUES, ARITH, SETF and DEFSTRUCT. If you | |
41 ;;;; add functions to this file, you might want to put them in a place | |
42 ;;;; that is compatible with the division above (or invent your own | |
43 ;;;; categories). | |
44 ;;;; | |
45 ;;;; To compile this file, make sure you load it first. This is | |
46 ;;;; because many things are implemented as macros and now that all | |
47 ;;;; the files are concatenated together one cannot ensure that | |
48 ;;;; declaration always precedes use. | |
49 ;;;; | |
50 | |
51 ;;;; GLOBAL | |
52 ;;;; This file provides utilities and declarations that are global | |
53 ;;;; to Common Lisp and so might be used by more than one of the | |
54 ;;;; other libraries. Especially, I intend to keep here some | |
55 ;;;; utilities that help parsing/destructuring some difficult calls. | |
56 ;;;; | |
57 ;;;; | |
58 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
59 ;;;; (quiroz@cs.rochester.edu) | |
60 | |
61 ;;; Too many pieces of the rest of this package use psetq. So it is unwise to | |
62 ;;; use here anything but plain Emacs Lisp! There is a neater recursive form | |
63 ;;; for the algorithm that deals with the bodies. | |
64 | |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
65 ;;; Code: |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
66 |
188 | 67 (defmacro psetq (&rest body) |
68 "(psetq {var value }...) => nil | |
69 Like setq, but all the values are computed before any assignment is made." | |
70 (let ((length (length body))) | |
71 (cond ((/= (% length 2) 0) | |
72 (error "psetq needs an even number of arguments, %d given" | |
73 length)) | |
74 ((null body) | |
75 '()) | |
76 (t | |
77 (list 'prog1 nil | |
78 (let ((setqs '()) | |
79 (bodyforms (reverse body))) | |
80 (while bodyforms | |
81 (let* ((value (car bodyforms)) | |
82 (place (cadr bodyforms))) | |
83 (setq bodyforms (cddr bodyforms)) | |
84 (if (null setqs) | |
85 (setq setqs (list 'setq place value)) | |
86 (setq setqs (list 'setq place | |
87 (list 'prog1 value | |
88 setqs)))))) | |
89 setqs)))))) | |
90 | |
91 ;;; utilities | |
92 ;;; | |
93 ;;; pair-with-newsyms takes a list and returns a list of lists of the | |
94 ;;; form (newsym form), such that a let* can then bind the evaluation | |
95 ;;; of the forms to the newsyms. The idea is to guarantee correct | |
96 ;;; order of evaluation of the subforms of a setf. It also returns a | |
97 ;;; list of the newsyms generated, in the corresponding order. | |
98 | |
99 (defun pair-with-newsyms (oldforms) | |
100 "PAIR-WITH-NEWSYMS OLDFORMS | |
101 The top-level components of the list oldforms are paired with fresh | |
102 symbols, the pairings list and the newsyms list are returned." | |
103 (do ((ptr oldforms (cdr ptr)) | |
104 (bindings '()) | |
105 (newsyms '())) | |
106 ((endp ptr) (values (nreverse bindings) (nreverse newsyms))) | |
107 (let ((newsym (gentemp))) | |
108 (setq bindings (cons (list newsym (car ptr)) bindings)) | |
109 (setq newsyms (cons newsym newsyms))))) | |
110 | |
111 (defun zip-lists (evens odds) | |
112 "Merge two lists EVENS and ODDS, taking elts from each list alternatingly. | |
113 EVENS and ODDS are two lists. ZIP-LISTS constructs a new list, whose | |
114 even numbered elements (0,2,...) come from EVENS and whose odd numbered | |
115 elements (1,3,...) come from ODDS. | |
116 The construction stops when the shorter list is exhausted." | |
117 (do* ((p0 evens (cdr p0)) | |
118 (p1 odds (cdr p1)) | |
119 (even (car p0) (car p0)) | |
120 (odd (car p1) (car p1)) | |
121 (result '())) | |
122 ((or (endp p0) (endp p1)) | |
123 (nreverse result)) | |
124 (setq result | |
125 (cons odd (cons even result))))) | |
126 | |
127 (defun unzip-list (list) | |
128 "Extract even and odd elements of LIST into two separate lists. | |
129 The argument LIST is separated in two strands, the even and the odd | |
130 numbered elements. Numbering starts with 0, so the first element | |
131 belongs in EVENS. No check is made that there is an even number of | |
132 elements to start with." | |
133 (do* ((ptr list (cddr ptr)) | |
134 (this (car ptr) (car ptr)) | |
135 (next (cadr ptr) (cadr ptr)) | |
136 (evens '()) | |
137 (odds '())) | |
138 ((endp ptr) | |
139 (values (nreverse evens) (nreverse odds))) | |
140 (setq evens (cons this evens)) | |
141 (setq odds (cons next odds)))) | |
142 | |
143 (defun reassemble-argslists (argslists) | |
144 "(reassemble-argslists ARGSLISTS) => a list of lists | |
145 ARGSLISTS is a list of sequences. Return a list of lists, the first | |
146 sublist being all the entries coming from ELT 0 of the original | |
147 sublists, the next those coming from ELT 1 and so on, until the | |
148 shortest list is exhausted." | |
149 (let* ((minlen (apply 'min (mapcar 'length argslists))) | |
150 (result '())) | |
151 (dotimes (i minlen (nreverse result)) | |
152 ;; capture all the elements at index i | |
153 (setq result | |
154 (cons (mapcar (function (lambda (sublist) (elt sublist i))) | |
155 argslists) | |
156 result))))) | |
157 | |
158 | |
159 ;;; Checking that a list of symbols contains no duplicates is a common | |
160 ;;; task when checking the legality of some macros. The check for 'eq | |
161 ;;; pairs can be too expensive, as it is quadratic on the length of | |
162 ;;; the list. I use a 4-pass, linear, counting approach. It surely | |
163 ;;; loses on small lists (less than 5 elements?), but should win for | |
164 ;;; larger lists. The fourth pass could be eliminated. | |
165 ;;; 10 dec 1986. Emacs Lisp has no REMPROP, so I just eliminated the | |
166 ;;; 4th pass. | |
167 (defun duplicate-symbols-p (list) | |
168 "Find all symbols appearing more than once in LIST. | |
169 Return a list of all such duplicates; nil if there are no duplicates." | |
170 (let ((duplicates '()) ;result built here | |
171 (propname (gensym)) ;we use a fresh property | |
172 ) | |
173 ;; check validity | |
174 (unless (and (listp list) | |
175 (every 'symbolp list)) | |
176 (error "a list of symbols is needed")) | |
177 ;; pass 1: mark | |
178 (dolist (x list) | |
179 (put x propname 0)) | |
180 ;; pass 2: count | |
181 (dolist (x list) | |
182 (put x propname (1+ (get x propname)))) | |
183 ;; pass 3: collect | |
184 (dolist (x list) | |
185 (if (> (get x propname) 1) | |
186 (setq duplicates (cons x duplicates)))) | |
187 ;; pass 4: unmark. eliminated. | |
188 ;; (dolist (x list) (remprop x propname)) | |
189 ;; return result | |
190 duplicates)) | |
191 | |
192 ;;;; end of cl-global.el | |
193 | |
194 ;;;; SYMBOLS | |
195 ;;;; This file provides the gentemp function, which generates fresh | |
196 ;;;; symbols, plus some other minor Common Lisp symbol tools. | |
197 ;;;; | |
198 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
199 ;;;; (quiroz@cs.rochester.edu) | |
200 | |
201 ;;; Keywords. There are no packages in Emacs Lisp, so this is only a | |
202 ;;; kludge around to let things be "as if" a keyword package was around. | |
203 | |
204 (defmacro defkeyword (x &optional docstring) | |
205 "Make symbol X a keyword (symbol whose value is itself). | |
206 Optional second arg DOCSTRING is a documentation string for it." | |
207 (cond ((symbolp x) | |
208 (list 'defconst x (list 'quote x) docstring)) | |
209 (t | |
210 (error "`%s' is not a symbol" (prin1-to-string x))))) | |
211 | |
212 (defun keywordp (sym) | |
213 "Return t if SYM is a keyword." | |
214 (if (and (symbolp sym) (char-equal (aref (symbol-name sym) 0) ?\:)) | |
215 ;; looks like one, make sure value is right | |
216 (set sym sym) | |
217 nil)) | |
218 | |
219 (defun keyword-of (sym) | |
220 "Return a keyword that is naturally associated with symbol SYM. | |
221 If SYM is keyword, the value is SYM. | |
222 Otherwise it is a keyword whose name is `:' followed by SYM's name." | |
223 (cond ((keywordp sym) | |
224 sym) | |
225 ((symbolp sym) | |
226 (let ((newsym (intern (concat ":" (symbol-name sym))))) | |
227 (set newsym newsym))) | |
228 (t | |
229 (error "expected a symbol, not `%s'" (prin1-to-string sym))))) | |
230 | |
231 ;;; Temporary symbols. | |
232 ;;; | |
233 | |
234 (defvar *gentemp-index* 0 | |
235 "Integer used by `gentemp' to produce new names.") | |
236 | |
237 (defvar *gentemp-prefix* "T$$_" | |
238 "Names generated by `gentemp begin' with this string by default.") | |
239 | |
240 (defun gentemp (&optional prefix oblist) | |
241 "Generate a fresh interned symbol. | |
242 There are two optional arguments, PREFIX and OBLIST. PREFIX is the string | |
243 that begins the new name, OBLIST is the obarray used to search for old | |
244 names. The defaults are just right, YOU SHOULD NEVER NEED THESE ARGUMENTS | |
245 IN YOUR OWN CODE." | |
246 (if (null prefix) | |
247 (setq prefix *gentemp-prefix*)) | |
248 (if (null oblist) | |
249 (setq oblist obarray)) ;default for the intern functions | |
250 (let ((newsymbol nil) | |
251 (newname)) | |
252 (while (not newsymbol) | |
253 (setq newname (concat prefix *gentemp-index*)) | |
254 (setq *gentemp-index* (+ *gentemp-index* 1)) | |
255 (if (not (intern-soft newname oblist)) | |
256 (setq newsymbol (intern newname oblist)))) | |
257 newsymbol)) | |
258 | |
259 (defvar *gensym-index* 0 | |
260 "Integer used by `gensym' to produce new names.") | |
261 | |
262 (defvar *gensym-prefix* "G$$_" | |
263 "Names generated by `gensym' begin with this string by default.") | |
264 | |
265 (defun gensym (&optional prefix) | |
266 "Generate a fresh uninterned symbol. | |
267 Optional arg PREFIX is the string that begins the new name. Most people | |
268 take just the default, except when debugging needs suggest otherwise." | |
269 (if (null prefix) | |
270 (setq prefix *gensym-prefix*)) | |
271 (let ((newsymbol nil) | |
272 (newname "")) | |
273 (while (not newsymbol) | |
274 (setq newname (concat prefix *gensym-index*)) | |
275 (setq *gensym-index* (+ *gensym-index* 1)) | |
276 (if (not (intern-soft newname)) | |
277 (setq newsymbol (make-symbol newname)))) | |
278 newsymbol)) | |
279 | |
280 ;;;; end of cl-symbols.el | |
281 | |
282 ;;;; CONDITIONALS | |
283 ;;;; This file provides some of the conditional constructs of | |
284 ;;;; Common Lisp. Total compatibility is again impossible, as the | |
285 ;;;; 'if' form is different in both languages, so only a good | |
286 ;;;; approximation is desired. | |
287 ;;;; | |
288 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
289 ;;;; (quiroz@cs.rochester.edu) | |
290 | |
291 ;;; indentation info | |
292 (put 'case 'lisp-indent-function 1) | |
293 (put 'ecase 'lisp-indent-function 1) | |
294 (put 'when 'lisp-indent-function 1) | |
295 (put 'unless 'lisp-indent-function 1) | |
296 | |
297 ;;; WHEN and UNLESS | |
298 ;;; These two forms are simplified ifs, with a single branch. | |
299 | |
300 (defmacro when (condition &rest body) | |
301 "(when CONDITION . BODY) => evaluate BODY if CONDITION is true." | |
302 (list* 'if (list 'not condition) '() body)) | |
303 | |
304 (defmacro unless (condition &rest body) | |
305 "(unless CONDITION . BODY) => evaluate BODY if CONDITION is false." | |
306 (list* 'if condition '() body)) | |
307 | |
308 ;;; CASE and ECASE | |
309 ;;; CASE selects among several clauses, based on the value (evaluated) | |
310 ;;; of a expression and a list of (unevaluated) key values. ECASE is | |
311 ;;; the same, but signals an error if no clause is activated. | |
312 | |
313 (defmacro case (expr &rest cases) | |
314 "(case EXPR . CASES) => evals EXPR, chooses from CASES on that value. | |
315 EXPR -> any form | |
316 CASES -> list of clauses, non empty | |
317 CLAUSE -> HEAD . BODY | |
318 HEAD -> t = catch all, must be last clause | |
319 -> otherwise = same as t | |
320 -> nil = illegal | |
321 -> atom = activated if (eql EXPR HEAD) | |
322 -> list of atoms = activated if (memq EXPR HEAD) | |
323 BODY -> list of forms, implicit PROGN is built around it. | |
324 EXPR is evaluated only once." | |
325 (let* ((newsym (gentemp)) | |
326 (clauses (case-clausify cases newsym))) | |
327 ;; convert case into a cond inside a let | |
328 (list 'let | |
329 (list (list newsym expr)) | |
330 (list* 'cond (nreverse clauses))))) | |
331 | |
332 (defmacro ecase (expr &rest cases) | |
333 "(ecase EXPR . CASES) => like `case', but error if no case fits. | |
334 `t'-clauses are not allowed." | |
335 (let* ((newsym (gentemp)) | |
336 (clauses (case-clausify cases newsym))) | |
337 ;; check that no 't clause is present. | |
338 ;; case-clausify would put one such at the beginning of clauses | |
339 (if (eq (caar clauses) t) | |
340 (error "no clause-head should be `t' or `otherwise' for `ecase'")) | |
341 ;; insert error-catching clause | |
342 (setq clauses | |
343 (cons | |
344 (list 't (list 'error | |
345 "ecase on %s = %s failed to take any branch" | |
346 (list 'quote expr) | |
347 (list 'prin1-to-string newsym))) | |
348 clauses)) | |
349 ;; generate code as usual | |
350 (list 'let | |
351 (list (list newsym expr)) | |
352 (list* 'cond (nreverse clauses))))) | |
353 | |
354 | |
355 (defun case-clausify (cases newsym) | |
356 "CASE-CLAUSIFY CASES NEWSYM => clauses for a 'cond' | |
357 Converts the CASES of a [e]case macro into cond clauses to be | |
358 evaluated inside a let that binds NEWSYM. Returns the clauses in | |
359 reverse order." | |
360 (do* ((currentpos cases (cdr currentpos)) | |
361 (nextpos (cdr cases) (cdr nextpos)) | |
362 (curclause (car cases) (car currentpos)) | |
363 (result '())) | |
364 ((endp currentpos) result) | |
365 (let ((head (car curclause)) | |
366 (body (cdr curclause))) | |
367 ;; construct a cond-clause according to the head | |
368 (cond ((null head) | |
369 (error "case clauses cannot have null heads: `%s'" | |
370 (prin1-to-string curclause))) | |
371 ((or (eq head 't) | |
372 (eq head 'otherwise)) | |
373 ;; check it is the last clause | |
374 (if (not (endp nextpos)) | |
375 (error "clause with `t' or `otherwise' head must be last")) | |
376 ;; accept this clause as a 't' for cond | |
377 (setq result (cons (cons 't body) result))) | |
378 ((atom head) | |
379 (setq result | |
380 (cons (cons (list 'eql newsym (list 'quote head)) body) | |
381 result))) | |
382 ((listp head) | |
383 (setq result | |
384 (cons (cons (list 'memq newsym (list 'quote head)) body) | |
385 result))) | |
386 (t | |
387 ;; catch-all for this parser | |
388 (error "don't know how to parse case clause `%s'" | |
389 (prin1-to-string head))))))) | |
390 | |
391 ;;;; end of cl-conditionals.el | |
392 | |
393 ;;;; ITERATIONS | |
394 ;;;; This file provides simple iterative macros (a la Common Lisp) | |
395 ;;;; constructed on the basis of let, let* and while, which are the | |
396 ;;;; primitive binding/iteration constructs of Emacs Lisp | |
397 ;;;; | |
398 ;;;; The Common Lisp iterations use to have a block named nil | |
399 ;;;; wrapped around them, and allow declarations at the beginning | |
400 ;;;; of their bodies and you can return a value using (return ...). | |
401 ;;;; Nothing of the sort exists in Emacs Lisp, so I haven't tried | |
402 ;;;; to imitate these behaviors. | |
403 ;;;; | |
404 ;;;; Other than the above, the semantics of Common Lisp are | |
405 ;;;; correctly reproduced to the extent this was reasonable. | |
406 ;;;; | |
407 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
408 ;;;; (quiroz@cs.rochester.edu) | |
409 | |
410 ;;; some lisp-indentation information | |
411 (put 'do 'lisp-indent-function 2) | |
412 (put 'do* 'lisp-indent-function 2) | |
413 (put 'dolist 'lisp-indent-function 1) | |
414 (put 'dotimes 'lisp-indent-function 1) | |
415 (put 'do-symbols 'lisp-indent-function 1) | |
416 (put 'do-all-symbols 'lisp-indent-function 1) | |
417 | |
418 | |
419 (defmacro do (stepforms endforms &rest body) | |
420 "(do STEPFORMS ENDFORMS . BODY): Iterate BODY, stepping some local | |
421 variables. STEPFORMS must be a list of symbols or lists. In the second | |
422 case, the lists must start with a symbol and contain up to two more forms. | |
423 In the STEPFORMS, a symbol is the same as a (symbol). The other two forms | |
424 are the initial value (def. NIL) and the form to step (def. itself). | |
425 | |
426 The values used by initialization and stepping are computed in parallel. | |
427 The ENDFORMS are a list (CONDITION . ENDBODY). If the CONDITION evaluates | |
428 to true in any iteration, ENDBODY is evaluated and the last form in it is | |
429 returned. | |
430 | |
431 The BODY (which may be empty) is evaluated at every iteration, with the | |
432 symbols of the STEPFORMS bound to the initial or stepped values." | |
433 | |
434 ;; check the syntax of the macro | |
435 (and (check-do-stepforms stepforms) | |
436 (check-do-endforms endforms)) | |
437 ;; construct emacs-lisp equivalent | |
438 (let ((initlist (extract-do-inits stepforms)) | |
439 (steplist (extract-do-steps stepforms)) | |
440 (endcond (car endforms)) | |
441 (endbody (cdr endforms))) | |
442 (cons 'let (cons initlist | |
443 (cons (cons 'while (cons (list 'not endcond) | |
444 (append body steplist))) | |
445 (append endbody)))))) | |
446 | |
447 | |
448 (defmacro do* (stepforms endforms &rest body) | |
449 "`do*' is to `do' as `let*' is to `let'. | |
450 STEPFORMS must be a list of symbols or lists. In the second case, the | |
451 lists must start with a symbol and contain up to two more forms. In the | |
452 STEPFORMS, a symbol is the same as a (symbol). The other two forms are | |
453 the initial value (def. NIL) and the form to step (def. itself). | |
454 | |
455 Initializations and steppings are done in the sequence they are written. | |
456 | |
457 The ENDFORMS are a list (CONDITION . ENDBODY). If the CONDITION evaluates | |
458 to true in any iteration, ENDBODY is evaluated and the last form in it is | |
459 returned. | |
460 | |
461 The BODY (which may be empty) is evaluated at every iteration, with | |
462 the symbols of the STEPFORMS bound to the initial or stepped values." | |
463 ;; check the syntax of the macro | |
464 (and (check-do-stepforms stepforms) | |
465 (check-do-endforms endforms)) | |
466 ;; construct emacs-lisp equivalent | |
467 (let ((initlist (extract-do-inits stepforms)) | |
468 (steplist (extract-do*-steps stepforms)) | |
469 (endcond (car endforms)) | |
470 (endbody (cdr endforms))) | |
471 (cons 'let* (cons initlist | |
472 (cons (cons 'while (cons (list 'not endcond) | |
473 (append body steplist))) | |
474 (append endbody)))))) | |
475 | |
476 | |
477 ;;; DO and DO* share the syntax checking functions that follow. | |
478 | |
479 (defun check-do-stepforms (forms) | |
480 "True if FORMS is a valid stepforms for the do[*] macro (q.v.)" | |
481 (if (nlistp forms) | |
482 (error "init/step form for do[*] should be a list, not `%s'" | |
483 (prin1-to-string forms)) | |
484 (mapcar | |
485 (function | |
486 (lambda (entry) | |
487 (if (not (or (symbolp entry) | |
488 (and (listp entry) | |
489 (symbolp (car entry)) | |
490 (< (length entry) 4)))) | |
491 (error "init/step must be %s, not `%s'" | |
492 "symbol or (symbol [init [step]])" | |
493 (prin1-to-string entry))))) | |
494 forms))) | |
495 | |
496 (defun check-do-endforms (forms) | |
497 "True if FORMS is a valid endforms for the do[*] macro (q.v.)" | |
498 (if (nlistp forms) | |
499 (error "termination form for do macro should be a list, not `%s'" | |
500 (prin1-to-string forms)))) | |
501 | |
502 (defun extract-do-inits (forms) | |
503 "Returns a list of the initializations (for do) in FORMS | |
504 (a stepforms, see the do macro). | |
505 FORMS is assumed syntactically valid." | |
506 (mapcar | |
507 (function | |
508 (lambda (entry) | |
509 (cond ((symbolp entry) | |
510 (list entry nil)) | |
511 ((listp entry) | |
512 (list (car entry) (cadr entry)))))) | |
513 forms)) | |
514 | |
515 ;;; There used to be a reason to deal with DO differently than with | |
516 ;;; DO*. The writing of PSETQ has made it largely unnecessary. | |
517 | |
518 (defun extract-do-steps (forms) | |
519 "EXTRACT-DO-STEPS FORMS => an s-expr. | |
520 FORMS is the stepforms part of a DO macro (q.v.). This function constructs | |
521 an s-expression that does the stepping at the end of an iteration." | |
522 (list (cons 'psetq (select-stepping-forms forms)))) | |
523 | |
524 (defun extract-do*-steps (forms) | |
525 "EXTRACT-DO*-STEPS FORMS => an s-expr. | |
526 FORMS is the stepforms part of a DO* macro (q.v.). This function constructs | |
527 an s-expression that does the stepping at the end of an iteration." | |
528 (list (cons 'setq (select-stepping-forms forms)))) | |
529 | |
530 (defun select-stepping-forms (forms) | |
531 "Separate only the forms that cause stepping." | |
532 (let ((result '()) ;ends up being (... var form ...) | |
533 (ptr forms) ;to traverse the forms | |
534 entry ;to explore each form in turn | |
535 ) | |
536 (while ptr ;(not (endp entry)) might be safer | |
537 (setq entry (car ptr)) | |
538 (cond ((and (listp entry) (= (length entry) 3)) | |
539 (setq result (append ;append in reverse order! | |
540 (list (caddr entry) (car entry)) | |
541 result)))) | |
542 (setq ptr (cdr ptr))) ;step in the list of forms | |
543 (nreverse result))) | |
544 | |
545 ;;; Other iterative constructs | |
546 | |
547 (defmacro dolist (stepform &rest body) | |
548 "(dolist (VAR LIST [RESULTFORM]) . BODY): do BODY for each elt of LIST. | |
549 The RESULTFORM defaults to nil. The VAR is bound to successive elements | |
550 of the value of LIST and remains bound (to the nil value) when the | |
551 RESULTFORM is evaluated." | |
552 ;; check sanity | |
553 (cond | |
554 ((nlistp stepform) | |
555 (error "stepform for `dolist' should be (VAR LIST [RESULT]), not `%s'" | |
556 (prin1-to-string stepform))) | |
557 ((not (symbolp (car stepform))) | |
558 (error "first component of stepform should be a symbol, not `%s'" | |
559 (prin1-to-string (car stepform)))) | |
560 ((> (length stepform) 3) | |
561 (error "too many components in stepform `%s'" | |
562 (prin1-to-string stepform)))) | |
563 ;; generate code | |
564 (let* ((var (car stepform)) | |
565 (listform (cadr stepform)) | |
566 (resultform (caddr stepform))) | |
567 (list 'progn | |
568 (list 'mapcar | |
569 (list 'function | |
570 (cons 'lambda (cons (list var) body))) | |
571 listform) | |
572 (list 'let | |
573 (list (list var nil)) | |
574 resultform)))) | |
575 | |
576 (defmacro dotimes (stepform &rest body) | |
577 "(dotimes (VAR COUNTFORM [RESULTFORM]) . BODY): Repeat BODY, counting in VAR. | |
578 The COUNTFORM should return a positive integer. The VAR is bound to | |
579 successive integers from 0 to COUNTFORM - 1 and the BODY is repeated for | |
580 each of them. At the end, the RESULTFORM is evaluated and its value | |
581 returned. During this last evaluation, the VAR is still bound, and its | |
582 value is the number of times the iteration occurred. An omitted RESULTFORM | |
583 defaults to nil." | |
584 ;; check sanity | |
585 (cond | |
586 ((nlistp stepform) | |
587 (error "stepform for `dotimes' should be (VAR COUNT [RESULT]), not `%s'" | |
588 (prin1-to-string stepform))) | |
589 ((not (symbolp (car stepform))) | |
590 (error "first component of stepform should be a symbol, not `%s'" | |
591 (prin1-to-string (car stepform)))) | |
592 ((> (length stepform) 3) | |
593 (error "too many components in stepform `%s'" | |
594 (prin1-to-string stepform)))) | |
595 ;; generate code | |
596 (let* ((var (car stepform)) | |
597 (countform (cadr stepform)) | |
598 (resultform (caddr stepform)) | |
599 (newsym (gentemp))) | |
600 (list | |
601 'let* (list (list newsym countform)) | |
602 (list* | |
603 'do* | |
604 (list (list var 0 (list '+ var 1))) | |
605 (list (list '>= var newsym) resultform) | |
606 body)))) | |
607 | |
608 (defmacro do-symbols (stepform &rest body) | |
609 "(do_symbols (VAR [OBARRAY [RESULTFORM]]) . BODY) | |
610 The VAR is bound to each of the symbols in OBARRAY (def. obarray) and | |
611 the BODY is repeatedly performed for each of those bindings. At the | |
612 end, RESULTFORM (def. nil) is evaluated and its value returned. | |
613 During this last evaluation, the VAR is still bound and its value is nil. | |
614 See also the function `mapatoms'." | |
615 ;; check sanity | |
616 (cond | |
617 ((nlistp stepform) | |
618 (error "stepform for `do-symbols' should be (VAR OBARRAY [RESULT]), not `%s'" | |
619 (prin1-to-string stepform))) | |
620 ((not (symbolp (car stepform))) | |
621 (error "first component of stepform should be a symbol, not `%s'" | |
622 (prin1-to-string (car stepform)))) | |
623 ((> (length stepform) 3) | |
624 (error "too many components in stepform `%s'" | |
625 (prin1-to-string stepform)))) | |
626 ;; generate code | |
627 (let* ((var (car stepform)) | |
628 (oblist (cadr stepform)) | |
629 (resultform (caddr stepform))) | |
630 (list 'progn | |
631 (list 'mapatoms | |
632 (list 'function | |
633 (cons 'lambda (cons (list var) body))) | |
634 oblist) | |
635 (list 'let | |
636 (list (list var nil)) | |
637 resultform)))) | |
638 | |
639 | |
640 (defmacro do-all-symbols (stepform &rest body) | |
641 "(do-all-symbols (VAR [RESULTFORM]) . BODY) | |
642 Is the same as (do-symbols (VAR obarray RESULTFORM) . BODY)." | |
643 (list* | |
644 'do-symbols | |
645 (list (car stepform) 'obarray (cadr stepform)) | |
646 body)) | |
647 | |
648 (defmacro loop (&rest body) | |
649 "(loop . BODY) repeats BODY indefinitely and does not return. | |
650 Normally BODY uses `throw' or `signal' to cause an exit. | |
651 The forms in BODY should be lists, as non-lists are reserved for new features." | |
652 ;; check that the body doesn't have atomic forms | |
653 (if (nlistp body) | |
654 (error "body of `loop' should be a list of lists or nil") | |
655 ;; ok, it is a list, check for atomic components | |
656 (mapcar | |
657 (function (lambda (component) | |
658 (if (nlistp component) | |
659 (error "components of `loop' should be lists")))) | |
660 body) | |
661 ;; build the infinite loop | |
662 (cons 'while (cons 't body)))) | |
663 | |
664 ;;;; end of cl-iterations.el | |
665 | |
666 ;;;; LISTS | |
667 ;;;; This file provides some of the lists machinery of Common-Lisp | |
668 ;;;; in a way compatible with Emacs Lisp. Especially, see the the | |
669 ;;;; typical c[ad]*r functions. | |
670 ;;;; | |
671 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
672 ;;;; (quiroz@cs.rochester.edu) | |
673 | |
674 | |
922 | 675 |
957 | 676 ;;; To make these faster, we define them using defsubst. This directs the |
677 ;;; compiler to open-code these functions. | |
188 | 678 |
679 ;;; Synonyms for list functions | |
957 | 680 (defsubst first (x) |
188 | 681 "Synonym for `car'" |
682 (car x)) | |
683 | |
957 | 684 (defsubst second (x) |
188 | 685 "Return the second element of the list LIST." |
686 (nth 1 x)) | |
687 | |
957 | 688 (defsubst third (x) |
188 | 689 "Return the third element of the list LIST." |
690 (nth 2 x)) | |
691 | |
957 | 692 (defsubst fourth (x) |
188 | 693 "Return the fourth element of the list LIST." |
694 (nth 3 x)) | |
695 | |
957 | 696 (defsubst fifth (x) |
188 | 697 "Return the fifth element of the list LIST." |
698 (nth 4 x)) | |
699 | |
957 | 700 (defsubst sixth (x) |
188 | 701 "Return the sixth element of the list LIST." |
702 (nth 5 x)) | |
703 | |
957 | 704 (defsubst seventh (x) |
188 | 705 "Return the seventh element of the list LIST." |
706 (nth 6 x)) | |
707 | |
957 | 708 (defsubst eighth (x) |
188 | 709 "Return the eighth element of the list LIST." |
710 (nth 7 x)) | |
711 | |
957 | 712 (defsubst ninth (x) |
188 | 713 "Return the ninth element of the list LIST." |
714 (nth 8 x)) | |
715 | |
957 | 716 (defsubst tenth (x) |
188 | 717 "Return the tenth element of the list LIST." |
718 (nth 9 x)) | |
719 | |
957 | 720 (defsubst rest (x) |
188 | 721 "Synonym for `cdr'" |
722 (cdr x)) | |
723 | |
724 (defun endp (x) | |
725 "t if X is nil, nil if X is a cons; error otherwise." | |
726 (if (listp x) | |
727 (null x) | |
728 (error "endp received a non-cons, non-null argument `%s'" | |
729 (prin1-to-string x)))) | |
730 | |
731 (defun last (x) | |
732 "Returns the last link in the list LIST." | |
733 (if (nlistp x) | |
734 (error "arg to `last' must be a list")) | |
735 (do ((current-cons x (cdr current-cons)) | |
736 (next-cons (cdr x) (cdr next-cons))) | |
737 ((endp next-cons) current-cons))) | |
738 | |
739 (defun list-length (x) ;taken from CLtL sect. 15.2 | |
740 "Returns the length of a non-circular list, or `nil' for a circular one." | |
741 (do ((n 0) ;counter | |
742 (fast x (cddr fast)) ;fast pointer, leaps by 2 | |
743 (slow x (cdr slow)) ;slow pointer, leaps by 1 | |
744 (ready nil)) ;indicates termination | |
745 (ready n) | |
746 (cond ((endp fast) | |
747 (setq ready t)) ;return n | |
748 ((endp (cdr fast)) | |
749 (setq n (+ n 1)) | |
750 (setq ready t)) ;return n+1 | |
751 ((and (eq fast slow) (> n 0)) | |
752 (setq n nil) | |
753 (setq ready t)) ;return nil | |
754 (t | |
755 (setq n (+ n 2)))))) ;just advance counter | |
756 | |
757 (defun butlast (list &optional n) | |
758 "Return a new list like LIST but sans the last N elements. | |
759 N defaults to 1. If the list doesn't have N elements, nil is returned." | |
760 (if (null n) (setq n 1)) | |
761 (reverse (nthcdr n (reverse list)))) | |
762 | |
763 (defun list* (arg &rest others) | |
764 "Return a new list containing the first arguments consed onto the last arg. | |
765 Thus, (list* 1 2 3 '(a b)) returns (1 2 3 a b)." | |
766 (if (null others) | |
767 arg | |
768 (let* ((allargs (cons arg others)) | |
769 (front (butlast allargs)) | |
770 (back (last allargs))) | |
771 (rplacd (last front) (car back)) | |
772 front))) | |
773 | |
774 (defun adjoin (item list) | |
775 "Return a list which contains ITEM but is otherwise like LIST. | |
776 If ITEM occurs in LIST, the value is LIST. Otherwise it is (cons ITEM LIST). | |
777 When comparing ITEM against elements, `eql' is used." | |
778 (if (memq item list) | |
779 list | |
780 (cons item list))) | |
781 | |
782 (defun ldiff (list sublist) | |
783 "Return a new list like LIST but sans SUBLIST. | |
784 SUBLIST must be one of the links in LIST; otherwise the value is LIST itself." | |
785 (do ((result '()) | |
786 (curcons list (cdr curcons))) | |
787 ((or (endp curcons) (eq curcons sublist)) | |
788 (reverse result)) | |
789 (setq result (cons (car curcons) result)))) | |
790 | |
791 ;;; The popular c[ad]*r functions and other list accessors. | |
792 | |
957 | 793 ;;; To implement this efficiently, we define them using defsubst, |
794 ;;; which directs the compiler to open-code these functions. | |
922 | 795 |
957 | 796 (defsubst caar (X) |
188 | 797 "Return the car of the car of X." |
798 (car (car X))) | |
799 | |
957 | 800 (defsubst cadr (X) |
188 | 801 "Return the car of the cdr of X." |
802 (car (cdr X))) | |
803 | |
957 | 804 (defsubst cdar (X) |
188 | 805 "Return the cdr of the car of X." |
806 (cdr (car X))) | |
807 | |
957 | 808 (defsubst cddr (X) |
188 | 809 "Return the cdr of the cdr of X." |
810 (cdr (cdr X))) | |
811 | |
957 | 812 (defsubst caaar (X) |
188 | 813 "Return the car of the car of the car of X." |
814 (car (car (car X)))) | |
815 | |
957 | 816 (defsubst caadr (X) |
188 | 817 "Return the car of the car of the cdr of X." |
818 (car (car (cdr X)))) | |
819 | |
957 | 820 (defsubst cadar (X) |
188 | 821 "Return the car of the cdr of the car of X." |
822 (car (cdr (car X)))) | |
823 | |
957 | 824 (defsubst cdaar (X) |
188 | 825 "Return the cdr of the car of the car of X." |
826 (cdr (car (car X)))) | |
827 | |
957 | 828 (defsubst caddr (X) |
188 | 829 "Return the car of the cdr of the cdr of X." |
830 (car (cdr (cdr X)))) | |
831 | |
957 | 832 (defsubst cdadr (X) |
188 | 833 "Return the cdr of the car of the cdr of X." |
834 (cdr (car (cdr X)))) | |
835 | |
957 | 836 (defsubst cddar (X) |
188 | 837 "Return the cdr of the cdr of the car of X." |
838 (cdr (cdr (car X)))) | |
839 | |
957 | 840 (defsubst cdddr (X) |
188 | 841 "Return the cdr of the cdr of the cdr of X." |
842 (cdr (cdr (cdr X)))) | |
843 | |
957 | 844 (defsubst caaaar (X) |
188 | 845 "Return the car of the car of the car of the car of X." |
846 (car (car (car (car X))))) | |
847 | |
957 | 848 (defsubst caaadr (X) |
188 | 849 "Return the car of the car of the car of the cdr of X." |
850 (car (car (car (cdr X))))) | |
851 | |
957 | 852 (defsubst caadar (X) |
188 | 853 "Return the car of the car of the cdr of the car of X." |
854 (car (car (cdr (car X))))) | |
855 | |
957 | 856 (defsubst cadaar (X) |
188 | 857 "Return the car of the cdr of the car of the car of X." |
858 (car (cdr (car (car X))))) | |
859 | |
957 | 860 (defsubst cdaaar (X) |
188 | 861 "Return the cdr of the car of the car of the car of X." |
862 (cdr (car (car (car X))))) | |
863 | |
957 | 864 (defsubst caaddr (X) |
188 | 865 "Return the car of the car of the cdr of the cdr of X." |
866 (car (car (cdr (cdr X))))) | |
867 | |
957 | 868 (defsubst cadadr (X) |
188 | 869 "Return the car of the cdr of the car of the cdr of X." |
870 (car (cdr (car (cdr X))))) | |
871 | |
957 | 872 (defsubst cdaadr (X) |
188 | 873 "Return the cdr of the car of the car of the cdr of X." |
874 (cdr (car (car (cdr X))))) | |
875 | |
957 | 876 (defsubst caddar (X) |
188 | 877 "Return the car of the cdr of the cdr of the car of X." |
878 (car (cdr (cdr (car X))))) | |
879 | |
957 | 880 (defsubst cdadar (X) |
188 | 881 "Return the cdr of the car of the cdr of the car of X." |
882 (cdr (car (cdr (car X))))) | |
883 | |
957 | 884 (defsubst cddaar (X) |
188 | 885 "Return the cdr of the cdr of the car of the car of X." |
886 (cdr (cdr (car (car X))))) | |
887 | |
957 | 888 (defsubst cadddr (X) |
188 | 889 "Return the car of the cdr of the cdr of the cdr of X." |
890 (car (cdr (cdr (cdr X))))) | |
891 | |
957 | 892 (defsubst cddadr (X) |
188 | 893 "Return the cdr of the cdr of the car of the cdr of X." |
894 (cdr (cdr (car (cdr X))))) | |
895 | |
957 | 896 (defsubst cdaddr (X) |
188 | 897 "Return the cdr of the car of the cdr of the cdr of X." |
898 (cdr (car (cdr (cdr X))))) | |
899 | |
957 | 900 (defsubst cdddar (X) |
188 | 901 "Return the cdr of the cdr of the cdr of the car of X." |
902 (cdr (cdr (cdr (car X))))) | |
903 | |
957 | 904 (defsubst cddddr (X) |
188 | 905 "Return the cdr of the cdr of the cdr of the cdr of X." |
906 (cdr (cdr (cdr (cdr X))))) | |
907 | |
908 ;;; some inverses of the accessors are needed for setf purposes | |
909 | |
910 (defun setnth (n list newval) | |
911 "Set (nth N LIST) to NEWVAL. Returns NEWVAL." | |
912 (rplaca (nthcdr n list) newval)) | |
913 | |
914 (defun setnthcdr (n list newval) | |
915 "(setnthcdr N LIST NEWVAL) => NEWVAL | |
916 As a side effect, sets the Nth cdr of LIST to NEWVAL." | |
917 (cond ((< n 0) | |
918 (error "N must be 0 or greater, not %d" n)) | |
919 ((= n 0) | |
920 (rplaca list (car newval)) | |
921 (rplacd list (cdr newval)) | |
922 newval) | |
923 (t | |
924 (rplacd (nthcdr (- n 1) list) newval)))) | |
925 | |
926 ;;; A-lists machinery | |
927 | |
928 (defun acons (key item alist) | |
929 "Return a new alist with KEY paired with ITEM; otherwise like ALIST. | |
930 Does not copy ALIST." | |
931 (cons (cons key item) alist)) | |
932 | |
933 (defun pairlis (keys data &optional alist) | |
934 "Return a new alist with each elt of KEYS paired with an elt of DATA; | |
935 optional 3rd arg ALIST is nconc'd at the end. KEYS and DATA must | |
936 have the same length." | |
937 (unless (= (length keys) (length data)) | |
938 (error "keys and data should be the same length")) | |
939 (do* ;;collect keys and data in front of alist | |
940 ((kptr keys (cdr kptr)) ;traverses the keys | |
941 (dptr data (cdr dptr)) ;traverses the data | |
942 (key (car kptr) (car kptr)) ;current key | |
943 (item (car dptr) (car dptr)) ;current data item | |
944 (result alist)) | |
945 ((endp kptr) result) | |
946 (setq result (acons key item result)))) | |
947 | |
948 | |
949 ;;;; SEQUENCES | |
950 ;;;; Emacs Lisp provides many of the 'sequences' functionality of | |
951 ;;;; Common Lisp. This file provides a few things that were left out. | |
952 ;;;; | |
953 | |
954 | |
955 (defkeyword :test "Used to designate positive (selection) tests.") | |
956 (defkeyword :test-not "Used to designate negative (rejection) tests.") | |
957 (defkeyword :key "Used to designate component extractions.") | |
958 (defkeyword :predicate "Used to define matching of sequence components.") | |
959 (defkeyword :start "Inclusive low index in sequence") | |
960 (defkeyword :end "Exclusive high index in sequence") | |
961 (defkeyword :start1 "Inclusive low index in first of two sequences.") | |
962 (defkeyword :start2 "Inclusive low index in second of two sequences.") | |
963 (defkeyword :end1 "Exclusive high index in first of two sequences.") | |
964 (defkeyword :end2 "Exclusive high index in second of two sequences.") | |
965 (defkeyword :count "Number of elements to affect.") | |
966 (defkeyword :from-end "T when counting backwards.") | |
967 | |
968 (defun some (pred seq &rest moreseqs) | |
969 "Test PREDICATE on each element of SEQUENCE; is it ever non-nil? | |
970 Extra args are additional sequences; PREDICATE gets one arg from each | |
971 sequence and we advance down all the sequences together in lock-step. | |
972 A sequence means either a list or a vector." | |
973 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
974 (do* ((ready nil) ;flag: return when t | |
975 (result nil) ;resulting value | |
976 (applyval nil) ;result of applying pred once | |
977 (remaining args | |
978 (cdr remaining)) ;remaining argument sets | |
979 (current (car remaining) ;current argument set | |
980 (car remaining))) | |
981 ((or ready (endp remaining)) result) | |
982 (setq applyval (apply pred current)) | |
983 (when applyval | |
984 (setq ready t) | |
985 (setq result applyval))))) | |
986 | |
987 (defun every (pred seq &rest moreseqs) | |
988 "Test PREDICATE on each element of SEQUENCE; is it always non-nil? | |
989 Extra args are additional sequences; PREDICATE gets one arg from each | |
990 sequence and we advance down all the sequences together in lock-step. | |
991 A sequence means either a list or a vector." | |
992 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
993 (do* ((ready nil) ;flag: return when t | |
994 (result t) ;resulting value | |
995 (applyval nil) ;result of applying pred once | |
996 (remaining args | |
997 (cdr remaining)) ;remaining argument sets | |
998 (current (car remaining) ;current argument set | |
999 (car remaining))) | |
1000 ((or ready (endp remaining)) result) | |
1001 (setq applyval (apply pred current)) | |
1002 (unless applyval | |
1003 (setq ready t) | |
1004 (setq result nil))))) | |
1005 | |
1006 (defun notany (pred seq &rest moreseqs) | |
1007 "Test PREDICATE on each element of SEQUENCE; is it always nil? | |
1008 Extra args are additional sequences; PREDICATE gets one arg from each | |
1009 sequence and we advance down all the sequences together in lock-step. | |
1010 A sequence means either a list or a vector." | |
1011 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
1012 (do* ((ready nil) ;flag: return when t | |
1013 (result t) ;resulting value | |
1014 (applyval nil) ;result of applying pred once | |
1015 (remaining args | |
1016 (cdr remaining)) ;remaining argument sets | |
1017 (current (car remaining) ;current argument set | |
1018 (car remaining))) | |
1019 ((or ready (endp remaining)) result) | |
1020 (setq applyval (apply pred current)) | |
1021 (when applyval | |
1022 (setq ready t) | |
1023 (setq result nil))))) | |
1024 | |
1025 (defun notevery (pred seq &rest moreseqs) | |
1026 "Test PREDICATE on each element of SEQUENCE; is it sometimes nil? | |
1027 Extra args are additional sequences; PREDICATE gets one arg from each | |
1028 sequence and we advance down all the sequences together in lock-step. | |
1029 A sequence means either a list or a vector." | |
1030 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
1031 (do* ((ready nil) ;flag: return when t | |
1032 (result nil) ;resulting value | |
1033 (applyval nil) ;result of applying pred once | |
1034 (remaining args | |
1035 (cdr remaining)) ;remaining argument sets | |
1036 (current (car remaining) ;current argument set | |
1037 (car remaining))) | |
1038 ((or ready (endp remaining)) result) | |
1039 (setq applyval (apply pred current)) | |
1040 (unless applyval | |
1041 (setq ready t) | |
1042 (setq result t))))) | |
1043 | |
1044 ;;; More sequence functions that don't need keyword arguments | |
1045 | |
1046 (defun concatenate (type &rest sequences) | |
1047 "(concatenate TYPE &rest SEQUENCES) => a sequence | |
1048 The sequence returned is of type TYPE (must be 'list, 'string, or 'vector) and | |
1049 contains the concatenation of the elements of all the arguments, in the order | |
1050 given." | |
1051 (let ((sequences (append sequences '(())))) | |
1052 (case type | |
1053 (list | |
1054 (apply (function append) sequences)) | |
1055 (string | |
1056 (apply (function concat) sequences)) | |
1057 (vector | |
1058 (apply (function vector) (apply (function append) sequences))) | |
1059 (t | |
1060 (error "type for concatenate `%s' not 'list, 'string or 'vector" | |
1061 (prin1-to-string type)))))) | |
1062 | |
1063 (defun map (type function &rest sequences) | |
1064 "(map TYPE FUNCTION &rest SEQUENCES) => a sequence | |
1065 The FUNCTION is called on each set of elements from the SEQUENCES \(stopping | |
1066 when the shortest sequence is terminated\) and the results are possibly | |
1067 returned in a sequence of type TYPE \(one of 'list, 'vector, 'string, or nil\) | |
1068 giving NIL for TYPE gets rid of the values." | |
1069 (if (not (memq type (list 'list 'string 'vector nil))) | |
1070 (error "type for map `%s' not 'list, 'string, 'vector or nil" | |
1071 (prin1-to-string type))) | |
1072 (let ((argslists (reassemble-argslists sequences)) | |
1073 results) | |
1074 (if (null type) | |
1075 (while argslists ;don't bother accumulating | |
1076 (apply function (car argslists)) | |
1077 (setq argslists (cdr argslists))) | |
1078 (setq results (mapcar (function (lambda (args) (apply function args))) | |
1079 argslists)) | |
1080 (case type | |
1081 (list | |
1082 results) | |
1083 (string | |
1084 (funcall (function concat) results)) | |
1085 (vector | |
1086 (apply (function vector) results)))))) | |
1087 | |
1088 ;;; an inverse of elt is needed for setf purposes | |
1089 | |
1090 (defun setelt (seq n newval) | |
1091 "In SEQUENCE, set the Nth element to NEWVAL. Returns NEWVAL. | |
1092 A sequence means either a list or a vector." | |
1093 (let ((l (length seq))) | |
1094 (if (or (< n 0) (>= n l)) | |
1095 (error "N(%d) should be between 0 and %d" n l) | |
1096 ;; only two cases need be considered valid, as strings are arrays | |
1097 (cond ((listp seq) | |
1098 (setnth n seq newval)) | |
1099 ((arrayp seq) | |
1100 (aset seq n newval)) | |
1101 (t | |
1102 (error "SEQ should be a sequence, not `%s'" | |
1103 (prin1-to-string seq))))))) | |
1104 | |
1105 ;;; Testing with keyword arguments. | |
1106 ;;; | |
1107 ;;; Many of the sequence functions use keywords to denote some stylized | |
1108 ;;; form of selecting entries in a sequence. The involved arguments | |
1109 ;;; are collected with a &rest marker (as Emacs Lisp doesn't have a &key | |
1110 ;;; marker), then they are passed to build-klist, who | |
1111 ;;; constructs an association list. That association list is used to | |
1112 ;;; test for satisfaction and matching. | |
1113 | |
1114 ;;; DON'T USE MEMBER, NOR ANY FUNCTION THAT COULD TAKE KEYWORDS HERE!!! | |
1115 | |
1116 (defun build-klist (argslist acceptable &optional allow-other-keys) | |
1117 "Decode a keyword argument list ARGSLIST for keywords in ACCEPTABLE. | |
1118 ARGSLIST is a list, presumably the &rest argument of a call, whose | |
1119 even numbered elements must be keywords. | |
1120 ACCEPTABLE is a list of keywords, the only ones that are truly acceptable. | |
1121 The result is an alist containing the arguments named by the keywords | |
1122 in ACCEPTABLE, or an error is signalled, if something failed. | |
1123 If the third argument (an optional) is non-nil, other keys are acceptable." | |
1124 ;; check legality of the arguments, then destructure them | |
1125 (unless (and (listp argslist) | |
1126 (evenp (length argslist))) | |
1127 (error "build-klist: odd number of keyword-args")) | |
1128 (unless (and (listp acceptable) | |
1129 (every 'keywordp acceptable)) | |
1130 (error "build-klist: second arg should be a list of keywords")) | |
1131 (multiple-value-bind | |
1132 (keywords forms) | |
1133 (unzip-list argslist) | |
1134 (unless (every 'keywordp keywords) | |
1135 (error "build-klist: expected keywords, found `%s'" | |
1136 (prin1-to-string keywords))) | |
1137 (unless (or allow-other-keys | |
1138 (every (function (lambda (keyword) | |
1139 (memq keyword acceptable))) | |
1140 keywords)) | |
1141 (error "bad keyword[s]: %s not in %s" | |
1142 (prin1-to-string (mapcan (function (lambda (keyword) | |
1143 (if (memq keyword acceptable) | |
1144 nil | |
1145 (list keyword)))) | |
1146 keywords)) | |
1147 (prin1-to-string acceptable))) | |
1148 (do* ;;pick up the pieces | |
1149 ((auxlist ;auxiliary a-list, may | |
1150 (pairlis keywords forms)) ;contain repetitions and junk | |
1151 (ptr acceptable (cdr ptr)) ;pointer in acceptable | |
1152 (this (car ptr) (car ptr)) ;current acceptable keyword | |
1153 (auxval nil) ;used to move values around | |
1154 (alist '())) ;used to build the result | |
1155 ((endp ptr) alist) | |
1156 ;; if THIS appears in auxlist, use its value | |
1157 (when (setq auxval (assq this auxlist)) | |
1158 (setq alist (cons auxval alist)))))) | |
1159 | |
1160 | |
1161 (defun extract-from-klist (klist key &optional default) | |
1162 "(extract-from-klist KLIST KEY [DEFAULT]) => value of KEY or DEFAULT | |
1163 Extract value associated with KEY in KLIST (return DEFAULT if nil)." | |
1164 (let ((retrieved (cdr (assq key klist)))) | |
1165 (or retrieved default))) | |
1166 | |
1167 (defun keyword-argument-supplied-p (klist key) | |
1168 "(keyword-argument-supplied-p KLIST KEY) => nil or something | |
1169 NIL if KEY (a keyword) does not appear in the KLIST." | |
1170 (assq key klist)) | |
1171 | |
1172 (defun add-to-klist (key item klist) | |
1173 "(ADD-TO-KLIST KEY ITEM KLIST) => new KLIST | |
1174 Add association (KEY . ITEM) to KLIST." | |
1175 (setq klist (acons key item klist))) | |
1176 | |
1177 (defun elt-satisfies-test-p (item elt klist) | |
1178 "(elt-satisfies-test-p ITEM ELT KLIST) => t or nil | |
1179 KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL. | |
1180 True if the given ITEM and ELT satisfy the test." | |
1181 (let ((test (extract-from-klist klist :test)) | |
1182 (test-not (extract-from-klist klist :test-not)) | |
1183 (keyfn (extract-from-klist klist :key 'identity))) | |
1184 (cond (test | |
1185 (funcall test item (funcall keyfn elt))) | |
1186 (test-not | |
1187 (not (funcall test-not item (funcall keyfn elt)))) | |
1188 (t ;should never happen | |
1189 (error "neither :test nor :test-not in `%s'" | |
1190 (prin1-to-string klist)))))) | |
1191 | |
1192 (defun elt-satisfies-if-p (item klist) | |
1193 "(elt-satisfies-if-p ITEM KLIST) => t or nil | |
1194 True if an -if style function was called and ITEM satisfies the | |
1195 predicate under :predicate in KLIST." | |
1196 (let ((predicate (extract-from-klist klist :predicate)) | |
1197 (keyfn (extract-from-klist klist :key 'identity))) | |
1198 (funcall predicate item (funcall keyfn elt)))) | |
1199 | |
1200 (defun elt-satisfies-if-not-p (item klist) | |
1201 "(elt-satisfies-if-not-p ITEM KLIST) => t or nil | |
1202 KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL. | |
1203 True if an -if-not style function was called and ITEM does not satisfy | |
1204 the predicate under :predicate in KLIST." | |
1205 (let ((predicate (extract-from-klist klist :predicate)) | |
1206 (keyfn (extract-from-klist klist :key 'identity))) | |
1207 (not (funcall predicate item (funcall keyfn elt))))) | |
1208 | |
1209 (defun elts-match-under-klist-p (e1 e2 klist) | |
1210 "(elts-match-under-klist-p E1 E2 KLIST) => t or nil | |
1211 KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL. | |
1212 True if elements E1 and E2 match under the tests encoded in KLIST." | |
1213 (let ((test (extract-from-klist klist :test)) | |
1214 (test-not (extract-from-klist klist :test-not)) | |
1215 (keyfn (extract-from-klist klist :key 'identity))) | |
1216 (if (and test test-not) | |
1217 (error "both :test and :test-not in `%s'" | |
1218 (prin1-to-string klist))) | |
1219 (cond (test | |
1220 (funcall test (funcall keyfn e1) (funcall keyfn e2))) | |
1221 (test-not | |
1222 (not (funcall test-not (funcall keyfn e1) (funcall keyfn e2)))) | |
1223 (t ;should never happen | |
1224 (error "neither :test nor :test-not in `%s'" | |
1225 (prin1-to-string klist)))))) | |
1226 | |
1227 ;;; This macro simplifies using keyword args. It is less clumsy than using | |
1228 ;;; the primitives build-klist, etc... For instance, member could be written | |
1229 ;;; this way: | |
1230 | |
1231 ;;; (defun member (item list &rest kargs) | |
1232 ;;; (with-keyword-args kargs (test test-not (key 'identity)) | |
1233 ;;; ...)) | |
1234 | |
1235 ;;; Suggested by Robert Potter (potter@cs.rochester.edu, 15 Nov 1989) | |
1236 | |
1237 (defmacro with-keyword-args (keyargslist vardefs &rest body) | |
1238 "(WITH-KEYWORD-ARGS KEYARGSLIST VARDEFS . BODY) | |
1239 KEYARGSLIST can be either a symbol or a list of one or two symbols. | |
1240 In the second case, the second symbol is either T or NIL, indicating whether | |
1241 keywords other than the mentioned ones are tolerable. | |
1242 | |
1243 VARDEFS is a list. Each entry is either a VAR (symbol) or matches | |
1244 \(VAR [DEFAULT [KEYWORD]]). Just giving VAR is the same as giving | |
1245 \(VAR nil :VAR). | |
1246 | |
1247 The BODY is executed in an environment where each VAR (a symbol) is bound to | |
1248 the value present in the KEYARGSLIST provided, or to the DEFAULT. The value | |
1249 is searched by using the keyword form of VAR (i.e., :VAR) or the optional | |
1250 keyword if provided. | |
1251 | |
1252 Notice that this macro doesn't distinguish between a default value given | |
1253 explicitly by the user and one provided by default. See also the more | |
1254 primitive functions build-klist, add-to-klist, extract-from-klist, | |
1255 keyword-argument-supplied-p, elt-satisfies-test-p, elt-satisfies-if-p, | |
1256 elt-satisfies-if-not-p, elts-match-under-klist-p. They provide more complete, | |
1257 if clumsier, control over this feature." | |
1258 (let (allow-other-keys) | |
1259 (if (listp keyargslist) | |
1260 (if (> (length keyargslist) 2) | |
1261 (error | |
1262 "`%s' should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)" | |
1263 (prin1-to-string keyargslist)) | |
1264 (setq allow-other-keys (cadr keyargslist) | |
1265 keyargslist (car keyargslist)) | |
1266 (if (not (and | |
1267 (symbolp keyargslist) | |
1268 (memq allow-other-keys '(t nil)))) | |
1269 (error | |
1270 "first subform should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)" | |
1271 ))) | |
1272 (if (symbolp keyargslist) | |
1273 (setq allow-other-keys nil) | |
1274 (error | |
1275 "first subform should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)"))) | |
1276 (let (vars defaults keywords forms | |
1277 (klistname (gensym "KLIST_"))) | |
1278 (mapcar (function (lambda (entry) | |
1279 (if (symbolp entry) ;defaulty case | |
1280 (setq entry (list entry nil (keyword-of entry)))) | |
1281 (let* ((l (length entry)) | |
1282 (v (car entry)) | |
1283 (d (cadr entry)) | |
1284 (k (caddr entry))) | |
1285 (if (or (< l 1) (> l 3)) | |
1286 (error | |
1287 "`%s' must match (VAR [DEFAULT [KEYWORD]])" | |
1288 (prin1-to-string entry))) | |
1289 (if (or (null v) (not (symbolp v))) | |
1290 (error | |
1291 "bad variable `%s': must be non-null symbol" | |
1292 (prin1-to-string v))) | |
1293 (setq vars (cons v vars)) | |
1294 (setq defaults (cons d defaults)) | |
1295 (if (< l 3) | |
1296 (setq k (keyword-of v))) | |
1297 (if (and (= l 3) | |
1298 (or (null k) | |
1299 (not (keywordp k)))) | |
1300 (error | |
1301 "bad keyword `%s'" (prin1-to-string k))) | |
1302 (setq keywords (cons k keywords)) | |
1303 (setq forms (cons (list v (list 'extract-from-klist | |
1304 klistname | |
1305 k | |
1306 d)) | |
1307 forms))))) | |
1308 vardefs) | |
1309 (append | |
1310 (list 'let* (nconc (list (list klistname | |
1311 (list 'build-klist keyargslist | |
1312 (list 'quote keywords) | |
1313 allow-other-keys))) | |
1314 (nreverse forms))) | |
1315 body)))) | |
1316 (put 'with-keyword-args 'lisp-indent-function 1) | |
1317 | |
1318 | |
1319 ;;; REDUCE | |
1320 ;;; It is here mostly as an example of how to use KLISTs. | |
1321 ;;; | |
1322 ;;; First of all, you need to declare the keywords (done elsewhere in this | |
1323 ;;; file): | |
1324 ;;; (defkeyword :from-end "syntax of sequence functions") | |
1325 ;;; (defkeyword :start "syntax of sequence functions") | |
1326 ;;; etc... | |
1327 ;;; | |
1328 ;;; Then, you capture all the possible keyword arguments with a &rest | |
1329 ;;; argument. You can pass that list downward again, of course, but | |
1330 ;;; internally you need to parse it into a KLIST (an alist, really). One uses | |
1331 ;;; (build-klist REST-ARGS ACCEPTABLE-KEYWORDS [ALLOW-OTHER]). You can then | |
1332 ;;; test for presence by using (keyword-argument-supplied-p KLIST KEY) and | |
1333 ;;; extract a value with (extract-from-klist KLIST KEY [DEFAULT]). | |
1334 | |
1335 (defun reduce (function sequence &rest kargs) | |
1336 "Apply FUNCTION (a function of two arguments) to succesive pairs of elements | |
1337 from SEQUENCE. Some keyword arguments are valid after FUNCTION and SEQUENCE: | |
1338 :from-end If non-nil, process the values backwards | |
1339 :initial-value If given, prefix it to the SEQUENCE. Suffix, if :from-end | |
1340 :start Restrict reduction to the subsequence from this index | |
1341 :end Restrict reduction to the subsequence BEFORE this index. | |
1342 If the sequence is empty and no :initial-value is given, the FUNCTION is | |
1343 called on zero (not two) arguments. Otherwise, if there is exactly one | |
1344 element in the combination of SEQUENCE and the initial value, that element is | |
1345 returned." | |
1346 (let* ((klist (build-klist kargs '(:from-end :start :end :initial-value))) | |
1347 (length (length sequence)) | |
1348 (from-end (extract-from-klist klist :from-end)) | |
1349 (initial-value-given (keyword-argument-supplied-p | |
1350 klist :initial-value)) | |
1351 (start (extract-from-klist kargs :start 0)) | |
1352 (end (extract-from-klist kargs :end length))) | |
1353 (setq sequence (cl$subseq-as-list sequence start end)) | |
1354 (if from-end | |
1355 (setq sequence (reverse sequence))) | |
1356 (if initial-value-given | |
1357 (setq sequence (cons (extract-from-klist klist :initial-value) | |
1358 sequence))) | |
1359 (if (null sequence) | |
1360 (funcall function) ;only use of 0 arguments | |
1361 (let* ((result (car sequence)) | |
1362 (sequence (cdr sequence))) | |
1363 (while sequence | |
1364 (setq result (if from-end | |
1365 (funcall function (car sequence) result) | |
1366 (funcall function result (car sequence))) | |
1367 sequence (cdr sequence))) | |
1368 result)))) | |
1369 | |
1370 (defun cl$subseq-as-list (sequence start end) | |
1371 "(cl$subseq-as-list SEQUENCE START END) => a list" | |
1372 (let ((list (append sequence nil)) | |
1373 (length (length sequence)) | |
1374 result) | |
1375 (if (< start 0) | |
1376 (error "start should be >= 0, not %d" start)) | |
1377 (if (> end length) | |
1378 (error "end should be <= %d, not %d" length end)) | |
1379 (if (and (zerop start) (= end length)) | |
1380 list | |
1381 (let ((i start) | |
1382 (vector (apply 'vector list))) | |
1383 (while (/= i end) | |
1384 (setq result (cons (elt vector i) result)) | |
1385 (setq i (+ i 1))) | |
1386 (nreverse result))))) | |
1387 | |
1388 ;;;; end of cl-sequences.el | |
1389 | |
1390 ;;;; Some functions with keyword arguments | |
1391 ;;;; | |
1392 ;;;; Both list and sequence functions are considered here together. This | |
1393 ;;;; doesn't fit any more with the original split of functions in files. | |
1394 | |
1395 (defun member (item list &rest kargs) | |
1396 "Look for ITEM in LIST; return first tail of LIST the car of whose first | |
1397 cons cell tests the same as ITEM. Admits arguments :key, :test, and :test-not." | |
1398 (if (null kargs) ;treat this fast for efficiency | |
1399 (memq item list) | |
1400 (let* ((klist (build-klist kargs '(:test :test-not :key))) | |
1401 (test (extract-from-klist klist :test)) | |
1402 (testnot (extract-from-klist klist :test-not)) | |
1403 (key (extract-from-klist klist :key 'identity))) | |
1404 ;; another workaround allegledly for speed | |
1405 (if (and (or (eq test 'eq) (eq test 'eql) | |
1406 (eq test (symbol-function 'eq)) | |
1407 (eq test (symbol-function 'eql))) | |
1408 (null testnot) | |
1409 (or (eq key 'identity) ;either by default or so given | |
1410 (eq key (function identity)) ;could this happen? | |
1411 (eq key (symbol-function 'identity)) ;sheer paranoia | |
1412 )) | |
1413 (memq item list) | |
1414 (if (and test testnot) | |
1415 (error ":test and :test-not both specified for member")) | |
1416 (if (not (or test testnot)) | |
1417 (setq test 'eql)) | |
1418 ;; final hack: remove the indirection through the function names | |
1419 (if testnot | |
1420 (if (symbolp testnot) | |
1421 (setq testnot (symbol-function testnot))) | |
1422 (if (symbolp test) | |
1423 (setq test (symbol-function test)))) | |
1424 (if (symbolp key) | |
1425 (setq key (symbol-function key))) | |
1426 ;; ok, go for it | |
1427 (let ((ptr list) | |
1428 (done nil) | |
1429 (result '())) | |
1430 (if testnot | |
1431 (while (not (or done (endp ptr))) | |
1432 (cond ((not (funcall testnot item (funcall key (car ptr)))) | |
1433 (setq done t) | |
1434 (setq result ptr))) | |
1435 (setq ptr (cdr ptr))) | |
1436 (while (not (or done (endp ptr))) | |
1437 (cond ((funcall test item (funcall key (car ptr))) | |
1438 (setq done t) | |
1439 (setq result ptr))) | |
1440 (setq ptr (cdr ptr)))) | |
1441 result))))) | |
1442 | |
1443 ;;;; MULTIPLE VALUES | |
1444 ;;;; This package approximates the behavior of the multiple-values | |
1445 ;;;; forms of Common Lisp. | |
1446 ;;;; | |
1447 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
1448 ;;;; (quiroz@cs.rochester.edu) | |
1449 | |
1450 ;;; Lisp indentation information | |
1451 (put 'multiple-value-bind 'lisp-indent-function 2) | |
1452 (put 'multiple-value-setq 'lisp-indent-function 2) | |
1453 (put 'multiple-value-list 'lisp-indent-function nil) | |
1454 (put 'multiple-value-call 'lisp-indent-function 1) | |
1455 (put 'multiple-value-prog1 'lisp-indent-function 1) | |
1456 | |
1457 ;;; Global state of the package is kept here | |
1458 (defvar *mvalues-values* nil | |
1459 "Most recently returned multiple-values") | |
1460 (defvar *mvalues-count* nil | |
1461 "Count of multiple-values returned, or nil if the mechanism was not used") | |
1462 | |
1463 ;;; values is the standard multiple-value-return form. Must be the | |
1464 ;;; last thing evaluated inside a function. If the caller is not | |
1465 ;;; expecting multiple values, only the first one is passed. (values) | |
1466 ;;; is the same as no-values returned (unaware callers see nil). The | |
1467 ;;; alternative (values-list <list>) is just a convenient shorthand | |
1468 ;;; and complements multiple-value-list. | |
1469 | |
1470 (defun values (&rest val-forms) | |
1471 "Produce multiple values (zero or more). Each arg is one value. | |
1472 See also `multiple-value-bind', which is one way to examine the | |
1473 multiple values produced by a form. If the containing form or caller | |
1474 does not check specially to see multiple values, it will see only | |
1475 the first value." | |
1476 (setq *mvalues-values* val-forms) | |
1477 (setq *mvalues-count* (length *mvalues-values*)) | |
1478 (car *mvalues-values*)) | |
1479 | |
1480 (defun values-list (&optional val-forms) | |
1481 "Produce multiple values (zero or mode). Each element of LIST is one value. | |
1482 This is equivalent to (apply 'values LIST)." | |
1483 (cond ((nlistp val-forms) | |
1484 (error "Argument to values-list must be a list, not `%s'" | |
1485 (prin1-to-string val-forms)))) | |
1486 (setq *mvalues-values* val-forms) | |
1487 (setq *mvalues-count* (length *mvalues-values*)) | |
1488 (car *mvalues-values*)) | |
1489 | |
1490 ;;; Callers that want to see the multiple values use these macros. | |
1491 | |
1492 (defmacro multiple-value-list (form) | |
1493 "Execute FORM and return a list of all the (multiple) values FORM produces. | |
1494 See `values' and `multiple-value-bind'." | |
1495 (list 'progn | |
1496 (list 'setq '*mvalues-count* nil) | |
1497 (list 'let (list (list 'it '(gensym))) | |
1498 (list 'set 'it form) | |
1499 (list 'if '*mvalues-count* | |
1500 (list 'copy-sequence '*mvalues-values*) | |
1501 (list 'progn | |
1502 (list 'setq '*mvalues-count* 1) | |
1503 (list 'setq '*mvalues-values* | |
1504 (list 'list (list 'symbol-value 'it))) | |
1505 (list 'copy-sequence '*mvalues-values*)))))) | |
1506 | |
1507 (defmacro multiple-value-call (function &rest args) | |
1508 "Call FUNCTION on all the values produced by the remaining arguments. | |
1509 (multiple-value-call '+ (values 1 2) (values 3 4)) is 10." | |
1510 (let* ((result (gentemp)) | |
1511 (arg (gentemp))) | |
1512 (list 'apply (list 'function (eval function)) | |
1513 (list 'let* (list (list result '())) | |
1514 (list 'dolist (list arg (list 'quote args) result) | |
1515 (list 'setq result | |
1516 (list 'append | |
1517 result | |
1518 (list 'multiple-value-list | |
1519 (list 'eval arg))))))))) | |
1520 | |
1521 (defmacro multiple-value-bind (vars form &rest body) | |
1522 "Bind VARS to the (multiple) values produced by FORM, then do BODY. | |
1523 VARS is a list of variables; each is bound to one of FORM's values. | |
1524 If FORM doesn't make enough values, the extra variables are bound to nil. | |
1525 (Ordinary forms produce only one value; to produce more, use `values'.) | |
1526 Extra values are ignored. | |
1527 BODY (zero or more forms) is executed with the variables bound, | |
1528 then the bindings are unwound." | |
1529 (let* ((vals (gentemp)) ;name for intermediate values | |
1530 (clauses (mv-bind-clausify ;convert into clauses usable | |
1531 vars vals))) ; in a let form | |
1532 (list* 'let* | |
1533 (cons (list vals (list 'multiple-value-list form)) | |
1534 clauses) | |
1535 body))) | |
1536 | |
1537 (defmacro multiple-value-setq (vars form) | |
1538 "Set VARS to the (multiple) values produced by FORM. | |
1539 VARS is a list of variables; each is set to one of FORM's values. | |
1540 If FORM doesn't make enough values, the extra variables are set to nil. | |
1541 (Ordinary forms produce only one value; to produce more, use `values'.) | |
1542 Extra values are ignored." | |
1543 (let* ((vals (gentemp)) ;name for intermediate values | |
1544 (clauses (mv-bind-clausify ;convert into clauses usable | |
1545 vars vals))) ; in a setq (after append). | |
1546 (list 'let* | |
1547 (list (list vals (list 'multiple-value-list form))) | |
1548 (cons 'setq (apply (function append) clauses))))) | |
1549 | |
1550 (defmacro multiple-value-prog1 (form &rest body) | |
1551 "Evaluate FORM, then BODY, then produce the same values FORM produced. | |
1552 Thus, (multiple-value-prog1 (values 1 2) (foobar)) produces values 1 and 2. | |
1553 This is like `prog1' except that `prog1' would produce only one value, | |
1554 which would be the first of FORM's values." | |
1555 (let* ((heldvalues (gentemp))) | |
1556 (cons 'let* | |
1557 (cons (list (list heldvalues (list 'multiple-value-list form))) | |
1558 (append body (list (list 'values-list heldvalues))))))) | |
1559 | |
1560 ;;; utility functions | |
1561 ;;; | |
1562 ;;; mv-bind-clausify makes the pairs needed to have the variables in | |
1563 ;;; the variable list correspond with the values returned by the form. | |
1564 ;;; vals is a fresh symbol that intervenes in all the bindings. | |
1565 | |
1566 (defun mv-bind-clausify (vars vals) | |
1567 "MV-BIND-CLAUSIFY VARS VALS => Auxiliary list | |
1568 Forms a list of pairs `(,(nth i vars) (nth i vals)) for i from 0 to | |
1569 the length of VARS (a list of symbols). VALS is just a fresh symbol." | |
1570 (if (or (nlistp vars) | |
1571 (notevery 'symbolp vars)) | |
1572 (error "expected a list of symbols, not `%s'" | |
1573 (prin1-to-string vars))) | |
1574 (let* ((nvars (length vars)) | |
1575 (clauses '())) | |
1576 (dotimes (n nvars clauses) | |
1577 (setq clauses (cons (list (nth n vars) | |
1578 (list 'nth n vals)) clauses))))) | |
1579 | |
1580 ;;;; end of cl-multiple-values.el | |
1581 | |
1582 ;;;; ARITH | |
1583 ;;;; This file provides integer arithmetic extensions. Although | |
1584 ;;;; Emacs Lisp doesn't really support anything but integers, that | |
1585 ;;;; has still to be made to look more or less standard. | |
1586 ;;;; | |
1587 ;;;; | |
1588 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
1589 ;;;; (quiroz@cs.rochester.edu) | |
1590 | |
1591 | |
1592 (defun plusp (number) | |
1593 "True if NUMBER is strictly greater than zero." | |
1594 (> number 0)) | |
1595 | |
1596 (defun minusp (number) | |
1597 "True if NUMBER is strictly less than zero." | |
1598 (< number 0)) | |
1599 | |
1600 (defun oddp (number) | |
1601 "True if INTEGER is not divisible by 2." | |
1602 (/= (% number 2) 0)) | |
1603 | |
1604 (defun evenp (number) | |
1605 "True if INTEGER is divisible by 2." | |
1606 (= (% number 2) 0)) | |
1607 | |
1608 (defun abs (number) | |
1609 "Return the absolute value of NUMBER." | |
1610 (if (< number 0) | |
1611 (- number) | |
1612 number)) | |
1613 | |
1614 (defun signum (number) | |
1615 "Return -1, 0 or 1 according to the sign of NUMBER." | |
1616 (cond ((< number 0) | |
1617 -1) | |
1618 ((> number 0) | |
1619 1) | |
1620 (t ;exactly zero | |
1621 0))) | |
1622 | |
1623 (defun gcd (&rest integers) | |
1624 "Return the greatest common divisor of all the arguments. | |
1625 The arguments must be integers. With no arguments, value is zero." | |
1626 (let ((howmany (length integers))) | |
1627 (cond ((= howmany 0) | |
1628 0) | |
1629 ((= howmany 1) | |
1630 (abs (car integers))) | |
1631 ((> howmany 2) | |
1632 (apply (function gcd) | |
1633 (cons (gcd (nth 0 integers) (nth 1 integers)) | |
1634 (nthcdr 2 integers)))) | |
1635 (t ;howmany=2 | |
1636 ;; essentially the euclidean algorithm | |
1637 (when (zerop (* (nth 0 integers) (nth 1 integers))) | |
1638 (error "a zero argument is invalid for `gcd'")) | |
1639 (do* ((absa (abs (nth 0 integers))) ; better to operate only | |
1640 (absb (abs (nth 1 integers))) ;on positives. | |
1641 (dd (max absa absb)) ; setup correct order for the | |
1642 (ds (min absa absb)) ;succesive divisions. | |
1643 ;; intermediate results | |
1644 (q 0) | |
1645 (r 0) | |
1646 ;; final results | |
1647 (done nil) ; flag: end of iterations | |
1648 (result 0)) ; final value | |
1649 (done result) | |
1650 (setq q (/ dd ds)) | |
1651 (setq r (% dd ds)) | |
1652 (cond ((zerop r) (setq done t) (setq result ds)) | |
1653 (t (setq dd ds) (setq ds r)))))))) | |
1654 | |
1655 (defun lcm (integer &rest more) | |
1656 "Return the least common multiple of all the arguments. | |
1657 The arguments must be integers and there must be at least one of them." | |
1658 (let ((howmany (length more)) | |
1659 (a integer) | |
1660 (b (nth 0 more)) | |
1661 prod ; intermediate product | |
1662 (yetmore (nthcdr 1 more))) | |
1663 (cond ((zerop howmany) | |
1664 (abs a)) | |
1665 ((> howmany 1) ; recursive case | |
1666 (apply (function lcm) | |
1667 (cons (lcm a b) yetmore))) | |
1668 (t ; base case, just 2 args | |
1669 (setq prod (* a b)) | |
1670 (cond | |
1671 ((zerop prod) | |
1672 0) | |
1673 (t | |
1674 (/ (abs prod) (gcd a b)))))))) | |
1675 | |
1676 (defun isqrt (number) | |
1677 "Return the integer square root of NUMBER. | |
1678 NUMBER must not be negative. Result is largest integer less than or | |
1679 equal to the real square root of the argument." | |
1680 ;; The method used here is essentially the Newtonian iteration | |
1681 ;; x[n+1] <- (x[n] + Number/x[n]) / 2 | |
1682 ;; suitably adapted to integer arithmetic. | |
1683 ;; Thanks to Philippe Schnoebelen <phs@lifia.imag.fr> for suggesting the | |
1684 ;; termination condition. | |
1685 (cond ((minusp number) | |
1686 (error "argument to `isqrt' (%d) must not be negative" | |
1687 number)) | |
1688 ((zerop number) | |
1689 0) | |
1690 (t ;so (>= number 0) | |
1691 (do* ((approx 1) ;any positive integer will do | |
1692 (new 0) ;init value irrelevant | |
1693 (done nil)) | |
1694 (done (if (> (* approx approx) number) | |
1695 (- approx 1) | |
1696 approx)) | |
1697 (setq new (/ (+ approx (/ number approx)) 2) | |
1698 done (or (= new approx) (= new (+ approx 1))) | |
1699 approx new))))) | |
1700 | |
1701 (defun floor (number &optional divisor) | |
1702 "Divide DIVIDEND by DIVISOR, rounding toward minus infinity. | |
1703 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1704 (cond | |
1705 ((and (null divisor) ; trivial case | |
1706 (numberp number)) | |
1707 (values number 0)) | |
1708 (t ; do the division | |
1709 (multiple-value-bind | |
1710 (q r s) | |
1711 (safe-idiv number divisor) | |
1712 (cond ((zerop s) | |
1713 (values 0 0)) | |
1714 ((plusp s) | |
1715 (values q r)) | |
1716 (t ;opposite-signs case | |
1717 (if (zerop r) | |
1718 (values (- q) 0) | |
1719 (let ((q (- (+ q 1)))) | |
1720 (values q (- number (* q divisor))))))))))) | |
1721 | |
1722 (defun ceiling (number &optional divisor) | |
1723 "Divide DIVIDEND by DIVISOR, rounding toward plus infinity. | |
1724 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1725 (cond | |
1726 ((and (null divisor) ; trivial case | |
1727 (numberp number)) | |
1728 (values number 0)) | |
1729 (t ; do the division | |
1730 (multiple-value-bind | |
1731 (q r s) | |
1732 (safe-idiv number divisor) | |
1733 (cond ((zerop s) | |
1734 (values 0 0)) | |
1735 ((plusp s) | |
1736 (values (+ q 1) (- r divisor))) | |
1737 (t | |
1738 (values (- q) (+ number (* q divisor))))))))) | |
1739 | |
1740 (defun truncate (number &optional divisor) | |
1741 "Divide DIVIDEND by DIVISOR, rounding toward zero. | |
1742 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1743 (cond | |
1744 ((and (null divisor) ; trivial case | |
1745 (numberp number)) | |
1746 (values number 0)) | |
1747 (t ; do the division | |
1748 (multiple-value-bind | |
1749 (q r s) | |
1750 (safe-idiv number divisor) | |
1751 (cond ((zerop s) | |
1752 (values 0 0)) | |
1753 ((plusp s) ;same as floor | |
1754 (values q r)) | |
1755 (t ;same as ceiling | |
1756 (values (- q) (+ number (* q divisor))))))))) | |
1757 | |
1758 (defun round (number &optional divisor) | |
1759 "Divide DIVIDEND by DIVISOR, rounding to nearest integer. | |
1760 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1761 (cond ((and (null divisor) ; trivial case | |
1762 (numberp number)) | |
1763 (values number 0)) | |
1764 (t ; do the division | |
1765 (multiple-value-bind | |
1766 (q r s) | |
1767 (safe-idiv number divisor) | |
1768 (setq r (abs r)) | |
1769 ;; adjust magnitudes first, and then signs | |
1770 (let ((other-r (- (abs divisor) r))) | |
1771 (cond ((> r other-r) | |
1772 (setq q (+ q 1))) | |
1773 ((and (= r other-r) | |
1774 (oddp q)) | |
1775 ;; round to even is mandatory | |
1776 (setq q (+ q 1)))) | |
1777 (setq q (* s q)) | |
1778 (setq r (- number (* q divisor))) | |
1779 (values q r)))))) | |
1780 | |
1781 (defun mod (number divisor) | |
1782 "Return remainder of X by Y (rounding quotient toward minus infinity). | |
1783 That is, the remainder goes with the quotient produced by `floor'." | |
1784 (multiple-value-bind (q r) (floor number divisor) | |
1785 r)) | |
1786 | |
1787 (defun rem (number divisor) | |
1788 "Return remainder of X by Y (rounding quotient toward zero). | |
1789 That is, the remainder goes with the quotient produced by `truncate'." | |
1790 (multiple-value-bind (q r) (truncate number divisor) | |
1791 r)) | |
1792 | |
1793 ;;; internal utilities | |
1794 ;;; | |
1795 ;;; safe-idiv performs an integer division with positive numbers only. | |
1796 ;;; It is known that some machines/compilers implement weird remainder | |
1797 ;;; computations when working with negatives, so the idea here is to | |
1798 ;;; make sure we know what is coming back to the caller in all cases. | |
1799 | |
1800 ;;; Signum computation fixed by mad@math.keio.JUNET (MAEDA Atusi) | |
1801 | |
1802 (defun safe-idiv (a b) | |
1803 "SAFE-IDIV A B => Q R S | |
1804 Q=|A|/|B|, R is the rest, S is the sign of A/B." | |
1805 (unless (and (numberp a) (numberp b)) | |
1806 (error "arguments to `safe-idiv' must be numbers")) | |
1807 (when (zerop b) | |
1808 (error "cannot divide %d by zero" a)) | |
1809 (let* ((absa (abs a)) | |
1810 (absb (abs b)) | |
1811 (q (/ absa absb)) | |
1812 (s (* (signum a) (signum b))) | |
1813 (r (- a (* (* s q) b)))) | |
1814 (values q r s))) | |
1815 | |
1816 ;;;; end of cl-arith.el | |
1817 | |
1818 ;;;; SETF | |
1819 ;;;; This file provides the setf macro and friends. The purpose has | |
1820 ;;;; been modest, only the simplest defsetf forms are accepted. | |
1821 ;;;; Use it and enjoy. | |
1822 ;;;; | |
1823 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
1824 ;;;; (quiroz@cs.rochester.edu) | |
1825 | |
1826 | |
1827 (defkeyword :setf-update-fn | |
1828 "Property, its value is the function setf must invoke to update a | |
1829 generalized variable whose access form is a function call of the | |
1830 symbol that has this property.") | |
1831 | |
1832 (defkeyword :setf-update-doc | |
1833 "Property of symbols that have a `defsetf' update function on them, | |
1834 installed by the `defsetf' from its optional third argument.") | |
1835 | |
1836 (defmacro setf (&rest pairs) | |
1837 "Generalized `setq' that can set things other than variable values. | |
1838 A use of `setf' looks like (setf {PLACE VALUE}...). | |
1839 The behavior of (setf PLACE VALUE) is to access the generalized variable | |
1840 at PLACE and store VALUE there. It returns VALUE. If there is more | |
1841 than one PLACE and VALUE, each PLACE is set from its VALUE before | |
1842 the next PLACE is evaluated." | |
1843 (let ((nforms (length pairs))) | |
1844 ;; check the number of subforms | |
1845 (cond ((/= (% nforms 2) 0) | |
1846 (error "odd number of arguments to `setf'")) | |
1847 ((= nforms 0) | |
1848 nil) | |
1849 ((> nforms 2) | |
1850 ;; this is the recursive case | |
1851 (cons 'progn | |
1852 (do* ;collect the place-value pairs | |
1853 ((args pairs (cddr args)) | |
1854 (place (car args) (car args)) | |
1855 (value (cadr args) (cadr args)) | |
1856 (result '())) | |
1857 ((endp args) (nreverse result)) | |
1858 (setq result | |
1859 (cons (list 'setf place value) | |
1860 result))))) | |
1861 (t ;i.e., nforms=2 | |
1862 ;; this is the base case (SETF PLACE VALUE) | |
1863 (let* ((place (car pairs)) | |
1864 (value (cadr pairs)) | |
1865 (head nil) | |
1866 (updatefn nil)) | |
1867 ;; dispatch on the type of the PLACE | |
1868 (cond ((symbolp place) | |
1869 (list 'setq place value)) | |
1870 ((and (listp place) | |
1871 (setq head (car place)) | |
1872 (symbolp head) | |
1873 (setq updatefn (get head :setf-update-fn))) | |
1874 (if (or (and (consp updatefn) (eq (car updatefn) 'lambda)) | |
1875 (and (symbolp updatefn) | |
1876 (fboundp updatefn) | |
1877 (let ((defn (symbol-function updatefn))) | |
1878 (or (subrp defn) | |
1879 (and (consp defn) | |
1880 (eq (car defn) 'lambda)))))) | |
1881 (cons updatefn (append (cdr place) (list value))) | |
1882 (multiple-value-bind | |
1883 (bindings newsyms) | |
1884 (pair-with-newsyms (append (cdr place) (list value))) | |
1885 ;; this let gets new symbols to ensure adequate | |
1886 ;; order of evaluation of the subforms. | |
1887 (list 'let | |
1888 bindings | |
1889 (cons updatefn newsyms))))) | |
1890 (t | |
1891 (error "no `setf' update-function for `%s'" | |
1892 (prin1-to-string place))))))))) | |
1893 | |
1894 (defmacro defsetf (accessfn updatefn &optional docstring) | |
1895 "Define how `setf' works on a certain kind of generalized variable. | |
1896 A use of `defsetf' looks like (defsetf ACCESSFN UPDATEFN [DOCSTRING]). | |
1897 ACCESSFN is a symbol. UPDATEFN is a function or macro which takes | |
1898 one more argument than ACCESSFN does. DEFSETF defines the translation | |
1899 of (SETF (ACCESFN . ARGS) NEWVAL) to be a form like (UPDATEFN ARGS... NEWVAL). | |
1900 The function UPDATEFN must return its last arg, after performing the | |
1901 updating called for." | |
1902 ;; reject ill-formed requests. too bad one can't test for functionp | |
1903 ;; or macrop. | |
1904 (when (not (symbolp accessfn)) | |
1905 (error "first argument of `defsetf' must be a symbol, not `%s'" | |
1906 (prin1-to-string accessfn))) | |
1907 ;; update properties | |
1908 (list 'progn | |
1909 (list 'put (list 'quote accessfn) | |
1910 :setf-update-fn (list 'function updatefn)) | |
1911 (list 'put (list 'quote accessfn) :setf-update-doc docstring) | |
1912 ;; any better thing to return? | |
1913 (list 'quote accessfn))) | |
1914 | |
1915 ;;; This section provides the "default" setfs for Common-Emacs-Lisp | |
1916 ;;; The user will not normally add anything to this, although | |
1917 ;;; defstruct will introduce new ones as a matter of fact. | |
1918 ;;; | |
1919 ;;; Apply is a special case. The Common Lisp | |
1920 ;;; standard makes the case of apply be useful when the user writes | |
1921 ;;; something like (apply #'name ...), Emacs Lisp doesn't have the # | |
1922 ;;; stuff, but it has (function ...). Notice that V18 includes a new | |
1923 ;;; apply: this file is compatible with V18 and pre-V18 Emacses. | |
1924 | |
1925 ;;; INCOMPATIBILITY: the SETF macro evaluates its arguments in the | |
1926 ;;; (correct) left to right sequence *before* checking for apply | |
1927 ;;; methods (which should really be an special case inside setf). Due | |
1928 ;;; to this, the lambda expression defsetf'd to apply will succeed in | |
1929 ;;; applying the right function even if the name was not quoted, but | |
1930 ;;; computed! That extension is not Common Lisp (nor is particularly | |
1931 ;;; useful, I think). | |
1932 | |
1933 (defsetf apply | |
1934 (lambda (&rest args) | |
1935 ;; dissasemble the calling form | |
1936 ;; "(((quote fn) x1 x2 ... xn) val)" (function instead of quote, too) | |
1937 (let* ((fnform (car args)) ;functional form | |
1938 (applyargs (append ;arguments "to apply fnform" | |
1939 (apply 'list* (butlast (cdr args))) | |
1940 (last args))) | |
1941 (newupdater nil)) ; its update-fn, if any | |
1942 (if (and (symbolp fnform) | |
1943 (setq newupdater (get fnform :setf-update-fn))) | |
1944 (apply newupdater applyargs) | |
1945 (error "can't `setf' to `%s'" | |
1946 (prin1-to-string fnform))))) | |
1947 "`apply' is a special case for `setf'") | |
1948 | |
1949 | |
1950 (defsetf aref | |
1951 aset | |
1952 "`setf' inversion for `aref'") | |
1953 | |
1954 (defsetf nth | |
1955 setnth | |
1956 "`setf' inversion for `nth'") | |
1957 | |
1958 (defsetf nthcdr | |
1959 setnthcdr | |
1960 "`setf' inversion for `nthcdr'") | |
1961 | |
1962 (defsetf elt | |
1963 setelt | |
1964 "`setf' inversion for `elt'") | |
1965 | |
1966 (defsetf first | |
1967 (lambda (list val) (setnth 0 list val)) | |
1968 "`setf' inversion for `first'") | |
1969 | |
1970 (defsetf second | |
1971 (lambda (list val) (setnth 1 list val)) | |
1972 "`setf' inversion for `second'") | |
1973 | |
1974 (defsetf third | |
1975 (lambda (list val) (setnth 2 list val)) | |
1976 "`setf' inversion for `third'") | |
1977 | |
1978 (defsetf fourth | |
1979 (lambda (list val) (setnth 3 list val)) | |
1980 "`setf' inversion for `fourth'") | |
1981 | |
1982 (defsetf fifth | |
1983 (lambda (list val) (setnth 4 list val)) | |
1984 "`setf' inversion for `fifth'") | |
1985 | |
1986 (defsetf sixth | |
1987 (lambda (list val) (setnth 5 list val)) | |
1988 "`setf' inversion for `sixth'") | |
1989 | |
1990 (defsetf seventh | |
1991 (lambda (list val) (setnth 6 list val)) | |
1992 "`setf' inversion for `seventh'") | |
1993 | |
1994 (defsetf eighth | |
1995 (lambda (list val) (setnth 7 list val)) | |
1996 "`setf' inversion for `eighth'") | |
1997 | |
1998 (defsetf ninth | |
1999 (lambda (list val) (setnth 8 list val)) | |
2000 "`setf' inversion for `ninth'") | |
2001 | |
2002 (defsetf tenth | |
2003 (lambda (list val) (setnth 9 list val)) | |
2004 "`setf' inversion for `tenth'") | |
2005 | |
2006 (defsetf rest | |
2007 (lambda (list val) (setcdr list val)) | |
2008 "`setf' inversion for `rest'") | |
2009 | |
2010 (defsetf car setcar "Replace the car of a cons") | |
2011 | |
2012 (defsetf cdr setcdr "Replace the cdr of a cons") | |
2013 | |
2014 (defsetf caar | |
2015 (lambda (list val) (setcar (nth 0 list) val)) | |
2016 "`setf' inversion for `caar'") | |
2017 | |
2018 (defsetf cadr | |
2019 (lambda (list val) (setcar (cdr list) val)) | |
2020 "`setf' inversion for `cadr'") | |
2021 | |
2022 (defsetf cdar | |
2023 (lambda (list val) (setcdr (car list) val)) | |
2024 "`setf' inversion for `cdar'") | |
2025 | |
2026 (defsetf cddr | |
2027 (lambda (list val) (setcdr (cdr list) val)) | |
2028 "`setf' inversion for `cddr'") | |
2029 | |
2030 (defsetf caaar | |
2031 (lambda (list val) (setcar (caar list) val)) | |
2032 "`setf' inversion for `caaar'") | |
2033 | |
2034 (defsetf caadr | |
2035 (lambda (list val) (setcar (cadr list) val)) | |
2036 "`setf' inversion for `caadr'") | |
2037 | |
2038 (defsetf cadar | |
2039 (lambda (list val) (setcar (cdar list) val)) | |
2040 "`setf' inversion for `cadar'") | |
2041 | |
2042 (defsetf cdaar | |
2043 (lambda (list val) (setcdr (caar list) val)) | |
2044 "`setf' inversion for `cdaar'") | |
2045 | |
2046 (defsetf caddr | |
2047 (lambda (list val) (setcar (cddr list) val)) | |
2048 "`setf' inversion for `caddr'") | |
2049 | |
2050 (defsetf cdadr | |
2051 (lambda (list val) (setcdr (cadr list) val)) | |
2052 "`setf' inversion for `cdadr'") | |
2053 | |
2054 (defsetf cddar | |
2055 (lambda (list val) (setcdr (cdar list) val)) | |
2056 "`setf' inversion for `cddar'") | |
2057 | |
2058 (defsetf cdddr | |
2059 (lambda (list val) (setcdr (cddr list) val)) | |
2060 "`setf' inversion for `cdddr'") | |
2061 | |
2062 (defsetf caaaar | |
2063 (lambda (list val) (setcar (caaar list) val)) | |
2064 "`setf' inversion for `caaaar'") | |
2065 | |
2066 (defsetf caaadr | |
2067 (lambda (list val) (setcar (caadr list) val)) | |
2068 "`setf' inversion for `caaadr'") | |
2069 | |
2070 (defsetf caadar | |
2071 (lambda (list val) (setcar (cadar list) val)) | |
2072 "`setf' inversion for `caadar'") | |
2073 | |
2074 (defsetf cadaar | |
2075 (lambda (list val) (setcar (cdaar list) val)) | |
2076 "`setf' inversion for `cadaar'") | |
2077 | |
2078 (defsetf cdaaar | |
2079 (lambda (list val) (setcdr (caar list) val)) | |
2080 "`setf' inversion for `cdaaar'") | |
2081 | |
2082 (defsetf caaddr | |
2083 (lambda (list val) (setcar (caddr list) val)) | |
2084 "`setf' inversion for `caaddr'") | |
2085 | |
2086 (defsetf cadadr | |
2087 (lambda (list val) (setcar (cdadr list) val)) | |
2088 "`setf' inversion for `cadadr'") | |
2089 | |
2090 (defsetf cdaadr | |
2091 (lambda (list val) (setcdr (caadr list) val)) | |
2092 "`setf' inversion for `cdaadr'") | |
2093 | |
2094 (defsetf caddar | |
2095 (lambda (list val) (setcar (cddar list) val)) | |
2096 "`setf' inversion for `caddar'") | |
2097 | |
2098 (defsetf cdadar | |
2099 (lambda (list val) (setcdr (cadar list) val)) | |
2100 "`setf' inversion for `cdadar'") | |
2101 | |
2102 (defsetf cddaar | |
2103 (lambda (list val) (setcdr (cdaar list) val)) | |
2104 "`setf' inversion for `cddaar'") | |
2105 | |
2106 (defsetf cadddr | |
2107 (lambda (list val) (setcar (cdddr list) val)) | |
2108 "`setf' inversion for `cadddr'") | |
2109 | |
2110 (defsetf cddadr | |
2111 (lambda (list val) (setcdr (cdadr list) val)) | |
2112 "`setf' inversion for `cddadr'") | |
2113 | |
2114 (defsetf cdaddr | |
2115 (lambda (list val) (setcdr (caddr list) val)) | |
2116 "`setf' inversion for `cdaddr'") | |
2117 | |
2118 (defsetf cdddar | |
2119 (lambda (list val) (setcdr (cddar list) val)) | |
2120 "`setf' inversion for `cdddar'") | |
2121 | |
2122 (defsetf cddddr | |
2123 (lambda (list val) (setcdr (cddr list) val)) | |
2124 "`setf' inversion for `cddddr'") | |
2125 | |
2126 (defsetf get put "`setf' inversion for `get' is `put'") | |
2127 | |
2128 (defsetf symbol-function fset | |
2129 "`setf' inversion for `symbol-function' is `fset'") | |
2130 | |
2131 (defsetf symbol-plist setplist | |
2132 "`setf' inversion for `symbol-plist' is `setplist'") | |
2133 | |
2134 (defsetf symbol-value set | |
2135 "`setf' inversion for `symbol-value' is `set'") | |
2136 | |
2137 (defsetf point goto-char | |
2138 "To set (point) to N, use (goto-char N)") | |
2139 | |
2140 ;; how about defsetfing other Emacs forms? | |
2141 | |
2142 ;;; Modify macros | |
2143 ;;; | |
2144 ;;; It could be nice to implement define-modify-macro, but I don't | |
2145 ;;; think it really pays. | |
2146 | |
2147 (defmacro incf (ref &optional delta) | |
2148 "(incf REF [DELTA]) -> increment the g.v. REF by DELTA (default 1)" | |
2149 (if (null delta) | |
2150 (setq delta 1)) | |
2151 (list 'setf ref (list '+ ref delta))) | |
2152 | |
2153 (defmacro decf (ref &optional delta) | |
2154 "(decf REF [DELTA]) -> decrement the g.v. REF by DELTA (default 1)" | |
2155 (if (null delta) | |
2156 (setq delta 1)) | |
2157 (list 'setf ref (list '- ref delta))) | |
2158 | |
2159 (defmacro push (item ref) | |
2160 "(push ITEM REF) -> cons ITEM at the head of the g.v. REF (a list)" | |
2161 (list 'setf ref (list 'cons item ref))) | |
2162 | |
2163 (defmacro pushnew (item ref) | |
2164 "(pushnew ITEM REF): adjoin ITEM at the head of the g.v. REF (a list)" | |
2165 (list 'setf ref (list 'adjoin item ref))) | |
2166 | |
2167 (defmacro pop (ref) | |
2168 "(pop REF) -> (prog1 (car REF) (setf REF (cdr REF)))" | |
2169 (let ((listname (gensym))) | |
2170 (list 'let (list (list listname ref)) | |
2171 (list 'prog1 | |
2172 (list 'car listname) | |
2173 (list 'setf ref (list 'cdr listname)))))) | |
2174 | |
2175 ;;; PSETF | |
2176 ;;; | |
2177 ;;; Psetf is the generalized variable equivalent of psetq. The right | |
2178 ;;; hand sides are evaluated and assigned (via setf) to the left hand | |
2179 ;;; sides. The evaluations are done in an environment where they | |
2180 ;;; appear to occur in parallel. | |
2181 | |
2182 (defmacro psetf (&rest body) | |
2183 "(psetf {var value }...) => nil | |
2184 Like setf, but all the values are computed before any assignment is made." | |
2185 (let ((length (length body))) | |
2186 (cond ((/= (% length 2) 0) | |
2187 (error "psetf needs an even number of arguments, %d given" | |
2188 length)) | |
2189 ((null body) | |
2190 '()) | |
2191 (t | |
2192 (list 'prog1 nil | |
2193 (let ((setfs '()) | |
2194 (bodyforms (reverse body))) | |
2195 (while bodyforms | |
2196 (let* ((value (car bodyforms)) | |
2197 (place (cadr bodyforms))) | |
2198 (setq bodyforms (cddr bodyforms)) | |
2199 (if (null setfs) | |
2200 (setq setfs (list 'setf place value)) | |
2201 (setq setfs (list 'setf place | |
2202 (list 'prog1 value | |
2203 setfs)))))) | |
2204 setfs)))))) | |
2205 | |
2206 ;;; SHIFTF and ROTATEF | |
2207 ;;; | |
2208 | |
2209 (defmacro shiftf (&rest forms) | |
2210 "(shiftf PLACE1 PLACE2... NEWVALUE) | |
2211 Set PLACE1 to PLACE2, PLACE2 to PLACE3... | |
2212 Each PLACE is set to the old value of the following PLACE, | |
2213 and the last PLACE is set to the value NEWVALUE. | |
2214 Returns the old value of PLACE1." | |
2215 (unless (> (length forms) 1) | |
2216 (error "`shiftf' needs more than one argument")) | |
2217 (let ((places (butlast forms)) | |
2218 (newvalue (car (last forms)))) | |
2219 ;; the places are accessed to fresh symbols | |
2220 (multiple-value-bind | |
2221 (bindings newsyms) | |
2222 (pair-with-newsyms places) | |
2223 (list 'let bindings | |
2224 (cons 'setf | |
2225 (zip-lists places | |
2226 (append (cdr newsyms) (list newvalue)))) | |
2227 (car newsyms))))) | |
2228 | |
2229 (defmacro rotatef (&rest places) | |
2230 "(rotatef PLACE...) sets each PLACE to the old value of the following PLACE. | |
2231 The last PLACE is set to the old value of the first PLACE. | |
2232 Thus, the values rotate through the PLACEs. Returns nil." | |
2233 (if (null places) | |
2234 nil | |
2235 (multiple-value-bind | |
2236 (bindings newsyms) | |
2237 (pair-with-newsyms places) | |
2238 (list | |
2239 'let bindings | |
2240 (cons 'setf | |
2241 (zip-lists places | |
2242 (append (cdr newsyms) (list (car newsyms))))) | |
2243 nil)))) | |
2244 | |
2245 ;;;; STRUCTS | |
2246 ;;;; This file provides the structures mechanism. See the | |
2247 ;;;; documentation for Common-Lisp's defstruct. Mine doesn't | |
2248 ;;;; implement all the functionality of the standard, although some | |
2249 ;;;; more could be grafted if so desired. More details along with | |
2250 ;;;; the code. | |
2251 ;;;; | |
2252 ;;;; | |
2253 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
2254 ;;;; (quiroz@cs.rochester.edu) | |
2255 | |
2256 | |
2257 (defkeyword :include "Syntax of `defstruct'") | |
2258 (defkeyword :named "Syntax of `defstruct'") | |
2259 (defkeyword :conc-name "Syntax of `defstruct'") | |
2260 (defkeyword :copier "Syntax of `defstruct'") | |
2261 (defkeyword :predicate "Syntax of `defstruct'") | |
2262 (defkeyword :print-function "Syntax of `defstruct'") | |
2263 (defkeyword :type "Syntax of `defstruct'") | |
2264 (defkeyword :initial-offset "Syntax of `defstruct'") | |
2265 | |
2266 (defkeyword :structure-doc "Documentation string for a structure.") | |
2267 (defkeyword :structure-slotsn "Number of slots in structure") | |
2268 (defkeyword :structure-slots "List of the slot's names") | |
2269 (defkeyword :structure-indices "List of (KEYWORD-NAME . INDEX)") | |
2270 (defkeyword :structure-initforms "List of (KEYWORD-NAME . INITFORM)") | |
2271 (defkeyword :structure-includes | |
2272 "() or list of a symbol, that this struct includes") | |
2273 (defkeyword :structure-included-in | |
2274 "List of the structs that include this") | |
2275 | |
2276 | |
2277 (defmacro defstruct (&rest args) | |
2278 "(defstruct NAME [DOC-STRING] . SLOTS) define NAME as structure type. | |
2279 NAME must be a symbol, the name of the new structure. It could also | |
2280 be a list (NAME . OPTIONS). | |
2281 | |
2282 Each option is either a symbol, or a list of a keyword symbol taken from the | |
2283 list \{:conc-name, :copier, :constructor, :predicate, :include, | |
2284 :print-function, :type, :initial-offset\}. The meanings of these are as in | |
2285 CLtL, except that no BOA-constructors are provided, and the options | |
2286 \{:print-fuction, :type, :initial-offset\} are ignored quietly. All these | |
2287 structs are named, in the sense that their names can be used for type | |
2288 discrimination. | |
2289 | |
2290 The DOC-STRING is established as the `structure-doc' property of NAME. | |
2291 | |
2292 The SLOTS are one or more of the following: | |
2293 SYMBOL -- meaning the SYMBOL is the name of a SLOT of NAME | |
2294 list of SYMBOL and VALUE -- meaning that VALUE is the initial value of | |
2295 the slot. | |
2296 `defstruct' defines functions `make-NAME', `NAME-p', `copy-NAME' for the | |
2297 structure, and functions with the same name as the slots to access | |
2298 them. `setf' of the accessors sets their values." | |
2299 (multiple-value-bind | |
2300 (name options docstring slotsn slots initlist) | |
2301 (parse$defstruct$args args) | |
2302 ;; Names for the member functions come from the options. The | |
2303 ;; slots* stuff collects info about the slots declared explicitly. | |
2304 (multiple-value-bind | |
2305 (conc-name constructor copier predicate | |
2306 moreslotsn moreslots moreinits included) | |
2307 (parse$defstruct$options name options slots) | |
2308 ;; The moreslots* stuff refers to slots gained as a consequence | |
2309 ;; of (:include clauses). -- Oct 89: Only one :include tolerated | |
2310 (when (and (numberp moreslotsn) | |
2311 (> moreslotsn 0)) | |
2312 (setf slotsn (+ slotsn moreslotsn)) | |
2313 (setf slots (append moreslots slots)) | |
2314 (setf initlist (append moreinits initlist))) | |
2315 (unless (> slotsn 0) | |
2316 (error "%s needs at least one slot" | |
2317 (prin1-to-string name))) | |
2318 (let ((dups (duplicate-symbols-p slots))) | |
2319 (when dups | |
2320 (error "`%s' are duplicates" | |
2321 (prin1-to-string dups)))) | |
2322 (setq initlist (simplify$inits slots initlist)) | |
2323 (let (properties functions keywords accessors alterators returned) | |
2324 ;; compute properties of NAME | |
2325 (setq properties | |
2326 (append | |
2327 (list | |
2328 (list 'put (list 'quote name) :structure-doc | |
2329 docstring) | |
2330 (list 'put (list 'quote name) :structure-slotsn | |
2331 slotsn) | |
2332 (list 'put (list 'quote name) :structure-slots | |
2333 (list 'quote slots)) | |
2334 (list 'put (list 'quote name) :structure-initforms | |
2335 (list 'quote initlist)) | |
2336 (list 'put (list 'quote name) :structure-indices | |
2337 (list 'quote (extract$indices initlist)))) | |
2338 ;; If this definition :includes another defstruct, | |
2339 ;; modify both property lists. | |
2340 (cond (included | |
2341 (list | |
2342 (list 'put | |
2343 (list 'quote name) | |
2344 :structure-includes | |
2345 (list 'quote included)) | |
2346 (list 'pushnew | |
2347 (list 'quote name) | |
2348 (list 'get (list 'quote (car included)) | |
2349 :structure-included-in)))) | |
2350 (t | |
2351 (list | |
2352 (let ((old (gensym))) | |
2353 (list 'let | |
2354 (list (list old | |
2355 (list 'car | |
2356 (list 'get | |
2357 (list 'quote name) | |
2358 :structure-includes)))) | |
2359 (list 'when old | |
2360 (list 'put | |
2361 old | |
2362 :structure-included-in | |
2363 (list 'delq | |
2364 (list 'quote name) | |
2365 ;; careful with destructive | |
2366 ;;manipulation! | |
2367 (list | |
2368 'append | |
2369 (list | |
2370 'get | |
2371 old | |
2372 :structure-included-in) | |
2373 '()) | |
2374 ))))) | |
2375 (list 'put | |
2376 (list 'quote name) | |
2377 :structure-includes | |
2378 '())))) | |
2379 ;; If this definition used to be :included in another, warn | |
2380 ;; that things make break. On the other hand, the redefinition | |
2381 ;; may be trivial, so don't call it an error. | |
2382 (let ((old (gensym))) | |
2383 (list | |
2384 (list 'let | |
2385 (list (list old (list 'get | |
2386 (list 'quote name) | |
2387 :structure-included-in))) | |
2388 (list 'when old | |
2389 (list 'message | |
2390 "`%s' redefined. Should redefine `%s'?" | |
2391 (list 'quote name) | |
2392 (list 'prin1-to-string old)))))))) | |
2393 | |
2394 ;; Compute functions associated with NAME. This is not | |
2395 ;; handling BOA constructors yet, but here would be the place. | |
2396 (setq functions | |
2397 (list | |
2398 (list 'fset (list 'quote constructor) | |
2399 (list 'function | |
2400 (list 'lambda (list '&rest 'args) | |
2401 (list 'make$structure$instance | |
2402 (list 'quote name) | |
2403 'args)))) | |
2404 (list 'fset (list 'quote copier) | |
2405 (list 'function | |
2406 (list 'lambda (list 'struct) | |
2407 (list 'copy-sequence 'struct)))) | |
2408 (let ((typetag (gensym))) | |
2409 (list 'fset (list 'quote predicate) | |
2410 (list | |
2411 'function | |
2412 (list | |
2413 'lambda (list 'thing) | |
2414 (list 'and | |
2415 (list 'vectorp 'thing) | |
2416 (list 'let | |
2417 (list (list typetag | |
2418 (list 'elt 'thing 0))) | |
2419 (list 'or | |
2420 (list | |
2421 'and | |
2422 (list 'eq | |
2423 typetag | |
2424 (list 'quote name)) | |
2425 (list '= | |
2426 (list 'length 'thing) | |
2427 (1+ slotsn))) | |
2428 (list | |
2429 'memq | |
2430 typetag | |
2431 (list 'get | |
2432 (list 'quote name) | |
2433 :structure-included-in)))))) | |
2434 ))))) | |
2435 ;; compute accessors for NAME's slots | |
2436 (multiple-value-setq | |
2437 (accessors alterators keywords) | |
2438 (build$accessors$for name conc-name predicate slots slotsn)) | |
2439 ;; generate returned value -- not defined by the standard | |
2440 (setq returned | |
2441 (list | |
2442 (cons 'vector | |
2443 (mapcar | |
2444 '(lambda (x) (list 'quote x)) | |
2445 (cons name slots))))) | |
2446 ;; generate code | |
2447 (cons 'progn | |
2448 (nconc properties functions keywords | |
2449 accessors alterators returned)))))) | |
2450 | |
2451 (defun parse$defstruct$args (args) | |
2452 "(parse$defstruct$args ARGS) => NAME OPTIONS DOCSTRING SLOTSN SLOTS INITLIST | |
2453 NAME=symbol, OPTIONS=list of, DOCSTRING=string, SLOTSN=count of slots, | |
2454 SLOTS=list of their names, INITLIST=alist (keyword . initform)." | |
2455 (let (name ;args=(symbol...) or ((symbol...)...) | |
2456 options ;args=((symbol . options) ...) | |
2457 (docstring "") ;args=(head docstring . slotargs) | |
2458 slotargs ;second or third cdr of args | |
2459 (slotsn 0) ;number of slots | |
2460 (slots '()) ;list of slot names | |
2461 (initlist '())) ;list of (slot keyword . initform) | |
2462 ;; extract name and options | |
2463 (cond ((symbolp (car args)) ;simple name | |
2464 (setq name (car args) | |
2465 options '())) | |
2466 ((and (listp (car args)) ;(name . options) | |
2467 (symbolp (caar args))) | |
2468 (setq name (caar args) | |
2469 options (cdar args))) | |
2470 (t | |
2471 (error "first arg to `defstruct' must be symbol or (symbol ...)"))) | |
2472 (setq slotargs (cdr args)) | |
2473 ;; is there a docstring? | |
2474 (when (stringp (car slotargs)) | |
2475 (setq docstring (car slotargs) | |
2476 slotargs (cdr slotargs))) | |
2477 ;; now for the slots | |
2478 (multiple-value-bind | |
2479 (slotsn slots initlist) | |
2480 (process$slots slotargs) | |
2481 (values name options docstring slotsn slots initlist)))) | |
2482 | |
2483 (defun process$slots (slots) | |
2484 "(process$slots SLOTS) => SLOTSN SLOTSLIST INITLIST | |
2485 Converts a list of symbols or lists of symbol and form into the last 3 | |
2486 values returned by PARSE$DEFSTRUCT$ARGS." | |
2487 (let ((slotsn (length slots)) ;number of slots | |
2488 slotslist ;(slot1 slot2 ...) | |
2489 initlist) ;((:slot1 . init1) ...) | |
2490 (do* | |
2491 ((ptr slots (cdr ptr)) | |
2492 (this (car ptr) (car ptr))) | |
2493 ((endp ptr)) | |
2494 (cond ((symbolp this) | |
2495 (setq slotslist (cons this slotslist)) | |
2496 (setq initlist (acons (keyword-of this) nil initlist))) | |
2497 ((and (listp this) | |
2498 (symbolp (car this))) | |
2499 (let ((name (car this)) | |
2500 (form (cadr this))) | |
2501 ;; this silently ignores any slot options. bad... | |
2502 (setq slotslist (cons name slotslist)) | |
2503 (setq initlist (acons (keyword-of name) form initlist)))) | |
2504 (t | |
2505 (error "slot should be symbol or (symbol ...), not `%s'" | |
2506 (prin1-to-string this))))) | |
2507 (values slotsn (nreverse slotslist) (nreverse initlist)))) | |
2508 | |
2509 (defun parse$defstruct$options (name options slots) | |
2510 "(parse$defstruct$options name OPTIONS SLOTS) => many values | |
2511 A defstruct named NAME, with options list OPTIONS, has already slots SLOTS. | |
2512 Parse the OPTIONS and return the updated form of the struct's slots and other | |
2513 information. The values returned are: | |
2514 | |
2515 CONC-NAME is the string to use as prefix/suffix in the methods, | |
2516 CONST is the name of the official constructor, | |
2517 COPIER is the name of the structure copier, | |
2518 PRED is the name of the type predicate, | |
2519 MORESLOTSN is the number of slots added by :include, | |
2520 MORESLOTS is the list of slots added by :include, | |
2521 MOREINITS is the list of initialization forms added by :include, | |
2522 INCLUDED is nil, or the list of the symbol added by :include" | |
2523 (let* ((namestring (symbol-name name)) | |
2524 ;; to build the return values | |
2525 (conc-name (concat namestring "-")) | |
2526 (const (intern (concat "make-" namestring))) | |
2527 (copier (intern (concat "copy-" namestring))) | |
2528 (pred (intern (concat namestring "-p"))) | |
2529 (moreslotsn 0) | |
2530 (moreslots '()) | |
2531 (moreinits '()) | |
2532 ;; auxiliaries | |
2533 option-head ;When an option is not a plain | |
2534 option-second ; keyword, it must be a list of | |
2535 option-rest ; the form (head second . rest) | |
2536 these-slotsn ;When :include is found, the | |
2537 these-slots ; info about the included | |
2538 these-inits ; structure is added here. | |
2539 included ;NIL or (list INCLUDED) | |
2540 ) | |
2541 ;; Values above are the defaults. Now we read the options themselves | |
2542 (dolist (option options) | |
2543 ;; 2 cases arise, as options must be a keyword or a list | |
2544 (cond | |
2545 ((keywordp option) | |
2546 (case option | |
2547 (:named | |
2548 ) ;ignore silently | |
2549 (t | |
2550 (error "can't recognize option `%s'" | |
2551 (prin1-to-string option))))) | |
2552 ((and (listp option) | |
2553 (keywordp (setq option-head (car option)))) | |
2554 (setq option-second (second option)) | |
2555 (setq option-rest (nthcdr 2 option)) | |
2556 (case option-head | |
2557 (:conc-name | |
2558 (setq conc-name | |
2559 (cond | |
2560 ((stringp option-second) | |
2561 option-second) | |
2562 ((null option-second) | |
2563 "") | |
2564 (t | |
2565 (error "`%s' is invalid as `conc-name'" | |
2566 (prin1-to-string option-second)))))) | |
2567 (:copier | |
2568 (setq copier | |
2569 (cond | |
2570 ((and (symbolp option-second) | |
2571 (null option-rest)) | |
2572 option-second) | |
2573 (t | |
2574 (error "can't recognize option `%s'" | |
2575 (prin1-to-string option)))))) | |
2576 | |
2577 (:constructor ;no BOA-constructors allowed | |
2578 (setq const | |
2579 (cond | |
2580 ((and (symbolp option-second) | |
2581 (null option-rest)) | |
2582 option-second) | |
2583 (t | |
2584 (error "can't recognize option `%s'" | |
2585 (prin1-to-string option)))))) | |
2586 (:predicate | |
2587 (setq pred | |
2588 (cond | |
2589 ((and (symbolp option-second) | |
2590 (null option-rest)) | |
2591 option-second) | |
2592 (t | |
2593 (error "can't recognize option `%s'" | |
2594 (prin1-to-string option)))))) | |
2595 (:include | |
2596 (unless (symbolp option-second) | |
2597 (error "arg to `:include' should be a symbol, not `%s'" | |
2598 (prin1-to-string option-second))) | |
2599 (setq these-slotsn (get option-second :structure-slotsn) | |
2600 these-slots (get option-second :structure-slots) | |
2601 these-inits (get option-second :structure-initforms)) | |
2602 (unless (and (numberp these-slotsn) | |
2603 (> these-slotsn 0)) | |
2604 (error "`%s' is not a valid structure" | |
2605 (prin1-to-string option-second))) | |
2606 (if included | |
2607 (error "`%s' already includes `%s', can't include `%s' too" | |
2608 name (car included) option-second) | |
2609 (push option-second included)) | |
2610 (multiple-value-bind | |
2611 (xtra-slotsn xtra-slots xtra-inits) | |
2612 (process$slots option-rest) | |
2613 (when (> xtra-slotsn 0) | |
2614 (dolist (xslot xtra-slots) | |
2615 (unless (memq xslot these-slots) | |
2616 (error "`%s' is not a slot of `%s'" | |
2617 (prin1-to-string xslot) | |
2618 (prin1-to-string option-second)))) | |
2619 (setq these-inits (append xtra-inits these-inits))) | |
2620 (setq moreslotsn (+ moreslotsn these-slotsn)) | |
2621 (setq moreslots (append these-slots moreslots)) | |
2622 (setq moreinits (append these-inits moreinits)))) | |
2623 ((:print-function :type :initial-offset) | |
2624 ) ;ignore silently | |
2625 (t | |
2626 (error "can't recognize option `%s'" | |
2627 (prin1-to-string option))))) | |
2628 (t | |
2629 (error "can't recognize option `%s'" | |
2630 (prin1-to-string option))))) | |
2631 ;; Return values found | |
2632 (values conc-name const copier pred | |
2633 moreslotsn moreslots moreinits | |
2634 included))) | |
2635 | |
2636 (defun simplify$inits (slots initlist) | |
2637 "(simplify$inits SLOTS INITLIST) => new INITLIST | |
2638 Removes from INITLIST - an ALIST - any shadowed bindings." | |
2639 (let ((result '()) ;built here | |
2640 key ;from the slot | |
2641 ) | |
2642 (dolist (slot slots) | |
2643 (setq key (keyword-of slot)) | |
2644 (setq result (acons key (cdr (assoc key initlist)) result))) | |
2645 (nreverse result))) | |
2646 | |
2647 (defun extract$indices (initlist) | |
2648 "(extract$indices INITLIST) => indices list | |
2649 Kludge. From a list of pairs (keyword . form) build a list of pairs | |
2650 of the form (keyword . position in list from 0). Useful to precompute | |
2651 some of the work of MAKE$STRUCTURE$INSTANCE." | |
2652 (let ((result '()) | |
2653 (index 0)) | |
2654 (dolist (entry initlist (nreverse result)) | |
2655 (setq result (acons (car entry) index result) | |
2656 index (+ index 1))))) | |
2657 | |
2658 (defun build$accessors$for (name conc-name predicate slots slotsn) | |
2659 "(build$accessors$for NAME PREDICATE SLOTS SLOTSN) => FSETS DEFSETFS KWDS | |
2660 Generate the code for accesors and defsetfs of a structure called | |
2661 NAME, whose slots are SLOTS. Also, establishes the keywords for the | |
2662 slots names." | |
2663 (do ((i 0 (1+ i)) | |
2664 (accessors '()) | |
2665 (alterators '()) | |
2666 (keywords '()) | |
2667 (canonic "")) ;slot name with conc-name prepended | |
2668 ((>= i slotsn) | |
2669 (values | |
2670 (nreverse accessors) (nreverse alterators) (nreverse keywords))) | |
2671 (setq canonic (intern (concat conc-name (symbol-name (nth i slots))))) | |
2672 (setq accessors | |
2673 (cons | |
2674 (list 'fset (list 'quote canonic) | |
2675 (list 'function | |
2676 (list 'lambda (list 'object) | |
2677 (list 'cond | |
2678 (list (list predicate 'object) | |
2679 (list 'aref 'object (1+ i))) | |
2680 (list 't | |
2681 (list 'error | |
2682 "`%s' is not a struct %s" | |
2683 (list 'prin1-to-string | |
2684 'object) | |
2685 (list 'prin1-to-string | |
2686 (list 'quote | |
2687 name)))))))) | |
2688 accessors)) | |
2689 (setq alterators | |
2690 (cons | |
2691 (list 'defsetf canonic | |
2692 (list 'lambda (list 'object 'newval) | |
2693 (list 'cond | |
2694 (list (list predicate 'object) | |
2695 (list 'aset 'object (1+ i) 'newval)) | |
2696 (list 't | |
2697 (list 'error | |
2698 "`%s' not a `%s'" | |
2699 (list 'prin1-to-string | |
2700 'object) | |
2701 (list 'prin1-to-string | |
2702 (list 'quote | |
2703 name))))))) | |
2704 alterators)) | |
2705 (setq keywords | |
2706 (cons (list 'defkeyword (keyword-of (nth i slots))) | |
2707 keywords)))) | |
2708 | |
2709 (defun make$structure$instance (name args) | |
2710 "(make$structure$instance NAME ARGS) => new struct NAME | |
2711 A struct of type NAME is created, some slots might be initialized | |
2712 according to ARGS (the &rest argument of MAKE-name)." | |
2713 (unless (symbolp name) | |
2714 (error "`%s' is not a possible name for a structure" | |
2715 (prin1-to-string name))) | |
2716 (let ((initforms (get name :structure-initforms)) | |
2717 (slotsn (get name :structure-slotsn)) | |
2718 (indices (get name :structure-indices)) | |
2719 initalist ;pairlis'd on initforms | |
2720 initializers ;definitive initializers | |
2721 ) | |
2722 ;; check sanity of the request | |
2723 (unless (and (numberp slotsn) | |
2724 (> slotsn 0)) | |
2725 (error "`%s' is not a defined structure" | |
2726 (prin1-to-string name))) | |
2727 (unless (evenp (length args)) | |
2728 (error "slot initializers `%s' not of even length" | |
2729 (prin1-to-string args))) | |
2730 ;; analyze the initializers provided by the call | |
2731 (multiple-value-bind | |
2732 (speckwds specvals) ;keywords and values given | |
2733 (unzip-list args) ; by the user | |
2734 ;; check that all the arguments are introduced by keywords | |
2735 (unless (every (function keywordp) speckwds) | |
2736 (error "all of the names in `%s' should be keywords" | |
2737 (prin1-to-string speckwds))) | |
2738 ;; check that all the keywords are known | |
2739 (dolist (kwd speckwds) | |
2740 (unless (numberp (cdr (assoc kwd indices))) | |
2741 (error "`%s' is not a valid slot name for %s" | |
2742 (prin1-to-string kwd) (prin1-to-string name)))) | |
2743 ;; update initforms | |
2744 (setq initalist | |
2745 (pairlis speckwds | |
2746 (do* ;;protect values from further evaluation | |
2747 ((ptr specvals (cdr ptr)) | |
2748 (val (car ptr) (car ptr)) | |
2749 (result '())) | |
2750 ((endp ptr) (nreverse result)) | |
2751 (setq result | |
2752 (cons (list 'quote val) | |
2753 result))) | |
2754 (copy-sequence initforms))) | |
2755 ;; compute definitive initializers | |
2756 (setq initializers | |
2757 (do* ;;gather the values of the most definitive forms | |
2758 ((ptr indices (cdr ptr)) | |
2759 (key (caar ptr) (caar ptr)) | |
2760 (result '())) | |
2761 ((endp ptr) (nreverse result)) | |
2762 (setq result | |
2763 (cons (eval (cdr (assoc key initalist))) result)))) | |
2764 ;; do real initialization | |
2765 (apply (function vector) | |
2766 (cons name initializers))))) | |
2767 | |
2768 ;;;; end of cl-structs.el | |
2769 | |
2770 ;;; For lisp-interaction mode, so that multiple values can be seen when passed | |
2771 ;;; back. Lies every now and then... | |
2772 | |
2773 (defvar - nil "form currently under evaluation") | |
2774 (defvar + nil "previous -") | |
2775 (defvar ++ nil "previous +") | |
2776 (defvar +++ nil "previous ++") | |
2777 (defvar / nil "list of values returned by +") | |
2778 (defvar // nil "list of values returned by ++") | |
2779 (defvar /// nil "list of values returned by +++") | |
2780 (defvar * nil "(first) value of +") | |
2781 (defvar ** nil "(first) value of ++") | |
2782 (defvar *** nil "(first) value of +++") | |
2783 | |
2784 (defun cl-eval-print-last-sexp () | |
2785 "Evaluate sexp before point; print value\(s\) into current buffer. | |
2786 If the evaled form returns multiple values, they are shown one to a line. | |
2787 The variables -, +, ++, +++, *, **, ***, /, //, /// have their usual meaning. | |
2788 | |
2789 It clears the multiple-value passing mechanism, and does not pass back | |
2790 multiple values. Use this only if you are debugging cl.el and understand well | |
2791 how the multiple-value stuff works, because it can be fooled into believing | |
2792 that multiple values have been returned when they actually haven't, for | |
2793 instance | |
2794 \(identity \(values nil 1\)\) | |
2795 However, even when this fails, you can trust the first printed value to be | |
2796 \(one of\) the returned value\(s\)." | |
2797 (interactive) | |
2798 ;; top level call, can reset mvalues | |
2799 (setq *mvalues-count* nil | |
2800 *mvalues-values* nil) | |
2801 (setq - (car (read-from-string | |
2802 (buffer-substring | |
2803 (let ((stab (syntax-table))) | |
2804 (unwind-protect | |
2805 (save-excursion | |
2806 (set-syntax-table emacs-lisp-mode-syntax-table) | |
2807 (forward-sexp -1) | |
2808 (point)) | |
2809 (set-syntax-table stab))) | |
2810 (point))))) | |
2811 (setq *** ** | |
2812 ** * | |
2813 * (eval -)) | |
2814 (setq /// // | |
2815 // / | |
2816 / *mvalues-values*) | |
2817 (setq +++ ++ | |
2818 ++ + | |
2819 + -) | |
2820 (cond ((or (null *mvalues-count*) ;mvalues mechanism not used | |
2821 (not (eq * (car *mvalues-values*)))) | |
2822 (print * (current-buffer))) | |
2823 ((null /) ;no values returned | |
2824 (terpri (current-buffer))) | |
2825 (t ;more than zero mvalues | |
2826 (terpri (current-buffer)) | |
2827 (mapcar (function (lambda (value) | |
2828 (prin1 value (current-buffer)) | |
2829 (terpri (current-buffer)))) | |
2830 /))) | |
2831 (setq *mvalues-count* nil ;make sure | |
2832 *mvalues-values* nil)) | |
2833 | |
2834 ;;;; More LISTS functions | |
2835 ;;;; | |
2836 | |
2837 ;;; Some mapping functions on lists, commonly useful. | |
2838 ;;; They take no extra sequences, to go along with Emacs Lisp's MAPCAR. | |
2839 | |
2840 (defun mapc (function list) | |
2841 "(MAPC FUNCTION LIST) => LIST | |
2842 Apply FUNCTION to each element of LIST, return LIST. | |
2843 Like mapcar, but called only for effect." | |
2844 (let ((args list)) | |
2845 (while args | |
2846 (funcall function (car args)) | |
2847 (setq args (cdr args)))) | |
2848 list) | |
2849 | |
2850 (defun maplist (function list) | |
2851 "(MAPLIST FUNCTION LIST) => list'ed results of FUNCTION on cdrs of LIST | |
2852 Apply FUNCTION to successive sublists of LIST, return the list of the results" | |
2853 (let ((args list) | |
2854 results '()) | |
2855 (while args | |
2856 (setq results (cons (funcall function args) results) | |
2857 args (cdr args))) | |
2858 (nreverse results))) | |
2859 | |
2860 (defun mapl (function list) | |
2861 "(MAPL FUNCTION LIST) => LIST | |
2862 Apply FUNCTION to successive cdrs of LIST, return LIST. | |
2863 Like maplist, but called only for effect." | |
2864 (let ((args list)) | |
2865 (while args | |
2866 (funcall function args) | |
2867 (setq args (cdr args))) | |
2868 list)) | |
2869 | |
2870 (defun mapcan (function list) | |
2871 "(MAPCAN FUNCTION LIST) => nconc'd results of FUNCTION on LIST | |
2872 Apply FUNCTION to each element of LIST, nconc the results. | |
2873 Beware: nconc destroys its first argument! See copy-list." | |
2874 (let ((args list) | |
2875 (results '())) | |
2876 (while args | |
2877 (setq results (nconc (funcall function (car args)) results) | |
2878 args (cdr args))) | |
2879 (nreverse results))) | |
2880 | |
2881 (defun mapcon (function list) | |
2882 "(MAPCON FUNCTION LIST) => nconc'd results of FUNCTION on cdrs of LIST | |
2883 Apply FUNCTION to successive sublists of LIST, nconc the results. | |
2884 Beware: nconc destroys its first argument! See copy-list." | |
2885 (let ((args list) | |
2886 (results '())) | |
2887 (while args | |
2888 (setq results (nconc (funcall function args) results) | |
2889 args (cdr args))) | |
2890 (nreverse results))) | |
2891 | |
2892 ;;; Copiers | |
2893 | |
2894 (defun copy-list (list) | |
2895 "Build a copy of LIST" | |
2896 (append list '())) | |
2897 | |
2898 (defun copy-tree (tree) | |
2899 "Build a copy of the tree of conses TREE | |
2900 The argument is a tree of conses, it is recursively copied down to | |
2901 non conses. Circularity and sharing of substructure are not | |
2902 necessarily preserved." | |
2903 (if (consp tree) | |
2904 (cons (copy-tree (car tree)) | |
2905 (copy-tree (cdr tree))) | |
2906 tree)) | |
2907 | |
2908 ;;; reversals, and destructive manipulations of a list's spine | |
2909 | |
2910 (defun revappend (x y) | |
2911 "does what (append (reverse X) Y) would, only faster" | |
2912 (if (endp x) | |
2913 y | |
2914 (revappend (cdr x) (cons (car x) y)))) | |
2915 | |
2916 (defun nreconc (x y) | |
2917 "does (nconc (nreverse X) Y) would, only faster | |
2918 Destructive on X, be careful." | |
2919 (if (endp x) | |
2920 y | |
2921 ;; reuse the first cons of x, making it point to y | |
2922 (nreconc (cdr x) (prog1 x (rplacd x y))))) | |
2923 | |
2924 (defun nbutlast (list &optional n) | |
2925 "Side-effected LIST truncated N+1 conses from the end. | |
2926 This is the destructive version of BUTLAST. Returns () and does not | |
2927 modify the LIST argument if the length of the list is not at least N." | |
2928 (when (null n) (setf n 1)) | |
2929 (let ((length (list-length list))) | |
2930 (cond ((null length) | |
2931 list) | |
2932 ((< length n) | |
2933 '()) | |
2934 (t | |
2935 (setnthcdr (- length n) list nil) | |
2936 list)))) | |
2937 | |
2938 ;;; Substitutions | |
2939 | |
2940 (defun subst (new old tree) | |
2941 "NEW replaces OLD in a copy of TREE | |
2942 Uses eql for the test." | |
2943 (subst-if new (function (lambda (x) (eql x old))) tree)) | |
2944 | |
2945 (defun subst-if-not (new test tree) | |
2946 "NEW replaces any subtree or leaf that fails TEST in a copy of TREE" | |
2947 ;; (subst-if new (function (lambda (x) (not (funcall test x)))) tree) | |
2948 (cond ((not (funcall test tree)) | |
2949 new) | |
2950 ((atom tree) | |
2951 tree) | |
2952 (t ;no match so far | |
2953 (let ((head (subst-if-not new test (car tree))) | |
2954 (tail (subst-if-not new test (cdr tree)))) | |
2955 ;; If nothing changed, return originals. Else use the new | |
2956 ;; components to assemble a new tree. | |
2957 (if (and (eql head (car tree)) | |
2958 (eql tail (cdr tree))) | |
2959 tree | |
2960 (cons head tail)))))) | |
2961 | |
2962 (defun subst-if (new test tree) | |
2963 "NEW replaces any subtree or leaf that satisfies TEST in a copy of TREE" | |
2964 (cond ((funcall test tree) | |
2965 new) | |
2966 ((atom tree) | |
2967 tree) | |
2968 (t ;no match so far | |
2969 (let ((head (subst-if new test (car tree))) | |
2970 (tail (subst-if new test (cdr tree)))) | |
2971 ;; If nothing changed, return originals. Else use the new | |
2972 ;; components to assemble a new tree. | |
2973 (if (and (eql head (car tree)) | |
2974 (eql tail (cdr tree))) | |
2975 tree | |
2976 (cons head tail)))))) | |
2977 | |
2978 (defun sublis (alist tree) | |
2979 "Use association list ALIST to modify a copy of TREE | |
2980 If a subtree or leaf of TREE is a key in ALIST, it is replaced by the | |
2981 associated value. Not exactly Common Lisp, but close in spirit and | |
2982 compatible with the native Emacs Lisp ASSOC, which uses EQUAL." | |
2983 (let ((toplevel (assoc tree alist))) | |
2984 (cond (toplevel ;Bingo at top | |
2985 (cdr toplevel)) | |
2986 ((atom tree) ;Give up on this | |
2987 tree) | |
2988 (t | |
2989 (let ((head (sublis alist (car tree))) | |
2990 (tail (sublis alist (cdr tree)))) | |
2991 (if (and (eql head (car tree)) | |
2992 (eql tail (cdr tree))) | |
2993 tree | |
2994 (cons head tail))))))) | |
2995 | |
2996 (defun member-if (predicate list) | |
2997 "PREDICATE is applied to the members of LIST. As soon as one of them | |
2998 returns true, that tail of the list if returned. Else NIL." | |
2999 (catch 'found-member-if | |
3000 (while (not (endp list)) | |
3001 (if (funcall predicate (car list)) | |
3002 (throw 'found-member-if list) | |
3003 (setq list (cdr list)))) | |
3004 nil)) | |
3005 | |
3006 (defun member-if-not (predicate list) | |
3007 "PREDICATE is applied to the members of LIST. As soon as one of them | |
3008 returns false, that tail of the list if returned. Else NIL." | |
3009 (catch 'found-member-if-not | |
3010 (while (not (endp list)) | |
3011 (if (funcall predicate (car list)) | |
3012 (setq list (cdr list)) | |
3013 (throw 'found-member-if-not list))) | |
3014 nil)) | |
3015 | |
3016 (defun tailp (sublist list) | |
3017 "(tailp SUBLIST LIST) => True if SUBLIST is a sublist of LIST." | |
3018 (catch 'tailp-found | |
3019 (while (not (endp list)) | |
3020 (if (eq sublist list) | |
3021 (throw 'tailp-found t) | |
3022 (setq list (cdr list)))) | |
3023 nil)) | |
3024 | |
3025 ;;; Suggestion of phr%widow.Berkeley.EDU@lilac.berkeley.edu | |
3026 | |
3027 (defmacro declare (&rest decls) | |
3028 "Ignore a Common-Lisp declaration." | |
3029 "declarations are ignored in this implementation") | |
3030 | |
3031 (defun proclaim (&rest decls) | |
3032 "Ignore a Common-Lisp proclamation." | |
3033 "declarations are ignored in this implementation") | |
3034 | |
3035 (defmacro the (type form) | |
3036 "(the TYPE FORM) macroexpands to FORM | |
3037 No checking is even attempted. This is just for compatibility with | |
3038 Common-Lisp codes." | |
3039 form) | |
3040 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
3041 (provide 'cl) |
584 | 3042 |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
3043 ;;; cl.el ends here |