Commit | Line | Data |
---|---|---|
38a93523 NJ |
1 | @page |
2 | @node Data Types | |
3 | @chapter Data Types for Generic Use | |
4 | ||
5 | This chapter describes all the data types that Guile provides for | |
6 | ``generic use''. | |
7 | ||
8 | One of the great strengths of Scheme is that there is no straightforward | |
9 | distinction between ``data'' and ``functionality''. For example, | |
10 | Guile's support for dynamic linking could be described | |
11 | ||
12 | @itemize | |
13 | @item | |
14 | either in a ``data-centric'' way, as the behaviour and properties of the | |
15 | ``dynamically linked object'' data type, and the operations that may be | |
16 | applied to instances of this type | |
17 | ||
18 | @item | |
19 | or in a ``functionality-centric'' way, as the set of procedures that | |
20 | constitute Guile's support for dynamic linking, in the context of the | |
21 | module system. | |
22 | @end itemize | |
23 | ||
24 | The contents of this chapter are, therefore, a matter of judgement. By | |
25 | ``generic use'', we mean to select those data types whose typical use as | |
26 | @emph{data} in a wide variety of programming contexts is more important | |
27 | than their use in the implementation of a particular piece of | |
28 | @emph{functionality}. | |
29 | ||
30 | @ifinfo | |
31 | The following menu | |
32 | @end ifinfo | |
33 | @iftex | |
34 | The table of contents for this chapter | |
35 | @end iftex | |
36 | @ifhtml | |
37 | The following table of contents | |
38 | @end ifhtml | |
39 | shows the data types that are documented in this chapter. The final | |
40 | section of this chapter lists all the core Guile data types that are not | |
41 | documented here, and provides links to the ``functionality-centric'' | |
42 | sections of this manual that cover them. | |
43 | ||
44 | @menu | |
45 | * Booleans:: True/false values. | |
46 | * Numbers:: Numerical data types. | |
47 | * Characters:: New character names. | |
48 | * Strings:: Special things about strings. | |
49 | * Regular Expressions:: Pattern matching and substitution. | |
50 | * Symbols and Variables:: Manipulating the Scheme symbol table. | |
51 | * Keywords:: Self-quoting, customizable display keywords. | |
52 | * Pairs:: Scheme's basic building block. | |
53 | * Lists:: Special list functions supported by Guile. | |
54 | * Records:: | |
55 | * Structures:: | |
56 | * Arrays:: | |
57 | * Association Lists and Hash Tables:: | |
58 | * Vectors:: | |
59 | * Hooks:: User-customizable event lists. | |
60 | * Other Data Types:: Data types that are documented elsewhere. | |
61 | @end menu | |
62 | ||
63 | ||
64 | @node Booleans | |
65 | @section Booleans | |
66 | ||
67 | The two boolean values are @code{#t} for true and @code{#f} for false. | |
68 | ||
69 | Boolean values are returned by predicate procedures, such as the general | |
70 | equality predicates @code{eq?}, @code{eqv?} and @code{equal?} | |
71 | (@pxref{Equality}) and numerical and string comparison operators like | |
72 | @code{string=?} (REFFIXME) and @code{<=} (REFFIXME). | |
73 | ||
74 | @lisp | |
75 | (<= 3 8) | |
76 | @result{} | |
77 | #t | |
78 | ||
79 | (<= 3 -3) | |
80 | @result{} | |
81 | #f | |
82 | ||
83 | (equal? "house" "houses") | |
84 | @result{} | |
85 | #f | |
86 | ||
87 | (eq? #f #f) | |
88 | @result{} | |
89 | #t | |
90 | @end lisp | |
91 | ||
92 | In test condition contexts like @code{if} (REFFIXME) and @code{cond} | |
93 | (REFFIXME), where a group of subexpressions will be evaluated only if a | |
94 | @var{condition} expression evaluates to ``true'', ``true'' means any | |
95 | value at all except @code{#f}. | |
96 | ||
97 | @lisp | |
98 | (if #t "yes" "no") | |
99 | @result{} | |
100 | "yes" | |
101 | ||
102 | (if 0 "yes" "no") | |
103 | @result{} | |
104 | "yes" | |
105 | ||
106 | (if #f "yes" "no") | |
107 | @result{} | |
108 | "no" | |
109 | @end lisp | |
110 | ||
111 | A result of this asymmetry is that typical Scheme source code more often | |
112 | uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to | |
113 | represent an @code{if} or @code{cond} false value, whereas @code{#t} is | |
114 | not necessary to represent an @code{if} or @code{cond} true value. | |
115 | ||
116 | It is important to note that @code{#f} is @strong{not} equivalent to any | |
117 | other Scheme value. In particular, @code{#f} is not the same as the | |
118 | number 0 (like in C and C++), and not the same as the ``empty list'' | |
119 | (like in some Lisp dialects). | |
120 | ||
121 | The @code{not} procedure returns the boolean inverse of its argument: | |
122 | ||
123 | @c docstring begin (texi-doc-string "guile" "not") | |
124 | @deffn primitive not x | |
125 | Return @code{#t} iff @var{x} is @code{#f}, else return @code{#f}. | |
126 | @end deffn | |
127 | ||
128 | The @code{boolean?} procedure is a predicate that returns @code{#t} if | |
129 | its argument is one of the boolean values, otherwise @code{#f}. | |
130 | ||
131 | @c docstring begin (texi-doc-string "guile" "boolean?") | |
132 | @deffn primitive boolean? obj | |
133 | Return @code{#t} iff @var{obj} is either @code{#t} or @code{#f}. | |
134 | @end deffn | |
135 | ||
136 | ||
137 | @node Numbers | |
138 | @section Numerical data types | |
139 | ||
140 | Guile supports a rich ``tower'' of numerical types --- integer, | |
141 | rational, real and complex --- and provides an extensive set of | |
142 | mathematical and scientific functions for operating on numerical | |
143 | data. This section of the manual documents those types and functions. | |
144 | ||
145 | You may also find it illuminating to read R5RS's presentation of numbers | |
146 | in Scheme, which is particularly clear and accessible: see | |
147 | @xref{Numbers,,,r5rs}. | |
148 | ||
149 | @menu | |
150 | * Numerical Tower:: Scheme's numerical "tower". | |
151 | * Integers:: Whole numbers. | |
152 | * Reals and Rationals:: Real and rational numbers. | |
153 | * Complex Numbers:: Complex numbers. | |
154 | * Exactness:: Exactness and inexactness. | |
155 | * Number Syntax:: Read syntax for numerical data. | |
156 | * Integer Operations:: Operations on integer values. | |
157 | * Comparison:: Comparison predicates. | |
158 | * Conversion:: Converting numbers to and from strings. | |
159 | * Complex:: Complex number operations. | |
160 | * Arithmetic:: Arithmetic functions. | |
161 | * Scientific:: Scientific functions. | |
162 | * Primitive Numerics:: Primitive numeric functions. | |
163 | * Bitwise Operations:: Logical AND, OR, NOT, and so on. | |
164 | * Random:: Random number generation. | |
165 | @end menu | |
166 | ||
167 | ||
168 | @node Numerical Tower | |
169 | @subsection Scheme's Numerical ``Tower'' | |
170 | ||
171 | Scheme's numerical ``tower'' consists of the following categories of | |
172 | numbers: | |
173 | ||
174 | @itemize | |
175 | @item | |
176 | integers (whole numbers) | |
177 | ||
178 | @item | |
179 | rationals (the set of numbers that can be expressed as P/Q where P and Q | |
180 | are integers) | |
181 | ||
182 | @item | |
183 | real numbers (the set of numbers that describes all possible positions | |
184 | along a one dimensional line) | |
185 | ||
186 | @item | |
187 | complex numbers (the set of numbers that describes all possible | |
188 | positions in a two dimensional space) | |
189 | @end itemize | |
190 | ||
191 | It is called a tower because each category ``sits on'' the one that | |
192 | follows it, in the sense that every integer is also a rational, every | |
193 | rational is also real, and every real number is also a complex number | |
194 | (but with zero imaginary part). | |
195 | ||
196 | Of these, Guile implements integers, reals and complex numbers as | |
197 | distinct types. Rationals are implemented as regards the read syntax | |
198 | for rational numbers that is specified by R5RS, but are immediately | |
199 | converted by Guile to the corresponding real number. | |
200 | ||
201 | The @code{number?} predicate may be applied to any Scheme value to | |
202 | discover whether the value is any of the supported numerical types. | |
203 | ||
204 | @c docstring begin (texi-doc-string "guile" "number?") | |
205 | @deffn primitive number? obj | |
206 | Return @code{#t} if @var{obj} is any kind of number, @code{#f} else. | |
207 | @end deffn | |
208 | ||
209 | For example: | |
210 | ||
211 | @lisp | |
212 | (number? 3) | |
213 | @result{} | |
214 | #t | |
215 | ||
216 | (number? "hello there!") | |
217 | @result{} | |
218 | #f | |
219 | ||
220 | (define pi 3.141592654) | |
221 | (number? pi) | |
222 | @result{} | |
223 | #t | |
224 | @end lisp | |
225 | ||
226 | The next few subsections document each of Guile's numerical data types | |
227 | in detail. | |
228 | ||
229 | ||
230 | @node Integers | |
231 | @subsection Integers | |
232 | ||
233 | Integers are whole numbers, that is numbers with no fractional part, | |
234 | such as 2, 83 and -3789. | |
235 | ||
236 | Integers in Guile can be arbitrarily big, as shown by the following | |
237 | example. | |
238 | ||
239 | @lisp | |
240 | (define (factorial n) | |
241 | (let loop ((n n) (product 1)) | |
242 | (if (= n 0) | |
243 | product | |
244 | (loop (- n 1) (* product n))))) | |
245 | ||
246 | (factorial 3) | |
247 | @result{} | |
248 | 6 | |
249 | ||
250 | (factorial 20) | |
251 | @result{} | |
252 | 2432902008176640000 | |
253 | ||
254 | (- (factorial 45)) | |
255 | @result{} | |
256 | -119622220865480194561963161495657715064383733760000000000 | |
257 | @end lisp | |
258 | ||
259 | Readers whose background is in programming languages where integers are | |
260 | limited by the need to fit into just 4 or 8 bytes of memory may find | |
261 | this surprising, or suspect that Guile's representation of integers is | |
262 | inefficient. In fact, Guile achieves a near optimal balance of | |
263 | convenience and efficiency by using the host computer's native | |
264 | representation of integers where possible, and a more general | |
265 | representation where the required number does not fit in the native | |
266 | form. Conversion between these two representations is automatic and | |
267 | completely invisible to the Scheme level programmer. | |
268 | ||
269 | @c REFFIXME Maybe point here to discussion of handling immediates/bignums | |
270 | @c on the C level, where the conversion is not so automatic - NJ | |
271 | ||
272 | @c docstring begin (texi-doc-string "guile" "integer?") | |
273 | @deffn primitive integer? obj | |
274 | Return @code{#t} if @var{obj} is an integer number, @code{#f} else. | |
275 | ||
276 | @lisp | |
277 | (integer? 487) | |
278 | @result{} | |
279 | #t | |
280 | ||
281 | (integer? -3.4) | |
282 | @result{} | |
283 | #f | |
284 | @end lisp | |
285 | @end deffn | |
286 | ||
287 | ||
288 | @node Reals and Rationals | |
289 | @subsection Real and Rational Numbers | |
290 | ||
291 | Mathematically, the real numbers are the set of numbers that describe | |
292 | all possible points along a continuous, infinite, one-dimensional line. | |
293 | The rational numbers are the set of all numbers that can be written as | |
294 | fractions P/Q, where P and Q are integers. All rational numbers are | |
295 | also real, but there are real numbers that are not rational, for example | |
296 | the square root of 2, and pi. | |
297 | ||
298 | Guile represents both real and rational numbers approximately using a | |
299 | floating point encoding with limited precision. Even though the actual | |
300 | encoding is in binary, it may be helpful to think of it as a decimal | |
301 | number with a limited number of significant figures and a decimal point | |
302 | somewhere, since this corresponds to the standard notation for non-whole | |
303 | numbers. For example: | |
304 | ||
305 | @lisp | |
306 | 0.34 | |
307 | -0.00000142857931198 | |
308 | -5648394822220000000000.0 | |
309 | 4.0 | |
310 | @end lisp | |
311 | ||
312 | The limited precision of Guile's encoding means that any ``real'' number | |
313 | in Guile can be written in a rational form, by multiplying and then dividing | |
314 | by sufficient powers of 10 (or in fact, 2). For example, | |
315 | @code{-0.00000142857931198} is the same as @code{142857931198} divided by | |
316 | @code{100000000000000000}. In Guile's current incarnation, therefore, | |
317 | the @code{rational?} and @code{real?} predicates are equivalent. | |
318 | ||
319 | Another aspect of this equivalence is that Guile currently does not | |
320 | preserve the exactness that is possible with rational arithmetic. | |
321 | If such exactness is needed, it is of course possible to implement | |
322 | exact rational arithmetic at the Scheme level using Guile's arbitrary | |
323 | size integers. | |
324 | ||
325 | A planned future revision of Guile's numerical tower will make it | |
326 | possible to implement exact representations and arithmetic for both | |
327 | rational numbers and real irrational numbers such as square roots, | |
328 | and in such a way that the new kinds of number integrate seamlessly | |
329 | with those that are already implemented. | |
330 | ||
331 | @c docstring begin (texi-doc-string "guile" "real?") | |
332 | @deffn primitive real? obj | |
333 | Return @code{#t} if @var{obj} is a real number, @code{#f} else. | |
334 | Note that the sets of integer and rational values form subsets | |
335 | of the set of real numbers, so the predicate will also be fulfilled | |
336 | if @var{obj} is an integer number or a rational number. | |
337 | @end deffn | |
338 | ||
339 | @c docstring begin (texi-doc-string "guile" "rational?") | |
340 | @deffn primitive rational? obj | |
341 | Return @code{#t} if @var{obj} is a rational number, @code{#f} else. | |
342 | Note that the set of integer values forms a subset of the set of | |
343 | rational numbers, so the predicate will also be fulfilled if @var{obj} | |
344 | is an integer number. | |
345 | @end deffn | |
346 | ||
347 | ||
348 | @node Complex Numbers | |
349 | @subsection Complex Numbers | |
350 | ||
351 | Complex numbers are the set of numbers that describe all possible points | |
352 | in a two-dimensional space. The two coordinates of a particular point | |
353 | in this space are known as the @dfn{real} and @dfn{imaginary} parts of | |
354 | the complex number that describes that point. | |
355 | ||
356 | In Guile, complex numbers are written in rectangular form as the sum of | |
357 | their real and imaginary parts, using the symbol @code{i} to indicate | |
358 | the imaginary part. | |
359 | ||
360 | @lisp | |
361 | 3+4i | |
362 | @result{} | |
363 | 3.0+4.0i | |
364 | ||
365 | (* 3-8i 2.3+0.3i) | |
366 | @result{} | |
367 | 9.3-17.5i | |
368 | @end lisp | |
369 | ||
370 | Guile represents a complex number as a pair of numbers both of which are | |
371 | real, so the real and imaginary parts of a complex number have the same | |
372 | properties of inexactness and limited precision as single real numbers. | |
373 | ||
374 | @c docstring begin (texi-doc-string "guile" "complex?") | |
375 | @deffn primitive complex? obj | |
376 | Return @code{#t} if @var{obj} is a complex number, @code{#f} else. | |
377 | Note that the sets of real, rational and integer values form subsets of | |
378 | the set of complex numbers, so the predicate will also be fulfilled if | |
379 | @var{obj} is a real, rational or integer number. | |
380 | @end deffn | |
381 | ||
382 | ||
383 | @node Exactness | |
384 | @subsection Exact and Inexact Numbers | |
385 | ||
386 | R5RS requires that a calculation involving inexact numbers always | |
387 | produces an inexact result. To meet this requirement, Guile | |
388 | distinguishes between an exact integer value such as @code{5} and the | |
389 | corresponding inexact real value which, to the limited precision | |
390 | available, has no fractional part, and is printed as @code{5.0}. Guile | |
391 | will only convert the latter value to the former when forced to do so by | |
392 | an invocation of the @code{inexact->exact} procedure. | |
393 | ||
394 | @c docstring begin (texi-doc-string "guile" "exact?") | |
395 | @deffn primitive exact? x | |
396 | Return #t if X is an exact number, #f otherwise. | |
397 | @end deffn | |
398 | ||
399 | @c docstring begin (texi-doc-string "guile" "inexact?") | |
400 | @deffn primitive inexact? x | |
401 | Return #t if X is an inexact number, #f else. | |
402 | @end deffn | |
403 | ||
404 | @c docstring begin (texi-doc-string "guile" "inexact->exact") | |
405 | @deffn primitive inexact->exact z | |
406 | Returns an exact number that is numerically closest to Z. | |
407 | @end deffn | |
408 | ||
409 | @c begin (texi-doc-string "guile" "exact->inexact") | |
410 | @deffn primitive exact->inexact | |
411 | @end deffn | |
412 | ||
413 | ||
414 | @node Number Syntax | |
415 | @subsection Read Syntax for Numerical Data | |
416 | ||
417 | The read syntax for integers is a string of digits, optionally | |
418 | preceded by a minus or plus character, a code indicating the | |
419 | base in which the integer is encoded, and a code indicating whether | |
420 | the number is exact or inexact. The supported base codes are: | |
421 | ||
422 | @itemize @bullet | |
423 | @item | |
424 | @code{#b}, @code{#B} --- the integer is written in binary (base 2) | |
425 | ||
426 | @item | |
427 | @code{#o}, @code{#O} --- the integer is written in octal (base 8) | |
428 | ||
429 | @item | |
430 | @code{#d}, @code{#D} --- the integer is written in decimal (base 10) | |
431 | ||
432 | @item | |
433 | @code{#x}, @code{#X} --- the integer is written in hexadecimal (base 16). | |
434 | @end itemize | |
435 | ||
436 | If the base code is omitted, the integer is assumed to be decimal. The | |
437 | following examples show how these base codes are used. | |
438 | ||
439 | @lisp | |
440 | -13 | |
441 | @result{} | |
442 | -13 | |
443 | ||
444 | #d-13 | |
445 | @result{} | |
446 | -13 | |
447 | ||
448 | #x-13 | |
449 | @result{} | |
450 | -19 | |
451 | ||
452 | #b+1101 | |
453 | @result{} | |
454 | 13 | |
455 | ||
456 | #o377 | |
457 | @result{} | |
458 | 255 | |
459 | @end lisp | |
460 | ||
461 | The codes for indicating exactness (which can, incidentally, be applied | |
462 | to all numerical values) are: | |
463 | ||
464 | @itemize @bullet | |
465 | @item | |
466 | @code{#e}, @code{#E} --- the number is exact | |
467 | ||
468 | @item | |
469 | @code{#i}, @code{#I} --- the number is inexact. | |
470 | @end itemize | |
471 | ||
472 | If the exactness indicator is omitted, the integer is assumed to be exact, | |
473 | since Guile's internal representation for integers is always exact. | |
474 | Real numbers have limited precision similar to the precision of the | |
475 | @code{double} type in C. A consequence of the limited precision is that | |
476 | all real numbers in Guile are also rational, since any number R with a | |
477 | limited number of decimal places, say N, can be made into an integer by | |
478 | multiplying by 10^N. | |
479 | ||
480 | ||
481 | @node Integer Operations | |
482 | @subsection Operations on Integer Values | |
483 | ||
484 | @c docstring begin (texi-doc-string "guile" "odd?") | |
485 | @deffn primitive odd? n | |
486 | Return #t if N is an odd number, #f otherwise. | |
487 | @end deffn | |
488 | ||
489 | @c docstring begin (texi-doc-string "guile" "even?") | |
490 | @deffn primitive even? n | |
491 | Return #t if N is an even number, #f otherwise. | |
492 | @end deffn | |
493 | ||
494 | @c begin (texi-doc-string "guile" "quotient") | |
495 | @deffn primitive quotient | |
496 | @end deffn | |
497 | ||
498 | @c begin (texi-doc-string "guile" "remainder") | |
499 | @deffn primitive remainder | |
500 | @end deffn | |
501 | ||
502 | @c begin (texi-doc-string "guile" "modulo") | |
503 | @deffn primitive modulo | |
504 | @end deffn | |
505 | ||
506 | @c begin (texi-doc-string "guile" "gcd") | |
507 | @deffn primitive gcd | |
508 | @end deffn | |
509 | ||
510 | @c begin (texi-doc-string "guile" "lcm") | |
511 | @deffn primitive lcm | |
512 | @end deffn | |
513 | ||
514 | ||
515 | @node Comparison | |
516 | @subsection Comparison Predicates | |
517 | ||
518 | @c begin (texi-doc-string "guile" "=") | |
519 | @deffn primitive = | |
520 | @end deffn | |
521 | ||
522 | @c begin (texi-doc-string "guile" "<") | |
523 | @deffn primitive < | |
524 | @end deffn | |
525 | ||
526 | @c begin (texi-doc-string "guile" ">") | |
527 | @deffn primitive > | |
528 | @end deffn | |
529 | ||
530 | @c begin (texi-doc-string "guile" "<=") | |
531 | @deffn primitive <= | |
532 | @end deffn | |
533 | ||
534 | @c begin (texi-doc-string "guile" ">=") | |
535 | @deffn primitive >= | |
536 | @end deffn | |
537 | ||
538 | @c begin (texi-doc-string "guile" "zero?") | |
539 | @deffn primitive zero? | |
540 | @end deffn | |
541 | ||
542 | @c begin (texi-doc-string "guile" "positive?") | |
543 | @deffn primitive positive? | |
544 | @end deffn | |
545 | ||
546 | @c begin (texi-doc-string "guile" "negative?") | |
547 | @deffn primitive negative? | |
548 | @end deffn | |
549 | ||
550 | ||
551 | @node Conversion | |
552 | @subsection Converting Numbers To and From Strings | |
553 | ||
554 | @c docstring begin (texi-doc-string "guile" "number->string") | |
555 | @deffn primitive number->string n [radix] | |
556 | Return a string holding the external representation of the | |
557 | number N in the given RADIX. If N is inexact, a radix of 10 | |
558 | will be used. | |
559 | @end deffn | |
560 | ||
561 | @c docstring begin (texi-doc-string "guile" "string->number") | |
562 | @deffn primitive string->number string [radix] | |
563 | Returns a number of the maximally precise representation | |
564 | expressed by the given STRING. RADIX must be an exact integer, | |
565 | either 2, 8, 10, or 16. If supplied, RADIX is a default radix | |
566 | that may be overridden by an explicit radix prefix in STRING | |
567 | (e.g. "#o177"). If RADIX is not supplied, then the default | |
568 | radix is 10. If string is not a syntactically valid notation | |
569 | for a number, then `string->number' returns #f. (r5rs) | |
570 | @end deffn | |
571 | ||
572 | ||
573 | @node Complex | |
574 | @subsection Complex Number Operations | |
575 | ||
576 | @c docstring begin (texi-doc-string "guile" "make-rectangular") | |
577 | @deffn primitive make-rectangular real imaginary | |
578 | Return a complex number constructed of the given REAL and | |
579 | IMAGINARY parts. | |
580 | @end deffn | |
581 | ||
582 | @c docstring begin (texi-doc-string "guile" "make-polar") | |
583 | @deffn primitive make-polar x y | |
584 | Return the complex number X * e^(i * Y). | |
585 | @end deffn | |
586 | ||
587 | @c begin (texi-doc-string "guile" "real-part") | |
588 | @deffn primitive real-part | |
589 | @end deffn | |
590 | ||
591 | @c begin (texi-doc-string "guile" "imag-part") | |
592 | @deffn primitive imag-part | |
593 | @end deffn | |
594 | ||
595 | @c begin (texi-doc-string "guile" "magnitude") | |
596 | @deffn primitive magnitude | |
597 | @end deffn | |
598 | ||
599 | @c begin (texi-doc-string "guile" "angle") | |
600 | @deffn primitive angle | |
601 | @end deffn | |
602 | ||
603 | ||
604 | @node Arithmetic | |
605 | @subsection Arithmetic Functions | |
606 | ||
607 | @c begin (texi-doc-string "guile" "+") | |
608 | @deffn primitive + | |
609 | @end deffn | |
610 | ||
611 | @c begin (texi-doc-string "guile" "-") | |
612 | @deffn primitive - | |
613 | @end deffn | |
614 | ||
615 | @c begin (texi-doc-string "guile" "*") | |
616 | @deffn primitive * | |
617 | @end deffn | |
618 | ||
619 | @c begin (texi-doc-string "guile" "/") | |
620 | @deffn primitive / | |
621 | @end deffn | |
622 | ||
623 | @c begin (texi-doc-string "guile" "abs") | |
624 | @deffn primitive abs | |
625 | @end deffn | |
626 | ||
627 | @c begin (texi-doc-string "guile" "max") | |
628 | @deffn primitive max | |
629 | @end deffn | |
630 | ||
631 | @c begin (texi-doc-string "guile" "min") | |
632 | @deffn primitive min | |
633 | @end deffn | |
634 | ||
635 | @c begin (texi-doc-string "guile" "truncate") | |
636 | @deffn primitive truncate | |
637 | @end deffn | |
638 | ||
639 | @c begin (texi-doc-string "guile" "round") | |
640 | @deffn primitive round | |
641 | @end deffn | |
642 | ||
643 | @c begin (texi-doc-string "guile" "floor") | |
644 | @deffn primitive floor | |
645 | @end deffn | |
646 | ||
647 | @c begin (texi-doc-string "guile" "ceiling") | |
648 | @deffn primitive ceiling | |
649 | @end deffn | |
650 | ||
651 | ||
652 | @node Scientific | |
653 | @subsection Scientific Functions | |
654 | ||
655 | The following procedures accept any kind of number as arguments, | |
656 | including complex numbers. | |
657 | ||
658 | @c begin (texi-doc-string "guile" "sqrt") | |
659 | @deffn procedure sqrt z | |
660 | Return the square root of @var{z}. | |
661 | @end deffn | |
662 | ||
663 | @c begin (texi-doc-string "guile" "expt") | |
664 | @deffn procedure expt z1 z2 | |
665 | Return @var{z1} raised to the power of @var{z2}. | |
666 | @end deffn | |
667 | ||
668 | @c begin (texi-doc-string "guile" "sin") | |
669 | @deffn procedure sin z | |
670 | Return the sine of @var{z}. | |
671 | @end deffn | |
672 | ||
673 | @c begin (texi-doc-string "guile" "cos") | |
674 | @deffn procedure cos z | |
675 | Return the cosine of @var{z}. | |
676 | @end deffn | |
677 | ||
678 | @c begin (texi-doc-string "guile" "tan") | |
679 | @deffn procedure tan z | |
680 | Return the tangent of @var{z}. | |
681 | @end deffn | |
682 | ||
683 | @c begin (texi-doc-string "guile" "asin") | |
684 | @deffn procedure asin z | |
685 | Return the arcsine of @var{z}. | |
686 | @end deffn | |
687 | ||
688 | @c begin (texi-doc-string "guile" "acos") | |
689 | @deffn procedure acos z | |
690 | Return the arccosine of @var{z}. | |
691 | @end deffn | |
692 | ||
693 | @c begin (texi-doc-string "guile" "atan") | |
694 | @deffn procedure atan z | |
695 | Return the arctangent of @var{z}. | |
696 | @end deffn | |
697 | ||
698 | @c begin (texi-doc-string "guile" "exp") | |
699 | @deffn procedure exp z | |
700 | Return e to the power of @var{z}, where e is the base of natural | |
701 | logarithms (2.71828@dots{}). | |
702 | @end deffn | |
703 | ||
704 | @c begin (texi-doc-string "guile" "log") | |
705 | @deffn procedure log z | |
706 | Return the natural logarithm of @var{z}. | |
707 | @end deffn | |
708 | ||
709 | @c begin (texi-doc-string "guile" "log10") | |
710 | @deffn procedure log10 z | |
711 | Return the base 10 logarithm of @var{z}. | |
712 | @end deffn | |
713 | ||
714 | @c begin (texi-doc-string "guile" "sinh") | |
715 | @deffn procedure sinh z | |
716 | Return the hyperbolic sine of @var{z}. | |
717 | @end deffn | |
718 | ||
719 | @c begin (texi-doc-string "guile" "cosh") | |
720 | @deffn procedure cosh z | |
721 | Return the hyperbolic cosine of @var{z}. | |
722 | @end deffn | |
723 | ||
724 | @c begin (texi-doc-string "guile" "tanh") | |
725 | @deffn procedure tanh z | |
726 | Return the hyperbolic tangent of @var{z}. | |
727 | @end deffn | |
728 | ||
729 | @c begin (texi-doc-string "guile" "asinh") | |
730 | @deffn procedure asinh z | |
731 | Return the hyperbolic arcsine of @var{z}. | |
732 | @end deffn | |
733 | ||
734 | @c begin (texi-doc-string "guile" "acosh") | |
735 | @deffn procedure acosh z | |
736 | Return the hyperbolic arccosine of @var{z}. | |
737 | @end deffn | |
738 | ||
739 | @c begin (texi-doc-string "guile" "atanh") | |
740 | @deffn procedure atanh z | |
741 | Return the hyperbolic arctangent of @var{z}. | |
742 | @end deffn | |
743 | ||
744 | ||
745 | @node Primitive Numerics | |
746 | @subsection Primitive Numeric Functions | |
747 | ||
748 | Many of Guile's numeric procedures which accept any kind of numbers as | |
749 | arguments, including complex numbers, are implemented as Scheme | |
750 | procedures that use the following real number-based primitives. These | |
751 | primitives signal an error if they are called with complex arguments. | |
752 | ||
753 | @c begin (texi-doc-string "guile" "$abs") | |
754 | @deffn primitive $abs x | |
755 | Return the absolute value of @var{x}. | |
756 | @end deffn | |
757 | ||
758 | @c begin (texi-doc-string "guile" "$sqrt") | |
759 | @deffn primitive $sqrt x | |
760 | Return the square root of @var{x}. | |
761 | @end deffn | |
762 | ||
763 | @c docstring begin (texi-doc-string "guile" "$expt") | |
764 | @deffn primitive $expt x y | |
765 | Return @var{x} raised to the power of @var{y}. This | |
766 | procedure does not accept complex arguments. | |
767 | @end deffn | |
768 | ||
769 | @c begin (texi-doc-string "guile" "$sin") | |
770 | @deffn primitive $sin x | |
771 | Return the sine of @var{x}. | |
772 | @end deffn | |
773 | ||
774 | @c begin (texi-doc-string "guile" "$cos") | |
775 | @deffn primitive $cos x | |
776 | Return the cosine of @var{x}. | |
777 | @end deffn | |
778 | ||
779 | @c begin (texi-doc-string "guile" "$tan") | |
780 | @deffn primitive $tan x | |
781 | Return the tangent of @var{x}. | |
782 | @end deffn | |
783 | ||
784 | @c begin (texi-doc-string "guile" "$asin") | |
785 | @deffn primitive $asin x | |
786 | Return the arcsine of @var{x}. | |
787 | @end deffn | |
788 | ||
789 | @c begin (texi-doc-string "guile" "$acos") | |
790 | @deffn primitive $acos x | |
791 | Return the arccosine of @var{x}. | |
792 | @end deffn | |
793 | ||
794 | @c begin (texi-doc-string "guile" "$atan") | |
795 | @deffn primitive $atan x | |
796 | Return the arctangent of @var{x} in the range -PI/2 to PI/2. | |
797 | @end deffn | |
798 | ||
799 | @c docstring begin (texi-doc-string "guile" "$atan2") | |
800 | @deffn primitive $atan2 x y | |
801 | Return the arc tangent of the two arguments @var{x} and | |
802 | @var{y}. This is similar to calculating the arc tangent of | |
803 | @var{x} / @var{y}, except that the signs of both arguments | |
804 | are used to determine the quadrant of the result. This | |
805 | procedure does not accept complex arguments. | |
806 | @end deffn | |
807 | ||
808 | @c begin (texi-doc-string "guile" "$exp") | |
809 | @deffn primitive $exp x | |
810 | Return e to the power of @var{x}, where e is the base of natural | |
811 | logarithms (2.71828@dots{}). | |
812 | @end deffn | |
813 | ||
814 | @c begin (texi-doc-string "guile" "$log") | |
815 | @deffn primitive $log x | |
816 | Return the natural logarithm of @var{x}. | |
817 | @end deffn | |
818 | ||
819 | @c begin (texi-doc-string "guile" "$sinh") | |
820 | @deffn primitive $sinh x | |
821 | Return the hyperbolic sine of @var{x}. | |
822 | @end deffn | |
823 | ||
824 | @c begin (texi-doc-string "guile" "$cosh") | |
825 | @deffn primitive $cosh x | |
826 | Return the hyperbolic cosine of @var{x}. | |
827 | @end deffn | |
828 | ||
829 | @c begin (texi-doc-string "guile" "$tanh") | |
830 | @deffn primitive $tanh x | |
831 | Return the hyperbolic tangent of @var{x}. | |
832 | @end deffn | |
833 | ||
834 | @c begin (texi-doc-string "guile" "$asinh") | |
835 | @deffn primitive $asinh x | |
836 | Return the hyperbolic arcsine of @var{x}. | |
837 | @end deffn | |
838 | ||
839 | @c begin (texi-doc-string "guile" "$acosh") | |
840 | @deffn primitive $acosh x | |
841 | Return the hyperbolic arccosine of @var{x}. | |
842 | @end deffn | |
843 | ||
844 | @c begin (texi-doc-string "guile" "$atanh") | |
845 | @deffn primitive $atanh x | |
846 | Return the hyperbolic arctangent of @var{x}. | |
847 | @end deffn | |
848 | ||
849 | ||
850 | @node Bitwise Operations | |
851 | @subsection Bitwise Operations | |
852 | ||
853 | @c docstring begin (texi-doc-string "guile" "logand") | |
854 | @deffn primitive logand n1 n2 | |
855 | Returns the integer which is the bit-wise AND of the two integer | |
856 | arguments. | |
857 | ||
858 | Example: | |
859 | @lisp | |
860 | (number->string (logand #b1100 #b1010) 2) | |
861 | @result{} "1000" | |
862 | @end lisp | |
863 | @end deffn | |
864 | ||
865 | @c docstring begin (texi-doc-string "guile" "logior") | |
866 | @deffn primitive logior n1 n2 | |
867 | Returns the integer which is the bit-wise OR of the two integer | |
868 | arguments. | |
869 | ||
870 | Example: | |
871 | @lisp | |
872 | (number->string (logior #b1100 #b1010) 2) | |
873 | @result{} "1110" | |
874 | @end lisp | |
875 | @end deffn | |
876 | ||
877 | @c docstring begin (texi-doc-string "guile" "logxor") | |
878 | @deffn primitive logxor n1 n2 | |
879 | Returns the integer which is the bit-wise XOR of the two integer | |
880 | arguments. | |
881 | ||
882 | Example: | |
883 | @lisp | |
884 | (number->string (logxor #b1100 #b1010) 2) | |
885 | @result{} "110" | |
886 | @end lisp | |
887 | @end deffn | |
888 | ||
889 | @c docstring begin (texi-doc-string "guile" "lognot") | |
890 | @deffn primitive lognot n | |
891 | Returns the integer which is the 2s-complement of the integer argument. | |
892 | ||
893 | Example: | |
894 | @lisp | |
895 | (number->string (lognot #b10000000) 2) | |
896 | @result{} "-10000001" | |
897 | (number->string (lognot #b0) 2) | |
898 | @result{} "-1" | |
899 | @end lisp | |
900 | @end deffn | |
901 | ||
902 | @c ARGFIXME j/n1 k/n2 | |
903 | @c docstring begin (texi-doc-string "guile" "logtest") | |
904 | @deffn primitive logtest n1 n2 | |
905 | @example | |
906 | (logtest j k) @equiv{} (not (zero? (logand j k))) | |
907 | ||
908 | (logtest #b0100 #b1011) @result{} #f | |
909 | (logtest #b0100 #b0111) @result{} #t | |
910 | @end example | |
911 | @end deffn | |
912 | ||
913 | @c docstring begin (texi-doc-string "guile" "logbit?") | |
914 | @deffn primitive logbit? index j | |
915 | @example | |
916 | (logbit? index j) @equiv{} (logtest (integer-expt 2 index) j) | |
917 | ||
918 | (logbit? 0 #b1101) @result{} #t | |
919 | (logbit? 1 #b1101) @result{} #f | |
920 | (logbit? 2 #b1101) @result{} #t | |
921 | (logbit? 3 #b1101) @result{} #t | |
922 | (logbit? 4 #b1101) @result{} #f | |
923 | @end example | |
924 | @end deffn | |
925 | ||
926 | @c ARGFIXME n/int cnt/count | |
927 | @c docstring begin (texi-doc-string "guile" "ash") | |
928 | @deffn primitive ash n cnt | |
929 | The function ash performs an arithmetic shift left by CNT bits | |
930 | (or shift right, if CNT is negative). 'Arithmetic' means, that | |
931 | the function does not guarantee to keep the bit structure of N, | |
932 | but rather guarantees that the result will always be rounded | |
933 | towards minus infinity. Therefore, the results of ash and a | |
934 | corresponding bitwise shift will differ if N is negative. | |
935 | ||
936 | Formally, the function returns an integer equivalent to | |
937 | @code{(inexact->exact (floor (* N (expt 2 CNT))))}.@refill | |
938 | ||
939 | Example: | |
940 | @lisp | |
941 | (number->string (ash #b1 3) 2) | |
942 | @result{} "1000" | |
943 | (number->string (ash #b1010 -1) 2) | |
944 | @result{} "101" | |
945 | @end lisp | |
946 | @end deffn | |
947 | ||
948 | @c docstring begin (texi-doc-string "guile" "logcount") | |
949 | @deffn primitive logcount n | |
950 | Returns the number of bits in integer @var{n}. If integer is positive, | |
951 | the 1-bits in its binary representation are counted. If negative, the | |
952 | 0-bits in its two's-complement binary representation are counted. If 0, | |
953 | 0 is returned. | |
954 | ||
955 | Example: | |
956 | @lisp | |
957 | (logcount #b10101010) | |
958 | @result{} 4 | |
959 | (logcount 0) | |
960 | @result{} 0 | |
961 | (logcount -2) | |
962 | @result{} 1 | |
963 | @end lisp | |
964 | @end deffn | |
965 | ||
966 | @c docstring begin (texi-doc-string "guile" "integer-length") | |
967 | @deffn primitive integer-length n | |
968 | Returns the number of bits neccessary to represent @var{n}. | |
969 | ||
970 | Example: | |
971 | @lisp | |
972 | (integer-length #b10101010) | |
973 | @result{} 8 | |
974 | (integer-length 0) | |
975 | @result{} 0 | |
976 | (integer-length #b1111) | |
977 | @result{} 4 | |
978 | @end lisp | |
979 | @end deffn | |
980 | ||
981 | @c docstring begin (texi-doc-string "guile" "integer-expt") | |
982 | @deffn primitive integer-expt n k | |
983 | Returns @var{n} raised to the non-negative integer exponent @var{k}. | |
984 | ||
985 | Example: | |
986 | @lisp | |
987 | (integer-expt 2 5) | |
988 | @result{} 32 | |
989 | (integer-expt -3 3) | |
990 | @result{} -27 | |
991 | @end lisp | |
992 | @end deffn | |
993 | ||
994 | @c docstring begin (texi-doc-string "guile" "bit-extract") | |
995 | @deffn primitive bit-extract n start end | |
996 | Returns the integer composed of the @var{start} (inclusive) through | |
997 | @var{end} (exclusive) bits of @var{n}. The @var{start}th bit becomes | |
998 | the 0-th bit in the result.@refill | |
999 | ||
1000 | Example: | |
1001 | @lisp | |
1002 | (number->string (bit-extract #b1101101010 0 4) 2) | |
1003 | @result{} "1010" | |
1004 | (number->string (bit-extract #b1101101010 4 9) 2) | |
1005 | @result{} "10110" | |
1006 | @end lisp | |
1007 | @end deffn | |
1008 | ||
1009 | ||
1010 | @node Random | |
1011 | @subsection Random Number Generation | |
1012 | ||
1013 | @c docstring begin (texi-doc-string "guile" "copy-random-state") | |
1014 | @deffn primitive copy-random-state [state] | |
1015 | Return a copy of the random state @var{state}. | |
1016 | @end deffn | |
1017 | ||
1018 | @c docstring begin (texi-doc-string "guile" "random") | |
1019 | @deffn primitive random n [state] | |
1020 | Return a number in [0,N). | |
1021 | Accepts a positive integer or real n and returns a | |
1022 | number of the same type between zero (inclusive) and | |
1023 | N (exclusive). The values returned have a uniform | |
1024 | distribution. | |
1025 | The optional argument @var{state} must be of the type produced | |
1026 | by @code{seed->random-state}. It defaults to the value of the | |
1027 | variable @var{*random-state*}. This object is used to maintain | |
1028 | the state of the pseudo-random-number generator and is altered | |
1029 | as a side effect of the random operation. | |
1030 | @end deffn | |
1031 | ||
1032 | @c docstring begin (texi-doc-string "guile" "random:exp") | |
1033 | @deffn primitive random:exp [state] | |
1034 | Returns an inexact real in an exponential distribution with mean 1. | |
1035 | For an exponential distribution with mean u use (* u (random:exp)). | |
1036 | @end deffn | |
1037 | ||
1038 | @c docstring begin (texi-doc-string "guile" "random:hollow-sphere!") | |
1039 | @deffn primitive random:hollow-sphere! v [state] | |
1040 | Fills vect with inexact real random numbers | |
1041 | the sum of whose squares is equal to 1.0. | |
1042 | Thinking of vect as coordinates in space of | |
1043 | dimension n = (vector-length vect), the coordinates | |
1044 | are uniformly distributed over the surface of the | |
1045 | unit n-shere. | |
1046 | @end deffn | |
1047 | ||
1048 | @c docstring begin (texi-doc-string "guile" "random:normal") | |
1049 | @deffn primitive random:normal [state] | |
1050 | Returns an inexact real in a normal distribution. | |
1051 | The distribution used has mean 0 and standard deviation 1. | |
1052 | For a normal distribution with mean m and standard deviation | |
1053 | d use @code{(+ m (* d (random:normal)))}. | |
1054 | @end deffn | |
1055 | ||
1056 | @c docstring begin (texi-doc-string "guile" "random:normal-vector!") | |
1057 | @deffn primitive random:normal-vector! v [state] | |
1058 | Fills vect with inexact real random numbers that are | |
1059 | independent and standard normally distributed | |
1060 | (i.e., with mean 0 and variance 1). | |
1061 | @end deffn | |
1062 | ||
1063 | @c docstring begin (texi-doc-string "guile" "random:solid-sphere!") | |
1064 | @deffn primitive random:solid-sphere! v [state] | |
1065 | Fills vect with inexact real random numbers | |
1066 | the sum of whose squares is less than 1.0. | |
1067 | Thinking of vect as coordinates in space of | |
1068 | dimension n = (vector-length vect), the coordinates | |
1069 | are uniformly distributed within the unit n-shere. | |
1070 | The sum of the squares of the numbers is returned. | |
1071 | @end deffn | |
1072 | ||
1073 | @c docstring begin (texi-doc-string "guile" "random:uniform") | |
1074 | @deffn primitive random:uniform [state] | |
1075 | Returns a uniformly distributed inexact real random number in [0,1). | |
1076 | @end deffn | |
1077 | ||
1078 | @c docstring begin (texi-doc-string "guile" "seed->random-state") | |
1079 | @deffn primitive seed->random-state seed | |
1080 | Return a new random state using @var{seed}. | |
1081 | @end deffn | |
1082 | ||
1083 | ||
1084 | @node Characters | |
1085 | @section Characters | |
1086 | ||
1087 | Most of the characters in the ASCII character set may be referred to by | |
1088 | name: for example, @code{#\tab}, @code{#\esc}, @code{#\stx}, and so on. | |
1089 | The following table describes the ASCII names for each character. | |
1090 | ||
1091 | @multitable @columnfractions .25 .25 .25 .25 | |
1092 | @item 0 = @code{#\nul} | |
1093 | @tab 1 = @code{#\soh} | |
1094 | @tab 2 = @code{#\stx} | |
1095 | @tab 3 = @code{#\etx} | |
1096 | @item 4 = @code{#\eot} | |
1097 | @tab 5 = @code{#\enq} | |
1098 | @tab 6 = @code{#\ack} | |
1099 | @tab 7 = @code{#\bel} | |
1100 | @item 8 = @code{#\bs} | |
1101 | @tab 9 = @code{#\ht} | |
1102 | @tab 10 = @code{#\nl} | |
1103 | @tab 11 = @code{#\vt} | |
1104 | @item 12 = @code{#\np} | |
1105 | @tab 13 = @code{#\cr} | |
1106 | @tab 14 = @code{#\so} | |
1107 | @tab 15 = @code{#\si} | |
1108 | @item 16 = @code{#\dle} | |
1109 | @tab 17 = @code{#\dc1} | |
1110 | @tab 18 = @code{#\dc2} | |
1111 | @tab 19 = @code{#\dc3} | |
1112 | @item 20 = @code{#\dc4} | |
1113 | @tab 21 = @code{#\nak} | |
1114 | @tab 22 = @code{#\syn} | |
1115 | @tab 23 = @code{#\etb} | |
1116 | @item 24 = @code{#\can} | |
1117 | @tab 25 = @code{#\em} | |
1118 | @tab 26 = @code{#\sub} | |
1119 | @tab 27 = @code{#\esc} | |
1120 | @item 28 = @code{#\fs} | |
1121 | @tab 29 = @code{#\gs} | |
1122 | @tab 30 = @code{#\rs} | |
1123 | @tab 31 = @code{#\us} | |
1124 | @item 32 = @code{#\sp} | |
1125 | @end multitable | |
1126 | ||
1127 | The @code{delete} character (octal 177) may be referred to with the name | |
1128 | @code{#\del}. | |
1129 | ||
1130 | Several characters have more than one name: | |
1131 | ||
1132 | @itemize @bullet | |
1133 | @item | |
1134 | #\space, #\sp | |
1135 | @item | |
1136 | #\newline, #\nl | |
1137 | @item | |
1138 | #\tab, #\ht | |
1139 | @item | |
1140 | #\backspace, #\bs | |
1141 | @item | |
1142 | #\return, #\cr | |
1143 | @item | |
1144 | #\page, #\np | |
1145 | @item | |
1146 | #\null, #\nul | |
1147 | @end itemize | |
1148 | ||
1149 | @c docstring begin (texi-doc-string "guile" "char?") | |
1150 | @deffn primitive char? x | |
1151 | Return @code{#t} iff @var{x} is a character, else @code{#f}. | |
1152 | @end deffn | |
1153 | ||
1154 | @c docstring begin (texi-doc-string "guile" "char=?") | |
1155 | @deffn primitive char=? x y | |
1156 | Return @code{#t} iff @var{x} is the same character as @var{y}, else @code{#f}. | |
1157 | @end deffn | |
1158 | ||
1159 | @c docstring begin (texi-doc-string "guile" "char<?") | |
1160 | @deffn primitive char<? x y | |
1161 | Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence, | |
1162 | else @code{#f}. | |
1163 | @end deffn | |
1164 | ||
1165 | @c docstring begin (texi-doc-string "guile" "char<=?") | |
1166 | @deffn primitive char<=? x y | |
1167 | Return @code{#t} iff @var{x} is less than or equal to @var{y} in the | |
1168 | ASCII sequence, else @code{#f}. | |
1169 | @end deffn | |
1170 | ||
1171 | @c docstring begin (texi-doc-string "guile" "char>?") | |
1172 | @deffn primitive char>? x y | |
1173 | Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII | |
1174 | sequence, else @code{#f}. | |
1175 | @end deffn | |
1176 | ||
1177 | @c docstring begin (texi-doc-string "guile" "char>=?") | |
1178 | @deffn primitive char>=? x y | |
1179 | Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the | |
1180 | ASCII sequence, else @code{#f}. | |
1181 | @end deffn | |
1182 | ||
1183 | @c docstring begin (texi-doc-string "guile" "char-ci=?") | |
1184 | @deffn primitive char-ci=? x y | |
1185 | Return @code{#t} iff @var{x} is the same character as @var{y} ignoring | |
1186 | case, else @code{#f}. | |
1187 | @end deffn | |
1188 | ||
1189 | @c docstring begin (texi-doc-string "guile" "char-ci<?") | |
1190 | @deffn primitive char-ci<? x y | |
1191 | Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence | |
1192 | ignoring case, else @code{#f}. | |
1193 | @end deffn | |
1194 | ||
1195 | @c docstring begin (texi-doc-string "guile" "char-ci<=?") | |
1196 | @deffn primitive char-ci<=? x y | |
1197 | Return @code{#t} iff @var{x} is less than or equal to @var{y} in the | |
1198 | ASCII sequence ignoring case, else @code{#f}. | |
1199 | @end deffn | |
1200 | ||
1201 | @c docstring begin (texi-doc-string "guile" "char-ci>?") | |
1202 | @deffn primitive char-ci>? x y | |
1203 | Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII | |
1204 | sequence ignoring case, else @code{#f}. | |
1205 | @end deffn | |
1206 | ||
1207 | @c docstring begin (texi-doc-string "guile" "char-ci>=?") | |
1208 | @deffn primitive char-ci>=? x y | |
1209 | Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the | |
1210 | ASCII sequence ignoring case, else @code{#f}. | |
1211 | @end deffn | |
1212 | ||
1213 | @c docstring begin (texi-doc-string "guile" "char-alphabetic?") | |
1214 | @deffn primitive char-alphabetic? chr | |
1215 | Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}. | |
1216 | Alphabetic means the same thing as the isalpha C library function. | |
1217 | @end deffn | |
1218 | ||
1219 | @c docstring begin (texi-doc-string "guile" "char-numeric?") | |
1220 | @deffn primitive char-numeric? chr | |
1221 | Return @code{#t} iff @var{chr} is numeric, else @code{#f}. | |
1222 | Numeric means the same thing as the isdigit C library function. | |
1223 | @end deffn | |
1224 | ||
1225 | @c docstring begin (texi-doc-string "guile" "char-whitespace?") | |
1226 | @deffn primitive char-whitespace? chr | |
1227 | Return @code{#t} iff @var{chr} is whitespace, else @code{#f}. | |
1228 | Whitespace means the same thing as the isspace C library function. | |
1229 | @end deffn | |
1230 | ||
1231 | @c docstring begin (texi-doc-string "guile" "char-upper-case?") | |
1232 | @deffn primitive char-upper-case? chr | |
1233 | Return @code{#t} iff @var{chr} is uppercase, else @code{#f}. | |
1234 | Uppercase means the same thing as the isupper C library function. | |
1235 | @end deffn | |
1236 | ||
1237 | @c docstring begin (texi-doc-string "guile" "char-lower-case?") | |
1238 | @deffn primitive char-lower-case? chr | |
1239 | Return @code{#t} iff @var{chr} is lowercase, else @code{#f}. | |
1240 | Lowercase means the same thing as the islower C library function. | |
1241 | @end deffn | |
1242 | ||
1243 | @c docstring begin (texi-doc-string "guile" "char-is-both?") | |
1244 | @deffn primitive char-is-both? chr | |
1245 | Return @code{#t} iff @var{chr} is either uppercase or lowercase, else @code{#f}. | |
1246 | Uppercase and lowercase are as defined by the isupper and islower | |
1247 | C library functions. | |
1248 | @end deffn | |
1249 | ||
1250 | @c docstring begin (texi-doc-string "guile" "char->integer") | |
1251 | @deffn primitive char->integer chr | |
1252 | Return the number corresponding to ordinal position of @var{chr} in the | |
1253 | ASCII sequence. | |
1254 | @end deffn | |
1255 | ||
1256 | @c docstring begin (texi-doc-string "guile" "integer->char") | |
1257 | @deffn primitive integer->char n | |
1258 | Return the character at position @var{n} in the ASCII sequence. | |
1259 | @end deffn | |
1260 | ||
1261 | @c docstring begin (texi-doc-string "guile" "char-upcase") | |
1262 | @deffn primitive char-upcase chr | |
1263 | Return the uppercase character version of @var{chr}. | |
1264 | @end deffn | |
1265 | ||
1266 | @c docstring begin (texi-doc-string "guile" "char-downcase") | |
1267 | @deffn primitive char-downcase chr | |
1268 | Return the lowercase character version of @var{chr}. | |
1269 | @end deffn | |
1270 | ||
1271 | ||
1272 | @node Strings | |
1273 | @section Strings | |
1274 | ||
1275 | [FIXME: this is pasted in from Tom Lord's original guile.texi and should | |
1276 | be reviewed] | |
1277 | ||
1278 | For the sake of efficiency, two special kinds of strings are available | |
1279 | in Guile: shared substrings and the misleadingly named ``read-only'' | |
1280 | strings. It is not necessary to know about these to program in Guile, | |
1281 | but you are likely to run into one or both of these special string types | |
1282 | eventually, and it will be helpful to know how they work. | |
1283 | ||
1284 | @menu | |
1285 | * String Fun:: New functions for manipulating strings. | |
1286 | * Shared Substrings:: Strings which share memory with each other. | |
1287 | * Read Only Strings:: Treating certain non-strings as strings. | |
1288 | @end menu | |
1289 | ||
1290 | @node String Fun | |
1291 | @subsection String Fun | |
1292 | ||
1293 | @c docstring begin (texi-doc-string "guile" "string") | |
1294 | @c docstring begin (texi-doc-string "guile" "list->string") | |
1295 | @deffn primitive string . chrs | |
1296 | @deffnx primitive list->string chrs | |
1297 | Returns a newly allocated string composed of the arguments, | |
1298 | @var{chrs}. | |
1299 | @end deffn | |
1300 | ||
1301 | @c docstring begin (texi-doc-string "guile" "make-string") | |
1302 | @deffn primitive make-string k [chr] | |
1303 | Return a newly allocated string of | |
1304 | length @var{k}. If @var{chr} is given, then all elements of | |
1305 | the string are initialized to @var{chr}, otherwise the contents | |
1306 | of the @var{string} are unspecified. | |
1307 | @end deffn | |
1308 | ||
1309 | @c docstring begin (texi-doc-string "guile" "string-append") | |
1310 | @deffn primitive string-append . args | |
1311 | Return a newly allocated string whose characters form the | |
1312 | concatenation of the given strings, @var{args}. | |
1313 | @end deffn | |
1314 | ||
1315 | @c docstring begin (texi-doc-string "guile" "string-length") | |
1316 | @deffn primitive string-length string | |
1317 | Return the number of characters in @var{string}. | |
1318 | @end deffn | |
1319 | ||
1320 | @c docstring begin (texi-doc-string "guile" "string-ref") | |
1321 | @deffn primitive string-ref str k | |
1322 | Return character @var{k} of @var{str} using zero-origin | |
1323 | indexing. @var{k} must be a valid index of @var{str}. | |
1324 | @end deffn | |
1325 | ||
1326 | @c docstring begin (texi-doc-string "guile" "string-set!") | |
1327 | @deffn primitive string-set! str k chr | |
1328 | Store @var{chr} in element @var{k} of @var{str} and return | |
1329 | an unspecified value. @var{k} must be a valid index of | |
1330 | @var{str}. | |
1331 | @end deffn | |
1332 | ||
1333 | @c docstring begin (texi-doc-string "guile" "string?") | |
1334 | @deffn primitive string? obj | |
1335 | Returns @code{#t} iff @var{obj} is a string, else returns | |
1336 | @code{#f}. | |
1337 | @end deffn | |
1338 | ||
1339 | @c docstring begin (texi-doc-string "guile" "substring") | |
1340 | @deffn primitive substring str start [end] | |
1341 | Return a newly allocated string formed from the characters | |
1342 | of @var{str} beginning with index @var{start} (inclusive) and | |
1343 | ending with index @var{end} (exclusive). | |
1344 | @var{str} must be a string, @var{start} and @var{end} must be | |
1345 | exact integers satisfying: | |
1346 | ||
1347 | 0 <= @var{start} <= @var{end} <= (string-length @var{str}). | |
1348 | @end deffn | |
1349 | ||
1350 | @c docstring begin (texi-doc-string "guile" "string-index") | |
1351 | @deffn primitive string-index str chr [frm [to]] | |
1352 | Return the index of the first occurrence of @var{chr} in @var{str}. The | |
1353 | optional integer arguments @var{frm} and @var{to} limit the search to | |
1354 | a portion of the string. This procedure essentially implements the | |
1355 | @code{index} or @code{strchr} functions from the C library. | |
1356 | ||
1357 | (qdocs:) Returns the index of @var{char} in @var{str}, or @code{#f} if the | |
1358 | @var{char} isn't in @var{str}. If @var{frm} is given and not @code{#f}, | |
1359 | it is used as the starting index; if @var{to} is given and not @var{#f}, | |
1360 | it is used as the ending index (exclusive). | |
1361 | ||
1362 | @example | |
1363 | (string-index "weiner" #\e) | |
1364 | @result{} 1 | |
1365 | ||
1366 | (string-index "weiner" #\e 2) | |
1367 | @result{} 4 | |
1368 | ||
1369 | (string-index "weiner" #\e 2 4) | |
1370 | @result{} #f | |
1371 | @end example | |
1372 | @end deffn | |
1373 | ||
1374 | @c docstring begin (texi-doc-string "guile" "string-rindex") | |
1375 | @deffn primitive string-rindex str chr [frm [to]] | |
1376 | Like @code{string-index}, but search from the right of the string rather | |
1377 | than from the left. This procedure essentially implements the | |
1378 | @code{rindex} or @code{strrchr} functions from the C library. | |
1379 | ||
1380 | (qdocs:) The same as @code{string-index}, except it gives the rightmost occurance | |
1381 | of @var{char} in the range [@var{frm}, @var{to}-1], which defaults to | |
1382 | the entire string. | |
1383 | ||
1384 | @example | |
1385 | (string-rindex "weiner" #\e) | |
1386 | @result{} 4 | |
1387 | ||
1388 | (string-rindex "weiner" #\e 2 4) | |
1389 | @result{} #f | |
1390 | ||
1391 | (string-rindex "weiner" #\e 2 5) | |
1392 | @result{} 4 | |
1393 | @end example | |
1394 | @end deffn | |
1395 | ||
1396 | @c docstring begin (texi-doc-string "guile" "substring-move!") | |
1397 | @c docstring begin (texi-doc-string "guile" "substring-move-left!") | |
1398 | @c docstring begin (texi-doc-string "guile" "substring-move-right!") | |
1399 | @deffn primitive substring-move! str1 start1 end1 str2 start2 | |
1400 | @deffnx primitive substring-move-left! str1 start1 end1 str2 start2 | |
1401 | @deffnx primitive substring-move-right! str1 start1 end1 str2 start2 | |
1402 | Copy the substring of @var{str1} bounded by @var{start1} and @var{end1} | |
1403 | into @var{str2} beginning at position @var{end2}. | |
1404 | @code{substring-move-right!} begins copying from the rightmost character | |
1405 | and moves left, and @code{substring-move-left!} copies from the leftmost | |
1406 | character moving right. | |
1407 | ||
1408 | It is useful to have two functions that copy in different directions so | |
1409 | that substrings can be copied back and forth within a single string. If | |
1410 | you wish to copy text from the left-hand side of a string to the | |
1411 | right-hand side of the same string, and the source and destination | |
1412 | overlap, you must be careful to copy the rightmost characters of the | |
1413 | text first, to avoid clobbering your data. Hence, when @var{str1} and | |
1414 | @var{str2} are the same string, you should use | |
1415 | @code{substring-move-right!} when moving text from left to right, and | |
1416 | @code{substring-move-left!} otherwise. If @code{str1} and @samp{str2} | |
1417 | are different strings, it does not matter which function you use. | |
1418 | @end deffn | |
1419 | ||
1420 | @deffn primitive substring-move-left! str1 start1 end1 str2 start2 | |
1421 | @end deffn | |
1422 | @deftypefn {C Function} SCM scm_substring_move_left_x (SCM @var{str1}, SCM @var{start1}, SCM @var{end1}, SCM @var{str2}, SCM @var{start2}) | |
1423 | [@strong{Note:} this is only valid if you've applied the strop patch]. | |
1424 | ||
1425 | Moves a substring of @var{str1}, from @var{start1} to @var{end1} | |
1426 | (@var{end1} is exclusive), into @var{str2}, starting at | |
1427 | @var{start2}. Allows overlapping strings. | |
1428 | ||
1429 | @example | |
1430 | (define x (make-string 10 #\a)) | |
1431 | (define y "bcd") | |
1432 | (substring-move-left! x 2 5 y 0) | |
1433 | y | |
1434 | @result{} "aaa" | |
1435 | ||
1436 | x | |
1437 | @result{} "aaaaaaaaaa" | |
1438 | ||
1439 | (define y "bcdefg") | |
1440 | (substring-move-left! x 2 5 y 0) | |
1441 | y | |
1442 | @result{} "aaaefg" | |
1443 | ||
1444 | (define y "abcdefg") | |
1445 | (substring-move-left! y 2 5 y 3) | |
1446 | y | |
1447 | @result{} "abccccg" | |
1448 | @end example | |
1449 | @end deftypefn | |
1450 | ||
1451 | @deffn substring-move-right! str1 start1 end1 str2 start2 | |
1452 | @end deffn | |
1453 | @deftypefn {C Function} SCM scm_substring_move_right_x (SCM @var{str1}, SCM @var{start1}, SCM @var{end1}, SCM @var{str2}, SCM @var{start2}) | |
1454 | [@strong{Note:} this is only valid if you've applied the strop patch, if | |
1455 | it hasn't made it into the guile tree]. | |
1456 | ||
1457 | Does much the same thing as @code{substring-move-left!}, except it | |
1458 | starts moving at the end of the sequence, rather than the beginning. | |
1459 | @example | |
1460 | (define y "abcdefg") | |
1461 | (substring-move-right! y 2 5 y 0) | |
1462 | y | |
1463 | @result{} "ededefg" | |
1464 | ||
1465 | (define y "abcdefg") | |
1466 | (substring-move-right! y 2 5 y 3) | |
1467 | y | |
1468 | @result{} "abccdeg" | |
1469 | @end example | |
1470 | @end deftypefn | |
1471 | ||
1472 | @c docstring begin (texi-doc-string "guile" "vector-move-left!") | |
1473 | @deffn primitive vector-move-left! vec1 start1 end1 vec2 start2 | |
1474 | Vector version of @code{substring-move-left!}. | |
1475 | @end deffn | |
1476 | ||
1477 | @c docstring begin (texi-doc-string "guile" "vector-move-right!") | |
1478 | @deffn primitive vector-move-right! vec1 start1 end1 vec2 start2 | |
1479 | Vector version of @code{substring-move-right!}. | |
1480 | @end deffn | |
1481 | ||
1482 | @c ARGFIXME fill/fill-char | |
1483 | @c docstring begin (texi-doc-string "guile" "substring-fill!") | |
1484 | @deffn primitive substring-fill! str start end fill | |
1485 | Change every character in @var{str} between @var{start} and @var{end} to | |
1486 | @var{fill-char}. | |
1487 | ||
1488 | (qdocs:) Destructively fills @var{str}, from @var{start} to @var{end}, with @var{fill}. | |
1489 | ||
1490 | @example | |
1491 | (define y "abcdefg") | |
1492 | (substring-fill! y 1 3 #\r) | |
1493 | y | |
1494 | @result{} "arrdefg" | |
1495 | @end example | |
1496 | @end deffn | |
1497 | ||
1498 | @c docstring begin (texi-doc-string "guile" "string-null?") | |
1499 | @deffn primitive string-null? str | |
1500 | Return @code{#t} if @var{str}'s length is nonzero, and @code{#f} | |
1501 | otherwise. | |
1502 | ||
1503 | (qdocs:) Returns @code{#t} if @var{str} is empty, else returns @code{#f}. | |
1504 | ||
1505 | @example | |
1506 | (string-null? "") | |
1507 | @result{} #t | |
1508 | ||
1509 | (string-null? y) | |
1510 | @result{} #f | |
1511 | @end example | |
1512 | @end deffn | |
1513 | ||
1514 | @c ARGFIXME v/str | |
1515 | @c docstring begin (texi-doc-string "guile" "string-upcase!") | |
1516 | @deffn primitive string-upcase! str | |
1517 | Destructively upcase every character in @code{str}. | |
1518 | ||
1519 | (qdocs:) Converts each element in @var{str} to upper case. | |
1520 | ||
1521 | @example | |
1522 | (string-upcase! y) | |
1523 | @result{} "ARRDEFG" | |
1524 | ||
1525 | y | |
1526 | @result{} "ARRDEFG" | |
1527 | @end example | |
1528 | @end deffn | |
1529 | ||
1530 | @c docstring begin (texi-doc-string "guile" "string-upcase") | |
1531 | @deffn primitive string-upcase str | |
1532 | Upcase every character in @code{str}. | |
1533 | @end deffn | |
1534 | ||
1535 | @c ARGFIXME v/str | |
1536 | @c docstring begin (texi-doc-string "guile" "string-downcase!") | |
1537 | @deffn primitive string-downcase! str | |
1538 | Destructively downcase every character in @code{str}. | |
1539 | ||
1540 | (qdocs:) Converts each element in @var{str} to lower case. | |
1541 | ||
1542 | @example | |
1543 | y | |
1544 | @result{} "ARRDEFG" | |
1545 | ||
1546 | (string-downcase! y) | |
1547 | @result{} "arrdefg" | |
1548 | ||
1549 | y | |
1550 | @result{} "arrdefg" | |
1551 | @end example | |
1552 | @end deffn | |
1553 | ||
1554 | @c docstring begin (texi-doc-string "guile" "string-downcase") | |
1555 | @deffn primitive string-downcase str | |
1556 | Downcase every character in @code{str}. | |
1557 | @end deffn | |
1558 | ||
1559 | @c docstring begin (texi-doc-string "guile" "string-capitalize!") | |
1560 | @deffn primitive string-capitalize! str | |
1561 | Destructively capitalize every character in @code{str}. | |
1562 | @end deffn | |
1563 | ||
1564 | @c docstring begin (texi-doc-string "guile" "string-capitalize") | |
1565 | @deffn primitive string-capitalize str | |
1566 | Capitalize every character in @code{str}. | |
1567 | @end deffn | |
1568 | ||
1569 | @c docstring begin (texi-doc-string "guile" "string-ci<=?") | |
1570 | @deffn primitive string-ci<=? s1 s2 | |
1571 | Case insensitive lexicographic ordering predicate; | |
1572 | returns @t{#t} if @var{s1} is lexicographically less than | |
1573 | or equal to @var{s2} regardless of case. (r5rs) | |
1574 | @end deffn | |
1575 | ||
1576 | @c docstring begin (texi-doc-string "guile" "string-ci<?") | |
1577 | @deffn primitive string-ci<? s1 s2 | |
1578 | Case insensitive lexicographic ordering predicate; | |
1579 | returns @t{#t} if @var{s1} is lexicographically less than | |
1580 | @var{s2} regardless of case. (r5rs) | |
1581 | @end deffn | |
1582 | ||
1583 | @c docstring begin (texi-doc-string "guile" "string-ci=?") | |
1584 | @deffn primitive string-ci=? s1 s2 | |
1585 | Case-insensitive string equality predicate; returns @t{#t} if | |
1586 | the two strings are the same length and their component characters | |
1587 | match (ignoring case) at each position; otherwise returns @t{#f}. (r5rs) | |
1588 | @end deffn | |
1589 | ||
1590 | @c docstring begin (texi-doc-string "guile" "string-ci>=?") | |
1591 | @deffn primitive string-ci>=? s1 s2 | |
1592 | Case insensitive lexicographic ordering predicate; | |
1593 | returns @t{#t} if @var{s1} is lexicographically greater than | |
1594 | or equal to @var{s2} regardless of case. (r5rs) | |
1595 | @end deffn | |
1596 | ||
1597 | @c docstring begin (texi-doc-string "guile" "string-ci>?") | |
1598 | @deffn primitive string-ci>? s1 s2 | |
1599 | Case insensitive lexicographic ordering predicate; | |
1600 | returns @t{#t} if @var{s1} is lexicographically greater than | |
1601 | @var{s2} regardless of case. (r5rs) | |
1602 | @end deffn | |
1603 | ||
1604 | @c docstring begin (texi-doc-string "guile" "string<=?") | |
1605 | @deffn primitive string<=? s1 s2 | |
1606 | Lexicographic ordering predicate; returns @t{#t} if @var{s1} | |
1607 | is lexicographically less than or equal to @var{s2}. (r5rs) | |
1608 | @end deffn | |
1609 | ||
1610 | @c docstring begin (texi-doc-string "guile" "string<?") | |
1611 | @deffn primitive string<? s1 s2 | |
1612 | Lexicographic ordering predicate; returns @t{#t} if @var{s1} | |
1613 | is lexicographically less than @var{s2}. (r5rs) | |
1614 | @end deffn | |
1615 | ||
1616 | @c docstring begin (texi-doc-string "guile" "string=?") | |
1617 | @deffn primitive string=? s1 s2 | |
1618 | Lexicographic equality predicate; | |
1619 | Returns @t{#t} if the two strings are the same length and contain the same | |
1620 | characters in the same positions, otherwise returns @t{#f}. (r5rs) | |
1621 | ||
1622 | @samp{String-ci=?} treats | |
1623 | upper and lower case letters as though they were the same character, but | |
1624 | @samp{string=?} treats upper and lower case as distinct characters. | |
1625 | @end deffn | |
1626 | ||
1627 | @c docstring begin (texi-doc-string "guile" "string>=?") | |
1628 | @deffn primitive string>=? s1 s2 | |
1629 | Lexicographic ordering predicate; returns @t{#t} if @var{s1} | |
1630 | is lexicographically greater than or equal to @var{s2}. (r5rs) | |
1631 | @end deffn | |
1632 | ||
1633 | @c docstring begin (texi-doc-string "guile" "string>?") | |
1634 | @deffn primitive string>? s1 s2 | |
1635 | Lexicographic ordering predicate; returns @t{#t} if @var{s1} | |
1636 | is lexicographically greater than @var{s2}. (r5rs) | |
1637 | @end deffn | |
1638 | ||
1639 | @c docstring begin (texi-doc-string "guile" "string->list") | |
1640 | @deffn primitive string->list str | |
1641 | @samp{String->list} returns a newly allocated list of the | |
1642 | characters that make up the given string. @samp{List->string} | |
1643 | returns a newly allocated string formed from the characters in the list | |
1644 | @var{list}, which must be a list of characters. @samp{String->list} | |
1645 | and @samp{list->string} are | |
1646 | inverses so far as @samp{equal?} is concerned. (r5rs) | |
1647 | @end deffn | |
1648 | ||
1649 | @c docstring begin (texi-doc-string "guile" "string-ci->symbol") | |
1650 | @deffn primitive string-ci->symbol str | |
1651 | Return the symbol whose name is @var{str}, downcased in necessary(???). | |
1652 | @end deffn | |
1653 | ||
1654 | @c docstring begin (texi-doc-string "guile" "string-copy") | |
1655 | @deffn primitive string-copy str | |
1656 | Returns a newly allocated copy of the given @var{string}. (r5rs) | |
1657 | @end deffn | |
1658 | ||
1659 | @c docstring begin (texi-doc-string "guile" "string-fill!") | |
1660 | @deffn primitive string-fill! str chr | |
1661 | Stores @var{char} in every element of the given @var{string} and returns an | |
1662 | unspecified value. (r5rs) | |
1663 | @end deffn | |
1664 | ||
1665 | ||
1666 | @node Shared Substrings | |
1667 | @subsection Shared Substrings | |
1668 | ||
1669 | Whenever you extract a substring using @code{substring}, the Scheme | |
1670 | interpreter allocates a new string and copies data from the old string. | |
1671 | This is expensive, but @code{substring} is so convenient for | |
1672 | manipulating text that programmers use it often. | |
1673 | ||
1674 | Guile Scheme provides the concept of the @dfn{shared substring} to | |
1675 | improve performance of many substring-related operations. A shared | |
1676 | substring is an object that mostly behaves just like an ordinary | |
1677 | substring, except that it actually shares storage space with its parent | |
1678 | string. | |
1679 | ||
1680 | @c ARGFIXME frm/start to/end | |
1681 | @c docstring begin (texi-doc-string "guile" "make-shared-substring") | |
1682 | @deffn primitive make-shared-substring str [frm [to]] | |
1683 | Return a shared substring of @var{str}. The semantics are the same as | |
1684 | for the @code{substring} function: the shared substring returned | |
1685 | includes all of the text from @var{str} between indexes @var{start} | |
1686 | (inclusive) and @var{end} (exclusive). If @var{end} is omitted, it | |
1687 | defaults to the end of @var{str}. The shared substring returned by | |
1688 | @code{make-shared-substring} occupies the same storage space as | |
1689 | @var{str}. | |
1690 | @end deffn | |
1691 | ||
1692 | Example: | |
1693 | ||
1694 | @example | |
1695 | (define foo "the quick brown fox") | |
1696 | (define bar (make-shared-substring some-string 4 9)) | |
1697 | ||
1698 | foo => "t h e q u i c k b r o w n f o x" | |
1699 | bar =========> |---------| | |
1700 | @end example | |
1701 | ||
1702 | The shared substring @var{bar} is not given its own storage space. | |
1703 | Instead, the Guile interpreter notes internally that @var{bar} points to | |
1704 | a portion of the memory allocated to @var{foo}. However, @var{bar} | |
1705 | behaves like an ordinary string in most respects: it may be used with | |
1706 | string primitives like @code{string-length}, @code{string-ref}, | |
1707 | @code{string=?}. Guile makes the necessary translation between indices | |
1708 | of @var{bar} and indices of @var{foo} automatically. | |
1709 | ||
1710 | @example | |
1711 | (string-length? bar) @result{} 5 ; bar only extends from indices 4 to 9 | |
1712 | (string-ref bar 3) @result{} #\c ; same as (string-ref foo 7) | |
1713 | (make-shared-substring bar 2) | |
1714 | @result{} "ick" ; can even make a shared substring! | |
1715 | @end example | |
1716 | ||
1717 | Because creating a shared substring does not require allocating new | |
1718 | storage from the heap, it is a very fast operation. However, because it | |
1719 | shares memory with its parent string, a change to the contents of the | |
1720 | parent string will implicitly change the contents of its shared | |
1721 | substrings. | |
1722 | ||
1723 | @example | |
1724 | (string-set! foo 7 #\r) | |
1725 | bar @result{} "quirk" | |
1726 | @end example | |
1727 | ||
1728 | Guile considers shared substrings to be immutable. This is because | |
1729 | programmers might not always be aware that a given string is really a | |
1730 | shared substring, and might innocently try to mutate it without | |
1731 | realizing that the change would affect its parent string. (We are | |
1732 | currently considering a "copy-on-write" strategy that would permit | |
1733 | modifying shared substrings without affecting the parent string.) | |
1734 | ||
1735 | In general, shared substrings are useful in circumstances where it is | |
1736 | important to divide a string into smaller portions, but you do not | |
1737 | expect to change the contents of any of the strings involved. | |
1738 | ||
1739 | @node Read Only Strings | |
1740 | @subsection Read Only Strings | |
1741 | ||
1742 | Type-checking in Guile primitives distinguishes between mutable strings | |
1743 | and read only strings. Mutable strings answer @code{#t} to | |
1744 | @code{string?} while read only strings may or may not. All kinds of | |
1745 | strings, whether or not they are mutable return #t to this: | |
1746 | ||
1747 | @c ARGFIXME x/obj | |
1748 | @c docstring begin (texi-doc-string "guile" "read-only-string?") | |
1749 | @deffn primitive read-only-string? obj | |
1750 | Return true if @var{obj} can be read as a string, | |
1751 | ||
1752 | This illustrates the difference between @code{string?} and | |
1753 | @code{read-only-string?}: | |
1754 | ||
1755 | @example | |
1756 | (string? "a string") @result{} #t | |
1757 | (string? 'a-symbol) @result{} #f | |
1758 | ||
1759 | (read-only-string? "a string") @result{} #t | |
1760 | (read-only-string? 'a-symbol) @result{} #t | |
1761 | @end example | |
1762 | @end deffn | |
1763 | ||
1764 | "Read-only" refers to how the string will be used, not how the string is | |
1765 | permitted to be used. In particular, all strings are "read-only | |
1766 | strings" even if they are mutable, because a function that only reads | |
1767 | from a string can certainly operate on even a mutable string. | |
1768 | ||
1769 | Symbols are an example of read-only strings. Many string functions, | |
1770 | such as @code{string-append} are happy to operate on symbols. Many | |
1771 | functions that expect a string argument, such as @code{open-file}, will | |
1772 | accept a symbol as well. | |
1773 | ||
1774 | Shared substrings, discussed in the previous chapter, also happen to be | |
1775 | read-only strings. | |
1776 | ||
1777 | ||
1778 | @node Regular Expressions | |
1779 | @section Regular Expressions | |
1780 | ||
1781 | @cindex regular expressions | |
1782 | @cindex regex | |
1783 | @cindex emacs regexp | |
1784 | ||
1785 | A @dfn{regular expression} (or @dfn{regexp}) is a pattern that | |
1786 | describes a whole class of strings. A full description of regular | |
1787 | expressions and their syntax is beyond the scope of this manual; | |
1788 | an introduction can be found in the Emacs manual (@pxref{Regexps, | |
1789 | , Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, or | |
1790 | in many general Unix reference books. | |
1791 | ||
1792 | If your system does not include a POSIX regular expression library, and | |
1793 | you have not linked Guile with a third-party regexp library such as Rx, | |
1794 | these functions will not be available. You can tell whether your Guile | |
1795 | installation includes regular expression support by checking whether the | |
1796 | @code{*features*} list includes the @code{regex} symbol. | |
1797 | ||
1798 | @menu | |
1799 | * Regexp Functions:: Functions that create and match regexps. | |
1800 | * Match Structures:: Finding what was matched by a regexp. | |
1801 | * Backslash Escapes:: Removing the special meaning of regexp metacharacters. | |
1802 | * Rx Interface:: Tom Lord's Rx library does things differently. | |
1803 | @end menu | |
1804 | ||
1805 | [FIXME: it may be useful to include an Examples section. Parts of this | |
1806 | interface are bewildering on first glance.] | |
1807 | ||
1808 | @node Regexp Functions | |
1809 | @subsection Regexp Functions | |
1810 | ||
1811 | By default, Guile supports POSIX extended regular expressions. | |
1812 | That means that the characters @samp{(}, @samp{)}, @samp{+} and | |
1813 | @samp{?} are special, and must be escaped if you wish to match the | |
1814 | literal characters. | |
1815 | ||
1816 | This regular expression interface was modeled after that | |
1817 | implemented by SCSH, the Scheme Shell. It is intended to be | |
1818 | upwardly compatible with SCSH regular expressions. | |
1819 | ||
1820 | @c begin (scm-doc-string "regex.scm" "string-match") | |
1821 | @deffn procedure string-match pattern str [start] | |
1822 | Compile the string @var{pattern} into a regular expression and compare | |
1823 | it with @var{str}. The optional numeric argument @var{start} specifies | |
1824 | the position of @var{str} at which to begin matching. | |
1825 | ||
1826 | @code{string-match} returns a @dfn{match structure} which | |
1827 | describes what, if anything, was matched by the regular | |
1828 | expression. @xref{Match Structures}. If @var{str} does not match | |
1829 | @var{pattern} at all, @code{string-match} returns @code{#f}. | |
1830 | @end deffn | |
1831 | ||
1832 | Each time @code{string-match} is called, it must compile its | |
1833 | @var{pattern} argument into a regular expression structure. This | |
1834 | operation is expensive, which makes @code{string-match} inefficient if | |
1835 | the same regular expression is used several times (for example, in a | |
1836 | loop). For better performance, you can compile a regular expression in | |
1837 | advance and then match strings against the compiled regexp. | |
1838 | ||
1839 | @c ARGFIXME pat/str flags/flag | |
1840 | @c docstring begin (texi-doc-string "guile" "make-regexp") | |
1841 | @deffn primitive make-regexp pat . flags | |
1842 | Compile the regular expression described by @var{str}, and return the | |
1843 | compiled regexp structure. If @var{str} does not describe a legal | |
1844 | regular expression, @code{make-regexp} throws a | |
1845 | @code{regular-expression-syntax} error. | |
1846 | ||
1847 | The @var{flag} arguments change the behavior of the compiled regexp. | |
1848 | The following flags may be supplied: | |
1849 | ||
1850 | @table @code | |
1851 | @item regexp/icase | |
1852 | Consider uppercase and lowercase letters to be the same when matching. | |
1853 | ||
1854 | @item regexp/newline | |
1855 | If a newline appears in the target string, then permit the @samp{^} and | |
1856 | @samp{$} operators to match immediately after or immediately before the | |
1857 | newline, respectively. Also, the @samp{.} and @samp{[^...]} operators | |
1858 | will never match a newline character. The intent of this flag is to | |
1859 | treat the target string as a buffer containing many lines of text, and | |
1860 | the regular expression as a pattern that may match a single one of those | |
1861 | lines. | |
1862 | ||
1863 | @item regexp/basic | |
1864 | Compile a basic (``obsolete'') regexp instead of the extended | |
1865 | (``modern'') regexps that are the default. Basic regexps do not | |
1866 | consider @samp{|}, @samp{+} or @samp{?} to be special characters, and | |
1867 | require the @samp{@{...@}} and @samp{(...)} metacharacters to be | |
1868 | backslash-escaped (@pxref{Backslash Escapes}). There are several other | |
1869 | differences between basic and extended regular expressions, but these | |
1870 | are the most significant. | |
1871 | ||
1872 | @item regexp/extended | |
1873 | Compile an extended regular expression rather than a basic regexp. This | |
1874 | is the default behavior; this flag will not usually be needed. If a | |
1875 | call to @code{make-regexp} includes both @code{regexp/basic} and | |
1876 | @code{regexp/extended} flags, the one which comes last will override | |
1877 | the earlier one. | |
1878 | @end table | |
1879 | @end deffn | |
1880 | ||
1881 | @c ARGFIXME rx/regexp | |
1882 | @c docstring begin (texi-doc-string "guile" "regexp-exec") | |
1883 | @deffn primitive regexp-exec rx str [start [flags]] | |
1884 | Match the compiled regular expression @var{regexp} against @code{str}. | |
1885 | If the optional integer @var{start} argument is provided, begin matching | |
1886 | from that position in the string. Return a match structure describing | |
1887 | the results of the match, or @code{#f} if no match could be found. | |
1888 | @end deffn | |
1889 | ||
1890 | @c ARGFIXME x/obj | |
1891 | @c docstring begin (texi-doc-string "guile" "regexp?") | |
1892 | @deffn primitive regexp? x | |
1893 | Return @code{#t} if @var{obj} is a compiled regular expression, or | |
1894 | @code{#f} otherwise. | |
1895 | @end deffn | |
1896 | ||
1897 | Regular expressions are commonly used to find patterns in one string and | |
1898 | replace them with the contents of another string. | |
1899 | ||
1900 | @c begin (scm-doc-string "regex.scm" "regexp-substitute") | |
1901 | @deffn procedure regexp-substitute port match [item@dots{}] | |
1902 | Write to the output port @var{port} selected contents of the match | |
1903 | structure @var{match}. Each @var{item} specifies what should be | |
1904 | written, and may be one of the following arguments: | |
1905 | ||
1906 | @itemize @bullet | |
1907 | @item | |
1908 | A string. String arguments are written out verbatim. | |
1909 | ||
1910 | @item | |
1911 | An integer. The submatch with that number is written. | |
1912 | ||
1913 | @item | |
1914 | The symbol @samp{pre}. The portion of the matched string preceding | |
1915 | the regexp match is written. | |
1916 | ||
1917 | @item | |
1918 | The symbol @samp{post}. The portion of the matched string following | |
1919 | the regexp match is written. | |
1920 | @end itemize | |
1921 | ||
1922 | @var{port} may be @code{#f}, in which case nothing is written; instead, | |
1923 | @code{regexp-substitute} constructs a string from the specified | |
1924 | @var{item}s and returns that. | |
1925 | @end deffn | |
1926 | ||
1927 | @c begin (scm-doc-string "regex.scm" "regexp-substitute") | |
1928 | @deffn procedure regexp-substitute/global port regexp target [item@dots{}] | |
1929 | Similar to @code{regexp-substitute}, but can be used to perform global | |
1930 | substitutions on @var{str}. Instead of taking a match structure as an | |
1931 | argument, @code{regexp-substitute/global} takes two string arguments: a | |
1932 | @var{regexp} string describing a regular expression, and a @var{target} | |
1933 | string which should be matched against this regular expression. | |
1934 | ||
1935 | Each @var{item} behaves as in @var{regexp-substitute}, with the | |
1936 | following exceptions: | |
1937 | ||
1938 | @itemize @bullet | |
1939 | @item | |
1940 | A function may be supplied. When this function is called, it will be | |
1941 | passed one argument: a match structure for a given regular expression | |
1942 | match. It should return a string to be written out to @var{port}. | |
1943 | ||
1944 | @item | |
1945 | The @samp{post} symbol causes @code{regexp-substitute/global} to recurse | |
1946 | on the unmatched portion of @var{str}. This @emph{must} be supplied in | |
1947 | order to perform global search-and-replace on @var{str}; if it is not | |
1948 | present among the @var{item}s, then @code{regexp-substitute/global} will | |
1949 | return after processing a single match. | |
1950 | @end itemize | |
1951 | @end deffn | |
1952 | ||
1953 | @node Match Structures | |
1954 | @subsection Match Structures | |
1955 | ||
1956 | @cindex match structures | |
1957 | ||
1958 | A @dfn{match structure} is the object returned by @code{string-match} and | |
1959 | @code{regexp-exec}. It describes which portion of a string, if any, | |
1960 | matched the given regular expression. Match structures include: a | |
1961 | reference to the string that was checked for matches; the starting and | |
1962 | ending positions of the regexp match; and, if the regexp included any | |
1963 | parenthesized subexpressions, the starting and ending positions of each | |
1964 | submatch. | |
1965 | ||
1966 | In each of the regexp match functions described below, the @code{match} | |
1967 | argument must be a match structure returned by a previous call to | |
1968 | @code{string-match} or @code{regexp-exec}. Most of these functions | |
1969 | return some information about the original target string that was | |
1970 | matched against a regular expression; we will call that string | |
1971 | @var{target} for easy reference. | |
1972 | ||
1973 | @c begin (scm-doc-string "regex.scm" "regexp-match?") | |
1974 | @deffn procedure regexp-match? obj | |
1975 | Return @code{#t} if @var{obj} is a match structure returned by a | |
1976 | previous call to @code{regexp-exec}, or @code{#f} otherwise. | |
1977 | @end deffn | |
1978 | ||
1979 | @c begin (scm-doc-string "regex.scm" "match:substring") | |
1980 | @deffn procedure match:substring match [n] | |
1981 | Return the portion of @var{target} matched by subexpression number | |
1982 | @var{n}. Submatch 0 (the default) represents the entire regexp match. | |
1983 | If the regular expression as a whole matched, but the subexpression | |
1984 | number @var{n} did not match, return @code{#f}. | |
1985 | @end deffn | |
1986 | ||
1987 | @c begin (scm-doc-string "regex.scm" "match:start") | |
1988 | @deffn procedure match:start match [n] | |
1989 | Return the starting position of submatch number @var{n}. | |
1990 | @end deffn | |
1991 | ||
1992 | @c begin (scm-doc-string "regex.scm" "match:end") | |
1993 | @deffn procedure match:end match [n] | |
1994 | Return the ending position of submatch number @var{n}. | |
1995 | @end deffn | |
1996 | ||
1997 | @c begin (scm-doc-string "regex.scm" "match:prefix") | |
1998 | @deffn procedure match:prefix match | |
1999 | Return the unmatched portion of @var{target} preceding the regexp match. | |
2000 | @end deffn | |
2001 | ||
2002 | @c begin (scm-doc-string "regex.scm" "match:suffix") | |
2003 | @deffn procedure match:suffix match | |
2004 | Return the unmatched portion of @var{target} following the regexp match. | |
2005 | @end deffn | |
2006 | ||
2007 | @c begin (scm-doc-string "regex.scm" "match:count") | |
2008 | @deffn procedure match:count match | |
2009 | Return the number of parenthesized subexpressions from @var{match}. | |
2010 | Note that the entire regular expression match itself counts as a | |
2011 | subexpression, and failed submatches are included in the count. | |
2012 | @end deffn | |
2013 | ||
2014 | @c begin (scm-doc-string "regex.scm" "match:string") | |
2015 | @deffn procedure match:string match | |
2016 | Return the original @var{target} string. | |
2017 | @end deffn | |
2018 | ||
2019 | @node Backslash Escapes | |
2020 | @subsection Backslash Escapes | |
2021 | ||
2022 | Sometimes you will want a regexp to match characters like @samp{*} or | |
2023 | @samp{$} exactly. For example, to check whether a particular string | |
2024 | represents a menu entry from an Info node, it would be useful to match | |
2025 | it against a regexp like @samp{^* [^:]*::}. However, this won't work; | |
2026 | because the asterisk is a metacharacter, it won't match the @samp{*} at | |
2027 | the beginning of the string. In this case, we want to make the first | |
2028 | asterisk un-magic. | |
2029 | ||
2030 | You can do this by preceding the metacharacter with a backslash | |
2031 | character @samp{\}. (This is also called @dfn{quoting} the | |
2032 | metacharacter, and is known as a @dfn{backslash escape}.) When Guile | |
2033 | sees a backslash in a regular expression, it considers the following | |
2034 | glyph to be an ordinary character, no matter what special meaning it | |
2035 | would ordinarily have. Therefore, we can make the above example work by | |
2036 | changing the regexp to @samp{^\* [^:]*::}. The @samp{\*} sequence tells | |
2037 | the regular expression engine to match only a single asterisk in the | |
2038 | target string. | |
2039 | ||
2040 | Since the backslash is itself a metacharacter, you may force a regexp to | |
2041 | match a backslash in the target string by preceding the backslash with | |
2042 | itself. For example, to find variable references in a @TeX{} program, | |
2043 | you might want to find occurrences of the string @samp{\let\} followed | |
2044 | by any number of alphabetic characters. The regular expression | |
2045 | @samp{\\let\\[A-Za-z]*} would do this: the double backslashes in the | |
2046 | regexp each match a single backslash in the target string. | |
2047 | ||
2048 | @c begin (scm-doc-string "regex.scm" "regexp-quote") | |
2049 | @deffn procedure regexp-quote str | |
2050 | Quote each special character found in @var{str} with a backslash, and | |
2051 | return the resulting string. | |
2052 | @end deffn | |
2053 | ||
2054 | @strong{Very important:} Using backslash escapes in Guile source code | |
2055 | (as in Emacs Lisp or C) can be tricky, because the backslash character | |
2056 | has special meaning for the Guile reader. For example, if Guile | |
2057 | encounters the character sequence @samp{\n} in the middle of a string | |
2058 | while processing Scheme code, it replaces those characters with a | |
2059 | newline character. Similarly, the character sequence @samp{\t} is | |
2060 | replaced by a horizontal tab. Several of these @dfn{escape sequences} | |
2061 | are processed by the Guile reader before your code is executed. | |
2062 | Unrecognized escape sequences are ignored: if the characters @samp{\*} | |
2063 | appear in a string, they will be translated to the single character | |
2064 | @samp{*}. | |
2065 | ||
2066 | This translation is obviously undesirable for regular expressions, since | |
2067 | we want to be able to include backslashes in a string in order to | |
2068 | escape regexp metacharacters. Therefore, to make sure that a backslash | |
2069 | is preserved in a string in your Guile program, you must use @emph{two} | |
2070 | consecutive backslashes: | |
2071 | ||
2072 | @lisp | |
2073 | (define Info-menu-entry-pattern (make-regexp "^\\* [^:]*")) | |
2074 | @end lisp | |
2075 | ||
2076 | The string in this example is preprocessed by the Guile reader before | |
2077 | any code is executed. The resulting argument to @code{make-regexp} is | |
2078 | the string @samp{^\* [^:]*}, which is what we really want. | |
2079 | ||
2080 | This also means that in order to write a regular expression that matches | |
2081 | a single backslash character, the regular expression string in the | |
2082 | source code must include @emph{four} backslashes. Each consecutive pair | |
2083 | of backslashes gets translated by the Guile reader to a single | |
2084 | backslash, and the resulting double-backslash is interpreted by the | |
2085 | regexp engine as matching a single backslash character. Hence: | |
2086 | ||
2087 | @lisp | |
2088 | (define tex-variable-pattern (make-regexp "\\\\let\\\\=[A-Za-z]*")) | |
2089 | @end lisp | |
2090 | ||
2091 | The reason for the unwieldiness of this syntax is historical. Both | |
2092 | regular expression pattern matchers and Unix string processing systems | |
2093 | have traditionally used backslashes with the special meanings | |
2094 | described above. The POSIX regular expression specification and ANSI C | |
2095 | standard both require these semantics. Attempting to abandon either | |
2096 | convention would cause other kinds of compatibility problems, possibly | |
2097 | more severe ones. Therefore, without extending the Scheme reader to | |
2098 | support strings with different quoting conventions (an ungainly and | |
2099 | confusing extension when implemented in other languages), we must adhere | |
2100 | to this cumbersome escape syntax. | |
2101 | ||
2102 | @node Rx Interface | |
2103 | @subsection Rx Interface | |
2104 | ||
2105 | [FIXME: this is taken from Gary and Mark's quick summaries and should be | |
2106 | reviewed and expanded. Rx is pretty stable, so could already be done!] | |
2107 | ||
2108 | @cindex rx | |
2109 | @cindex finite automaton | |
2110 | ||
2111 | Guile includes an interface to Tom Lord's Rx library (currently only to | |
2112 | POSIX regular expressions). Use of the library requires a two step | |
2113 | process: compile a regular expression into an efficient structure, then | |
2114 | use the structure in any number of string comparisons. | |
2115 | ||
2116 | For example, given the | |
2117 | regular expression @samp{abc.} (which matches any string containing | |
2118 | @samp{abc} followed by any single character): | |
2119 | ||
2120 | @smalllisp | |
2121 | guile> @kbd{(define r (regcomp "abc."))} | |
2122 | guile> @kbd{r} | |
2123 | #<rgx abc.> | |
2124 | guile> @kbd{(regexec r "abc")} | |
2125 | #f | |
2126 | guile> @kbd{(regexec r "abcd")} | |
2127 | #((0 . 4)) | |
2128 | guile> | |
2129 | @end smalllisp | |
2130 | ||
2131 | The definitions of @code{regcomp} and @code{regexec} are as follows: | |
2132 | ||
2133 | @c NJFIXME not in libguile! | |
2134 | @deffn primitive regcomp pattern [flags] | |
2135 | Compile the regular expression pattern using POSIX rules. Flags is | |
2136 | optional and should be specified using symbolic names: | |
2137 | @defvar REG_EXTENDED | |
2138 | use extended POSIX syntax | |
2139 | @end defvar | |
2140 | @defvar REG_ICASE | |
2141 | use case-insensitive matching | |
2142 | @end defvar | |
2143 | @defvar REG_NEWLINE | |
2144 | allow anchors to match after newline characters in the | |
2145 | string and prevents @code{.} or @code{[^...]} from matching newlines. | |
2146 | @end defvar | |
2147 | ||
2148 | The @code{logior} procedure can be used to combine multiple flags. | |
2149 | The default is to use | |
2150 | POSIX basic syntax, which makes @code{+} and @code{?} literals and @code{\+} | |
2151 | and @code{\?} | |
2152 | operators. Backslashes in @var{pattern} must be escaped if specified in a | |
2153 | literal string e.g., @code{"\\(a\\)\\?"}. | |
2154 | @end deffn | |
2155 | ||
2156 | @c NJFIXME not in libguile! | |
2157 | @deffn primitive regexec regex string [match-pick] [flags] | |
2158 | ||
2159 | Match @var{string} against the compiled POSIX regular expression | |
2160 | @var{regex}. | |
2161 | @var{match-pick} and @var{flags} are optional. Possible flags (which can be | |
2162 | combined using the logior procedure) are: | |
2163 | ||
2164 | @defvar REG_NOTBOL | |
2165 | The beginning of line operator won't match the beginning of | |
2166 | @var{string} (presumably because it's not the beginning of a line) | |
2167 | @end defvar | |
2168 | ||
2169 | @defvar REG_NOTEOL | |
2170 | Similar to REG_NOTBOL, but prevents the end of line operator | |
2171 | from matching the end of @var{string}. | |
2172 | @end defvar | |
2173 | ||
2174 | If no match is possible, regexec returns #f. Otherwise @var{match-pick} | |
2175 | determines the return value: | |
2176 | ||
2177 | @code{#t} or unspecified: a newly-allocated vector is returned, | |
2178 | containing pairs with the indices of the matched part of @var{string} and any | |
2179 | substrings. | |
2180 | ||
2181 | @code{""}: a list is returned: the first element contains a nested list | |
2182 | with the matched part of @var{string} surrounded by the the unmatched parts. | |
2183 | Remaining elements are matched substrings (if any). All returned | |
2184 | substrings share memory with @var{string}. | |
2185 | ||
2186 | @code{#f}: regexec returns #t if a match is made, otherwise #f. | |
2187 | ||
2188 | vector: the supplied vector is returned, with the first element replaced | |
2189 | by a pair containing the indices of the matched portion of @var{string} and | |
2190 | further elements replaced by pairs containing the indices of matched | |
2191 | substrings (if any). | |
2192 | ||
2193 | list: a list will be returned, with each member of the list | |
2194 | specified by a code in the corresponding position of the supplied list: | |
2195 | ||
2196 | a number: the numbered matching substring (0 for the entire match). | |
2197 | ||
2198 | @code{#\<}: the beginning of @var{string} to the beginning of the part matched | |
2199 | by regex. | |
2200 | ||
2201 | @code{#\>}: the end of the matched part of @var{string} to the end of | |
2202 | @var{string}. | |
2203 | ||
2204 | @code{#\c}: the "final tag", which seems to be associated with the "cut | |
2205 | operator", which doesn't seem to be available through the posix | |
2206 | interface. | |
2207 | ||
2208 | e.g., @code{(list #\< 0 1 #\>)}. The returned substrings share memory with | |
2209 | @var{string}. | |
2210 | @end deffn | |
2211 | ||
2212 | Here are some other procedures that might be used when using regular | |
2213 | expressions: | |
2214 | ||
2215 | @c NJFIXME not in libguile! | |
2216 | @deffn primitive compiled-regexp? obj | |
2217 | Test whether obj is a compiled regular expression. | |
2218 | @end deffn | |
2219 | ||
2220 | @c NJFIXME not in libguile! | |
2221 | @deffn primitive regexp->dfa regex [flags] | |
2222 | @end deffn | |
2223 | ||
2224 | @c NJFIXME not in libguile! | |
2225 | @deffn primitive dfa-fork dfa | |
2226 | @end deffn | |
2227 | ||
2228 | @c NJFIXME not in libguile! | |
2229 | @deffn primitive reset-dfa! dfa | |
2230 | @end deffn | |
2231 | ||
2232 | @c NJFIXME not in libguile! | |
2233 | @deffn primitive dfa-final-tag dfa | |
2234 | @end deffn | |
2235 | ||
2236 | @c NJFIXME not in libguile! | |
2237 | @deffn primitive dfa-continuable? dfa | |
2238 | @end deffn | |
2239 | ||
2240 | @c NJFIXME not in libguile! | |
2241 | @deffn primitive advance-dfa! dfa string | |
2242 | @end deffn | |
2243 | ||
2244 | ||
2245 | @node Symbols and Variables | |
2246 | @section Symbols and Variables | |
2247 | ||
2248 | Guile symbol tables are hash tables. Each hash table, also called an | |
2249 | @dfn{obarray} (for `object array'), is a vector of association lists. | |
2250 | Each entry in the alists is a pair (@var{SYMBOL} . @var{VALUE}). To | |
2251 | @dfn{intern} a symbol in a symbol table means to return its | |
2252 | (@var{SYMBOL} . @var{VALUE}) pair, adding a new entry to the symbol | |
2253 | table (with an undefined value) if none is yet present. | |
2254 | ||
2255 | @c docstring begin (texi-doc-string "guile" "builtin-bindings") | |
2256 | @deffn primitive builtin-bindings | |
2257 | Create and return a copy of the global symbol table, removing all | |
2258 | unbound symbols. | |
2259 | @end deffn | |
2260 | ||
2261 | @c docstring begin (texi-doc-string "guile" "gensym") | |
2262 | @deffn primitive gensym [prefix] | |
2263 | Create a new symbol with a name constructed from a prefix and | |
2264 | a counter value. The string @var{prefix} can be specified as | |
2265 | an optional argument. Default prefix is @code{g}. The counter | |
2266 | is increased by 1 at each call. There is no provision for | |
2267 | resetting the counter. | |
2268 | @end deffn | |
2269 | ||
2270 | @c docstring begin (texi-doc-string "guile" "gentemp") | |
2271 | @deffn primitive gentemp [prefix [obarray]] | |
2272 | Create a new symbol with a name unique in an obarray. | |
2273 | The name is constructed from an optional string @var{prefix} | |
2274 | and a counter value. The default prefix is @code{t}. The | |
2275 | @var{obarray} is specified as a second optional argument. | |
2276 | Default is the system obarray where all normal symbols are | |
2277 | interned. The counter is increased by 1 at each | |
2278 | call. There is no provision for resetting the counter. | |
2279 | @end deffn | |
2280 | ||
2281 | @c docstring begin (texi-doc-string "guile" "intern-symbol") | |
2282 | @deffn primitive intern-symbol obarray string | |
2283 | Add a new symbol to @var{obarray} with name @var{string}, bound to an | |
2284 | unspecified initial value. The symbol table is not modified if a symbol | |
2285 | with this name is already present. | |
2286 | @end deffn | |
2287 | ||
2288 | @c docstring begin (texi-doc-string "guile" "string->obarray-symbol") | |
2289 | @deffn primitive string->obarray-symbol obarray string [soft?] | |
2290 | Intern a new symbol in @var{obarray}, a symbol table, with name | |
2291 | @var{string}. | |
2292 | ||
2293 | If @var{obarray} is @code{#f}, use the default system symbol table. If | |
2294 | @var{obarray} is @code{#t}, the symbol should not be interned in any | |
2295 | symbol table; merely return the pair (@var{symbol} | |
2296 | . @var{#<undefined>}). | |
2297 | ||
2298 | The @var{soft?} argument determines whether new symbol table entries | |
2299 | should be created when the specified symbol is not already present in | |
2300 | @var{obarray}. If @var{soft?} is specified and is a true value, then | |
2301 | new entries should not be added for symbols not already present in the | |
2302 | table; instead, simply return @code{#f}. | |
2303 | @end deffn | |
2304 | ||
2305 | @c docstring begin (texi-doc-string "guile" "string->symbol") | |
2306 | @deffn primitive string->symbol string | |
2307 | Returns the symbol whose name is @var{string}. This procedure can | |
2308 | create symbols with names containing special characters or letters in | |
2309 | the non-standard case, but it is usually a bad idea to create such | |
2310 | symbols because in some implementations of Scheme they cannot be read as | |
2311 | themselves. See @samp{symbol->string}. | |
2312 | ||
2313 | The following examples assume that the implementation's standard case is | |
2314 | lower case: | |
2315 | ||
2316 | @format | |
2317 | @t{(eq? 'mISSISSIppi 'mississippi) | |
2318 | ==> #t | |
2319 | (string->symbol "mISSISSIppi") | |
2320 | ==> | |
2321 | @r{}the symbol with name "mISSISSIppi" | |
2322 | (eq? 'bitBlt (string->symbol "bitBlt")) | |
2323 | ==> #f | |
2324 | (eq? 'JollyWog | |
2325 | (string->symbol | |
2326 | (symbol->string 'JollyWog))) | |
2327 | ==> #t | |
2328 | (string=? "K. Harper, M.D." | |
2329 | (symbol->string | |
2330 | (string->symbol "K. Harper, M.D."))) | |
2331 | ==> #t | |
2332 | } | |
2333 | @end format | |
2334 | @end deffn | |
2335 | ||
2336 | @c docstring begin (texi-doc-string "guile" "symbol->string") | |
2337 | @deffn primitive symbol->string symbol | |
2338 | Returns the name of @var{symbol} as a string. If the symbol was part of | |
2339 | an object returned as the value of a literal expression (section | |
2340 | @pxref{Literal expressions,,,r4rs, The Revised^4 Report on Scheme}) or | |
2341 | by a call to the @samp{read} procedure, and its name contains alphabetic | |
2342 | characters, then the string returned will contain characters in the | |
2343 | implementation's preferred standard case---some implementations will | |
2344 | prefer upper case, others lower case. If the symbol was returned by | |
2345 | @samp{string->symbol}, the case of characters in the string returned | |
2346 | will be the same as the case in the string that was passed to | |
2347 | @samp{string->symbol}. It is an error to apply mutation procedures like | |
2348 | @code{string-set!} to strings returned by this procedure. (r5rs) | |
2349 | ||
2350 | The following examples assume that the implementation's standard case is | |
2351 | lower case: | |
2352 | ||
2353 | @format | |
2354 | @t{(symbol->string 'flying-fish) | |
2355 | ==> "flying-fish" | |
2356 | (symbol->string 'Martin) ==> "martin" | |
2357 | (symbol->string | |
2358 | (string->symbol "Malvina")) | |
2359 | ==> "Malvina" | |
2360 | } | |
2361 | @end format | |
2362 | @end deffn | |
2363 | ||
2364 | @c docstring begin (texi-doc-string "guile" "symbol-binding") | |
2365 | @deffn primitive symbol-binding obarray string | |
2366 | Look up in @var{obarray} the symbol whose name is @var{string}, and | |
2367 | return the value to which it is bound. If @var{obarray} is @code{#f}, | |
2368 | use the global symbol table. If @var{string} is not interned in | |
2369 | @var{obarray}, an error is signalled. | |
2370 | @end deffn | |
2371 | ||
2372 | @c docstring begin (texi-doc-string "guile" "symbol-bound?") | |
2373 | @deffn primitive symbol-bound? obarray string | |
2374 | Return @var{#t} if @var{obarray} contains a symbol with name | |
2375 | @var{string} bound to a defined value. This differs from | |
2376 | @var{symbol-interned?} in that the mere mention of a symbol usually causes | |
2377 | it to be interned; @code{symbol-bound?} determines whether a symbol has | |
2378 | been given any meaningful value. | |
2379 | @end deffn | |
2380 | ||
2381 | @c docstring begin (texi-doc-string "guile" "symbol-fref") | |
2382 | @deffn primitive symbol-fref symbol | |
2383 | Return the contents of @var{symbol}'s @dfn{function slot}. | |
2384 | @end deffn | |
2385 | ||
2386 | @c docstring begin (texi-doc-string "guile" "symbol-fset!") | |
2387 | @deffn primitive symbol-fset! symbol value | |
2388 | Change the binding of @var{symbol}'s function slot. | |
2389 | @end deffn | |
2390 | ||
2391 | @c docstring begin (texi-doc-string "guile" "symbol-hash") | |
2392 | @deffn primitive symbol-hash symbol | |
2393 | Return a hash value for @var{symbol}. | |
2394 | @end deffn | |
2395 | ||
2396 | @c docstring begin (texi-doc-string "guile" "symbol-interned?") | |
2397 | @deffn primitive symbol-interned? obarray string | |
2398 | Return @var{#t} if @var{obarray} contains a symbol with name | |
2399 | @var{string}, and @var{#f} otherwise. | |
2400 | @end deffn | |
2401 | ||
2402 | @c docstring begin (texi-doc-string "guile" "symbol-pref") | |
2403 | @deffn primitive symbol-pref symbol | |
2404 | Return the @dfn{property list} currently associated with @var{symbol}. | |
2405 | @end deffn | |
2406 | ||
2407 | @c docstring begin (texi-doc-string "guile" "symbol-pset!") | |
2408 | @deffn primitive symbol-pset! symbol value | |
2409 | Change the binding of @var{symbol}'s property slot. | |
2410 | @end deffn | |
2411 | ||
2412 | @c docstring begin (texi-doc-string "guile" "symbol-set!") | |
2413 | @deffn primitive symbol-set! obarray string value | |
2414 | Find the symbol in @var{obarray} whose name is @var{string}, and rebind | |
2415 | it to @var{value}. An error is signalled if @var{string} is not present | |
2416 | in @var{obarray}. | |
2417 | @end deffn | |
2418 | ||
2419 | @c docstring begin (texi-doc-string "guile" "symbol?") | |
2420 | @deffn primitive symbol? obj | |
2421 | Returns @t{#t} if @var{obj} is a symbol, otherwise returns @t{#f}. (r5rs) | |
2422 | @end deffn | |
2423 | ||
2424 | @c docstring begin (texi-doc-string "guile" "unintern-symbol") | |
2425 | @deffn primitive unintern-symbol obarray string | |
2426 | Remove the symbol with name @var{string} from @var{obarray}. This | |
2427 | function returns @code{#t} if the symbol was present and @code{#f} | |
2428 | otherwise. | |
2429 | @end deffn | |
2430 | ||
2431 | @c docstring begin (texi-doc-string "guile" "builtin-variable") | |
2432 | @deffn primitive builtin-variable name | |
2433 | Return the built-in variable with the name @var{name}. | |
2434 | @var{name} must be a symbol (not a string). | |
2435 | Then use @code{variable-ref} to access its value. | |
2436 | @end deffn | |
2437 | ||
2438 | @c docstring begin (texi-doc-string "guile" "make-undefined-variable") | |
2439 | @deffn primitive make-undefined-variable [name-hint] | |
2440 | Return a variable object initialized to an undefined value. | |
2441 | If given, uses @var{name-hint} as its internal (debugging) | |
2442 | name, otherwise just treat it as an anonymous variable. | |
2443 | Remember, of course, that multiple bindings to the same | |
2444 | variable may exist, so @var{name-hint} is just that---a hint. | |
2445 | @end deffn | |
2446 | ||
2447 | @c docstring begin (texi-doc-string "guile" "make-variable") | |
2448 | @deffn primitive make-variable init [name-hint] | |
2449 | Return a variable object initialized to value @var{init}. | |
2450 | If given, uses @var{name-hint} as its internal (debugging) | |
2451 | name, otherwise just treat it as an anonymous variable. | |
2452 | Remember, of course, that multiple bindings to the same | |
2453 | variable may exist, so @var{name-hint} is just that---a hint. | |
2454 | @end deffn | |
2455 | ||
2456 | @c docstring begin (texi-doc-string "guile" "variable-bound?") | |
2457 | @deffn primitive variable-bound? var | |
2458 | Return @code{#t} iff @var{var} is bound to a value. | |
2459 | Throws an error if @var{var} is not a variable object. | |
2460 | @end deffn | |
2461 | ||
2462 | @c docstring begin (texi-doc-string "guile" "variable-ref") | |
2463 | @deffn primitive variable-ref var | |
2464 | Dereference @var{var} and return its value. | |
2465 | @var{var} must be a variable object; see @code{make-variable} | |
2466 | and @code{make-undefined-variable}. | |
2467 | @end deffn | |
2468 | ||
2469 | @c docstring begin (texi-doc-string "guile" "variable-set!") | |
2470 | @deffn primitive variable-set! var val | |
2471 | Set the value of the variable @var{var} to @var{val}. | |
2472 | @var{var} must be a variable object, @var{val} can be any | |
2473 | value. Return an unspecified value. | |
2474 | @end deffn | |
2475 | ||
2476 | @c docstring begin (texi-doc-string "guile" "variable?") | |
2477 | @deffn primitive variable? obj | |
2478 | Return @code{#t} iff @var{obj} is a variable object, else | |
2479 | return @code{#f} | |
2480 | @end deffn | |
2481 | ||
2482 | ||
2483 | @node Keywords | |
2484 | @section Keywords | |
2485 | ||
2486 | Keywords are self-evaluating objects with a convenient read syntax that | |
2487 | makes them easy to type. | |
2488 | ||
2489 | Guile's keyword support conforms to R4RS, and adds a (switchable) read | |
2490 | syntax extension to permit keywords to begin with @code{:} as well as | |
2491 | @code{#:}. | |
2492 | ||
2493 | @menu | |
2494 | * Why Use Keywords?:: | |
2495 | * Coding With Keywords:: | |
2496 | * Keyword Read Syntax:: | |
2497 | * Keyword Primitives:: | |
2498 | @end menu | |
2499 | ||
2500 | @node Why Use Keywords? | |
2501 | @subsection Why Use Keywords? | |
2502 | ||
2503 | Keywords are useful in contexts where a program or procedure wants to be | |
2504 | able to accept a large number of optional arguments without making its | |
2505 | interface unmanageable. | |
2506 | ||
2507 | To illustrate this, consider a hypothetical @code{make-window} | |
2508 | procedure, which creates a new window on the screen for drawing into | |
2509 | using some graphical toolkit. There are many parameters that the caller | |
2510 | might like to specify, but which could also be sensibly defaulted, for | |
2511 | example: | |
2512 | ||
2513 | @itemize @bullet | |
2514 | @item | |
2515 | colour depth -- Default: the colour depth for the screen | |
2516 | ||
2517 | @item | |
2518 | background colour -- Default: white | |
2519 | ||
2520 | @item | |
2521 | width -- Default: 600 | |
2522 | ||
2523 | @item | |
2524 | height -- Default: 400 | |
2525 | @end itemize | |
2526 | ||
2527 | If @code{make-window} did not use keywords, the caller would have to | |
2528 | pass in a value for each possible argument, remembering the correct | |
2529 | argument order and using a special value to indicate the default value | |
2530 | for that argument: | |
2531 | ||
2532 | @lisp | |
2533 | (make-window 'default ;; Colour depth | |
2534 | 'default ;; Background colour | |
2535 | 800 ;; Width | |
2536 | 100 ;; Height | |
2537 | @dots{}) ;; More make-window arguments | |
2538 | @end lisp | |
2539 | ||
2540 | With keywords, on the other hand, defaulted arguments are omitted, and | |
2541 | non-default arguments are clearly tagged by the appropriate keyword. As | |
2542 | a result, the invocation becomes much clearer: | |
2543 | ||
2544 | @lisp | |
2545 | (make-window #:width 800 #:height 100) | |
2546 | @end lisp | |
2547 | ||
2548 | On the other hand, for a simpler procedure with few arguments, the use | |
2549 | of keywords would be a hindrance rather than a help. The primitive | |
2550 | procedure @code{cons}, for example, would not be improved if it had to | |
2551 | be invoked as | |
2552 | ||
2553 | @lisp | |
2554 | (cons #:car x #:cdr y) | |
2555 | @end lisp | |
2556 | ||
2557 | So the decision whether to use keywords or not is purely pragmatic: use | |
2558 | them if they will clarify the procedure invocation at point of call. | |
2559 | ||
2560 | @node Coding With Keywords | |
2561 | @subsection Coding With Keywords | |
2562 | ||
2563 | If a procedure wants to support keywords, it should take a rest argument | |
2564 | and then use whatever means is convenient to extract keywords and their | |
2565 | corresponding arguments from the contents of that rest argument. | |
2566 | ||
2567 | The following example illustrates the principle: the code for | |
2568 | @code{make-window} uses a helper procedure called | |
2569 | @code{get-keyword-value} to extract individual keyword arguments from | |
2570 | the rest argument. | |
2571 | ||
2572 | @lisp | |
2573 | (define (get-keyword-value args keyword default) | |
2574 | (let ((kv (memq keyword args))) | |
2575 | (if (and kv (>= (length kv) 2)) | |
2576 | (cadr kv) | |
2577 | default))) | |
2578 | ||
2579 | (define (make-window . args) | |
2580 | (let ((depth (get-keyword-value args #:depth screen-depth)) | |
2581 | (bg (get-keyword-value args #:bg "white")) | |
2582 | (width (get-keyword-value args #:width 800)) | |
2583 | (height (get-keyword-value args #:height 100)) | |
2584 | @dots{}) | |
2585 | @dots{})) | |
2586 | @end lisp | |
2587 | ||
2588 | But you don't need to write @code{get-keyword-value}. The @code{(ice-9 | |
2589 | optargs)} module provides a set of powerful macros that you can use to | |
2590 | implement keyword-supporting procedures like this: | |
2591 | ||
2592 | @lisp | |
2593 | (use-modules (ice-9 optargs)) | |
2594 | ||
2595 | (define (make-window . args) | |
2596 | (let-keywords args #f ((depth screen-depth) | |
2597 | (bg "white") | |
2598 | (width 800) | |
2599 | (height 100)) | |
2600 | ...)) | |
2601 | @end lisp | |
2602 | ||
2603 | @noindent | |
2604 | Or, even more economically, like this: | |
2605 | ||
2606 | @lisp | |
2607 | (use-modules (ice-9 optargs)) | |
2608 | ||
2609 | (define* (make-window #:key (depth screen-depth) | |
2610 | (bg "white") | |
2611 | (width 800) | |
2612 | (height 100)) | |
2613 | ...) | |
2614 | @end lisp | |
2615 | ||
2616 | For further details on @code{let-keywords}, @code{define*} and other | |
2617 | facilities provided by the @code{(ice-9 optargs)} module, @ref{Optional | |
2618 | Arguments}. | |
2619 | ||
2620 | ||
2621 | @node Keyword Read Syntax | |
2622 | @subsection Keyword Read Syntax | |
2623 | ||
2624 | Guile, by default, only recognizes the keyword syntax specified by R5RS. | |
2625 | A token of the form @code{#:NAME}, where @code{NAME} has the same syntax | |
2626 | as a Scheme symbol, is the external representation of the keyword named | |
2627 | @code{NAME}. Keyword objects print using this syntax as well, so values | |
2628 | containing keyword objects can be read back into Guile. When used in an | |
2629 | expression, keywords are self-quoting objects. | |
2630 | ||
2631 | If the @code{keyword} read option is set to @code{'prefix}, Guile also | |
2632 | recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens | |
2633 | of the form @code{:NAME} are read as symbols, as required by R4RS. | |
2634 | ||
2635 | To enable and disable the alternative non-R4RS keyword syntax, you use | |
2636 | the @code{read-options} procedure documented in @ref{General option | |
2637 | interface} and @ref{Reader options}. | |
2638 | ||
2639 | @smalllisp | |
2640 | (read-set! keywords 'prefix) | |
2641 | ||
2642 | #:type | |
2643 | @result{} | |
2644 | #:type | |
2645 | ||
2646 | :type | |
2647 | @result{} | |
2648 | #:type | |
2649 | ||
2650 | (read-set! keywords #f) | |
2651 | ||
2652 | #:type | |
2653 | @result{} | |
2654 | #:type | |
2655 | ||
2656 | :type | |
2657 | @result{} | |
2658 | ERROR: In expression :type: | |
2659 | ERROR: Unbound variable: :type | |
2660 | ABORT: (unbound-variable) | |
2661 | @end smalllisp | |
2662 | ||
2663 | @node Keyword Primitives | |
2664 | @subsection Keyword Primitives | |
2665 | ||
2666 | Internally, a keyword is implemented as something like a tagged symbol, | |
2667 | where the tag identifies the keyword as being self-evaluating, and the | |
2668 | symbol, known as the keyword's @dfn{dash symbol} has the same name as | |
2669 | the keyword name but prefixed by a single dash. For example, the | |
2670 | keyword @code{#:name} has the corresponding dash symbol @code{-name}. | |
2671 | ||
2672 | Most keyword objects are constructed automatically by the reader when it | |
2673 | reads a token beginning with @code{#:}. However, if you need to | |
2674 | construct a keyword object programmatically, you can do so by calling | |
2675 | @code{make-keyword-from-dash-symbol} with the corresponding dash symbol | |
2676 | (as the reader does). The dash symbol for a keyword object can be | |
2677 | retrieved using the @code{keyword-dash-symbol} procedure. | |
2678 | ||
2679 | @c docstring begin (texi-doc-string "guile" "make-keyword-from-dash-symbol") | |
2680 | @deffn primitive make-keyword-from-dash-symbol symbol | |
2681 | Make a keyword object from a @var{symbol} that starts with a dash. | |
2682 | @end deffn | |
2683 | ||
2684 | @c docstring begin (texi-doc-string "guile" "keyword?") | |
2685 | @deffn primitive keyword? obj | |
2686 | Returns @code{#t} if the argument @var{obj} is a keyword, else @code{#f}. | |
2687 | @end deffn | |
2688 | ||
2689 | @c docstring begin (texi-doc-string "guile" "keyword-dash-symbol") | |
2690 | @deffn primitive keyword-dash-symbol keyword | |
2691 | Return the dash symbol for @var{keyword}. | |
2692 | This is the inverse of @code{make-keyword-from-dash-symbol}. | |
2693 | @end deffn | |
2694 | ||
2695 | ||
2696 | @node Pairs | |
2697 | @section Pairs | |
2698 | ||
2699 | @c docstring begin (texi-doc-string "guile" "cons") | |
2700 | @deffn primitive cons x y | |
2701 | Returns a newly allocated pair whose car is @var{x} and whose cdr is | |
2702 | @var{y}. The pair is guaranteed to be different (in the sense of | |
2703 | @code{eqv?}) from every previously existing object. | |
2704 | @end deffn | |
2705 | ||
2706 | @c docstring begin (texi-doc-string "guile" "pair?") | |
2707 | @deffn primitive pair? x | |
2708 | Returns @code{#t} if @var{x} is a pair; otherwise returns @code{#f}. | |
2709 | @end deffn | |
2710 | ||
2711 | @c docstring begin (texi-doc-string "guile" "set-car!") | |
2712 | @deffn primitive set-car! pair value | |
2713 | Stores @var{value} in the car field of @var{pair}. The value returned | |
2714 | by @code{set-car!} is unspecified. | |
2715 | @end deffn | |
2716 | ||
2717 | @c docstring begin (texi-doc-string "guile" "set-cdr!") | |
2718 | @deffn primitive set-cdr! pair value | |
2719 | Stores @var{value} in the cdr field of @var{pair}. The value returned | |
2720 | by @code{set-cdr!} is unspecified. | |
2721 | @end deffn | |
2722 | ||
2723 | ||
2724 | @node Lists | |
2725 | @section Lists | |
2726 | ||
2727 | @c docstring begin (texi-doc-string "guile" "list") | |
2728 | @deffn primitive list . objs | |
2729 | Return a list containing OBJS, the arguments to `list'. | |
2730 | @end deffn | |
2731 | ||
2732 | @c docstring begin (texi-doc-string "guile" "cons*") | |
2733 | @deffn primitive cons* arg . rest | |
2734 | @deffnx primitive list* arg . rest | |
2735 | Like `list', but the last arg provides the tail of the constructed list, | |
2736 | returning (cons ARG1 (cons ARG2 (cons ... ARGn))). | |
2737 | Requires at least one argument. If given one argument, that argument | |
2738 | is returned as result. | |
2739 | This function is called `list*' in some other Schemes and in Common LISP. | |
2740 | @end deffn | |
2741 | ||
2742 | @c docstring begin (texi-doc-string "guile" "list?") | |
2743 | @deffn primitive list? x | |
2744 | Return #t iff X is a proper list, else #f. | |
2745 | @end deffn | |
2746 | ||
2747 | @c docstring begin (texi-doc-string "guile" "null?") | |
2748 | @deffn primitive null? x | |
2749 | Return #t iff X is the empty list, else #f. | |
2750 | @end deffn | |
2751 | ||
2752 | @c docstring begin (texi-doc-string "guile" "length") | |
2753 | @deffn primitive length lst | |
2754 | Return the number of elements in list LST. | |
2755 | @end deffn | |
2756 | ||
2757 | @c docstring begin (texi-doc-string "guile" "append") | |
2758 | @deffn primitive append . args | |
2759 | Returns a list consisting of the elements of the first LIST | |
2760 | followed by the elements of the other LISTs. | |
2761 | ||
2762 | @example | |
2763 | (append '(x) '(y)) => (x y) | |
2764 | (append '(a) '(b c d)) => (a b c d) | |
2765 | (append '(a (b)) '((c))) => (a (b) (c)) | |
2766 | @end example | |
2767 | ||
2768 | The resulting list is always newly allocated, except that it shares | |
2769 | structure with the last LIST argument. The last argument may | |
2770 | actually be any object; an improper list results if the last | |
2771 | argument is not a proper list. | |
2772 | ||
2773 | @example | |
2774 | (append '(a b) '(c . d)) => (a b c . d) | |
2775 | (append '() 'a) => a | |
2776 | @end example | |
2777 | @end deffn | |
2778 | ||
2779 | @c ARGFIXME args ? | |
2780 | @c docstring begin (texi-doc-string "guile" "append!") | |
2781 | @deffn primitive append! . args | |
2782 | A destructive version of @code{append} (@pxref{Pairs and Lists,,,r4rs, | |
2783 | The Revised^4 Report on Scheme}). The cdr field of each list's final | |
2784 | pair is changed to point to the head of the next list, so no consing is | |
2785 | performed. Return a pointer to the mutated list. | |
2786 | @end deffn | |
2787 | ||
2788 | @c docstring begin (texi-doc-string "guile" "last-pair") | |
2789 | @deffn primitive last-pair lst | |
2790 | Return a pointer to the last pair in @var{lst}, signalling an error if | |
2791 | @var{lst} is circular. | |
2792 | @end deffn | |
2793 | ||
2794 | @c docstring begin (texi-doc-string "guile" "reverse") | |
2795 | @deffn primitive reverse lst | |
2796 | Return a new list that contains the elements of LST but in reverse order. | |
2797 | @end deffn | |
2798 | ||
2799 | @c NJFIXME explain new_tail | |
2800 | @c docstring begin (texi-doc-string "guile" "reverse!") | |
2801 | @deffn primitive reverse! lst [new_tail] | |
2802 | A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r4rs, | |
2803 | The Revised^4 Report on Scheme}). The cdr of each cell in @var{lst} is | |
2804 | modified to point to the previous list element. Return a pointer to the | |
2805 | head of the reversed list. | |
2806 | ||
2807 | Caveat: because the list is modified in place, the tail of the original | |
2808 | list now becomes its head, and the head of the original list now becomes | |
2809 | the tail. Therefore, the @var{lst} symbol to which the head of the | |
2810 | original list was bound now points to the tail. To ensure that the head | |
2811 | of the modified list is not lost, it is wise to save the return value of | |
2812 | @code{reverse!} | |
2813 | @end deffn | |
2814 | ||
2815 | @c docstring begin (texi-doc-string "guile" "list-ref") | |
2816 | @deffn primitive list-ref list k | |
2817 | Return the Kth element from LIST. | |
2818 | @end deffn | |
2819 | ||
2820 | @c docstring begin (texi-doc-string "guile" "list-set!") | |
2821 | @deffn primitive list-set! list k val | |
2822 | Set the @var{k}th element of @var{list} to @var{val}. | |
2823 | @end deffn | |
2824 | ||
2825 | @c docstring begin (texi-doc-string "guile" "list-tail") | |
2826 | @c docstring begin (texi-doc-string "guile" "list-cdr-ref") | |
2827 | @deffn primitive list-tail lst k | |
2828 | @deffnx primitive list-cdr-ref lst k | |
2829 | Return the "tail" of @var{lst} beginning with its @var{k}th element. | |
2830 | The first element of the list is considered to be element 0. | |
2831 | ||
2832 | @code{list-tail} and @code{list-cdr-ref} are identical. It may help to | |
2833 | think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list, | |
2834 | or returning the results of cdring @var{k} times down @var{lst}. | |
2835 | @end deffn | |
2836 | ||
2837 | @c docstring begin (texi-doc-string "guile" "list-cdr-set!") | |
2838 | @deffn primitive list-cdr-set! list k val | |
2839 | Set the @var{k}th cdr of @var{list} to @var{val}. | |
2840 | @end deffn | |
2841 | ||
2842 | @c docstring begin (texi-doc-string "guile" "list-head") | |
2843 | @deffn primitive list-head lst k | |
2844 | Copy the first @var{k} elements from @var{lst} into a new list, and | |
2845 | return it. | |
2846 | @end deffn | |
2847 | ||
2848 | @c docstring begin (texi-doc-string "guile" "list-copy") | |
2849 | @deffn primitive list-copy lst | |
2850 | Return a (newly-created) copy of @var{lst}. | |
2851 | @end deffn | |
2852 | ||
2853 | @c docstring begin (texi-doc-string "guile" "memq") | |
2854 | @deffn primitive memq x lst | |
2855 | Return the first sublist of LST whose car is `eq?' to X | |
2856 | where the sublists of LST are the non-empty lists returned | |
2857 | by `(list-tail LST K)' for K less than the length of LST. If | |
2858 | X does not occur in LST, then `#f' (not the empty list) is | |
2859 | returned. | |
2860 | @end deffn | |
2861 | ||
2862 | @c docstring begin (texi-doc-string "guile" "memv") | |
2863 | @deffn primitive memv x lst | |
2864 | Return the first sublist of LST whose car is `eqv?' to X | |
2865 | where the sublists of LST are the non-empty lists returned | |
2866 | by `(list-tail LST K)' for K less than the length of LST. If | |
2867 | X does not occur in LST, then `#f' (not the empty list) is | |
2868 | returned. | |
2869 | @end deffn | |
2870 | ||
2871 | @c docstring begin (texi-doc-string "guile" "member") | |
2872 | @deffn primitive member x lst | |
2873 | Return the first sublist of LST whose car is `equal?' to X | |
2874 | where the sublists of LST are the non-empty lists returned | |
2875 | by `(list-tail LST K)' for K less than the length of LST. If | |
2876 | X does not occur in LST, then `#f' (not the empty list) is | |
2877 | returned. | |
2878 | @end deffn | |
2879 | ||
2880 | @c docstring begin (texi-doc-string "guile" "delq") | |
2881 | @deffn primitive delq item lst | |
2882 | Return a newly-created copy of @var{lst} with elements `eq?' to @var{item} removed. | |
2883 | This procedure mirrors @code{memq}: | |
2884 | @code{delq} compares elements of @var{lst} against @var{item} with | |
2885 | @code{eq?}. | |
2886 | @end deffn | |
2887 | ||
2888 | @c docstring begin (texi-doc-string "guile" "delv") | |
2889 | @deffn primitive delv item lst | |
2890 | Return a newly-created copy of @var{lst} with elements `eqv?' to @var{item} removed. | |
2891 | This procedure mirrors @code{memv}: | |
2892 | @code{delv} compares elements of @var{lst} against @var{item} with | |
2893 | @code{eqv?}. | |
2894 | @end deffn | |
2895 | ||
2896 | @c docstring begin (texi-doc-string "guile" "delete") | |
2897 | @deffn primitive delete item lst | |
2898 | Return a newly-created copy of @var{lst} with elements `equal?' to @var{item} removed. | |
2899 | This procedure mirrors @code{member}: | |
2900 | @code{delete} compares elements of @var{lst} against @var{item} with | |
2901 | @code{equal?}. | |
2902 | @end deffn | |
2903 | ||
2904 | @c docstring begin (texi-doc-string "guile" "delq!") | |
2905 | @c docstring begin (texi-doc-string "guile" "delv!") | |
2906 | @c docstring begin (texi-doc-string "guile" "delete!") | |
2907 | @deffn primitive delq! item lst | |
2908 | @deffnx primitive delv! item lst | |
2909 | @deffnx primitive delete! item lst | |
2910 | These procedures are destructive versions of @code{delq}, @code{delv} | |
2911 | and @code{delete}: they modify the pointers in the existing @var{lst} | |
2912 | rather than creating a new list. Caveat evaluator: Like other | |
2913 | destructive list functions, these functions cannot modify the binding of | |
2914 | @var{lst}, and so cannot be used to delete the first element of | |
2915 | @var{lst} destructively. | |
2916 | @end deffn | |
2917 | ||
2918 | @c docstring begin (texi-doc-string "guile" "delq1!") | |
2919 | @deffn primitive delq1! item lst | |
2920 | Like `delq!', but only deletes the first occurrence of ITEM from LST. | |
2921 | Tests for equality using `eq?'. See also `delv1!' and `delete1!'. | |
2922 | @end deffn | |
2923 | ||
2924 | @c docstring begin (texi-doc-string "guile" "delv1!") | |
2925 | @deffn primitive delv1! item lst | |
2926 | Like `delv!', but only deletes the first occurrence of ITEM from LST. | |
2927 | Tests for equality using `eqv?'. See also `delq1!' and `delete1!'. | |
2928 | @end deffn | |
2929 | ||
2930 | @c docstring begin (texi-doc-string "guile" "delete1!") | |
2931 | @deffn primitive delete1! item lst | |
2932 | Like `delete!', but only deletes the first occurrence of ITEM from LST. | |
2933 | Tests for equality using `equal?'. See also `delq1!' and `delv1!'. | |
2934 | @end deffn | |
2935 | ||
2936 | [FIXME: is there any reason to have the `sloppy' functions available at | |
2937 | high level at all? Maybe these docs should be relegated to a "Guile | |
2938 | Internals" node or something. -twp] | |
2939 | ||
2940 | @c docstring begin (texi-doc-string "guile" "sloppy-memq") | |
2941 | @deffn primitive sloppy-memq x lst | |
2942 | This procedure behaves like @code{memq}, but does no type or error checking. | |
2943 | Its use is recommended only in writing Guile internals, | |
2944 | not for high-level Scheme programs. | |
2945 | @end deffn | |
2946 | ||
2947 | @c docstring begin (texi-doc-string "guile" "sloppy-memv") | |
2948 | @deffn primitive sloppy-memv x lst | |
2949 | This procedure behaves like @code{memv}, but does no type or error checking. | |
2950 | Its use is recommended only in writing Guile internals, | |
2951 | not for high-level Scheme programs. | |
2952 | @end deffn | |
2953 | ||
2954 | @c docstring begin (texi-doc-string "guile" "sloppy-member") | |
2955 | @deffn primitive sloppy-member x lst | |
2956 | This procedure behaves like @code{member}, but does no type or error checking. | |
2957 | Its use is recommended only in writing Guile internals, | |
2958 | not for high-level Scheme programs. | |
2959 | @end deffn | |
2960 | ||
2961 | @c begin (texi-doc-string "guile" "map") | |
2962 | @c docstring begin (texi-doc-string "guile" "map-in-order") | |
2963 | @deffn primitive map proc arg1 . args | |
2964 | @deffnx primitive map-in-order proc arg1 . args | |
2965 | @end deffn | |
2966 | ||
2967 | @c begin (texi-doc-string "guile" "for-each") | |
2968 | @deffn primitive for-each proc arg1 . args | |
2969 | @end deffn | |
2970 | ||
2971 | ||
2972 | @node Records | |
2973 | @section Records | |
2974 | ||
2975 | [FIXME: this is pasted in from Tom Lord's original guile.texi and should | |
2976 | be reviewed] | |
2977 | ||
2978 | A @dfn{record type} is a first class object representing a user-defined | |
2979 | data type. A @dfn{record} is an instance of a record type. | |
2980 | ||
2981 | @deffn procedure record? obj | |
2982 | Returns @code{#t} if @var{obj} is a record of any type and @code{#f} | |
2983 | otherwise. | |
2984 | ||
2985 | Note that @code{record?} may be true of any Scheme value; there is no | |
2986 | promise that records are disjoint with other Scheme types. | |
2987 | @end deffn | |
2988 | ||
2989 | @deffn procedure make-record-type type-name field-names | |
2990 | Returns a @dfn{record-type descriptor}, a value representing a new data | |
2991 | type disjoint from all others. The @var{type-name} argument must be a | |
2992 | string, but is only used for debugging purposes (such as the printed | |
2993 | representation of a record of the new type). The @var{field-names} | |
2994 | argument is a list of symbols naming the @dfn{fields} of a record of the | |
2995 | new type. It is an error if the list contains any duplicates. It is | |
2996 | unspecified how record-type descriptors are represented.@refill | |
2997 | @end deffn | |
2998 | ||
2999 | @deffn procedure record-constructor rtd [field-names] | |
3000 | Returns a procedure for constructing new members of the type represented | |
3001 | by @var{rtd}. The returned procedure accepts exactly as many arguments | |
3002 | as there are symbols in the given list, @var{field-names}; these are | |
3003 | used, in order, as the initial values of those fields in a new record, | |
3004 | which is returned by the constructor procedure. The values of any | |
3005 | fields not named in that list are unspecified. The @var{field-names} | |
3006 | argument defaults to the list of field names in the call to | |
3007 | @code{make-record-type} that created the type represented by @var{rtd}; | |
3008 | if the @var{field-names} argument is provided, it is an error if it | |
3009 | contains any duplicates or any symbols not in the default list.@refill | |
3010 | @end deffn | |
3011 | ||
3012 | @deffn procedure record-predicate rtd | |
3013 | Returns a procedure for testing membership in the type represented by | |
3014 | @var{rtd}. The returned procedure accepts exactly one argument and | |
3015 | returns a true value if the argument is a member of the indicated record | |
3016 | type; it returns a false value otherwise.@refill | |
3017 | @end deffn | |
3018 | ||
3019 | @deffn procedure record-accessor rtd field-name | |
3020 | Returns a procedure for reading the value of a particular field of a | |
3021 | member of the type represented by @var{rtd}. The returned procedure | |
3022 | accepts exactly one argument which must be a record of the appropriate | |
3023 | type; it returns the current value of the field named by the symbol | |
3024 | @var{field-name} in that record. The symbol @var{field-name} must be a | |
3025 | member of the list of field-names in the call to @code{make-record-type} | |
3026 | that created the type represented by @var{rtd}.@refill | |
3027 | @end deffn | |
3028 | ||
3029 | @deffn procedure record-modifier rtd field-name | |
3030 | Returns a procedure for writing the value of a particular field of a | |
3031 | member of the type represented by @var{rtd}. The returned procedure | |
3032 | accepts exactly two arguments: first, a record of the appropriate type, | |
3033 | and second, an arbitrary Scheme value; it modifies the field named by | |
3034 | the symbol @var{field-name} in that record to contain the given value. | |
3035 | The returned value of the modifier procedure is unspecified. The symbol | |
3036 | @var{field-name} must be a member of the list of field-names in the call | |
3037 | to @code{make-record-type} that created the type represented by | |
3038 | @var{rtd}.@refill | |
3039 | @end deffn | |
3040 | ||
3041 | @deffn procedure record-type-descriptor record | |
3042 | Returns a record-type descriptor representing the type of the given | |
3043 | record. That is, for example, if the returned descriptor were passed to | |
3044 | @code{record-predicate}, the resulting predicate would return a true | |
3045 | value when passed the given record. Note that it is not necessarily the | |
3046 | case that the returned descriptor is the one that was passed to | |
3047 | @code{record-constructor} in the call that created the constructor | |
3048 | procedure that created the given record.@refill | |
3049 | @end deffn | |
3050 | ||
3051 | @deffn procedure record-type-name rtd | |
3052 | Returns the type-name associated with the type represented by rtd. The | |
3053 | returned value is @code{eqv?} to the @var{type-name} argument given in | |
3054 | the call to @code{make-record-type} that created the type represented by | |
3055 | @var{rtd}.@refill | |
3056 | @end deffn | |
3057 | ||
3058 | @deffn procedure record-type-fields rtd | |
3059 | Returns a list of the symbols naming the fields in members of the type | |
3060 | represented by @var{rtd}. The returned value is @code{equal?} to the | |
3061 | field-names argument given in the call to @code{make-record-type} that | |
3062 | created the type represented by @var{rtd}.@refill | |
3063 | @end deffn | |
3064 | ||
3065 | ||
3066 | @node Structures | |
3067 | @section Structures | |
3068 | ||
3069 | [FIXME: this is pasted in from Tom Lord's original guile.texi and should | |
3070 | be reviewed] | |
3071 | ||
3072 | A @dfn{structure type} is a first class user-defined data type. A | |
3073 | @dfn{structure} is an instance of a structure type. A structure type is | |
3074 | itself a structure. | |
3075 | ||
3076 | Structures are less abstract and more general than traditional records. | |
3077 | In fact, in Guile Scheme, records are implemented using structures. | |
3078 | ||
3079 | @menu | |
3080 | * Structure Concepts:: The structure of Structures | |
3081 | * Structure Layout:: Defining the layout of structure types | |
3082 | * Structure Basics:: make-, -ref and -set! procedures for structs | |
3083 | * Vtables:: Accessing type-specific data | |
3084 | @end menu | |
3085 | ||
3086 | @node Structure Concepts | |
3087 | @subsection Structure Concepts | |
3088 | ||
3089 | A structure object consists of a handle, structure data, and a vtable. | |
3090 | The handle is a Scheme value which points to both the vtable and the | |
3091 | structure's data. Structure data is a dynamically allocated region of | |
3092 | memory, private to the structure, divided up into typed fields. A | |
3093 | vtable is another structure used to hold type-specific data. Multiple | |
3094 | structures can share a common vtable. | |
3095 | ||
3096 | Three concepts are key to understanding structures. | |
3097 | ||
3098 | @itemize @bullet{} | |
3099 | @item @dfn{layout specifications} | |
3100 | ||
3101 | Layout specifications determine how memory allocated to structures is | |
3102 | divided up into fields. Programmers must write a layout specification | |
3103 | whenever a new type of structure is defined. | |
3104 | ||
3105 | @item @dfn{structural accessors} | |
3106 | ||
3107 | Structure access is by field number. There is only one set of | |
3108 | accessors common to all structure objects. | |
3109 | ||
3110 | @item @dfn{vtables} | |
3111 | ||
3112 | Vtables, themselves structures, are first class representations of | |
3113 | disjoint sub-types of structures in general. In most cases, when a | |
3114 | new structure is created, programmers must specifiy a vtable for the | |
3115 | new structure. Each vtable has a field describing the layout of its | |
3116 | instances. Vtables can have additional, user-defined fields as well. | |
3117 | @end itemize | |
3118 | ||
3119 | ||
3120 | ||
3121 | @node Structure Layout | |
3122 | @subsection Structure Layout | |
3123 | ||
3124 | When a structure is created, a region of memory is allocated to hold its | |
3125 | state. The @dfn{layout} of the structure's type determines how that | |
3126 | memory is divided into fields. | |
3127 | ||
3128 | Each field has a specified type. There are only three types allowed, each | |
3129 | corresponding to a one letter code. The allowed types are: | |
3130 | ||
3131 | @itemize @bullet{} | |
3132 | @item 'u' -- unprotected | |
3133 | ||
3134 | The field holds binary data that is not GC protected. | |
3135 | ||
3136 | @item 'p' -- protected | |
3137 | ||
3138 | The field holds a Scheme value and is GC protected. | |
3139 | ||
3140 | @item 's' -- self | |
3141 | ||
3142 | The field holds a Scheme value and is GC protected. When a structure is | |
3143 | created with this type of field, the field is initialized to refer to | |
3144 | the structure's own handle. This kind of field is mainly useful when | |
3145 | mixing Scheme and C code in which the C code may need to compute a | |
3146 | structure's handle given only the address of its malloced data. | |
3147 | @end itemize | |
3148 | ||
3149 | ||
3150 | Each field also has an associated access protection. There are only | |
3151 | three kinds of protection, each corresponding to a one letter code. | |
3152 | The allowed protections are: | |
3153 | ||
3154 | @itemize @bullet{} | |
3155 | @item 'w' -- writable | |
3156 | ||
3157 | The field can be read and written. | |
3158 | ||
3159 | @item 'r' -- readable | |
3160 | ||
3161 | The field can be read, but not written. | |
3162 | ||
3163 | @item 'o' -- opaque | |
3164 | ||
3165 | The field can be neither read nor written. This kind | |
3166 | of protection is for fields useful only to built-in routines. | |
3167 | @end itemize | |
3168 | ||
3169 | A layout specification is described by stringing together pairs | |
3170 | of letters: one to specify a field type and one to specify a field | |
3171 | protection. For example, a traditional cons pair type object could | |
3172 | be described as: | |
3173 | ||
3174 | @example | |
3175 | ; cons pairs have two writable fields of Scheme data | |
3176 | "pwpw" | |
3177 | @end example | |
3178 | ||
3179 | A pair object in which the first field is held constant could be: | |
3180 | ||
3181 | @example | |
3182 | "prpw" | |
3183 | @end example | |
3184 | ||
3185 | Binary fields, (fields of type "u"), hold one @emph{word} each. The | |
3186 | size of a word is a machine dependent value defined to be equal to the | |
3187 | value of the C expression: @code{sizeof (long)}. | |
3188 | ||
3189 | The last field of a structure layout may specify a tail array. | |
3190 | A tail array is indicated by capitalizing the field's protection | |
3191 | code ('W', 'R' or 'O'). A tail-array field is replaced by | |
3192 | a read-only binary data field containing an array size. The array | |
3193 | size is determined at the time the structure is created. It is followed | |
3194 | by a corresponding number of fields of the type specified for the | |
3195 | tail array. For example, a conventional Scheme vector can be | |
3196 | described as: | |
3197 | ||
3198 | @example | |
3199 | ; A vector is an arbitrary number of writable fields holding Scheme | |
3200 | ; values: | |
3201 | "pW" | |
3202 | @end example | |
3203 | ||
3204 | In the above example, field 0 contains the size of the vector and | |
3205 | fields beginning at 1 contain the vector elements. | |
3206 | ||
3207 | A kind of tagged vector (a constant tag followed by conventioal | |
3208 | vector elements) might be: | |
3209 | ||
3210 | @example | |
3211 | "prpW" | |
3212 | @end example | |
3213 | ||
3214 | ||
3215 | Structure layouts are represented by specially interned symbols whose | |
3216 | name is a string of type and protection codes. To create a new | |
3217 | structure layout, use this procedure: | |
3218 | ||
3219 | @c docstring begin (texi-doc-string "guile" "make-struct-layout") | |
3220 | @deffn primitive make-struct-layout fields | |
3221 | Return a new structure layout object. | |
3222 | ||
3223 | @var{fields} must be a string made up of pairs of characters | |
3224 | strung together. The first character of each pair describes a field | |
3225 | type, the second a field protection. Allowed types are 'p' for | |
3226 | GC-protected Scheme data, 'u' for unprotected binary data, and 's' for | |
3227 | a field that points to the structure itself. Allowed protections | |
3228 | are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque | |
3229 | fields. The last field protection specification may be capitalized to | |
3230 | indicate that the field is a tail-array. | |
3231 | @end deffn | |
3232 | ||
3233 | ||
3234 | ||
3235 | @node Structure Basics | |
3236 | @subsection Structure Basics | |
3237 | ||
3238 | This section describes the basic procedures for creating and accessing | |
3239 | structures. | |
3240 | ||
3241 | @c docstring begin (texi-doc-string "guile" "make-struct") | |
3242 | @deffn primitive make-struct vtable tail_array_size . init | |
3243 | Create a new structure. | |
3244 | ||
3245 | @var{type} must be a vtable structure (@pxref{Vtables}). | |
3246 | ||
3247 | @var{tail-elts} must be a non-negative integer. If the layout | |
3248 | specification indicated by @var{type} includes a tail-array, | |
3249 | this is the number of elements allocated to that array. | |
3250 | ||
3251 | The @var{init1}, @dots{} are optional arguments describing how | |
3252 | successive fields of the structure should be initialized. Only fields | |
3253 | with protection 'r' or 'w' can be initialized, except for fields of | |
3254 | type 's', which are automatically initialized to point to the new | |
3255 | structure itself; fields with protection 'o' can not be initialized by | |
3256 | Scheme programs. | |
3257 | ||
3258 | If fewer optional arguments than initializable fields are supplied, | |
3259 | fields of type 'p' get default value #f while fields of type 'u' are | |
3260 | initialized to 0. | |
3261 | ||
3262 | Structs are currently the basic representation for record-like data | |
3263 | structures in Guile. The plan is to eventually replace them with a | |
3264 | new representation which will at the same time be easier to use and | |
3265 | more powerful. | |
3266 | ||
3267 | For more information, see the documentation for @code{make-vtable-vtable}. | |
3268 | @end deffn | |
3269 | ||
3270 | @c docstring begin (texi-doc-string "guile" "struct?") | |
3271 | @deffn primitive struct? x | |
3272 | Return #t iff @var{obj} is a structure object, else #f. | |
3273 | @end deffn | |
3274 | ||
3275 | ||
3276 | @c docstring begin (texi-doc-string "guile" "struct-ref") | |
3277 | @c docstring begin (texi-doc-string "guile" "struct-set!") | |
3278 | @deffn primitive struct-ref handle pos | |
3279 | @deffnx primitive struct-set! struct n value | |
3280 | Access (or modify) the @var{n}th field of @var{struct}. | |
3281 | ||
3282 | If the field is of type 'p', then it can be set to an arbitrary value. | |
3283 | ||
3284 | If the field is of type 'u', then it can only be set to a non-negative | |
3285 | integer value small enough to fit in one machine word. | |
3286 | @end deffn | |
3287 | ||
3288 | ||
3289 | ||
3290 | @node Vtables | |
3291 | @subsection Vtables | |
3292 | ||
3293 | Vtables are structures that are used to represent structure types. Each | |
3294 | vtable contains a layout specification in field | |
3295 | @code{vtable-index-layout} -- instances of the type are laid out | |
3296 | according to that specification. Vtables contain additional fields | |
3297 | which are used only internally to libguile. The variable | |
3298 | @code{vtable-offset-user} is bound to a field number. Vtable fields | |
3299 | at that position or greater are user definable. | |
3300 | ||
3301 | @c docstring begin (texi-doc-string "guile" "struct-vtable") | |
3302 | @deffn primitive struct-vtable handle | |
3303 | Return the vtable structure that describes the type of @var{struct}. | |
3304 | @end deffn | |
3305 | ||
3306 | @c docstring begin (texi-doc-string "guile" "struct-vtable?") | |
3307 | @deffn primitive struct-vtable? x | |
3308 | Return #t iff obj is a vtable structure. | |
3309 | @end deffn | |
3310 | ||
3311 | If you have a vtable structure, @code{V}, you can create an instance of | |
3312 | the type it describes by using @code{(make-struct V ...)}. But where | |
3313 | does @code{V} itself come from? One possibility is that @code{V} is an | |
3314 | instance of a user-defined vtable type, @code{V'}, so that @code{V} is | |
3315 | created by using @code{(make-struct V' ...)}. Another possibility is | |
3316 | that @code{V} is an instance of the type it itself describes. Vtable | |
3317 | structures of the second sort are created by this procedure: | |
3318 | ||
3319 | @c docstring begin (texi-doc-string "guile" "make-vtable-vtable") | |
3320 | @deffn primitive make-vtable-vtable user_fields tail_array_size . init | |
3321 | Return a new, self-describing vtable structure. | |
3322 | ||
3323 | @var{user-fields} is a string describing user defined fields of the | |
3324 | vtable beginning at index @code{vtable-offset-user} | |
3325 | (see @code{make-struct-layout}). | |
3326 | ||
3327 | @var{tail-size} specifies the size of the tail-array (if any) of | |
3328 | this vtable. | |
3329 | ||
3330 | @var{init1}, @dots{} are the optional initializers for the fields of | |
3331 | the vtable. | |
3332 | ||
3333 | Vtables have one initializable system field---the struct printer. | |
3334 | This field comes before the user fields in the initializers passed | |
3335 | to @code{make-vtable-vtable} and @code{make-struct}, and thus works as | |
3336 | a third optional argument to @code{make-vtable-vtable} and a fourth to | |
3337 | @code{make-struct} when creating vtables: | |
3338 | ||
3339 | If the value is a procedure, it will be called instead of the standard | |
3340 | printer whenever a struct described by this vtable is printed. | |
3341 | The procedure will be called with arguments STRUCT and PORT. | |
3342 | ||
3343 | The structure of a struct is described by a vtable, so the vtable is | |
3344 | in essence the type of the struct. The vtable is itself a struct with | |
3345 | a vtable. This could go on forever if it weren't for the | |
3346 | vtable-vtables which are self-describing vtables, and thus terminate | |
3347 | the chain. | |
3348 | ||
3349 | There are several potential ways of using structs, but the standard | |
3350 | one is to use three kinds of structs, together building up a type | |
3351 | sub-system: one vtable-vtable working as the root and one or several | |
3352 | "types", each with a set of "instances". (The vtable-vtable should be | |
3353 | compared to the class <class> which is the class of itself.) | |
3354 | ||
3355 | @example | |
3356 | (define ball-root (make-vtable-vtable "pr" 0)) | |
3357 | ||
3358 | (define (make-ball-type ball-color) | |
3359 | (make-struct ball-root 0 | |
3360 | (make-struct-layout "pw") | |
3361 | (lambda (ball port) | |
3362 | (format port "#<a ~A ball owned by ~A>" | |
3363 | (color ball) | |
3364 | (owner ball))) | |
3365 | ball-color)) | |
3366 | (define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user)) | |
3367 | (define (owner ball) (struct-ref ball 0)) | |
3368 | ||
3369 | (define red (make-ball-type 'red)) | |
3370 | (define green (make-ball-type 'green)) | |
3371 | ||
3372 | (define (make-ball type owner) (make-struct type 0 owner)) | |
3373 | ||
3374 | (define ball (make-ball green 'Nisse)) | |
3375 | ball @result{} #<a green ball owned by Nisse> | |
3376 | @end example | |
3377 | @end deffn | |
3378 | ||
3379 | @c docstring begin (texi-doc-string "guile" "struct-vtable-name") | |
3380 | @deffn primitive struct-vtable-name vtable | |
3381 | Return the name of the vtable @var{vtable}. | |
3382 | @end deffn | |
3383 | ||
3384 | @c docstring begin (texi-doc-string "guile" "set-struct-vtable-name!") | |
3385 | @deffn primitive set-struct-vtable-name! vtable name | |
3386 | Set the name of the vtable @var{vtable} to @var{name}. | |
3387 | @end deffn | |
3388 | ||
3389 | @c docstring begin (texi-doc-string "guile" "struct-vtable-tag") | |
3390 | @deffn primitive struct-vtable-tag handle | |
3391 | Return the vtable tag of the structure @var{handle}. | |
3392 | @end deffn | |
3393 | ||
3394 | ||
3395 | @node Arrays | |
3396 | @section Arrays | |
3397 | ||
3398 | @menu | |
3399 | * Conventional Arrays:: Arrays with arbitrary data. | |
3400 | * Array Mapping:: Applying a procedure to the contents of an array. | |
3401 | * Uniform Arrays:: Arrays with data of a single type. | |
3402 | * Bit Vectors:: Vectors of bits. | |
3403 | @end menu | |
3404 | ||
3405 | @node Conventional Arrays | |
3406 | @subsection Conventional Arrays | |
3407 | ||
3408 | @dfn{Conventional arrays} are a collection of cells organised into an | |
3409 | arbitrary number of dimensions. Each cell can hold any kind of Scheme | |
3410 | value and can be accessed in constant time by supplying an index for | |
3411 | each dimension. This contrasts with uniform arrays, which use memory | |
3412 | more efficiently but can hold data of only a single type, and lists | |
3413 | where inserting and deleting cells is more efficient, but more time | |
3414 | is usually required to access a particular cell. | |
3415 | ||
3416 | A conventional array is displayed as @code{#} followed by the @dfn{rank} | |
3417 | (number of dimensions) followed by the cells, organised into dimensions | |
3418 | using parentheses. The nesting depth of the parentheses is equal to | |
3419 | the rank. | |
3420 | ||
3421 | When an array is created, the number of dimensions and range of each | |
3422 | dimension must be specified, e.g., to create a 2x3 array with a | |
3423 | zero-based index: | |
3424 | ||
3425 | @example | |
3426 | (make-array 'ho 2 3) @result{} | |
3427 | #2((ho ho ho) (ho ho ho)) | |
3428 | @end example | |
3429 | ||
3430 | The range of each dimension can also be given explicitly, e.g., another | |
3431 | way to create the same array: | |
3432 | ||
3433 | @example | |
3434 | (make-array 'ho '(0 1) '(0 2)) @result{} | |
3435 | #2((ho ho ho) (ho ho ho)) | |
3436 | @end example | |
3437 | ||
3438 | A conventional array with one dimension based at zero is identical to | |
3439 | a vector: | |
3440 | ||
3441 | @example | |
3442 | (make-array 'ho 3) @result{} | |
3443 | #(ho ho ho) | |
3444 | @end example | |
3445 | ||
3446 | The following procedures can be used with conventional arrays (or vectors). | |
3447 | ||
3448 | @c docstring begin (texi-doc-string "guile" "array?") | |
3449 | @deffn primitive array? v [prot] | |
3450 | Returns @code{#t} if the @var{obj} is an array, and @code{#f} if not. | |
3451 | ||
3452 | The @var{prototype} argument is used with uniform arrays and is described | |
3453 | elsewhere. | |
3454 | @end deffn | |
3455 | ||
3456 | @deffn procedure make-array initial-value bound1 bound2 @dots{} | |
3457 | Creates and returns an array that has as many dimensions as there are | |
3458 | @var{bound}s and fills it with @var{initial-value}. | |
3459 | @end deffn | |
3460 | ||
3461 | @c array-ref's type is `compiled-closure'. There's some weird stuff | |
3462 | @c going on in array.c, too. Let's call it a primitive. -twp | |
3463 | ||
3464 | @c docstring begin (texi-doc-string "guile" "uniform-vector-ref") | |
3465 | @c docstring begin (texi-doc-string "guile" "array-ref") | |
3466 | @deffn primitive uniform-vector-ref v args | |
3467 | @deffnx primitive array-ref v . args | |
3468 | Returns the element at the @code{(index1, index2)} element in @var{array}. | |
3469 | @end deffn | |
3470 | ||
3471 | @c docstring begin (texi-doc-string "guile" "array-in-bounds?") | |
3472 | @deffn primitive array-in-bounds? v . args | |
3473 | Returns @code{#t} if its arguments would be acceptable to array-ref. | |
3474 | @end deffn | |
3475 | ||
3476 | @c docstring begin (texi-doc-string "guile" "array-set!") | |
3477 | @c docstring begin (texi-doc-string "guile" "uniform-array-set1!") | |
3478 | @deffn primitive array-set! v obj . args | |
3479 | @deffnx primitive uniform-array-set1! v obj args | |
3480 | Sets the element at the @code{(index1, index2)} element in @var{array} to | |
3481 | @var{new-value}. The value returned by array-set! is unspecified. | |
3482 | @end deffn | |
3483 | ||
3484 | @c docstring begin (texi-doc-string "guile" "make-shared-array") | |
3485 | @deffn primitive make-shared-array oldra mapfunc . dims | |
3486 | @code{make-shared-array} can be used to create shared subarrays of other | |
3487 | arrays. The @var{mapper} is a function that translates coordinates in | |
3488 | the new array into coordinates in the old array. A @var{mapper} must be | |
3489 | linear, and its range must stay within the bounds of the old array, but | |
3490 | it can be otherwise arbitrary. A simple example: | |
3491 | @example | |
3492 | (define fred (make-array #f 8 8)) | |
3493 | (define freds-diagonal | |
3494 | (make-shared-array fred (lambda (i) (list i i)) 8)) | |
3495 | (array-set! freds-diagonal 'foo 3) | |
3496 | (array-ref fred 3 3) @result{} foo | |
3497 | (define freds-center | |
3498 | (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2)) | |
3499 | (array-ref freds-center 0 0) @result{} foo | |
3500 | @end example | |
3501 | @end deffn | |
3502 | ||
3503 | @c docstring begin (texi-doc-string "guile" "shared-array-increments") | |
3504 | @deffn primitive shared-array-increments ra | |
3505 | For each dimension, return the distance between elements in the root vector. | |
3506 | @end deffn | |
3507 | ||
3508 | @c docstring begin (texi-doc-string "guile" "shared-array-offset") | |
3509 | @deffn primitive shared-array-offset ra | |
3510 | Return the root vector index of the first element in the array. | |
3511 | @end deffn | |
3512 | ||
3513 | @c docstring begin (texi-doc-string "guile" "shared-array-root") | |
3514 | @deffn primitive shared-array-root ra | |
3515 | Return the root vector of a shared array. | |
3516 | @end deffn | |
3517 | ||
3518 | @c docstring begin (texi-doc-string "guile" "transpose-array") | |
3519 | @deffn primitive transpose-array ra . args | |
3520 | Returns an array sharing contents with @var{array}, but with dimensions | |
3521 | arranged in a different order. There must be one @var{dim} argument for | |
3522 | each dimension of @var{array}. @var{dim0}, @var{dim1}, @dots{} should | |
3523 | be integers between 0 and the rank of the array to be returned. Each | |
3524 | integer in that range must appear at least once in the argument list. | |
3525 | ||
3526 | The values of @var{dim0}, @var{dim1}, @dots{} correspond to dimensions | |
3527 | in the array to be returned, their positions in the argument list to | |
3528 | dimensions of @var{array}. Several @var{dim}s may have the same value, | |
3529 | in which case the returned array will have smaller rank than | |
3530 | @var{array}. | |
3531 | ||
3532 | examples: | |
3533 | @example | |
3534 | (transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d)) | |
3535 | (transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d) | |
3536 | (transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{} | |
3537 | #2((a 4) (b 5) (c 6)) | |
3538 | @end example | |
3539 | @end deffn | |
3540 | ||
3541 | @c docstring begin (texi-doc-string "guile" "enclose-array") | |
3542 | @deffn primitive enclose-array ra . axes | |
3543 | @var{dim0}, @var{dim1} @dots{} should be nonnegative integers less than | |
3544 | the rank of @var{array}. @var{enclose-array} returns an array | |
3545 | resembling an array of shared arrays. The dimensions of each shared | |
3546 | array are the same as the @var{dim}th dimensions of the original array, | |
3547 | the dimensions of the outer array are the same as those of the original | |
3548 | array that did not match a @var{dim}. | |
3549 | ||
3550 | An enclosed array is not a general Scheme array. Its elements may not | |
3551 | be set using @code{array-set!}. Two references to the same element of | |
3552 | an enclosed array will be @code{equal?} but will not in general be | |
3553 | @code{eq?}. The value returned by @var{array-prototype} when given an | |
3554 | enclosed array is unspecified. | |
3555 | ||
3556 | examples: | |
3557 | @example | |
3558 | (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1) @result{} | |
3559 | #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))> | |
3560 | ||
3561 | (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0) @result{} | |
3562 | #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))> | |
3563 | @end example | |
3564 | @end deffn | |
3565 | ||
3566 | @deffn procedure array-shape array | |
3567 | Returns a list of inclusive bounds of integers. | |
3568 | @example | |
3569 | (array-shape (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) (0 4)) | |
3570 | @end example | |
3571 | @end deffn | |
3572 | ||
3573 | @c docstring begin (texi-doc-string "guile" "array-dimensions") | |
3574 | @deffn primitive array-dimensions ra | |
3575 | @code{Array-dimensions} is similar to @code{array-shape} but replaces | |
3576 | elements with a @code{0} minimum with one greater than the maximum. So: | |
3577 | @example | |
3578 | (array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5) | |
3579 | @end example | |
3580 | @end deffn | |
3581 | ||
3582 | @c docstring begin (texi-doc-string "guile" "array-rank") | |
3583 | @deffn primitive array-rank ra | |
3584 | Returns the number of dimensions of @var{obj}. If @var{obj} is not an | |
3585 | array, @code{0} is returned. | |
3586 | @end deffn | |
3587 | ||
3588 | @c docstring begin (texi-doc-string "guile" "array->list") | |
3589 | @deffn primitive array->list v | |
3590 | Returns a list consisting of all the elements, in order, of @var{array}. | |
3591 | @end deffn | |
3592 | ||
3593 | @c docstring begin (texi-doc-string "guile" "array-copy!") | |
3594 | @c docstring begin (texi-doc-string "guile" "array-copy-in-order!") | |
3595 | @deffn primitive array-copy! src dst | |
3596 | @deffnx primitive array-copy-in-order! src dst | |
3597 | Copies every element from vector or array @var{source} to the | |
3598 | corresponding element of @var{destination}. @var{destination} must have | |
3599 | the same rank as @var{source}, and be at least as large in each | |
3600 | dimension. The order is unspecified. | |
3601 | @end deffn | |
3602 | ||
3603 | @c docstring begin (texi-doc-string "guile" "array-fill!") | |
3604 | @deffn primitive array-fill! ra fill | |
3605 | Stores @var{fill} in every element of @var{array}. The value returned | |
3606 | is unspecified. | |
3607 | @end deffn | |
3608 | ||
3609 | @c begin (texi-doc-string "guile" "array-equal?") | |
3610 | @deffn primitive array-equal? ra0 ra1 | |
3611 | Returns @code{#t} iff all arguments are arrays with the same shape, the | |
3612 | same type, and have corresponding elements which are either | |
3613 | @code{equal?} or @code{array-equal?}. This function differs from | |
3614 | @code{equal?} in that a one dimensional shared array may be | |
3615 | @var{array-equal?} but not @var{equal?} to a vector or uniform vector. | |
3616 | @end deffn | |
3617 | ||
3618 | @c docstring begin (texi-doc-string "guile" "array-contents") | |
3619 | @deffn primitive array-contents ra [strict] | |
3620 | @deffnx primitive array-contents array strict | |
3621 | If @var{array} may be @dfn{unrolled} into a one dimensional shared array | |
3622 | without changing their order (last subscript changing fastest), then | |
3623 | @code{array-contents} returns that shared array, otherwise it returns | |
3624 | @code{#f}. All arrays made by @var{make-array} and | |
3625 | @var{make-uniform-array} may be unrolled, some arrays made by | |
3626 | @var{make-shared-array} may not be. | |
3627 | ||
3628 | If the optional argument @var{strict} is provided, a shared array will | |
3629 | be returned only if its elements are stored internally contiguous in | |
3630 | memory. | |
3631 | @end deffn | |
3632 | ||
3633 | @node Array Mapping | |
3634 | @subsection Array Mapping | |
3635 | ||
3636 | @c docstring begin (texi-doc-string "guile" "array-map!") | |
3637 | @c docstring begin (texi-doc-string "guile" "array-map-in-order!") | |
3638 | @deffn primitive array-map! ra0 proc . lra | |
3639 | @deffnx primitive array-map-in-order! ra0 proc . lra | |
3640 | @var{array1}, @dots{} must have the same number of dimensions as | |
3641 | @var{array0} and have a range for each index which includes the range | |
3642 | for the corresponding index in @var{array0}. @var{proc} is applied to | |
3643 | each tuple of elements of @var{array1} @dots{} and the result is stored | |
3644 | as the corresponding element in @var{array0}. The value returned is | |
3645 | unspecified. The order of application is unspecified. | |
3646 | @end deffn | |
3647 | ||
3648 | @c docstring begin (texi-doc-string "guile" "array-for-each") | |
3649 | @deffn primitive array-for-each proc ra0 . lra | |
3650 | @var{proc} is applied to each tuple of elements of @var{array0} @dots{} | |
3651 | in row-major order. The value returned is unspecified. | |
3652 | @end deffn | |
3653 | ||
3654 | @c docstring begin (texi-doc-string "guile" "array-index-map!") | |
3655 | @deffn primitive array-index-map! ra proc | |
3656 | applies @var{proc} to the indices of each element of @var{array} in | |
3657 | turn, storing the result in the corresponding element. The value | |
3658 | returned and the order of application are unspecified. | |
3659 | ||
3660 | One can implement @var{array-indexes} as | |
3661 | @example | |
3662 | (define (array-indexes array) | |
3663 | (let ((ra (apply make-array #f (array-shape array)))) | |
3664 | (array-index-map! ra (lambda x x)) | |
3665 | ra)) | |
3666 | @end example | |
3667 | Another example: | |
3668 | @example | |
3669 | (define (apl:index-generator n) | |
3670 | (let ((v (make-uniform-vector n 1))) | |
3671 | (array-index-map! v (lambda (i) i)) | |
3672 | v)) | |
3673 | @end example | |
3674 | @end deffn | |
3675 | ||
3676 | @node Uniform Arrays | |
3677 | @subsection Uniform Arrays | |
3678 | ||
3679 | @noindent | |
3680 | @dfn{Uniform arrays} have elements all of the | |
3681 | same type and occupy less storage than conventional | |
3682 | arrays. Uniform arrays with a single zero-based dimension | |
3683 | are also known as @dfn{uniform vectors}. The procedures in | |
3684 | this section can also be used on conventional arrays, vectors, | |
3685 | bit-vectors and strings. | |
3686 | ||
3687 | @noindent | |
3688 | When creating a uniform array, the type of data to be stored | |
3689 | is indicated with a @var{prototype} argument. The following table | |
3690 | lists the types available and example prototypes: | |
3691 | ||
3692 | @example | |
3693 | prototype type printing character | |
3694 | ||
3695 | #t boolean (bit-vector) b | |
3696 | #\a char (string) a | |
3697 | #\nul byte (integer) y | |
3698 | 's short (integer) h | |
3699 | 1 unsigned long (integer) u | |
3700 | -1 signed long (integer) e | |
3701 | 'l signed long long (integer) l | |
3702 | 1.0 float (single precision) s | |
3703 | 1/3 double (double precision float) i | |
3704 | 0+i complex (double precision) c | |
3705 | () conventional vector | |
3706 | @end example | |
3707 | ||
3708 | @noindent | |
3709 | Unshared uniform arrays of characters with a single zero-based dimension | |
3710 | are identical to strings: | |
3711 | ||
3712 | @example | |
3713 | (make-uniform-array #\a 3) @result{} | |
3714 | "aaa" | |
3715 | @end example | |
3716 | ||
3717 | @noindent | |
3718 | Unshared uniform arrays of booleans with a single zero-based dimension | |
3719 | are identical to @ref{Bit Vectors, bit-vectors}. | |
3720 | ||
3721 | @example | |
3722 | (make-uniform-array #t 3) @result{} | |
3723 | #*111 | |
3724 | @end example | |
3725 | ||
3726 | @noindent | |
3727 | Other uniform vectors are written in a form similar to that of vectors, | |
3728 | except that a single character from the above table is put between | |
3729 | @code{#} and @code{(}. For example, a uniform vector of signed | |
3730 | long integers is displayed in the form @code{'#e(3 5 9)}. | |
3731 | ||
3732 | @c docstring begin (texi-doc-string "guile" "array?") | |
3733 | @deffn primitive array? v [prot] | |
3734 | Returns @code{#t} if the @var{obj} is an array, and @code{#f} if not. | |
3735 | ||
3736 | The @var{prototype} argument is used with uniform arrays and is described | |
3737 | elsewhere. | |
3738 | @end deffn | |
3739 | ||
3740 | @deffn procedure make-uniform-array prototype bound1 bound2 @dots{} | |
3741 | Creates and returns a uniform array of type corresponding to | |
3742 | @var{prototype} that has as many dimensions as there are @var{bound}s | |
3743 | and fills it with @var{prototype}. | |
3744 | @end deffn | |
3745 | ||
3746 | @c docstring begin (texi-doc-string "guile" "array-prototype") | |
3747 | @deffn primitive array-prototype ra | |
3748 | Returns an object that would produce an array of the same type as | |
3749 | @var{array}, if used as the @var{prototype} for | |
3750 | @code{make-uniform-array}. | |
3751 | @end deffn | |
3752 | ||
3753 | @c docstring begin (texi-doc-string "guile" "list->uniform-array") | |
3754 | @deffn primitive list->uniform-array ndim prot lst | |
3755 | @deffnx procedure list->uniform-vector prot lst | |
3756 | Returns a uniform array of the type indicated by prototype @var{prot} | |
3757 | with elements the same as those of @var{lst}. Elements must be of the | |
3758 | appropriate type, no coercions are done. | |
3759 | @end deffn | |
3760 | ||
3761 | @deffn primitive uniform-vector-fill! uve fill | |
3762 | Stores @var{fill} in every element of @var{uve}. The value returned is | |
3763 | unspecified. | |
3764 | @end deffn | |
3765 | ||
3766 | @c docstring begin (texi-doc-string "guile" "uniform-vector-length") | |
3767 | @deffn primitive uniform-vector-length v | |
3768 | Returns the number of elements in @var{uve}. | |
3769 | @end deffn | |
3770 | ||
3771 | @c docstring begin (texi-doc-string "guile" "dimensions->uniform-array") | |
3772 | @deffn primitive dimensions->uniform-array dims prot [fill] | |
3773 | @deffnx primitive make-uniform-vector length prototype [fill] | |
3774 | Creates and returns a uniform array or vector of type corresponding to | |
3775 | @var{prototype} with dimensions @var{dims} or length @var{length}. If | |
3776 | @var{fill} is supplied, it's used to fill the array, otherwise | |
3777 | @var{prototype} is used. | |
3778 | @end deffn | |
3779 | ||
3780 | @c Another compiled-closure. -twp | |
3781 | ||
3782 | @c docstring begin (texi-doc-string "guile" "uniform-array-read!") | |
3783 | @deffn primitive uniform-array-read! ra [port_or_fd [start [end]]] | |
3784 | @deffnx primitive uniform-vector-read! uve [port-or-fdes] [start] [end] | |
3785 | Attempts to read all elements of @var{ura}, in lexicographic order, as | |
3786 | binary objects from @var{port-or-fdes}. | |
3787 | If an end of file is encountered during | |
3788 | uniform-array-read! the objects up to that point only are put into @var{ura} | |
3789 | (starting at the beginning) and the remainder of the array is | |
3790 | unchanged. | |
3791 | ||
3792 | The optional arguments @var{start} and @var{end} allow | |
3793 | a specified region of a vector (or linearized array) to be read, | |
3794 | leaving the remainder of the vector unchanged. | |
3795 | ||
3796 | @code{uniform-array-read!} returns the number of objects read. | |
3797 | @var{port-or-fdes} may be omitted, in which case it defaults to the value | |
3798 | returned by @code{(current-input-port)}. | |
3799 | @end deffn | |
3800 | ||
3801 | @c docstring begin (texi-doc-string "guile" "uniform-array-write") | |
3802 | @deffn primitive uniform-array-write v [port_or_fd [start [end]]] | |
3803 | @deffnx primitive uniform-vector-write uve [port-or-fdes] [start] [end] | |
3804 | Writes all elements of @var{ura} as binary objects to | |
3805 | @var{port-or-fdes}. | |
3806 | ||
3807 | The optional arguments @var{start} | |
3808 | and @var{end} allow | |
3809 | a specified region of a vector (or linearized array) to be written. | |
3810 | ||
3811 | The number of objects actually written is returned. | |
3812 | @var{port-or-fdes} may be | |
3813 | omitted, in which case it defaults to the value returned by | |
3814 | @code{(current-output-port)}. | |
3815 | @end deffn | |
3816 | ||
3817 | @node Bit Vectors | |
3818 | @subsection Bit Vectors | |
3819 | ||
3820 | @noindent | |
3821 | Bit vectors are a specific type of uniform array: an array of booleans | |
3822 | with a single zero-based index. | |
3823 | ||
3824 | @noindent | |
3825 | They are displayed as a sequence of @code{0}s and | |
3826 | @code{1}s prefixed by @code{#*}, e.g., | |
3827 | ||
3828 | @example | |
3829 | (make-uniform-vector 8 #t #f) @result{} | |
3830 | #*00000000 | |
3831 | ||
3832 | #b(#t #f #t) @result{} | |
3833 | #*101 | |
3834 | @end example | |
3835 | ||
3836 | @c docstring begin (texi-doc-string "guile" "bit-count") | |
3837 | @deffn primitive bit-count b bitvector | |
3838 | Returns the number of occurrences of the boolean @var{b} in | |
3839 | @var{bitvector}. | |
3840 | @end deffn | |
3841 | ||
3842 | @c docstring begin (texi-doc-string "guile" "bit-position") | |
3843 | @deffn primitive bit-position item v k | |
3844 | Returns the minimum index of an occurrence of @var{bool} in @var{bv} | |
3845 | which is at least @var{k}. If no @var{bool} occurs within the specified | |
3846 | range @code{#f} is returned. | |
3847 | @end deffn | |
3848 | ||
3849 | @c docstring begin (texi-doc-string "guile" "bit-invert!") | |
3850 | @deffn primitive bit-invert! v | |
3851 | Modifies @var{bv} by replacing each element with its negation. | |
3852 | @end deffn | |
3853 | ||
3854 | @c docstring begin (texi-doc-string "guile" "bit-set*!") | |
3855 | @deffn primitive bit-set*! v kv obj | |
3856 | If uve is a bit-vector @var{bv} and uve must be of the same | |
3857 | length. If @var{bool} is @code{#t}, uve is OR'ed into | |
3858 | @var{bv}; If @var{bool} is @code{#f}, the inversion of uve is | |
3859 | AND'ed into @var{bv}. | |
3860 | ||
3861 | If uve is a unsigned integer vector all the elements of uve | |
3862 | must be between 0 and the @code{length} of @var{bv}. The bits | |
3863 | of @var{bv} corresponding to the indexes in uve are set to | |
3864 | @var{bool}. The return value is unspecified. | |
3865 | @end deffn | |
3866 | ||
3867 | @c docstring begin (texi-doc-string "guile" "bit-count*") | |
3868 | @deffn primitive bit-count* v kv obj | |
3869 | Returns | |
3870 | @example | |
3871 | (bit-count (bit-set*! (if bool bv (bit-invert! bv)) uve #t) #t). | |
3872 | @end example | |
3873 | @var{bv} is not modified. | |
3874 | @end deffn | |
3875 | ||
3876 | ||
3877 | @node Association Lists and Hash Tables | |
3878 | @section Association Lists and Hash Tables | |
3879 | ||
3880 | This chapter discusses dictionary objects: data structures that are | |
3881 | useful for organizing and indexing large bodies of information. | |
3882 | ||
3883 | @menu | |
3884 | * Dictionary Types:: About dictionary types; what they're good for. | |
3885 | * Association Lists:: | |
3886 | * Hash Tables:: | |
3887 | @end menu | |
3888 | ||
3889 | @node Dictionary Types | |
3890 | @subsection Dictionary Types | |
3891 | ||
3892 | A @dfn{dictionary} object is a data structure used to index | |
3893 | information in a user-defined way. In standard Scheme, the main | |
3894 | aggregate data types are lists and vectors. Lists are not really | |
3895 | indexed at all, and vectors are indexed only by number | |
3896 | (e.g. @code{(vector-ref foo 5)}). Often you will find it useful | |
3897 | to index your data on some other type; for example, in a library | |
3898 | catalog you might want to look up a book by the name of its | |
3899 | author. Dictionaries are used to help you organize information in | |
3900 | such a way. | |
3901 | ||
3902 | An @dfn{association list} (or @dfn{alist} for short) is a list of | |
3903 | key-value pairs. Each pair represents a single quantity or | |
3904 | object; the @code{car} of the pair is a key which is used to | |
3905 | identify the object, and the @code{cdr} is the object's value. | |
3906 | ||
3907 | A @dfn{hash table} also permits you to index objects with | |
3908 | arbitrary keys, but in a way that makes looking up any one object | |
3909 | extremely fast. A well-designed hash system makes hash table | |
3910 | lookups almost as fast as conventional array or vector references. | |
3911 | ||
3912 | Alists are popular among Lisp programmers because they use only | |
3913 | the language's primitive operations (lists, @dfn{car}, @dfn{cdr} | |
3914 | and the equality primitives). No changes to the language core are | |
3915 | necessary. Therefore, with Scheme's built-in list manipulation | |
3916 | facilities, it is very convenient to handle data stored in an | |
3917 | association list. Also, alists are highly portable and can be | |
3918 | easily implemented on even the most minimal Lisp systems. | |
3919 | ||
3920 | However, alists are inefficient, especially for storing large | |
3921 | quantities of data. Because we want Guile to be useful for large | |
3922 | software systems as well as small ones, Guile provides a rich set | |
3923 | of tools for using either association lists or hash tables. | |
3924 | ||
3925 | @node Association Lists | |
3926 | @subsection Association Lists | |
3927 | @cindex Association List | |
3928 | @cindex Alist | |
3929 | @cindex Database | |
3930 | ||
3931 | An association list is a conventional data structure that is often used | |
3932 | to implement simple key-value databases. It consists of a list of | |
3933 | entries in which each entry is a pair. The @dfn{key} of each entry is | |
3934 | the @code{car} of the pair and the @dfn{value} of each entry is the | |
3935 | @code{cdr}. | |
3936 | ||
3937 | @example | |
3938 | ASSOCIATION LIST ::= '( (KEY1 . VALUE1) | |
3939 | (KEY2 . VALUE2) | |
3940 | (KEY3 . VALUE3) | |
3941 | @dots{} | |
3942 | ) | |
3943 | @end example | |
3944 | ||
3945 | @noindent | |
3946 | Association lists are also known, for short, as @dfn{alists}. | |
3947 | ||
3948 | The structure of an association list is just one example of the infinite | |
3949 | number of possible structures that can be built using pairs and lists. | |
3950 | As such, the keys and values in an association list can be manipulated | |
3951 | using the general list structure procedures @code{cons}, @code{car}, | |
3952 | @code{cdr}, @code{set-car!}, @code{set-cdr!} and so on. However, | |
3953 | because association lists are so useful, Guile also provides specific | |
3954 | procedures for manipulating them. | |
3955 | ||
3956 | @menu | |
3957 | * Alist Key Equality:: | |
3958 | * Adding or Setting Alist Entries:: | |
3959 | * Retrieving Alist Entries:: | |
3960 | * Removing Alist Entries:: | |
3961 | * Sloppy Alist Functions:: | |
3962 | * Alist Example:: | |
3963 | @end menu | |
3964 | ||
3965 | @node Alist Key Equality | |
3966 | @subsubsection Alist Key Equality | |
3967 | ||
3968 | All of Guile's dedicated association list procedures, apart from | |
3969 | @code{acons}, come in three flavours, depending on the level of equality | |
3970 | that is required to decide whether an existing key in the association | |
3971 | list is the same as the key that the procedure call uses to identify the | |
3972 | required entry. | |
3973 | ||
3974 | @itemize @bullet | |
3975 | @item | |
3976 | Procedures with @dfn{assq} in their name use @code{eq?} to determine key | |
3977 | equality. | |
3978 | ||
3979 | @item | |
3980 | Procedures with @dfn{assv} in their name use @code{eqv?} to determine | |
3981 | key equality. | |
3982 | ||
3983 | @item | |
3984 | Procedures with @dfn{assoc} in their name use @code{equal?} to | |
3985 | determine key equality. | |
3986 | @end itemize | |
3987 | ||
3988 | @code{acons} is an exception because it is used to build association | |
3989 | lists which do not require their entries' keys to be unique. | |
3990 | ||
3991 | @node Adding or Setting Alist Entries | |
3992 | @subsubsection Adding or Setting Alist Entries | |
3993 | @findex acons | |
3994 | @findex assq-set! | |
3995 | @findex assv-set! | |
3996 | @findex assoc-set! | |
3997 | @r5index assq-set! | |
3998 | @r5index assv-set! | |
3999 | @r5index assoc-set! | |
4000 | ||
4001 | @code{acons} adds a new entry to an association list and returns the | |
4002 | combined association list. The combined alist is formed by consing the | |
4003 | new entry onto the head of the alist specified in the @code{acons} | |
4004 | procedure call. So the specified alist is not modified, but its | |
4005 | contents become shared with the tail of the combined alist that | |
4006 | @code{acons} returns. | |
4007 | ||
4008 | In the most common usage of @code{acons}, a variable holding the | |
4009 | original association list is updated with the combined alist: | |
4010 | ||
4011 | @example | |
4012 | (set! address-list (acons name address address-list)) | |
4013 | @end example | |
4014 | ||
4015 | In such cases, it doesn't matter that the old and new values of | |
4016 | @code{address-list} share some of their contents, since the old value is | |
4017 | usually no longer independently accessible. | |
4018 | ||
4019 | Note that @code{acons} adds the specified new entry regardless of | |
4020 | whether the alist may already contain entries with keys that are, in | |
4021 | some sense, the same as that of the new entry. Thus @code{acons} is | |
4022 | ideal for building alists where there is no concept of key uniqueness. | |
4023 | ||
4024 | @example | |
4025 | (set! task-list (acons 3 "pay gas bill" '())) | |
4026 | task-list | |
4027 | @result{} | |
4028 | ((3 . "pay gas bill")) | |
4029 | ||
4030 | (set! task-list (acons 3 "tidy bedroom" task-list)) | |
4031 | task-list | |
4032 | @result{} | |
4033 | ((3 . "tidy bedroom") (3 . "pay gas bill")) | |
4034 | @end example | |
4035 | ||
4036 | @code{assq-set!}, @code{assv-set!} and @code{assoc-set!} are used to add | |
4037 | or replace an entry in an association list where there @emph{is} a | |
4038 | concept of key uniqueness. If the specified association list already | |
4039 | contains an entry whose key is the same as that specified in the | |
4040 | procedure call, the existing entry is replaced by the new one. | |
4041 | Otherwise, the new entry is consed onto the head of the old association | |
4042 | list to create the combined alist. In all cases, these procedures | |
4043 | return the combined alist. | |
4044 | ||
4045 | @code{assq-set!} and friends @emph{may} destructively modify the | |
4046 | structure of the old association list in such a way that an existing | |
4047 | variable is correctly updated without having to @code{set!} it to the | |
4048 | value returned: | |
4049 | ||
4050 | @example | |
4051 | address-list | |
4052 | @result{} | |
4053 | (("mary" . "34 Elm Road") ("james" . "16 Bow Street")) | |
4054 | ||
4055 | (assoc-set! address-list "james" "1a London Road") | |
4056 | @result{} | |
4057 | (("mary" . "34 Elm Road") ("james" . "1a London Road")) | |
4058 | ||
4059 | address-list | |
4060 | @result{} | |
4061 | (("mary" . "34 Elm Road") ("james" . "1a London Road")) | |
4062 | @end example | |
4063 | ||
4064 | Or they may not: | |
4065 | ||
4066 | @example | |
4067 | (assoc-set! address-list "bob" "11 Newington Avenue") | |
4068 | @result{} | |
4069 | (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road") | |
4070 | ("james" . "1a London Road")) | |
4071 | ||
4072 | address-list | |
4073 | @result{} | |
4074 | (("mary" . "34 Elm Road") ("james" . "1a London Road")) | |
4075 | @end example | |
4076 | ||
4077 | The only safe way to update an association list variable when adding or | |
4078 | replacing an entry like this is to @code{set!} the variable to the | |
4079 | returned value: | |
4080 | ||
4081 | @example | |
4082 | (set! address-list | |
4083 | (assoc-set! address-list "bob" "11 Newington Avenue")) | |
4084 | address-list | |
4085 | @result{} | |
4086 | (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road") | |
4087 | ("james" . "1a London Road")) | |
4088 | @end example | |
4089 | ||
4090 | Because of this slight inconvenience, you may find it more convenient to | |
4091 | use hash tables to store dictionary data. If your application will not | |
4092 | be modifying the contents of an alist very often, this may not make much | |
4093 | difference to you. | |
4094 | ||
4095 | If you need to keep the old value of an association list in a form | |
4096 | independent from the list that results from modification by | |
4097 | @code{acons}, @code{assq-set!}, @code{assv-set!} or @code{assoc-set!}, | |
4098 | use @code{list-copy} to copy the old association list before modifying | |
4099 | it. | |
4100 | ||
4101 | @c docstring begin (texi-doc-string "guile" "acons") | |
4102 | @deffn primitive acons key value alist | |
4103 | Adds a new key-value pair to @var{alist}. A new pair is | |
4104 | created whose car is @var{key} and whose cdr is @var{value}, and the | |
4105 | pair is consed onto @var{alist}, and the new list is returned. This | |
4106 | function is @emph{not} destructive; @var{alist} is not modified. | |
4107 | @end deffn | |
4108 | ||
4109 | @c docstring begin (texi-doc-string "guile" "assq-set!") | |
4110 | @c docstring begin (texi-doc-string "guile" "assv-set!") | |
4111 | @c docstring begin (texi-doc-string "guile" "assoc-set!") | |
4112 | @deffn primitive assq-set! alist key val | |
4113 | @deffnx primitive assv-set! alist key value | |
4114 | @deffnx primitive assoc-set! alist key value | |
4115 | Reassociate @var{key} in @var{alist} with @var{value}: find any existing | |
4116 | @var{alist} entry for @var{key} and associate it with the new | |
4117 | @var{value}. If @var{alist} does not contain an entry for @var{key}, | |
4118 | add a new one. Return the (possibly new) alist. | |
4119 | ||
4120 | These functions do not attempt to verify the structure of @var{alist}, | |
4121 | and so may cause unusual results if passed an object that is not an | |
4122 | association list. | |
4123 | @end deffn | |
4124 | ||
4125 | @node Retrieving Alist Entries | |
4126 | @subsubsection Retrieving Alist Entries | |
4127 | @findex assq | |
4128 | @findex assv | |
4129 | @findex assoc | |
4130 | @findex assq-ref | |
4131 | @findex assv-ref | |
4132 | @findex assoc-ref | |
4133 | @r5index assq | |
4134 | @r5index assv | |
4135 | @r5index assoc | |
4136 | @r5index assq-ref | |
4137 | @r5index assv-ref | |
4138 | @r5index assoc-ref | |
4139 | ||
4140 | @code{assq}, @code{assv} and @code{assoc} take an alist and a key as | |
4141 | arguments and return the entry for that key if an entry exists, or | |
4142 | @code{#f} if there is no entry for that key. Note that, in the cases | |
4143 | where an entry exists, these procedures return the complete entry, that | |
4144 | is @code{(KEY . VALUE)}, not just the value. | |
4145 | ||
4146 | @c docstring begin (texi-doc-string "guile" "assq") | |
4147 | @c docstring begin (texi-doc-string "guile" "assv") | |
4148 | @c docstring begin (texi-doc-string "guile" "assoc") | |
4149 | @deffn primitive assq key alist | |
4150 | @deffnx primitive assv key alist | |
4151 | @deffnx primitive assoc key alist | |
4152 | Fetches the entry in @var{alist} that is associated with @var{key}. To | |
4153 | decide whether the argument @var{key} matches a particular entry in | |
4154 | @var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv} | |
4155 | uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key} | |
4156 | cannot be found in @var{alist} (according to whichever equality | |
4157 | predicate is in use), then @code{#f} is returned. These functions | |
4158 | return the entire alist entry found (i.e. both the key and the value). | |
4159 | @end deffn | |
4160 | ||
4161 | @code{assq-ref}, @code{assv-ref} and @code{assoc-ref}, on the other | |
4162 | hand, take an alist and a key and return @emph{just the value} for that | |
4163 | key, if an entry exists. If there is no entry for the specified key, | |
4164 | these procedures return @code{#f}. | |
4165 | ||
4166 | This creates an ambiguity: if the return value is @code{#f}, it means | |
4167 | either that there is no entry with the specified key, or that there | |
4168 | @emph{is} an entry for the specified key, with value @code{#f}. | |
4169 | Consequently, @code{assq-ref} and friends should only be used where it | |
4170 | is known that an entry exists, or where the ambiguity doesn't matter | |
4171 | for some other reason. | |
4172 | ||
4173 | @c docstring begin (texi-doc-string "guile" "assq-ref") | |
4174 | @c docstring begin (texi-doc-string "guile" "assv-ref") | |
4175 | @c docstring begin (texi-doc-string "guile" "assoc-ref") | |
4176 | @deffn primitive assq-ref alist key | |
4177 | @deffnx primitive assv-ref alist key | |
4178 | @deffnx primitive assoc-ref alist key | |
4179 | Like @code{assq}, @code{assv} and @code{assoc}, except that only the | |
4180 | value associated with @var{key} in @var{alist} is returned. These | |
4181 | functions are equivalent to | |
4182 | ||
4183 | @lisp | |
4184 | (let ((ent (@var{associator} @var{key} @var{alist}))) | |
4185 | (and ent (cdr ent))) | |
4186 | @end lisp | |
4187 | ||
4188 | where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}. | |
4189 | @end deffn | |
4190 | ||
4191 | @node Removing Alist Entries | |
4192 | @subsubsection Removing Alist Entries | |
4193 | @findex assq-remove! | |
4194 | @findex assv-remove! | |
4195 | @findex assoc-remove! | |
4196 | ||
4197 | To remove the element from an association list whose key matches a | |
4198 | specified key, use @code{assq-remove!}, @code{assv-remove!} or | |
4199 | @code{assoc-remove!} (depending, as usual, on the level of equality | |
4200 | required between the key that you specify and the keys in the | |
4201 | association list). | |
4202 | ||
4203 | As with @code{assq-set!} and friends, the specified alist may or may not | |
4204 | be modified destructively, and the only safe way to update a variable | |
4205 | containing the alist is to @code{set!} it to the value that | |
4206 | @code{assq-remove!} and friends return. | |
4207 | ||
4208 | @example | |
4209 | address-list | |
4210 | @result{} | |
4211 | (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road") | |
4212 | ("james" . "1a London Road")) | |
4213 | ||
4214 | (set! address-list (assoc-remove! address-list "mary")) | |
4215 | address-list | |
4216 | @result{} | |
4217 | (("bob" . "11 Newington Avenue") ("james" . "1a London Road")) | |
4218 | @end example | |
4219 | ||
4220 | Note that, when @code{assq/v/oc-remove!} is used to modify an | |
4221 | association list that has been constructed only using the corresponding | |
4222 | @code{assq/v/oc-set!}, there can be at most one matching entry in the | |
4223 | alist, so the question of multiple entries being removed in one go does | |
4224 | not arise. If @code{assq/v/oc-remove!} is applied to an association | |
4225 | list that has been constructed using @code{acons}, or an | |
4226 | @code{assq/v/oc-set!} with a different level of equality, or any mixture | |
4227 | of these, it removes only the first matching entry from the alist, even | |
4228 | if the alist might contain further matching entries. For example: | |
4229 | ||
4230 | @example | |
4231 | (define address-list '()) | |
4232 | (set! address-list (assq-set! address-list "mary" "11 Elm Street")) | |
4233 | (set! address-list (assq-set! address-list "mary" "57 Pine Drive")) | |
4234 | address-list | |
4235 | @result{} | |
4236 | (("mary" . "57 Pine Drive") ("mary" . "11 Elm Street")) | |
4237 | ||
4238 | (set! address-list (assoc-remove! address-list "mary")) | |
4239 | address-list | |
4240 | @result{} | |
4241 | (("mary" . "11 Elm Street")) | |
4242 | @end example | |
4243 | ||
4244 | In this example, the two instances of the string "mary" are not the same | |
4245 | when compared using @code{eq?}, so the two @code{assq-set!} calls add | |
4246 | two distinct entries to @code{address-list}. When compared using | |
4247 | @code{equal?}, both "mary"s in @code{address-list} are the same as the | |
4248 | "mary" in the @code{assoc-remove!} call, but @code{assoc-remove!} stops | |
4249 | after removing the first matching entry that it finds, and so one of the | |
4250 | "mary" entries is left in place. | |
4251 | ||
4252 | @c docstring begin (texi-doc-string "guile" "assq-remove!") | |
4253 | @c docstring begin (texi-doc-string "guile" "assv-remove!") | |
4254 | @c docstring begin (texi-doc-string "guile" "assoc-remove!") | |
4255 | @deffn primitive assq-remove! alist key | |
4256 | @deffnx primitive assv-remove! alist key | |
4257 | @deffnx primitive assoc-remove! alist key | |
4258 | Delete the first entry in @var{alist} associated with @var{key}, and return | |
4259 | the resulting alist. | |
4260 | @end deffn | |
4261 | ||
4262 | @node Sloppy Alist Functions | |
4263 | @subsubsection Sloppy Alist Functions | |
4264 | @findex sloppy-assq | |
4265 | @findex sloppy-assv | |
4266 | @findex sloppy-assoc | |
4267 | ||
4268 | @code{sloppy-assq}, @code{sloppy-assv} and @code{sloppy-assoc} behave | |
4269 | like the corresponding non-@code{sloppy-} procedures, except that they | |
4270 | return @code{#f} when the specified association list is not well-formed, | |
4271 | where the non-@code{sloppy-} versions would signal an error. | |
4272 | ||
4273 | Specifically, there are two conditions for which the non-@code{sloppy-} | |
4274 | procedures signal an error, which the @code{sloppy-} procedures handle | |
4275 | instead by returning @code{#f}. Firstly, if the specified alist as a | |
4276 | whole is not a proper list: | |
4277 | ||
4278 | @example | |
4279 | (assoc "mary" '((1 . 2) ("key" . "door") . "open sesame")) | |
4280 | @result{} | |
4281 | ERROR: In procedure assoc in expression (assoc "mary" (quote #)): | |
4282 | ERROR: Wrong type argument in position 2 (expecting NULLP): "open sesame" | |
4283 | ABORT: (wrong-type-arg) | |
4284 | ||
4285 | (sloppy-assoc "mary" '((1 . 2) ("key" . "door") . "open sesame")) | |
4286 | @result{} | |
4287 | #f | |
4288 | @end example | |
4289 | ||
4290 | @noindent | |
4291 | Secondly, if one of the entries in the specified alist is not a pair: | |
4292 | ||
4293 | @example | |
4294 | (assoc 2 '((1 . 1) 2 (3 . 9))) | |
4295 | @result{} | |
4296 | ERROR: In procedure assoc in expression (assoc 2 (quote #)): | |
4297 | ERROR: Wrong type argument in position 2 (expecting CONSP): 2 | |
4298 | ABORT: (wrong-type-arg) | |
4299 | ||
4300 | (sloppy-assoc 2 '((1 . 1) 2 (3 . 9))) | |
4301 | @result{} | |
4302 | #f | |
4303 | @end example | |
4304 | ||
4305 | Unless you are explicitly working with badly formed association lists, | |
4306 | it is much safer to use the non-@code{sloppy-} procedures, because they | |
4307 | help to highlight coding and data errors that the @code{sloppy-} | |
4308 | versions would silently cover up. | |
4309 | ||
4310 | @c docstring begin (texi-doc-string "guile" "sloppy-assq") | |
4311 | @deffn primitive sloppy-assq key alist | |
4312 | Behaves like @code{assq} but does not do any error checking. | |
4313 | Recommended only for use in Guile internals. | |
4314 | @end deffn | |
4315 | ||
4316 | @c docstring begin (texi-doc-string "guile" "sloppy-assv") | |
4317 | @deffn primitive sloppy-assv key alist | |
4318 | Behaves like @code{assv} but does not do any error checking. | |
4319 | Recommended only for use in Guile internals. | |
4320 | @end deffn | |
4321 | ||
4322 | @c docstring begin (texi-doc-string "guile" "sloppy-assoc") | |
4323 | @deffn primitive sloppy-assoc key alist | |
4324 | Behaves like @code{assoc} but does not do any error checking. | |
4325 | Recommended only for use in Guile internals. | |
4326 | @end deffn | |
4327 | ||
4328 | @node Alist Example | |
4329 | @subsubsection Alist Example | |
4330 | ||
4331 | Here is a longer example of how alists may be used in practice. | |
4332 | ||
4333 | @lisp | |
4334 | (define capitals '(("New York" . "Albany") | |
4335 | ("Oregon" . "Salem") | |
4336 | ("Florida" . "Miami"))) | |
4337 | ||
4338 | ;; What's the capital of Oregon? | |
4339 | (assoc "Oregon" capitals) @result{} ("Oregon" . "Salem") | |
4340 | (assoc-ref capitals "Oregon") @result{} "Salem" | |
4341 | ||
4342 | ;; We left out South Dakota. | |
4343 | (set! capitals | |
4344 | (assoc-set! capitals "South Dakota" "Bismarck")) | |
4345 | capitals | |
4346 | @result{} (("South Dakota" . "Bismarck") | |
4347 | ("New York" . "Albany") | |
4348 | ("Oregon" . "Salem") | |
4349 | ("Florida" . "Miami")) | |
4350 | ||
4351 | ;; And we got Florida wrong. | |
4352 | (set! capitals | |
4353 | (assoc-set! capitals "Florida" "Tallahassee")) | |
4354 | capitals | |
4355 | @result{} (("South Dakota" . "Bismarck") | |
4356 | ("New York" . "Albany") | |
4357 | ("Oregon" . "Salem") | |
4358 | ("Florida" . "Tallahassee")) | |
4359 | ||
4360 | ;; After Oregon secedes, we can remove it. | |
4361 | (set! capitals | |
4362 | (assoc-remove! capitals "Oregon")) | |
4363 | capitals | |
4364 | @result{} (("South Dakota" . "Bismarck") | |
4365 | ("New York" . "Albany") | |
4366 | ("Florida" . "Tallahassee")) | |
4367 | @end lisp | |
4368 | ||
4369 | @node Hash Tables | |
4370 | @subsection Hash Tables | |
4371 | ||
4372 | Like the association list functions, the hash table functions come | |
4373 | in several varieties: @code{hashq}, @code{hashv}, and @code{hash}. | |
4374 | The @code{hashq} functions use @code{eq?} to determine whether two | |
4375 | keys match. The @code{hashv} functions use @code{eqv?}, and the | |
4376 | @code{hash} functions use @code{equal?}. | |
4377 | ||
4378 | In each of the functions that follow, the @var{table} argument | |
4379 | must be a vector. The @var{key} and @var{value} arguments may be | |
4380 | any Scheme object. | |
4381 | ||
4382 | @c ARGFIXME obj/key | |
4383 | @c docstring begin (texi-doc-string "guile" "hashq-ref") | |
4384 | @deffn primitive hashq-ref table obj [dflt] | |
4385 | Look up @var{key} in the hash table @var{table}, and return the | |
4386 | value (if any) associated with it. If @var{key} is not found, | |
4387 | return @var{default} (or @code{#f} if no @var{default} argument is | |
4388 | supplied). Uses `eq?' for equality testing. | |
4389 | @end deffn | |
4390 | ||
4391 | @c ARGFIXME obj/key | |
4392 | @c docstring begin (texi-doc-string "guile" "hashv-ref") | |
4393 | @deffn primitive hashv-ref table obj [dflt] | |
4394 | Look up @var{key} in the hash table @var{table}, and return the | |
4395 | value (if any) associated with it. If @var{key} is not found, | |
4396 | return @var{default} (or @code{#f} if no @var{default} argument is | |
4397 | supplied). Uses `eqv?' for equality testing. | |
4398 | @end deffn | |
4399 | ||
4400 | @c ARGFIXME obj/key | |
4401 | @c docstring begin (texi-doc-string "guile" "hash-ref") | |
4402 | @deffn primitive hash-ref table obj [dflt] | |
4403 | Look up @var{key} in the hash table @var{table}, and return the | |
4404 | value (if any) associated with it. If @var{key} is not found, | |
4405 | return @var{default} (or @code{#f} if no @var{default} argument is | |
4406 | supplied). Uses `equal?' for equality testing. | |
4407 | @end deffn | |
4408 | ||
4409 | @c ARGFIXME obj/key | |
4410 | @c docstring begin (texi-doc-string "guile" "hashq-set!") | |
4411 | @deffn primitive hashq-set! table obj val | |
4412 | Find the entry in @var{table} associated with @var{key}, and store | |
4413 | @var{value} there. Uses `eq?' for equality testing. | |
4414 | @end deffn | |
4415 | ||
4416 | @c ARGFIXME obj/key | |
4417 | @c docstring begin (texi-doc-string "guile" "hashv-set!") | |
4418 | @deffn primitive hashv-set! table obj val | |
4419 | Find the entry in @var{table} associated with @var{key}, and store | |
4420 | @var{value} there. Uses `eqv?' for equality testing. | |
4421 | @end deffn | |
4422 | ||
4423 | @c ARGFIXME obj/key | |
4424 | @c docstring begin (texi-doc-string "guile" "hash-set!") | |
4425 | @deffn primitive hash-set! table obj val | |
4426 | Find the entry in @var{table} associated with @var{key}, and store | |
4427 | @var{value} there. Uses `equal?' for equality testing. | |
4428 | @end deffn | |
4429 | ||
4430 | @c ARGFIXME obj/key | |
4431 | @c docstring begin (texi-doc-string "guile" "hashq-remove!") | |
4432 | @deffn primitive hashq-remove! table obj | |
4433 | Remove @var{key} (and any value associated with it) from @var{table}. | |
4434 | Uses `eq?' for equality tests. | |
4435 | @end deffn | |
4436 | ||
4437 | @c ARGFIXME obj/key | |
4438 | @c docstring begin (texi-doc-string "guile" "hashv-remove!") | |
4439 | @deffn primitive hashv-remove! table obj | |
4440 | Remove @var{key} (and any value associated with it) from @var{table}. | |
4441 | Uses `eqv?' for equality tests. | |
4442 | @end deffn | |
4443 | ||
4444 | @c ARGFIXME obj/key | |
4445 | @c docstring begin (texi-doc-string "guile" "hash-remove!") | |
4446 | @deffn primitive hash-remove! table obj | |
4447 | Remove @var{key} (and any value associated with it) from @var{table}. | |
4448 | Uses `equal?' for equality tests. | |
4449 | @end deffn | |
4450 | ||
4451 | The standard hash table functions may be too limited for some | |
4452 | applications. For example, you may want a hash table to store | |
4453 | strings in a case-insensitive manner, so that references to keys | |
4454 | named ``foobar'', ``FOOBAR'' and ``FooBaR'' will all yield the | |
4455 | same item. Guile provides you with @dfn{extended} hash tables | |
4456 | that permit you to specify a hash function and associator function | |
4457 | of your choosing. The functions described in the rest of this section | |
4458 | can be used to implement such custom hash table structures. | |
4459 | ||
4460 | If you are unfamiliar with the inner workings of hash tables, then | |
4461 | this facility will probably be a little too abstract for you to | |
4462 | use comfortably. If you are interested in learning more, see an | |
4463 | introductory textbook on data structures or algorithms for an | |
4464 | explanation of how hash tables are implemented. | |
4465 | ||
4466 | @c docstring begin (texi-doc-string "guile" "hashq") | |
4467 | @deffn primitive hashq key size | |
4468 | Determine a hash value for KEY that is suitable for lookups in | |
4469 | a hashtable of size SIZE, where eq? is used as the equality | |
4470 | predicate. The function returns an integer in the range 0 to | |
4471 | SIZE - 1. NOTE that `hashq' may use internal addresses. | |
4472 | Thus two calls to hashq where the keys are eq? are not | |
4473 | guaranteed to deliver the same value if the key object gets | |
4474 | garbage collected in between. This can happen, for example | |
4475 | with symbols: (hashq 'foo n) (gc) (hashq 'foo n) may produce two | |
4476 | different values, since 'foo will be garbage collected. | |
4477 | @end deffn | |
4478 | ||
4479 | @c docstring begin (texi-doc-string "guile" "hashv") | |
4480 | @deffn primitive hashv key size | |
4481 | Determine a hash value for KEY that is suitable for lookups in | |
4482 | a hashtable of size SIZE, where eqv? is used as the equality | |
4483 | predicate. The function returns an integer in the range 0 to | |
4484 | SIZE - 1. NOTE that (hashv key) may use internal addresses. | |
4485 | Thus two calls to hashv where the keys are eqv? are not | |
4486 | guaranteed to deliver the same value if the key object gets | |
4487 | garbage collected in between. This can happen, for example | |
4488 | with symbols: (hashv 'foo n) (gc) (hashv 'foo n) may produce two | |
4489 | different values, since 'foo will be garbage collected. | |
4490 | @end deffn | |
4491 | ||
4492 | @c docstring begin (texi-doc-string "guile" "hash") | |
4493 | @deffn primitive hash key size | |
4494 | Determine a hash value for KEY that is suitable for lookups in | |
4495 | a hashtable of size SIZE, where equal? is used as the equality | |
4496 | predicate. The function returns an integer in the range 0 to | |
4497 | SIZE - 1. | |
4498 | @end deffn | |
4499 | ||
4500 | @c ARGFIXME hash/hasher | |
4501 | @c docstring begin (texi-doc-string "guile" "hashx-ref") | |
4502 | @deffn primitive hashx-ref hash assoc table obj [dflt] | |
4503 | This behaves the same way as the corresponding @code{ref} | |
4504 | function, but uses @var{hasher} as a | |
4505 | hash function and @var{assoc} to compare keys. @code{hasher} must | |
4506 | be a function that takes two arguments, a key to be hashed and a | |
4507 | table size. @code{assoc} must be an associator function, like | |
4508 | @code{assoc}, @code{assq} or @code{assv}. | |
4509 | ||
4510 | By way of illustration, @code{hashq-ref table key} is equivalent | |
4511 | to @code{hashx-ref hashq assq table key}. | |
4512 | @end deffn | |
4513 | ||
4514 | @c docstring begin (texi-doc-string "guile" "hashx-set!") | |
4515 | @deffn primitive hashx-set! hash assoc table obj val | |
4516 | This behaves the same way as the corresponding @code{set!} | |
4517 | function, but uses @var{hasher} as a | |
4518 | hash function and @var{assoc} to compare keys. @code{hasher} must | |
4519 | be a function that takes two arguments, a key to be hashed and a | |
4520 | table size. @code{assoc} must be an associator function, like | |
4521 | @code{assoc}, @code{assq} or @code{assv}. | |
4522 | ||
4523 | By way of illustration, @code{hashq-set! table key} is equivalent | |
4524 | to @code{hashx-set! hashq assq table key}. | |
4525 | @end deffn | |
4526 | ||
4527 | @c docstring begin (texi-doc-string "guile" "hashq-get-handle") | |
4528 | @deffn primitive hashq-get-handle table obj | |
4529 | This procedure is similar to its @code{-ref} cousin, but returns a | |
4530 | @dfn{handle} from the hash table rather than the value associated with | |
4531 | @var{key}. By convention, a handle in a hash table is the pair which | |
4532 | associates a key with a value. Where @code{hashq-ref table key} returns | |
4533 | only a @code{value}, @code{hashq-get-handle table key} returns the pair | |
4534 | @code{(key . value)}. | |
4535 | @end deffn | |
4536 | ||
4537 | @c docstring begin (texi-doc-string "guile" "hashv-get-handle") | |
4538 | @deffn primitive hashv-get-handle table obj | |
4539 | This procedure is similar to its @code{-ref} cousin, but returns a | |
4540 | @dfn{handle} from the hash table rather than the value associated with | |
4541 | @var{key}. By convention, a handle in a hash table is the pair which | |
4542 | associates a key with a value. Where @code{hashv-ref table key} returns | |
4543 | only a @code{value}, @code{hashv-get-handle table key} returns the pair | |
4544 | @code{(key . value)}. | |
4545 | @end deffn | |
4546 | ||
4547 | @c docstring begin (texi-doc-string "guile" "hash-get-handle") | |
4548 | @deffn primitive hash-get-handle table obj | |
4549 | This procedure is similar to its @code{-ref} cousin, but returns a | |
4550 | @dfn{handle} from the hash table rather than the value associated with | |
4551 | @var{key}. By convention, a handle in a hash table is the pair which | |
4552 | associates a key with a value. Where @code{hash-ref table key} returns | |
4553 | only a @code{value}, @code{hash-get-handle table key} returns the pair | |
4554 | @code{(key . value)}. | |
4555 | @end deffn | |
4556 | ||
4557 | @c docstring begin (texi-doc-string "guile" "hashx-get-handle") | |
4558 | @deffn primitive hashx-get-handle hash assoc table obj | |
4559 | This behaves the same way as the corresponding @code{-get-handle} | |
4560 | function, but uses @var{hasher} as a | |
4561 | hash function and @var{assoc} to compare keys. @code{hasher} must | |
4562 | be a function that takes two arguments, a key to be hashed and a | |
4563 | table size. @code{assoc} must be an associator function, like | |
4564 | @code{assoc}, @code{assq} or @code{assv}. | |
4565 | @end deffn | |
4566 | ||
4567 | @c docstring begin (texi-doc-string "guile" "hashq-create-handle!") | |
4568 | @deffn primitive hashq-create-handle! table key init | |
4569 | This function looks up @var{key} in @var{table} and returns its handle. | |
4570 | If @var{key} is not already present, a new handle is created which | |
4571 | associates @var{key} with @var{init}. | |
4572 | @end deffn | |
4573 | ||
4574 | @c docstring begin (texi-doc-string "guile" "hashv-create-handle!") | |
4575 | @deffn primitive hashv-create-handle! table key init | |
4576 | This function looks up @var{key} in @var{table} and returns its handle. | |
4577 | If @var{key} is not already present, a new handle is created which | |
4578 | associates @var{key} with @var{init}. | |
4579 | @end deffn | |
4580 | ||
4581 | @c docstring begin (texi-doc-string "guile" "hash-create-handle!") | |
4582 | @deffn primitive hash-create-handle! table key init | |
4583 | This function looks up @var{key} in @var{table} and returns its handle. | |
4584 | If @var{key} is not already present, a new handle is created which | |
4585 | associates @var{key} with @var{init}. | |
4586 | @end deffn | |
4587 | ||
4588 | @c docstring begin (texi-doc-string "guile" "hashx-create-handle!") | |
4589 | @deffn primitive hashx-create-handle! hash assoc table obj init | |
4590 | This behaves the same way as the corresponding @code{-create-handle} | |
4591 | function, but uses @var{hasher} as a | |
4592 | hash function and @var{assoc} to compare keys. @code{hasher} must | |
4593 | be a function that takes two arguments, a key to be hashed and a | |
4594 | table size. @code{assoc} must be an associator function, like | |
4595 | @code{assoc}, @code{assq} or @code{assv}. | |
4596 | @end deffn | |
4597 | ||
4598 | @c docstring begin (texi-doc-string "guile" "hash-fold") | |
4599 | @deffn primitive hash-fold proc init table | |
4600 | An iterator over hash-table elements. | |
4601 | Accumulates and returns a result by applying PROC successively. | |
4602 | The arguments to PROC are "(key value prior-result)" where key | |
4603 | and value are successive pairs from the hash table TABLE, and | |
4604 | prior-result is either INIT (for the first application of PROC) | |
4605 | or the return value of the previous application of PROC. | |
4606 | For example, @code{(hash-fold acons () tab)} will convert a hash | |
4607 | table into an a-list of key-value pairs. | |
4608 | @end deffn | |
4609 | ||
4610 | ||
4611 | @node Vectors | |
4612 | @section Vectors | |
4613 | ||
4614 | @c docstring begin (texi-doc-string "guile" "make-vector") | |
4615 | @deffn primitive make-vector k [fill] | |
4616 | Returns a newly allocated vector of @var{k} elements. If a second | |
4617 | argument is given, then each element is initialized to @var{fill}. | |
4618 | Otherwise the initial contents of each element is unspecified. (r5rs) | |
4619 | @end deffn | |
4620 | ||
4621 | @c docstring begin (texi-doc-string "guile" "vector") | |
4622 | @c docstring begin (texi-doc-string "guile" "list->vector") | |
4623 | @deffn primitive vector . l | |
4624 | @deffnx primitive list->vector l | |
4625 | Returns a newly allocated vector whose elements contain the given | |
4626 | arguments. Analogous to @samp{list}. (r5rs) | |
4627 | ||
4628 | @format | |
4629 | @t{(vector 'a 'b 'c) ==> #(a b c) } | |
4630 | @end format | |
4631 | @end deffn | |
4632 | ||
4633 | @c docstring begin (texi-doc-string "guile" "vector->list") | |
4634 | @deffn primitive vector->list v | |
4635 | @samp{Vector->list} returns a newly allocated list of the objects contained | |
4636 | in the elements of @var{vector}. (r5rs) | |
4637 | ||
4638 | @format | |
4639 | @t{(vector->list '#(dah dah didah)) | |
4640 | => (dah dah didah) | |
4641 | list->vector '(dididit dah)) | |
4642 | => #(dididit dah) | |
4643 | } | |
4644 | @end format | |
4645 | @end deffn | |
4646 | ||
4647 | @c docstring begin (texi-doc-string "guile" "vector-fill!") | |
4648 | @deffn primitive vector-fill! v fill_x | |
4649 | Stores @var{fill} in every element of @var{vector}. | |
4650 | The value returned by @samp{vector-fill!} is unspecified. (r5rs) | |
4651 | @end deffn | |
4652 | ||
4653 | @c docstring begin (texi-doc-string "guile" "vector?") | |
4654 | @deffn primitive vector? obj | |
4655 | Returns @t{#t} if @var{obj} is a vector, otherwise returns @t{#f}. (r5rs) | |
4656 | @end deffn | |
4657 | ||
4658 | ||
4659 | @node Hooks | |
4660 | @section Hooks | |
4661 | ||
4662 | @c docstring begin (texi-doc-string "guile" "make-hook-with-name") | |
4663 | @deffn primitive make-hook-with-name name [n_args] | |
4664 | Create a named hook with the name @var{name} for storing | |
4665 | procedures of arity @var{n_args}. | |
4666 | @end deffn | |
4667 | ||
4668 | @c docstring begin (texi-doc-string "guile" "make-hook") | |
4669 | @deffn primitive make-hook [n_args] | |
4670 | Create a hook for storing procedure of arity @var{n_args}. | |
4671 | @end deffn | |
4672 | ||
4673 | @c docstring begin (texi-doc-string "guile" "hook?") | |
4674 | @deffn primitive hook? x | |
4675 | Return @code{#t} if @var{x} is a hook. | |
4676 | @end deffn | |
4677 | ||
4678 | @c docstring begin (texi-doc-string "guile" "hook-empty?") | |
4679 | @deffn primitive hook-empty? hook | |
4680 | Return @code{#t} if @var{hook} is an empty hook. | |
4681 | @end deffn | |
4682 | ||
4683 | @c docstring begin (texi-doc-string "guile" "add-hook!") | |
4684 | @deffn primitive add-hook! hook proc [append_p] | |
4685 | Add the procedure @var{proc} to the hook @var{hook}. The | |
4686 | procedure is added to the end if @var{append_p} is true, | |
4687 | otherwise it is added to the front. | |
4688 | @end deffn | |
4689 | ||
4690 | @c docstring begin (texi-doc-string "guile" "remove-hook!") | |
4691 | @deffn primitive remove-hook! hook proc | |
4692 | Remove the procedure @var{proc} from the hook @var{hook}. | |
4693 | @end deffn | |
4694 | ||
4695 | @c docstring begin (texi-doc-string "guile" "reset-hook!") | |
4696 | @deffn primitive reset-hook! hook | |
4697 | Remove all procedures from the hook @var{hook}. | |
4698 | @end deffn | |
4699 | ||
4700 | @c docstring begin (texi-doc-string "guile" "run-hook") | |
4701 | @deffn primitive run-hook hook . args | |
4702 | Apply all procedures from the hook @var{hook} to the arguments | |
4703 | @var{args}. | |
4704 | @end deffn | |
4705 | ||
4706 | @c docstring begin (texi-doc-string "guile" "hook->list") | |
4707 | @deffn primitive hook->list hook | |
4708 | Convert the procedure list of @var{hook} to a list. | |
4709 | @end deffn | |
4710 | ||
4711 | ||
4712 | @node Other Data Types | |
4713 | @section Other Core Guile Data Types | |
4714 | ||
4715 | ||
4716 | @c Local Variables: | |
4717 | @c TeX-master: "guile.texi" | |
4718 | @c End: |