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