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