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