Commit | Line | Data |
---|---|---|
f9a8037d C |
1 | |
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | CHAPTER 8 | |
9 | ||
10 | ||
11 | Functions, Fclosures, and Macros | |
12 | ||
13 | ||
14 | ||
15 | ||
16 | ||
17 | ||
18 | 8.1. valid function objects | |
19 | ||
20 | There are many different objects which can occupy | |
21 | the function field of a symbol object. Table 8.1, on | |
22 | the following page, shows all of the possibilities, | |
23 | how to recognize them, and where to look for documen- | |
24 | tation. | |
25 | ||
26 | ||
27 | ||
28 | 8.2. functions | |
29 | ||
30 | The basic Lisp function is the lambda function. | |
31 | When a lambda function is called, the actual arguments | |
32 | are evaluated from left to right and are lambda-bound | |
33 | to the formal parameters of the lambda function. | |
34 | ||
35 | An nlambda function is usually used for functions | |
36 | which are invoked by the user at top level. Some | |
37 | built-in functions which evaluate their arguments in | |
38 | special ways are also nlambdas (e.g _\bc_\bo_\bn_\bd, _\bd_\bo, _\bo_\br). | |
39 | When an nlambda function is called, the list of | |
40 | unevaluated arguments is lambda bound to the single | |
41 | formal parameter of the nlambda function. | |
42 | ||
43 | Some programmers will use an nlambda function | |
44 | when they are not sure how many arguments will be | |
45 | passed. Then, the first thing the nlambda function | |
46 | does is map _\be_\bv_\ba_\bl over the list of unevaluated argu- | |
47 | ments it has been passed. This is usually the wrong | |
48 | thing to do, as it will not work compiled if any of | |
49 | the arguments are local variables. The solution is to | |
50 | use a lexpr. When a lexpr function is called, the | |
51 | arguments are evaluated and a fixnum whose value is | |
52 | the number of arguments is lambda-bound to the single | |
53 | formal parameter of the lexpr function. The lexpr can | |
54 | then access the arguments using the _\ba_\br_\bg function. | |
55 | ||
56 | When a function is compiled, _\bs_\bp_\be_\bc_\bi_\ba_\bl declarations | |
57 | may be needed to preserve its behavior. An argument | |
58 | is not lambda-bound to the name of the corresponding | |
59 | formal parameter unless that formal parameter has been | |
60 | declared _\bs_\bp_\be_\bc_\bi_\ba_\bl (see 12.3.2.2). | |
61 | ||
62 | ||
63 | Functions, Fclosures, and Macros 8-1 | |
64 | ||
65 | ||
66 | ||
67 | ||
68 | ||
69 | ||
70 | ||
71 | Functions, Fclosures, and Macros 8-2 | |
72 | ||
73 | ||
74 | ||
75 | ||
76 | ||
77 | \e8_________________________________________________________________ | |
78 | informal name object type documentation | |
79 | \e8_________________________________________________________________\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b_________________________________________________________________ | |
80 | interpreted list with _\bc_\ba_\br 8.2 | |
81 | lambda function _\be_\bq to lambda | |
82 | \e8_________________________________________________________________ | |
83 | interpreted list with _\bc_\ba_\br 8.2 | |
84 | nlambda function _\be_\bq to nlambda | |
85 | \e8_________________________________________________________________ | |
86 | interpreted list with _\bc_\ba_\br 8.2 | |
87 | lexpr function _\be_\bq to lexpr | |
88 | \e8_________________________________________________________________ | |
89 | interpreted list with _\bc_\ba_\br 8.3 | |
90 | macro _\be_\bq to macro | |
91 | \e8_________________________________________________________________ | |
92 | fclosure vector with _\bv_\bp_\br_\bo_\bp 8.4 | |
93 | _\be_\bq to fclosure | |
94 | \e8_________________________________________________________________ | |
95 | compiled binary with discipline 8.2 | |
96 | lambda or lexpr _\be_\bq to lambda | |
97 | function | |
98 | \e8_________________________________________________________________ | |
99 | compiled binary with discipline 8.2 | |
100 | nlambda function _\be_\bq to nlambda | |
101 | \e8_________________________________________________________________ | |
102 | compiled binary with discipline 8.3 | |
103 | macro _\be_\bq to macro | |
104 | \e8_________________________________________________________________ | |
105 | foreign binary with discipline 8.5 | |
106 | subroutine of "subroutine"[|\b-] | |
107 | \e8_________________________________________________________________ | |
108 | foreign binary with discipline 8.5 | |
109 | function of "function"[|\b-] | |
110 | \e8_________________________________________________________________ | |
111 | foreign binary with discipline 8.5 | |
112 | integer function of "integer-function"[|\b-] | |
113 | \e8_________________________________________________________________ | |
114 | foreign binary with discipline 8.5 | |
115 | real function of "real-function"[|\b-] | |
116 | \e8_________________________________________________________________ | |
117 | foreign binary with discipline 8.5 | |
118 | C function of "c-function"[|\b-] | |
119 | \e8_________________________________________________________________ | |
120 | foreign binary with discipline 8.5 | |
121 | double function of "double-c-function"[|\b-] | |
122 | \e8_________________________________________________________________ | |
123 | foreign binary with discipline 8.5 | |
124 | structure function of "vector-c-function"[|\b-] | |
125 | \e8_________________________________________________________________ | |
126 | array array object 9 | |
127 | \e8_________________________________________________________________ | |
128 | \e7\b|\b\e8|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7| | |
129 | ||
130 | ||
131 | ||
132 | ||
133 | ||
134 | ||
135 | ||
136 | ||
137 | ||
138 | ||
139 | ||
140 | ||
141 | ||
142 | ||
143 | ||
144 | ||
145 | ||
146 | ||
147 | ||
148 | ||
149 | ||
150 | ||
151 | ||
152 | ||
153 | ||
154 | ||
155 | ||
156 | ||
157 | ||
158 | ||
159 | ||
160 | ||
161 | ||
162 | ||
163 | ||
164 | ||
165 | ||
166 | ||
167 | ||
168 | \e9 |\b\e8|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7| | |
169 | ||
170 | ||
171 | ||
172 | ||
173 | ||
174 | ||
175 | ||
176 | ||
177 | ||
178 | ||
179 | ||
180 | ||
181 | ||
182 | ||
183 | ||
184 | ||
185 | ||
186 | ||
187 | ||
188 | ||
189 | ||
190 | ||
191 | ||
192 | ||
193 | ||
194 | ||
195 | ||
196 | ||
197 | ||
198 | ||
199 | ||
200 | ||
201 | ||
202 | ||
203 | ||
204 | ||
205 | ||
206 | ||
207 | ||
208 | \e9 |\b\e8|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7| | |
209 | ||
210 | ||
211 | ||
212 | ||
213 | ||
214 | ||
215 | ||
216 | ||
217 | ||
218 | ||
219 | ||
220 | ||
221 | ||
222 | ||
223 | ||
224 | ||
225 | ||
226 | ||
227 | ||
228 | ||
229 | ||
230 | ||
231 | ||
232 | ||
233 | ||
234 | ||
235 | ||
236 | ||
237 | ||
238 | ||
239 | ||
240 | ||
241 | ||
242 | ||
243 | ||
244 | ||
245 | ||
246 | ||
247 | ||
248 | \e9 |\b\e8|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7| | |
249 | ||
250 | ||
251 | ||
252 | ||
253 | ||
254 | ||
255 | ||
256 | ||
257 | ||
258 | ||
259 | ||
260 | ||
261 | ||
262 | ||
263 | ||
264 | ||
265 | ||
266 | ||
267 | ||
268 | ||
269 | ||
270 | ||
271 | ||
272 | ||
273 | ||
274 | ||
275 | ||
276 | ||
277 | ||
278 | ||
279 | ||
280 | ||
281 | ||
282 | ||
283 | ||
284 | ||
285 | ||
286 | ||
287 | ||
288 | ||
289 | ||
290 | \e9 Table 8.1 | |
291 | ||
292 | ____________________ | |
293 | \e9 [|\b-]Only the first character of the string is significant (i.e "s" | |
294 | is ok for "subroutine") | |
295 | ||
296 | ||
297 | ||
298 | \e9 Printed: January 31, 1984 | |
299 | ||
300 | ||
301 | ||
302 | ||
303 | ||
304 | ||
305 | ||
306 | Functions, Fclosures, and Macros 8-3 | |
307 | ||
308 | ||
309 | Lambda and lexpr functions both compile into a | |
310 | binary object with a discipline of lambda. However, a | |
311 | compiled lexpr still acts like an interpreted lexpr. | |
312 | ||
313 | ||
314 | ||
315 | 8.3. macros | |
316 | ||
317 | An important feature of Lisp is its ability to | |
318 | manipulate programs as data. As a result of this, | |
319 | most Lisp implementations have very powerful macro | |
320 | facilities. The Lisp language's macro facility can be | |
321 | used to incorporate popular features of the other | |
322 | languages into Lisp. For example, there are macro | |
323 | packages which allow one to create records (ala Pas- | |
324 | cal) and refer to elements of those records by the | |
325 | field names. The _\bs_\bt_\br_\bu_\bc_\bt package imported from Maclisp | |
326 | does this. Another popular use for macros is to | |
327 | create more readable control structures which expand | |
328 | into _\bc_\bo_\bn_\bd, _\bo_\br and _\ba_\bn_\bd. One such example is the If | |
329 | macro. It allows you to write | |
330 | ||
331 | (_\bI_\bf (_\be_\bq_\bu_\ba_\bl _\bn_\bu_\bm_\bb _\b0) _\bt_\bh_\be_\bn (_\bp_\br_\bi_\bn_\bt '_\bz_\be_\br_\bo) (_\bt_\be_\br_\bp_\br) | |
332 | _\be_\bl_\bs_\be_\bi_\bf (_\be_\bq_\bu_\ba_\bl _\bn_\bu_\bm_\bb _\b1) _\bt_\bh_\be_\bn (_\bp_\br_\bi_\bn_\bt '_\bo_\bn_\be) (_\bt_\be_\br_\bp_\br) | |
333 | _\be_\bl_\bs_\be (_\bp_\br_\bi_\bn_\bt '|_\bI _\bg_\bi_\bv_\be _\bu_\bp|)) | |
334 | ||
335 | which expands to | |
336 | ||
337 | (_\bc_\bo_\bn_\bd | |
338 | ((_\be_\bq_\bu_\ba_\bl _\bn_\bu_\bm_\bb _\b0) (_\bp_\br_\bi_\bn_\bt '_\bz_\be_\br_\bo) (_\bt_\be_\br_\bp_\br)) | |
339 | ((_\be_\bq_\bu_\ba_\bl _\bn_\bu_\bm_\bb _\b1) (_\bp_\br_\bi_\bn_\bt '_\bo_\bn_\be) (_\bt_\be_\br_\bp_\br)) | |
340 | (_\bt (_\bp_\br_\bi_\bn_\bt '|_\bI _\bg_\bi_\bv_\be _\bu_\bp|))) | |
341 | ||
342 | ||
343 | ||
344 | ||
345 | 8.3.1. macro forms | |
346 | ||
347 | A macro is a function which accepts a Lisp | |
348 | expression as input and returns another Lisp | |
349 | expression. The action the macro takes is called | |
350 | macro expansion. Here is a simple example: | |
351 | ||
352 | -> (_\bd_\be_\bf _\bf_\bi_\br_\bs_\bt (_\bm_\ba_\bc_\br_\bo (_\bx) (_\bc_\bo_\bn_\bs '_\bc_\ba_\br (_\bc_\bd_\br _\bx)))) | |
353 | first | |
354 | -> (_\bf_\bi_\br_\bs_\bt '(_\ba _\bb _\bc)) | |
355 | a | |
356 | -> (_\ba_\bp_\bp_\bl_\by '_\bf_\bi_\br_\bs_\bt '(_\bf_\bi_\br_\bs_\bt '(_\ba _\bb _\bc))) | |
357 | (car '(a b c)) | |
358 | ||
359 | The first input line defines a macro called _\bf_\bi_\br_\bs_\bt. | |
360 | Notice that the macro has one formal parameter, _\bx. | |
361 | On the second input line, we ask the interpreter to | |
362 | ||
363 | ||
364 | Printed: January 31, 1984 | |
365 | ||
366 | ||
367 | ||
368 | ||
369 | ||
370 | ||
371 | ||
372 | Functions, Fclosures, and Macros 8-4 | |
373 | ||
374 | ||
375 | evaluate (_\bf_\bi_\br_\bs_\bt '(_\ba _\bb _\bc)). _\bE_\bv_\ba_\bl sees that _\bf_\bi_\br_\bs_\bt | |
376 | has a function definition of type macro, so it | |
377 | evaluates _\bf_\bi_\br_\bs_\bt's definition, passing to _\bf_\bi_\br_\bs_\bt, as | |
378 | an argument, the form _\be_\bv_\ba_\bl itself was trying to | |
379 | evaluate: (_\bf_\bi_\br_\bs_\bt '(_\ba _\bb _\bc)). The _\bf_\bi_\br_\bs_\bt macro chops | |
380 | off the car of the argument with _\bc_\bd_\br, cons' a _\bc_\ba_\br | |
381 | at the beginning of the list and returns | |
382 | (_\bc_\ba_\br '(_\ba _\bb _\bc)), which _\be_\bv_\ba_\bl evaluates. The value _\ba | |
383 | is returned as the value of (_\bf_\bi_\br_\bs_\bt '(_\ba _\bb _\bc)). Thus | |
384 | whenever _\be_\bv_\ba_\bl tries to evaluate a list whose car | |
385 | has a macro definition it ends up doing (at least) | |
386 | two operations, the first of which is a call to the | |
387 | macro to let it macro expand the form, and the | |
388 | other is the evaluation of the result of the macro. | |
389 | The result of the macro may be yet another call to | |
390 | a macro, so _\be_\bv_\ba_\bl may have to do even more evalua- | |
391 | tions until it can finally determine the value of | |
392 | an expression. One way to see how a macro will | |
393 | expand is to use _\ba_\bp_\bp_\bl_\by as shown on the third input | |
394 | line above. | |
395 | ||
396 | ||
397 | ||
398 | 8.3.2. defmacro | |
399 | ||
400 | The macro _\bd_\be_\bf_\bm_\ba_\bc_\br_\bo makes it easier to define | |
401 | macros because it allows you to name the arguments | |
402 | to the macro call. For example, suppose we find | |
403 | ourselves often writing code like | |
404 | (_\bs_\be_\bt_\bq _\bs_\bt_\ba_\bc_\bk (_\bc_\bo_\bn_\bs _\bn_\be_\bw_\be_\bl_\bt _\bs_\bt_\ba_\bc_\bk). We could define a | |
405 | macro named _\bp_\bu_\bs_\bh to do this for us. One way to | |
406 | define it is: | |
407 | ||
408 | -> (_\bd_\be_\bf _\bp_\bu_\bs_\bh | |
409 | (_\bm_\ba_\bc_\br_\bo (_\bx) (_\bl_\bi_\bs_\bt '_\bs_\be_\bt_\bq (_\bc_\ba_\bd_\bd_\br _\bx) (_\bl_\bi_\bs_\bt '_\bc_\bo_\bn_\bs (_\bc_\ba_\bd_\br _\bx) (_\bc_\ba_\bd_\bd_\br _\bx))))) | |
410 | push | |
411 | ||
412 | then (_\bp_\bu_\bs_\bh _\bn_\be_\bw_\be_\bl_\bt _\bs_\bt_\ba_\bc_\bk) will expand to the form | |
413 | mentioned above. The same macro written using def- | |
414 | macro would be: | |
415 | ||
416 | -> (_\bd_\be_\bf_\bm_\ba_\bc_\br_\bo _\bp_\bu_\bs_\bh (_\bv_\ba_\bl_\bu_\be _\bs_\bt_\ba_\bc_\bk) | |
417 | (_\bl_\bi_\bs_\bt '_\bs_\be_\bt_\bq ,_\bs_\bt_\ba_\bc_\bk (_\bl_\bi_\bs_\bt '_\bc_\bo_\bn_\bs ,_\bv_\ba_\bl_\bu_\be ,_\bs_\bt_\ba_\bc_\bk))) | |
418 | push | |
419 | ||
420 | Defmacro allows you to name the arguments of the | |
421 | macro call, and makes the macro definition look | |
422 | more like a function definition. | |
423 | ||
424 | ||
425 | ||
426 | ||
427 | \e9 | |
428 | ||
429 | \e9 Printed: January 31, 1984 | |
430 | ||
431 | ||
432 | ||
433 | ||
434 | ||
435 | ||
436 | ||
437 | Functions, Fclosures, and Macros 8-5 | |
438 | ||
439 | ||
440 | 8.3.3. the backquote character macro | |
441 | ||
442 | The default syntax for FRANZ LISP has four | |
443 | characters with associated character macros. One | |
444 | is semicolon for comments. Two others are the | |
445 | backquote and comma which are used by the backquote | |
446 | character macro. The fourth is the sharp sign | |
447 | macro described in the next section. | |
448 | ||
449 | The backquote macro is used to create lists | |
450 | where many of the elements are fixed (quoted). This | |
451 | makes it very useful for creating macro defini- | |
452 | tions. In the simplest case, a backquote acts just | |
453 | like a single quote: | |
454 | ||
455 | ->`(_\ba _\bb _\bc _\bd _\be) | |
456 | (a b c d e) | |
457 | ||
458 | If a comma precedes an element of a backquoted list | |
459 | then that element is evaluated and its value is put | |
460 | in the list. | |
461 | ||
462 | ->(_\bs_\be_\bt_\bq _\bd '(_\bx _\by _\bz)) | |
463 | (x y z) | |
464 | ->`(_\ba _\bb _\bc ,_\bd _\be) | |
465 | (a b c (x y z) e) | |
466 | ||
467 | If a comma followed by an at sign precedes an ele- | |
468 | ment in a backquoted list, then that element is | |
469 | evaluated and spliced into the list with _\ba_\bp_\bp_\be_\bn_\bd. | |
470 | ||
471 | ->`(_\ba _\bb _\bc ,@_\bd _\be) | |
472 | (a b c x y z e) | |
473 | ||
474 | Once a list begins with a backquote, the commas may | |
475 | appear anywhere in the list as this example shows: | |
476 | ||
477 | ->`(_\ba _\bb (_\bc _\bd ,(_\bc_\bd_\br _\bd)) (_\be _\bf (_\bg _\bh ,@(_\bc_\bd_\bd_\br _\bd) ,@_\bd))) | |
478 | (a b (c d (y z)) (e f (g h z x y z))) | |
479 | ||
480 | It is also possible and sometimes even useful to | |
481 | use the backquote macro within itself. As a final | |
482 | demonstration of the backquote macro, we shall | |
483 | define the first and push macros using all the | |
484 | power at our disposal: defmacro and the backquote | |
485 | macro. | |
486 | ||
487 | ->(_\bd_\be_\bf_\bm_\ba_\bc_\br_\bo _\bf_\bi_\br_\bs_\bt (_\bl_\bi_\bs_\bt) `(_\bc_\ba_\br ,_\bl_\bi_\bs_\bt)) | |
488 | first | |
489 | ->(_\bd_\be_\bf_\bm_\ba_\bc_\br_\bo _\bp_\bu_\bs_\bh (_\bv_\ba_\bl_\bu_\be _\bs_\bt_\ba_\bc_\bk) `(_\bs_\be_\bt_\bq ,_\bs_\bt_\ba_\bc_\bk (_\bc_\bo_\bn_\bs ,_\bv_\ba_\bl_\bu_\be ,_\bs_\bt_\ba_\bc_\bk))) | |
490 | stack | |
491 | ||
492 | \e9 | |
493 | ||
494 | \e9 Printed: January 31, 1984 | |
495 | ||
496 | ||
497 | ||
498 | ||
499 | ||
500 | ||
501 | ||
502 | Functions, Fclosures, and Macros 8-6 | |
503 | ||
504 | ||
505 | 8.3.4. sharp sign character macro | |
506 | ||
507 | The sharp sign macro can perform a number of | |
508 | different functions at read time. The character | |
509 | directly following the sharp sign determines which | |
510 | function will be done, and following Lisp s- | |
511 | expressions may serve as arguments. | |
512 | ||
513 | ||
514 | ||
515 | 8.3.4.1. conditional inclusion | |
516 | ||
517 | If you plan to run one source file in more than | |
518 | one environment then you may want to some pieces | |
519 | of code to be included or not included depend- | |
520 | ing on the environment. The C language uses | |
521 | "#ifdef" and "#ifndef" for this purpose, and | |
522 | Lisp uses "#+" and "#-". The environment that | |
523 | the sharp sign macro checks is the | |
524 | (_\bs_\bt_\ba_\bt_\bu_\bs _\bf_\be_\ba_\bt_\bu_\br_\be_\bs) list which is initialized when | |
525 | the Lisp system is built and which may be | |
526 | altered by (_\bs_\bs_\bt_\ba_\bt_\bu_\bs _\bf_\be_\ba_\bt_\bu_\br_\be _\bf_\bo_\bo) and | |
527 | (_\bs_\bs_\bt_\ba_\bt_\bu_\bs _\bn_\bo_\bf_\be_\ba_\bt_\bu_\br_\be _\bb_\ba_\br) The form of conditional | |
528 | inclusion is | |
529 | _\b#_\b+_\bw_\bh_\be_\bn _\bw_\bh_\ba_\bt | |
530 | where _\bw_\bh_\be_\bn is either a symbol or an expression | |
531 | involving symbols and the functions _\ba_\bn_\bd, _\bo_\br, and | |
532 | _\bn_\bo_\bt. The meaning is that _\bw_\bh_\ba_\bt will only be read | |
533 | in if _\bw_\bh_\be_\bn is true. A symbol in _\bw_\bh_\be_\bn is true | |
534 | only if it appears in the (_\bs_\bt_\ba_\bt_\bu_\bs _\bf_\be_\ba_\bt_\bu_\br_\be_\bs) | |
535 | list. | |
536 | ||
537 | ||
538 | ____________________________________________________ | |
539 | ||
540 | ; suppose we want to write a program which references a file | |
541 | ; and which can run at ucb, ucsd and cmu where the file naming conventions | |
542 | ; are different. | |
543 | ; | |
544 | -> (_\bd_\be_\bf_\bu_\bn _\bh_\bo_\bw_\bo_\bl_\bd (_\bn_\ba_\bm_\be) | |
545 | (_\bt_\be_\br_\bp_\br) | |
546 | (_\bl_\bo_\ba_\bd #+(_\bo_\br _\bu_\bc_\bb _\bu_\bc_\bs_\bd) "/_\bu_\bs_\br/_\bl_\bi_\bb/_\bl_\bi_\bs_\bp/_\ba_\bg_\be_\bs._\bl" | |
547 | #+_\bc_\bm_\bu "/_\bu_\bs_\br/_\bl_\bi_\bs_\bp/_\bd_\bo_\bc/_\ba_\bg_\be_\bs._\bl") | |
548 | (_\bp_\ba_\bt_\bo_\bm _\bn_\ba_\bm_\be) | |
549 | (_\bp_\ba_\bt_\bo_\bm " _\bi_\bs ") | |
550 | (_\bp_\br_\bi_\bn_\bt (_\bc_\bd_\br (_\ba_\bs_\bs_\bo_\bc _\bn_\ba_\bm_\be _\ba_\bg_\be_\bf_\bi_\bl_\be))) | |
551 | (_\bp_\ba_\bt_\bo_\bm "_\by_\be_\ba_\br_\bs _\bo_\bl_\bd") | |
552 | (_\bt_\be_\br_\bp_\br)) | |
553 | ____________________________________________________ | |
554 | ||
555 | ||
556 | ||
557 | The form | |
558 | ||
559 | ||
560 | Printed: January 31, 1984 | |
561 | ||
562 | ||
563 | ||
564 | ||
565 | ||
566 | ||
567 | ||
568 | Functions, Fclosures, and Macros 8-7 | |
569 | ||
570 | ||
571 | _\b#_\b-_\bw_\bh_\be_\bn _\bw_\bh_\ba_\bt | |
572 | is equivalent to | |
573 | _\b#_\b+_\b(_\bn_\bo_\bt _\bw_\bh_\be_\bn_\b) _\bw_\bh_\ba_\bt | |
574 | ||
575 | ||
576 | ||
577 | 8.3.4.2. fixnum character equivalents | |
578 | ||
579 | When working with fixnum equivalents of charac- | |
580 | ters, it is often hard to remember the number | |
581 | corresponding to a character. The form | |
582 | _\b#_\b/_\bc | |
583 | is equivalent to the fixnum representation of | |
584 | character c. | |
585 | ||
586 | ||
587 | ____________________________________________________ | |
588 | ||
589 | ; a function which returns t if the user types y else it returns nil. | |
590 | ; | |
591 | -> (_\bd_\be_\bf_\bu_\bn _\by_\be_\bs_\bo_\br_\bn_\bo _\bn_\bi_\bl | |
592 | (_\bp_\br_\bo_\bg_\bn (_\ba_\bn_\bs) | |
593 | (_\bs_\be_\bt_\bq _\ba_\bn_\bs (_\bt_\by_\bi)) | |
594 | (_\bc_\bo_\bn_\bd ((_\be_\bq_\bu_\ba_\bl _\ba_\bn_\bs #/_\by) _\bt) | |
595 | (_\bt _\bn_\bi_\bl)))) | |
596 | ____________________________________________________ | |
597 | ||
598 | ||
599 | ||
600 | ||
601 | ||
602 | ||
603 | 8.3.4.3. read time evaluation | |
604 | ||
605 | Occasionally you want to express a constant as a | |
606 | Lisp expression, yet you don't want to pay the | |
607 | penalty of evaluating this expression each time | |
608 | it is referenced. The form | |
609 | _\b#_\b._\be_\bx_\bp_\br_\be_\bs_\bs_\bi_\bo_\bn | |
610 | evaluates the expression at read time and | |
611 | returns its value. | |
612 | ||
613 | ||
614 | ||
615 | ||
616 | ||
617 | ||
618 | ||
619 | ||
620 | ||
621 | ||
622 | ||
623 | \e9 | |
624 | ||
625 | \e9 Printed: January 31, 1984 | |
626 | ||
627 | ||
628 | ||
629 | ||
630 | ||
631 | ||
632 | ||
633 | Functions, Fclosures, and Macros 8-8 | |
634 | ||
635 | ||
636 | ||
637 | ____________________________________________________ | |
638 | ||
639 | ; a function to test if any of bits 1 3 or 12 are set in a fixnum. | |
640 | ; | |
641 | -> (_\bd_\be_\bf_\bu_\bn _\bt_\be_\bs_\bt_\bi_\bt (_\bn_\bu_\bm) | |
642 | (_\bc_\bo_\bn_\bd ((_\bz_\be_\br_\bo_\bp (_\bb_\bo_\bo_\bl_\be _\b1 _\bn_\bu_\bm #.(+ (_\bl_\bs_\bh _\b1 _\b1) (_\bl_\bs_\bh _\b1 _\b3) (_\bl_\bs_\bh _\b1 _\b1_\b2)))) | |
643 | _\bn_\bi_\bl) | |
644 | (_\bt _\bt))) | |
645 | ____________________________________________________ | |
646 | ||
647 | ||
648 | ||
649 | ||
650 | ||
651 | ||
652 | 8.4. fclosures | |
653 | ||
654 | Fclosures are a type of functional object. The | |
655 | purpose is to remember the values of some variables | |
656 | between invocations of the functional object and to | |
657 | protect this data from being inadvertently overwritten | |
658 | by other Lisp functions. Fortran programs usually | |
659 | exhibit this behavior for their variables. (In fact, | |
660 | some versions of Fortran would require the variables | |
661 | to be in COMMON). Thus it is easy to write a linear | |
662 | congruent random number generator in Fortran, merely | |
663 | by keeping the seed as a variable in the function. It | |
664 | is much more risky to do so in Lisp, since any special | |
665 | variable you picked, might be used by some other func- | |
666 | tion. Fclosures are an attempt to provide most of the | |
667 | same functionality as closures in Lisp Machine Lisp, | |
668 | to users of FRANZ LISP. Fclosures are related to clo- | |
669 | sures in this way: | |
670 | (fclosure '(a b) 'foo) <==> | |
671 | (let ((a a) (b b)) (closure '(a b) 'foo)) | |
672 | ||
673 | ||
674 | ||
675 | 8.4.1. an example | |
676 | ||
677 | ____________________________________________________________ | |
678 | ||
679 | % lisp | |
680 | Franz Lisp, Opus 38.60 | |
681 | ->(defun code (me count) | |
682 | (print (list 'in x)) | |
683 | (setq x (+ 1 x)) | |
684 | (cond ((greaterp count 1) (funcall me me (sub1 count)))) | |
685 | (print (list 'out x))) | |
686 | code | |
687 | ->(defun tester (object count) | |
688 | (funcall object object count) (terpri)) | |
689 | ||
690 | ||
691 | Printed: January 31, 1984 | |
692 | ||
693 | ||
694 | ||
695 | ||
696 | ||
697 | ||
698 | ||
699 | Functions, Fclosures, and Macros 8-9 | |
700 | ||
701 | ||
702 | tester | |
703 | ->(setq x 0) | |
704 | 0 | |
705 | ->(setq z (fclosure '(x) 'code)) | |
706 | fclosure[8] | |
707 | -> (tester z 3) | |
708 | (in 0)(in 1)(in 2)(out 3)(out 3)(out 3) | |
709 | nil | |
710 | ->x | |
711 | 0 | |
712 | ____________________________________________________________ | |
713 | ||
714 | ||
715 | ||
716 | ||
717 | ||
718 | The function _\bf_\bc_\bl_\bo_\bs_\bu_\br_\be creates a new object | |
719 | that we will call an fclosure, (although it is | |
720 | actually a vector). The fclosure contains a func- | |
721 | tional object, and a set of symbols and values for | |
722 | the symbols. In the above example, the fclosure | |
723 | functional object is the function code. The set of | |
724 | symbols and values just contains the symbol `x' and | |
725 | zero, the value of `x' when the fclosure was | |
726 | created. | |
727 | ||
728 | When an fclosure is funcall'ed: | |
729 | ||
730 | 1) The Lisp system lambda binds the symbols in | |
731 | the fclosure to their values in the fclosure. | |
732 | ||
733 | 2) It continues the funcall on the functional | |
734 | object of the fclosure. | |
735 | ||
736 | 3) Finally, it un-lambda binds the symbols in the | |
737 | fclosure and at the same time stores the | |
738 | current values of the symbols in the fclosure. | |
739 | ||
740 | ||
741 | Notice that the fclosure is saving the value | |
742 | of the symbol `x'. Each time a fclosure is | |
743 | created, new space is allocated for saving the | |
744 | values of the symbols. Thus if we execute fclosure | |
745 | again, over the same function, we can have two | |
746 | independent counters: | |
747 | ||
748 | ____________________________________________________________ | |
749 | ||
750 | -> (setq zz (fclosure '(x) 'code)) | |
751 | fclosure[1] | |
752 | -> (tester zz 2) | |
753 | (in 0)(in 1)(out 2)(out 2) | |
754 | -> (tester zz 2) | |
755 | ||
756 | ||
757 | Printed: January 31, 1984 | |
758 | ||
759 | ||
760 | ||
761 | ||
762 | ||
763 | ||
764 | ||
765 | Functions, Fclosures, and Macros 8-10 | |
766 | ||
767 | ||
768 | (in 2)(in 3)(out 4)(out 4) | |
769 | -> (tester z 3) | |
770 | (in 3)(in 4)(in 5)(out 6)(out 6)(out 6) | |
771 | ____________________________________________________________ | |
772 | ||
773 | ||
774 | ||
775 | ||
776 | ||
777 | ||
778 | ||
779 | 8.4.2. useful functions | |
780 | ||
781 | Here are some quick some summaries of func- | |
782 | tions dealing with closures. They are more for- | |
783 | mally defined in 2.8.4. To recap, fclosures are | |
784 | made by (_\bf_\bc_\bl_\bo_\bs_\bu_\br_\be '_\bl__\bv_\ba_\br_\bs '_\bg__\bf_\bu_\bn_\bc_\bo_\bb_\bj). l_vars is a | |
785 | list of symbols (not containing nil), g_funcobj is | |
786 | any object that can be funcalled. (Objects which | |
787 | can be funcalled, include compiled Lisp functions, | |
788 | lambda expressions, symbols, foreign functions, | |
789 | etc.) In general, if you want a compiled function | |
790 | to be closed over a variable, you must declare the | |
791 | variable to be special within the function. | |
792 | Another example would be: | |
793 | ||
794 | (fclosure '(a b) #'(lambda (x) (plus x a))) | |
795 | ||
796 | Here, the #' construction will make the compiler | |
797 | compile the lambda expression. | |
798 | ||
799 | There are times when you want to share vari- | |
800 | ables between fclosures. This can be done if the | |
801 | fclosures are created at the same time using | |
802 | _\bf_\bc_\bl_\bo_\bs_\bu_\br_\be-_\bl_\bi_\bs_\bt. The function _\bf_\bc_\bl_\bo_\bs_\bu_\br_\be-_\ba_\bl_\bi_\bs_\bt returns | |
803 | an assoc list giving the symbols and values in the | |
804 | fclosure. The predicate _\bf_\bc_\bl_\bo_\bs_\bu_\br_\be_\bp returns t iff | |
805 | its argument is a fclosure. Other functions | |
806 | imported from Lisp Machine Lisp are _\bs_\by_\bm_\be_\bv_\ba_\bl-_\bi_\bn- | |
807 | _\bf_\bc_\bl_\bo_\bs_\bu_\br_\be, _\bl_\be_\bt-_\bf_\bc_\bl_\bo_\bs_\be_\bd, and _\bs_\be_\bt-_\bi_\bn-_\bf_\bc_\bl_\bo_\bs_\bu_\br_\be. | |
808 | Lastly, the function _\bf_\bc_\bl_\bo_\bs_\bu_\br_\be-_\bf_\bu_\bn_\bc_\bt_\bi_\bo_\bn returns the | |
809 | function argument. | |
810 | ||
811 | ||
812 | ||
813 | 8.4.3. internal structure | |
814 | ||
815 | Currently, closures are implemented as vec- | |
816 | tors, with property being the symbol fclosure. The | |
817 | functional object is the first entry. The remain- | |
818 | ing entries are structures which point to the sym- | |
819 | bols and values for the closure, (with a reference | |
820 | count to determine if a recursive closure is | |
821 | ||
822 | ||
823 | Printed: January 31, 1984 | |
824 | ||
825 | ||
826 | ||
827 | ||
828 | ||
829 | ||
830 | ||
831 | Functions, Fclosures, and Macros 8-11 | |
832 | ||
833 | ||
834 | active). | |
835 | ||
836 | ||
837 | ||
838 | 8.5. foreign subroutines and functions | |
839 | ||
840 | FRANZ LISP has the ability to dynamically load | |
841 | object files produced by other compilers and to call | |
842 | functions defined in those files. These functions are | |
843 | called _\bf_\bo_\br_\be_\bi_\bg_\bn functions.* There are seven types of | |
844 | foreign functions. They are characterized by the type | |
845 | of result they return, and by differences in the | |
846 | interpretation of their arguments. They come from two | |
847 | families: a group suited for languages which pass | |
848 | arguments by reference (e.g. Fortran), and a group | |
849 | suited for languages which pass arguments by value | |
850 | (e.g. C). | |
851 | ||
852 | ||
853 | There are four types in the first group: | |
854 | ||
855 | subroutine | |
856 | This does not return anything. The Lisp system | |
857 | always returns t after calling a subroutine. | |
858 | ||
859 | function | |
860 | This returns whatever the function returns. This | |
861 | must be a valid Lisp object or it may cause the | |
862 | Lisp system to fail. | |
863 | ||
864 | integer-function | |
865 | This returns an integer which the Lisp system | |
866 | makes into a fixnum and returns. | |
867 | ||
868 | real-function | |
869 | This returns a double precision real number which | |
870 | the Lisp system makes into a flonum and returns. | |
871 | ||
872 | ||
873 | There are three types in the second group: | |
874 | ||
875 | c-function | |
876 | This is like an integer function, except for its | |
877 | different interpretation of arguments. | |
878 | ||
879 | ||
880 | ____________________ | |
881 | \e9 *This topic is also discussed in Report PAM-124 of the | |
882 | Center for Pure and Applied Mathematics, UCB, entitled | |
883 | ``Parlez-Vous Franz? An Informal Introduction to Interfac- | |
884 | ing Foreign Functions to Franz LISP'', by James R. Larus | |
885 | ||
886 | ||
887 | ||
888 | \e9 Printed: January 31, 1984 | |
889 | ||
890 | ||
891 | ||
892 | ||
893 | ||
894 | ||
895 | ||
896 | Functions, Fclosures, and Macros 8-12 | |
897 | ||
898 | ||
899 | double-c-function | |
900 | This is like a real-function. | |
901 | ||
902 | vector-c-function | |
903 | This is for C functions which return a structure. | |
904 | The first argument to such functions must be a | |
905 | vector (of type vectori), into which the result | |
906 | is stored. The second Lisp argument becomes the | |
907 | first argument to the C function, and so on | |
908 | ||
909 | A foreign function is accessed through a binary object | |
910 | just like a compiled Lisp function. The difference is | |
911 | that the discipline field of a binary object for a | |
912 | foreign function is a string whose first character is | |
913 | given in the following table: | |
914 | ||
915 | ||
916 | \e8 ____________________________ | |
917 | letter type | |
918 | \e8 ____________________________\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b____________________________ | |
919 | s subroutine | |
920 | \e8 ____________________________ | |
921 | f function | |
922 | \e8 ____________________________ | |
923 | i integer-function | |
924 | \e8 ____________________________ | |
925 | r real-function. | |
926 | \e8 ____________________________ | |
927 | c c-function | |
928 | \e8 ____________________________ | |
929 | v vector-c-function | |
930 | \e8 ____________________________ | |
931 | d double-c-function | |
932 | \e8 ____________________________ | |
933 | \e7 |\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7| | |
934 | ||
935 | ||
936 | ||
937 | ||
938 | ||
939 | ||
940 | ||
941 | ||
942 | ||
943 | ||
944 | |\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7| | |
945 | ||
946 | ||
947 | ||
948 | ||
949 | ||
950 | ||
951 | ||
952 | ||
953 | ||
954 | ||
955 | |\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7|\b\e7| | |
956 | ||
957 | ||
958 | ||
959 | ||
960 | ||
961 | ||
962 | ||
963 | ||
964 | ||
965 | ||
966 | ||
967 | ||
968 | Two functions are provided for setting-up foreign | |
969 | functions. _\bC_\bf_\ba_\bs_\bl loads an object file into the Lisp | |
970 | system and sets up one foreign function binary object. | |
971 | If there are more than one function in an object file, | |
972 | _\bg_\be_\bt_\ba_\bd_\bd_\br_\be_\bs_\bs can be used to set up additional foreign | |
973 | function objects. | |
974 | ||
975 | Foreign functions are called just like other | |
976 | functions, e.g (_\bf_\bu_\bn_\bn_\ba_\bm_\be _\ba_\br_\bg_\b1 _\ba_\br_\bg_\b2). When a function | |
977 | in the Fortran group is called, the arguments are | |
978 | evaluated and then examined. List, hunk and symbol | |
979 | arguments are passed unchanged to the foreign func- | |
980 | tion. Fixnum and flonum arguments are copied into a | |
981 | temporary location and a pointer to the value is | |
982 | passed (this is because Fortran uses call by reference | |
983 | and it is dangerous to modify the contents of a fixnum | |
984 | or flonum which something else might point to). If | |
985 | the argument is an array object, the data field of the | |
986 | array object is passed to the foreign function (This | |
987 | is the easiest way to send large amounts of data to | |
988 | and receive large amounts of data from a foreign func- | |
989 | tion). If a binary object is an argument, the entry | |
990 | ||
991 | ||
992 | \e9 Printed: January 31, 1984 | |
993 | ||
994 | ||
995 | ||
996 | ||
997 | ||
998 | ||
999 | ||
1000 | Functions, Fclosures, and Macros 8-13 | |
1001 | ||
1002 | ||
1003 | field of that object is passed to the foreign function | |
1004 | (the entry field is the address of a function, so this | |
1005 | amounts to passing a function as an argument). | |
1006 | ||
1007 | When a function in the C group is called, fixnum | |
1008 | and flownum arguments are passed by value. For almost | |
1009 | all other arguments, the address is merely provided to | |
1010 | the C routine. The only exception arises when you | |
1011 | want to invoke a C routine which expects a ``struc- | |
1012 | ture'' argument. Recall that a (rarely used) feature | |
1013 | of the C language is the ability to pass structures by | |
1014 | value. This copies the structure onto the stack. | |
1015 | Since the Franz's nearest equivalent to a C structure | |
1016 | is a vector, we provide an escape clause to copy the | |
1017 | contents of an immediate-type vector by value. If the | |
1018 | property field of a vectori argument, is the symbol | |
1019 | "value-structure-argument", then the binary data of | |
1020 | this immediate-type vector is copied into the argument | |
1021 | list of the C routine. | |
1022 | ||
1023 | The method a foreign function uses to access the | |
1024 | arguments provided by Lisp is dependent on the | |
1025 | language of the foreign function. The following | |
1026 | scripts demonstrate how how Lisp can interact with | |
1027 | three languages: C, Pascal and Fortran. C and Pascal | |
1028 | have pointer types and the first script shows how to | |
1029 | use pointers to extract information from Lisp objects. | |
1030 | There are two functions defined for each language. | |
1031 | The first (cfoo in C, pfoo in Pascal) is given four | |
1032 | arguments, a fixnum, a flonum-block array, a hunk of | |
1033 | at least two fixnums and a list of at least two fix- | |
1034 | nums. To demonstrate that the values were passed, | |
1035 | each ?foo function prints its arguments (or parts of | |
1036 | them). The ?foo function then modifies the second | |
1037 | element of the flonum-block array and returns a 3 to | |
1038 | Lisp. The second function (cmemq in C, pmemq in Pas- | |
1039 | cal) acts just like the Lisp _\bm_\be_\bm_\bq function (except it | |
1040 | won't work for fixnums whereas the lisp _\bm_\be_\bm_\bq will work | |
1041 | for small fixnums). In the script, typed input is in | |
1042 | bold, computer output is in roman and comments are in | |
1043 | _\bi_\bt_\ba_\bl_\bi_\bc. | |
1044 | ||
1045 | ||
1046 | ____________________________________________________________ | |
1047 | ||
1048 | _\bT_\bh_\be_\bs_\be _\ba_\br_\be _\bt_\bh_\be _\bC _\bc_\bo_\bd_\be_\bd _\bf_\bu_\bn_\bc_\bt_\bi_\bo_\bn_\bs | |
1049 | % cat ch8auxc.c | |
1050 | /* demonstration of c coded foreign integer-function */ | |
1051 | ||
1052 | /* the following will be used to extract fixnums out of a list of fixnums */ | |
1053 | struct listoffixnumscell | |
1054 | { struct listoffixnumscell *cdr; | |
1055 | int *fixnum; | |
1056 | ||
1057 | ||
1058 | Printed: January 31, 1984 | |
1059 | ||
1060 | ||
1061 | ||
1062 | ||
1063 | ||
1064 | ||
1065 | ||
1066 | Functions, Fclosures, and Macros 8-14 | |
1067 | ||
1068 | ||
1069 | }; | |
1070 | ||
1071 | struct listcell | |
1072 | { struct listcell *cdr; | |
1073 | int car; | |
1074 | }; | |
1075 | ||
1076 | cfoo(a,b,c,d) | |
1077 | int *a; | |
1078 | double b[]; | |
1079 | int *c[]; | |
1080 | struct listoffixnumscell *d; | |
1081 | { | |
1082 | printf("a: %d, b[0]: %f, b[1]: %f0, *a, b[0], b[1]); | |
1083 | printf(" c (first): %d c (second): %d0, | |
1084 | *c[0],*c[1]); | |
1085 | printf(" ( %d %d ... ) ", *(d->fixnum), *(d->cdr->fixnum)); | |
1086 | b[1] = 3.1415926; | |
1087 | return(3); | |
1088 | } | |
1089 | ||
1090 | struct listcell * | |
1091 | cmemq(element,list) | |
1092 | int element; | |
1093 | struct listcell *list; | |
1094 | { | |
1095 | for( ; list && element != list->car ; list = list->cdr); | |
1096 | return(list); | |
1097 | } | |
1098 | ||
1099 | ||
1100 | _\bT_\bh_\be_\bs_\be _\ba_\br_\be _\bt_\bh_\be _\bP_\ba_\bs_\bc_\ba_\bl _\bc_\bo_\bd_\be_\bd _\bf_\bu_\bn_\bc_\bt_\bi_\bo_\bn_\bs | |
1101 | % cat ch8auxp.p | |
1102 | type pinteger = ^integer; | |
1103 | realarray = array[0..10] of real; | |
1104 | pintarray = array[0..10] of pinteger; | |
1105 | listoffixnumscell = record | |
1106 | cdr : ^listoffixnumscell; | |
1107 | fixnum : pinteger; | |
1108 | end; | |
1109 | plistcell = ^listcell; | |
1110 | listcell = record | |
1111 | cdr : plistcell; | |
1112 | car : integer; | |
1113 | end; | |
1114 | ||
1115 | function pfoo ( var a : integer ; | |
1116 | var b : realarray; | |
1117 | var c : pintarray; | |
1118 | var d : listoffixnumscell) : integer; | |
1119 | begin | |
1120 | writeln(' a:',a, ' b[0]:', b[0], ' b[1]:', b[1]); | |
1121 | writeln(' c (first):', c[0]^,' c (second):', c[1]^); | |
1122 | ||
1123 | ||
1124 | Printed: January 31, 1984 | |
1125 | ||
1126 | ||
1127 | ||
1128 | ||
1129 | ||
1130 | ||
1131 | ||
1132 | Functions, Fclosures, and Macros 8-15 | |
1133 | ||
1134 | ||
1135 | writeln(' ( ', d.fixnum^, d.cdr^.fixnum^, ' ...) '); | |
1136 | b[1] := 3.1415926; | |
1137 | pfoo := 3 | |
1138 | end ; | |
1139 | ||
1140 | { the function pmemq looks for the Lisp pointer given as the first argument | |
1141 | in the list pointed to by the second argument. | |
1142 | Note that we declare " a : integer " instead of " var a : integer " since | |
1143 | we are interested in the pointer value instead of what it points to (which | |
1144 | could be any Lisp object) | |
1145 | } | |
1146 | function pmemq( a : integer; list : plistcell) : plistcell; | |
1147 | begin | |
1148 | while (list <> nil) and (list^.car <> a) do list := list^.cdr; | |
1149 | pmemq := list; | |
1150 | end ; | |
1151 | ||
1152 | ||
1153 | _\bT_\bh_\be _\bf_\bi_\bl_\be_\bs _\ba_\br_\be _\bc_\bo_\bm_\bp_\bi_\bl_\be_\bd | |
1154 | % cc -c ch8auxc.c | |
1155 | 1.0u 1.2s 0:15 14% 30+39k 33+20io 147pf+0w | |
1156 | % pc -c ch8auxp.p | |
1157 | 3.0u 1.7s 0:37 12% 27+32k 53+32io 143pf+0w | |
1158 | ||
1159 | ||
1160 | % lisp | |
1161 | Franz Lisp, Opus 38.60 | |
1162 | _\bF_\bi_\br_\bs_\bt _\bt_\bh_\be _\bf_\bi_\bl_\be_\bs _\ba_\br_\be _\bl_\bo_\ba_\bd_\be_\bd _\ba_\bn_\bd _\bw_\be _\bs_\be_\bt _\bu_\bp _\bo_\bn_\be _\bf_\bo_\br_\be_\bi_\bg_\bn _\bf_\bu_\bn_\bc_\b- | |
1163 | _\bt_\bi_\bo_\bn _\bb_\bi_\bn_\ba_\br_\by. _\bW_\be _\bh_\ba_\bv_\be _\bt_\bw_\bo _\bf_\bu_\bn_\bc_\bt_\bi_\bo_\bn_\bs _\bi_\bn _\be_\ba_\bc_\bh _\bf_\bi_\bl_\be _\bs_\bo _\bw_\be _\bm_\bu_\bs_\bt | |
1164 | _\bc_\bh_\bo_\bo_\bs_\be _\bo_\bn_\be _\bt_\bo _\bt_\be_\bl_\bl _\bc_\bf_\ba_\bs_\bl _\ba_\bb_\bo_\bu_\bt. _\bT_\bh_\be _\bc_\bh_\bo_\bi_\bc_\be _\bi_\bs _\ba_\br_\bb_\bi_\bt_\br_\ba_\br_\by. | |
1165 | -> (cfasl 'ch8auxc.o '_cfoo 'cfoo "integer-function") | |
1166 | /usr/lib/lisp/nld -N -A /usr/local/lisp -T 63000 ch8auxc.o -e _cfoo -o /tmp/Li7055.0 -lc | |
1167 | #63000-"integer-function" | |
1168 | -> (cfasl 'ch8auxp.o '_pfoo 'pfoo "integer-function" "-lpc") | |
1169 | /usr/lib/lisp/nld -N -A /tmp/Li7055.0 -T 63200 ch8auxp.o -e _pfoo -o /tmp/Li7055.1 -lpc -lc | |
1170 | #63200-"integer-function" | |
1171 | _\bH_\be_\br_\be _\bw_\be _\bs_\be_\bt _\bu_\bp _\bt_\bh_\be _\bo_\bt_\bh_\be_\br _\bf_\bo_\br_\be_\bi_\bg_\bn _\bf_\bu_\bn_\bc_\bt_\bi_\bo_\bn _\bb_\bi_\bn_\ba_\br_\by _\bo_\bb_\bj_\be_\bc_\bt_\bs | |
1172 | -> (getaddress '_cmemq 'cmemq "function" '_pmemq 'pmemq "function") | |
1173 | #6306c-"function" | |
1174 | _\bW_\be _\bw_\ba_\bn_\bt _\bt_\bo _\bc_\br_\be_\ba_\bt_\be _\ba_\bn_\bd _\bi_\bn_\bi_\bt_\bi_\ba_\bl_\bi_\bz_\be _\ba_\bn _\ba_\br_\br_\ba_\by _\bt_\bo _\bp_\ba_\bs_\bs _\bt_\bo _\bt_\bh_\be | |
1175 | _\bc_\bf_\bo_\bo _\bf_\bu_\bn_\bc_\bt_\bi_\bo_\bn. _\bI_\bn _\bt_\bh_\bi_\bs _\bc_\ba_\bs_\be _\bw_\be _\bc_\br_\be_\ba_\bt_\be _\ba_\bn _\bu_\bn_\bn_\ba_\bm_\be_\bd _\ba_\br_\br_\ba_\by _\ba_\bn_\bd | |
1176 | _\bs_\bt_\bo_\br_\be _\bi_\bt _\bi_\bn _\bt_\bh_\be _\bv_\ba_\bl_\bu_\be _\bc_\be_\bl_\bl _\bo_\bf _\bt_\be_\bs_\bt_\ba_\br_\br. _\bW_\bh_\be_\bn _\bw_\be _\bc_\br_\be_\ba_\bt_\be _\ba_\bn | |
1177 | _\ba_\br_\br_\ba_\by _\bt_\bo _\bp_\ba_\bs_\bs _\bt_\bo _\bt_\bh_\be _\bP_\ba_\bs_\bc_\ba_\bl _\bp_\br_\bo_\bg_\br_\ba_\bm _\bw_\be _\bw_\bi_\bl_\bl _\bu_\bs_\be _\ba _\bn_\ba_\bm_\be_\bd | |
1178 | _\ba_\br_\br_\ba_\by _\bj_\bu_\bs_\bt _\bt_\bo _\bd_\be_\bm_\bo_\bn_\bs_\bt_\br_\ba_\bt_\be _\bt_\bh_\be _\bd_\bi_\bf_\bf_\be_\br_\be_\bn_\bt _\bw_\ba_\by _\bt_\bh_\ba_\bt _\bn_\ba_\bm_\be_\bd _\ba_\bn_\bd | |
1179 | _\bu_\bn_\bn_\ba_\bm_\be_\bd _\ba_\br_\br_\ba_\by_\bs _\ba_\br_\be _\bc_\br_\be_\ba_\bt_\be_\bd _\ba_\bn_\bd _\ba_\bc_\bc_\be_\bs_\bs_\be_\bd. | |
1180 | -> (setq testarr (array nil flonum-block 2)) | |
1181 | array[2] | |
1182 | -> (store (funcall testarr 0) 1.234) | |
1183 | 1.234 | |
1184 | -> (store (funcall testarr 1) 5.678) | |
1185 | 5.678 | |
1186 | -> (cfoo 385 testarr (hunk 10 11 13 14) '(15 16 17)) | |
1187 | a: 385, b[0]: 1.234000, b[1]: 5.678000 | |
1188 | ||
1189 | ||
1190 | Printed: January 31, 1984 | |
1191 | ||
1192 | ||
1193 | ||
1194 | ||
1195 | ||
1196 | ||
1197 | ||
1198 | Functions, Fclosures, and Macros 8-16 | |
1199 | ||
1200 | ||
1201 | c (first): 10 c (second): 11 | |
1202 | ( 15 16 ... ) | |
1203 | 3 | |
1204 | _\bN_\bo_\bt_\be _\bt_\bh_\ba_\bt _\bc_\bf_\bo_\bo _\bh_\ba_\bs _\br_\be_\bt_\bu_\br_\bn_\be_\bd _\b3 _\ba_\bs _\bi_\bt _\bs_\bh_\bo_\bu_\bl_\bd. _\bI_\bt _\ba_\bl_\bs_\bo _\bh_\ba_\bd _\bt_\bh_\be | |
1205 | _\bs_\bi_\bd_\be _\be_\bf_\bf_\be_\bc_\bt _\bo_\bf _\bc_\bh_\ba_\bn_\bg_\bi_\bn_\bg _\bt_\bh_\be _\bs_\be_\bc_\bo_\bn_\bd _\bv_\ba_\bl_\bu_\be _\bo_\bf _\bt_\bh_\be _\ba_\br_\br_\ba_\by _\bt_\bo | |
1206 | _\b3._\b1_\b4_\b1_\b5_\b9_\b2_\b6 _\bw_\bh_\bi_\bc_\bh _\bc_\bh_\be_\bc_\bk _\bn_\be_\bx_\bt. | |
1207 | -> (funcall testarr 1) | |
1208 | 3.1415926 | |
1209 | ||
1210 | ||
1211 | _\bI_\bn _\bp_\br_\be_\bp_\ba_\br_\ba_\bt_\bi_\bo_\bn _\bf_\bo_\br _\bc_\ba_\bl_\bl_\bi_\bn_\bg _\bp_\bf_\bo_\bo _\bw_\be _\bc_\br_\be_\ba_\bt_\be _\ba_\bn _\ba_\br_\br_\ba_\by. | |
1212 | -> (array test flonum-block 2) | |
1213 | array[2] | |
1214 | -> (store (test 0) 1.234) | |
1215 | 1.234 | |
1216 | -> (store (test 1) 5.678) | |
1217 | 5.678 | |
1218 | -> (pfoo 385 (getd 'test) (hunk 10 11 13 14) '(15 16 17)) | |
1219 | a: 385 b[0]: 1.23400000000000E+00 b[1]: 5.67800000000000E+00 | |
1220 | c (first): 10 c (second): 11 | |
1221 | ( 15 16 ...) | |
1222 | 3 | |
1223 | -> (test 1) | |
1224 | 3.1415926 | |
1225 | ||
1226 | _\bN_\bo_\bw _\bt_\bo _\bt_\be_\bs_\bt _\bo_\bu_\bt _\bt_\bh_\be _\bm_\be_\bm_\bq'_\bs | |
1227 | -> (cmemq 'a '(b c a d e f)) | |
1228 | (_\ba _\bd _\be _\bf) | |
1229 | -> (pmemq 'e '(a d f g a x)) | |
1230 | _\bn_\bi_\bl | |
1231 | ____________________________________________________________ | |
1232 | ||
1233 | ||
1234 | ||
1235 | ||
1236 | ||
1237 | The Fortran example will be much shorter since in | |
1238 | Fortran you can't follow pointers as you can in other | |
1239 | languages. The Fortran function ffoo is given three | |
1240 | arguments: a fixnum, a fixnum-block array and a flo- | |
1241 | num. These arguments are printed out to verify that | |
1242 | they made it and then the first value of the array is | |
1243 | modified. The function returns a double precision | |
1244 | value which is converted to a flonum by lisp and | |
1245 | printed. Note that the entry point corresponding to | |
1246 | the Fortran function ffoo is _ffoo_ as opposed to the | |
1247 | C and Pascal convention of preceding the name with an | |
1248 | underscore. | |
1249 | ||
1250 | ____________________________________________________________ | |
1251 | ||
1252 | ||
1253 | % cat ch8auxf.f | |
1254 | ||
1255 | ||
1256 | Printed: January 31, 1984 | |
1257 | ||
1258 | ||
1259 | ||
1260 | ||
1261 | ||
1262 | ||
1263 | ||
1264 | Functions, Fclosures, and Macros 8-17 | |
1265 | ||
1266 | ||
1267 | double precision function ffoo(a,b,c) | |
1268 | integer a,b(10) | |
1269 | double precision c | |
1270 | print 2,a,b(1),b(2),c | |
1271 | 2 format(' a=',i4,', b(1)=',i5,', b(2)=',i5,' c=',f6.4) | |
1272 | b(1) = 22 | |
1273 | ffoo = 1.23456 | |
1274 | return | |
1275 | end | |
1276 | % f77 -c ch8auxf.f | |
1277 | ch8auxf.f: | |
1278 | ffoo: | |
1279 | 0.9u 1.8s 0:12 22% 20+22k 54+48io 158pf+0w | |
1280 | % lisp | |
1281 | Franz Lisp, Opus 38.60 | |
1282 | -> (cfasl 'ch8auxf.o '_ffoo_ 'ffoo "real-function" "-lF77 -lI77") | |
1283 | /usr/lib/lisp/nld -N -A /usr/local/lisp -T 63000 ch8auxf.o -e _ffoo_ | |
1284 | -o /tmp/Li11066.0 -lF77 -lI77 -lc | |
1285 | #6307c-"real-function" | |
1286 | ||
1287 | -> (array test fixnum-block 2) | |
1288 | array[2] | |
1289 | -> (store (test 0) 10) | |
1290 | 10 | |
1291 | -> (store (test 1) 11) | |
1292 | 11 | |
1293 | -> (ffoo 385 (getd 'test) 5.678) | |
1294 | a= 385, b(1)= 10, b(2)= 11 c=5.6780 | |
1295 | 1.234559893608093 | |
1296 | -> (test 0) | |
1297 | 22 | |
1298 | ||
1299 | ____________________________________________________________ | |
1300 | ||
1301 | ||
1302 | ||
1303 | ||
1304 | ||
1305 | ||
1306 | ||
1307 | ||
1308 | ||
1309 | ||
1310 | ||
1311 | ||
1312 | ||
1313 | ||
1314 | ||
1315 | ||
1316 | ||
1317 | ||
1318 | ||
1319 | \e9 | |
1320 | ||
1321 | \e9 Printed: January 31, 1984 | |
1322 | ||
1323 | ||
1324 |