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