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