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