3 @chapter Data Types for Generic Use
5 This chapter describes all the data types that Guile provides for
8 One of the great strengths of Scheme is that there is no straightforward
9 distinction between ``data'' and ``functionality''. For example,
10 Guile's support for dynamic linking could be described
14 either in a ``data-centric'' way, as the behaviour and properties of the
15 ``dynamically linked object'' data type, and the operations that may be
16 applied to instances of this type
19 or in a ``functionality-centric'' way, as the set of procedures that
20 constitute Guile's support for dynamic linking, in the context of the
24 The contents of this chapter are, therefore, a matter of judgement. By
25 ``generic use'', we mean to select those data types whose typical use as
26 @emph{data} in a wide variety of programming contexts is more important
27 than their use in the implementation of a particular piece of
34 The table of contents for this chapter
37 The following table of contents
39 shows the data types that are documented in this chapter. The final
40 section of this chapter lists all the core Guile data types that are not
41 documented here, and provides links to the ``functionality-centric''
42 sections of this manual that cover them.
45 * Booleans:: True/false values.
46 * Numbers:: Numerical data types.
47 * Characters:: New character names.
48 * Strings:: Special things about strings.
49 * Regular Expressions:: Pattern matching and substitution.
50 * Symbols and Variables:: Manipulating the Scheme symbol table.
51 * Keywords:: Self-quoting, customizable display keywords.
52 * Pairs:: Scheme's basic building block.
53 * Lists:: Special list functions supported by Guile.
54 * Vectors:: One-dimensional arrays of Scheme objects.
57 * Arrays:: Arrays of values.
58 * Association Lists and Hash Tables:: Dictionary data types.
59 * Hooks:: User-customizable event lists.
60 * Other Data Types:: Data types that are documented elsewhere.
68 The two boolean values are @code{#t} for true and @code{#f} for false.
70 Boolean values are returned by predicate procedures, such as the general
71 equality predicates @code{eq?}, @code{eqv?} and @code{equal?}
72 (@pxref{Equality}) and numerical and string comparison operators like
73 @code{string=?} (@pxref{String Comparison}) and @code{<=}
85 (equal? "house" "houses")
94 In test condition contexts like @code{if} and @code{cond} (@pxref{if
95 cond case}), where a group of subexpressions will be evaluated only if a
96 @var{condition} expression evaluates to ``true'', ``true'' means any
97 value at all except @code{#f}.
113 A result of this asymmetry is that typical Scheme source code more often
114 uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to
115 represent an @code{if} or @code{cond} false value, whereas @code{#t} is
116 not necessary to represent an @code{if} or @code{cond} true value.
118 It is important to note that @code{#f} is @strong{not} equivalent to any
119 other Scheme value. In particular, @code{#f} is not the same as the
120 number 0 (like in C and C++), and not the same as the ``empty list''
121 (like in some Lisp dialects).
123 The @code{not} procedure returns the boolean inverse of its argument:
126 @deffn primitive not x
127 Return @code{#t} iff @var{x} is @code{#f}, else return @code{#f}.
130 The @code{boolean?} procedure is a predicate that returns @code{#t} if
131 its argument is one of the boolean values, otherwise @code{#f}.
134 @deffn primitive boolean? obj
135 Return @code{#t} iff @var{obj} is either @code{#t} or @code{#f}.
140 @section Numerical data types
143 Guile supports a rich ``tower'' of numerical types --- integer,
144 rational, real and complex --- and provides an extensive set of
145 mathematical and scientific functions for operating on numerical
146 data. This section of the manual documents those types and functions.
148 You may also find it illuminating to read R5RS's presentation of numbers
149 in Scheme, which is particularly clear and accessible: see
150 @xref{Numbers,,,r5rs}.
153 * Numerical Tower:: Scheme's numerical "tower".
154 * Integers:: Whole numbers.
155 * Reals and Rationals:: Real and rational numbers.
156 * Complex Numbers:: Complex numbers.
157 * Exactness:: Exactness and inexactness.
158 * Number Syntax:: Read syntax for numerical data.
159 * Integer Operations:: Operations on integer values.
160 * Comparison:: Comparison predicates.
161 * Conversion:: Converting numbers to and from strings.
162 * Complex:: Complex number operations.
163 * Arithmetic:: Arithmetic functions.
164 * Scientific:: Scientific functions.
165 * Primitive Numerics:: Primitive numeric functions.
166 * Bitwise Operations:: Logical AND, OR, NOT, and so on.
167 * Random:: Random number generation.
171 @node Numerical Tower
172 @subsection Scheme's Numerical ``Tower''
175 Scheme's numerical ``tower'' consists of the following categories of
180 integers (whole numbers)
183 rationals (the set of numbers that can be expressed as P/Q where P and Q
187 real numbers (the set of numbers that describes all possible positions
188 along a one dimensional line)
191 complex numbers (the set of numbers that describes all possible
192 positions in a two dimensional space)
195 It is called a tower because each category ``sits on'' the one that
196 follows it, in the sense that every integer is also a rational, every
197 rational is also real, and every real number is also a complex number
198 (but with zero imaginary part).
200 Of these, Guile implements integers, reals and complex numbers as
201 distinct types. Rationals are implemented as regards the read syntax
202 for rational numbers that is specified by R5RS, but are immediately
203 converted by Guile to the corresponding real number.
205 The @code{number?} predicate may be applied to any Scheme value to
206 discover whether the value is any of the supported numerical types.
208 @deffn primitive number? obj
209 Return @code{#t} if @var{obj} is any kind of number, @code{#f} else.
219 (number? "hello there!")
223 (define pi 3.141592654)
229 The next few subsections document each of Guile's numerical data types
235 @tpindex Integer numbers
239 Integers are whole numbers, that is numbers with no fractional part,
240 such as 2, 83 and -3789.
242 Integers in Guile can be arbitrarily big, as shown by the following
246 (define (factorial n)
247 (let loop ((n n) (product 1))
250 (loop (- n 1) (* product n)))))
262 -119622220865480194561963161495657715064383733760000000000
265 Readers whose background is in programming languages where integers are
266 limited by the need to fit into just 4 or 8 bytes of memory may find
267 this surprising, or suspect that Guile's representation of integers is
268 inefficient. In fact, Guile achieves a near optimal balance of
269 convenience and efficiency by using the host computer's native
270 representation of integers where possible, and a more general
271 representation where the required number does not fit in the native
272 form. Conversion between these two representations is automatic and
273 completely invisible to the Scheme level programmer.
275 @c REFFIXME Maybe point here to discussion of handling immediates/bignums
276 @c on the C level, where the conversion is not so automatic - NJ
278 @deffn primitive integer? x
279 Return @code{#t} if @var{x} is an integer number, @code{#f} else.
293 @node Reals and Rationals
294 @subsection Real and Rational Numbers
295 @tpindex Real numbers
296 @tpindex Rational numbers
301 Mathematically, the real numbers are the set of numbers that describe
302 all possible points along a continuous, infinite, one-dimensional line.
303 The rational numbers are the set of all numbers that can be written as
304 fractions P/Q, where P and Q are integers. All rational numbers are
305 also real, but there are real numbers that are not rational, for example
306 the square root of 2, and pi.
308 Guile represents both real and rational numbers approximately using a
309 floating point encoding with limited precision. Even though the actual
310 encoding is in binary, it may be helpful to think of it as a decimal
311 number with a limited number of significant figures and a decimal point
312 somewhere, since this corresponds to the standard notation for non-whole
313 numbers. For example:
318 -5648394822220000000000.0
322 The limited precision of Guile's encoding means that any ``real'' number
323 in Guile can be written in a rational form, by multiplying and then dividing
324 by sufficient powers of 10 (or in fact, 2). For example,
325 @code{-0.00000142857931198} is the same as @code{142857931198} divided by
326 @code{100000000000000000}. In Guile's current incarnation, therefore,
327 the @code{rational?} and @code{real?} predicates are equivalent.
329 Another aspect of this equivalence is that Guile currently does not
330 preserve the exactness that is possible with rational arithmetic.
331 If such exactness is needed, it is of course possible to implement
332 exact rational arithmetic at the Scheme level using Guile's arbitrary
335 A planned future revision of Guile's numerical tower will make it
336 possible to implement exact representations and arithmetic for both
337 rational numbers and real irrational numbers such as square roots,
338 and in such a way that the new kinds of number integrate seamlessly
339 with those that are already implemented.
341 @deffn primitive real? obj
342 Return @code{#t} if @var{obj} is a real number, @code{#f} else.
343 Note that the sets of integer and rational values form subsets
344 of the set of real numbers, so the predicate will also be fulfilled
345 if @var{obj} is an integer number or a rational number.
348 @deffn primitive rational? x
349 Return @code{#t} if @var{x} is a rational number, @code{#f}
350 else. Note that the set of integer values forms a subset of
351 the set of rational numbers, i. e. the predicate will also be
352 fulfilled if @var{x} is an integer number. Real numbers
353 will also satisfy this predicate, because of their limited
358 @node Complex Numbers
359 @subsection Complex Numbers
360 @tpindex Complex numbers
364 Complex numbers are the set of numbers that describe all possible points
365 in a two-dimensional space. The two coordinates of a particular point
366 in this space are known as the @dfn{real} and @dfn{imaginary} parts of
367 the complex number that describes that point.
369 In Guile, complex numbers are written in rectangular form as the sum of
370 their real and imaginary parts, using the symbol @code{i} to indicate
383 Guile represents a complex number as a pair of numbers both of which are
384 real, so the real and imaginary parts of a complex number have the same
385 properties of inexactness and limited precision as single real numbers.
387 @deffn primitive complex? x
388 Return @code{#t} if @var{x} is a complex number, @code{#f}
389 else. Note that the sets of real, rational and integer
390 values form subsets of the set of complex numbers, i. e. the
391 predicate will also be fulfilled if @var{x} is a real,
392 rational or integer number.
397 @subsection Exact and Inexact Numbers
398 @tpindex Exact numbers
399 @tpindex Inexact numbers
403 @rnindex exact->inexact
404 @rnindex inexact->exact
406 R5RS requires that a calculation involving inexact numbers always
407 produces an inexact result. To meet this requirement, Guile
408 distinguishes between an exact integer value such as @code{5} and the
409 corresponding inexact real value which, to the limited precision
410 available, has no fractional part, and is printed as @code{5.0}. Guile
411 will only convert the latter value to the former when forced to do so by
412 an invocation of the @code{inexact->exact} procedure.
414 @deffn primitive exact? x
415 Return @code{#t} if @var{x} is an exact number, @code{#f}
419 @deffn primitive inexact? x
420 Return @code{#t} if @var{x} is an inexact number, @code{#f}
424 @deffn primitive inexact->exact z
425 Return an exact number that is numerically closest to @var{z}.
428 @c begin (texi-doc-string "guile" "exact->inexact")
429 @deffn primitive exact->inexact z
430 Convert the number @var{z} to its inexact representation.
435 @subsection Read Syntax for Numerical Data
437 The read syntax for integers is a string of digits, optionally
438 preceded by a minus or plus character, a code indicating the
439 base in which the integer is encoded, and a code indicating whether
440 the number is exact or inexact. The supported base codes are:
444 @code{#b}, @code{#B} --- the integer is written in binary (base 2)
447 @code{#o}, @code{#O} --- the integer is written in octal (base 8)
450 @code{#d}, @code{#D} --- the integer is written in decimal (base 10)
453 @code{#x}, @code{#X} --- the integer is written in hexadecimal (base 16).
456 If the base code is omitted, the integer is assumed to be decimal. The
457 following examples show how these base codes are used.
481 The codes for indicating exactness (which can, incidentally, be applied
482 to all numerical values) are:
486 @code{#e}, @code{#E} --- the number is exact
489 @code{#i}, @code{#I} --- the number is inexact.
492 If the exactness indicator is omitted, the integer is assumed to be exact,
493 since Guile's internal representation for integers is always exact.
494 Real numbers have limited precision similar to the precision of the
495 @code{double} type in C. A consequence of the limited precision is that
496 all real numbers in Guile are also rational, since any number R with a
497 limited number of decimal places, say N, can be made into an integer by
501 @node Integer Operations
502 @subsection Operations on Integer Values
511 @deffn primitive odd? n
512 Return @code{#t} if @var{n} is an odd number, @code{#f}
516 @deffn primitive even? n
517 Return @code{#t} if @var{n} is an even number, @code{#f}
521 @c begin (texi-doc-string "guile" "quotient")
522 @deffn primitive quotient
523 Return the quotient of the numbers @var{x} and @var{y}.
526 @c begin (texi-doc-string "guile" "remainder")
527 @deffn primitive remainder
528 Return the remainder of the numbers @var{x} and @var{y}.
530 (remainder 13 4) @result{} 1
531 (remainder -13 4) @result{} -1
535 @c begin (texi-doc-string "guile" "modulo")
536 @deffn primitive modulo
537 Return the modulo of the numbers @var{x} and @var{y}.
539 (modulo 13 4) @result{} 1
540 (modulo -13 4) @result{} 3
544 @c begin (texi-doc-string "guile" "gcd")
546 Return the greatest common divisor of all arguments.
547 If called without arguments, 0 is returned.
550 @c begin (texi-doc-string "guile" "lcm")
552 Return the least common multiple of the arguments.
553 If called without arguments, 1 is returned.
558 @subsection Comparison Predicates
563 @c begin (texi-doc-string "guile" "=")
565 Return @code{#t} if all parameters are numerically equal.
568 @c begin (texi-doc-string "guile" "<")
570 Return @code{#t} if the list of parameters is monotonically
574 @c begin (texi-doc-string "guile" ">")
576 Return @code{#t} if the list of parameters is monotonically
580 @c begin (texi-doc-string "guile" "<=")
582 Return @code{#t} if the list of parameters is monotonically
586 @c begin (texi-doc-string "guile" ">=")
588 Return @code{#t} if the list of parameters is monotonically
592 @c begin (texi-doc-string "guile" "zero?")
593 @deffn primitive zero?
594 Return @code{#t} if @var{z} is an exact or inexact number equal to
598 @c begin (texi-doc-string "guile" "positive?")
599 @deffn primitive positive?
600 Return @code{#t} if @var{x} is an exact or inexact number greater than
604 @c begin (texi-doc-string "guile" "negative?")
605 @deffn primitive negative?
606 Return @code{#t} if @var{x} is an exact or inexact number less than
612 @subsection Converting Numbers To and From Strings
613 @rnindex number->string
614 @rnindex string->number
616 @deffn primitive number->string n [radix]
617 Return a string holding the external representation of the
618 number @var{n} in the given @var{radix}. If @var{n} is
619 inexact, a radix of 10 will be used.
622 @deffn primitive string->number string [radix]
623 Return a number of the maximally precise representation
624 expressed by the given @var{string}. @var{radix} must be an
625 exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}
626 is a default radix that may be overridden by an explicit radix
627 prefix in @var{string} (e.g. "#o177"). If @var{radix} is not
628 supplied, then the default radix is 10. If string is not a
629 syntactically valid notation for a number, then
630 @code{string->number} returns @code{#f}.
635 @subsection Complex Number Operations
636 @rnindex make-rectangular
643 @deffn primitive make-rectangular real imaginary
644 Return a complex number constructed of the given @var{real} and
645 @var{imaginary} parts.
648 @deffn primitive make-polar x y
649 Return the complex number @var{x} * e^(i * @var{y}).
652 @c begin (texi-doc-string "guile" "real-part")
653 @deffn primitive real-part
654 Return the real part of the number @var{z}.
657 @c begin (texi-doc-string "guile" "imag-part")
658 @deffn primitive imag-part
659 Return the imaginary part of the number @var{z}.
662 @c begin (texi-doc-string "guile" "magnitude")
663 @deffn primitive magnitude
664 Return the magnitude of the number @var{z}. This is the same as
665 @code{abs} for real arguments, but also allows complex numbers.
668 @c begin (texi-doc-string "guile" "angle")
669 @deffn primitive angle
670 Return the angle of the complex number @var{z}.
675 @subsection Arithmetic Functions
688 @c begin (texi-doc-string "guile" "+")
689 @deffn primitive + z1 @dots{}
690 Return the sum of all parameter values. Return 0 if called without any
694 @c begin (texi-doc-string "guile" "-")
695 @deffn primitive - z1 z2 @dots{}
696 If called with one argument @var{z1}, -@var{z1} is returned. Otherwise
697 the sum of all but the first argument are subtracted from the first
701 @c begin (texi-doc-string "guile" "*")
702 @deffn primitive * z1 @dots{}
703 Return the product of all arguments. If called without arguments, 1 is
707 @c begin (texi-doc-string "guile" "/")
708 @deffn primitive / z1 z2 @dots{}
709 Divide the first argument by the product of the remaining arguments. If
710 called with one argument @var{z1}, 1/@var{z1} is returned.
713 @c begin (texi-doc-string "guile" "abs")
714 @deffn primitive abs x
715 Return the absolute value of @var{x}.
718 @c begin (texi-doc-string "guile" "max")
719 @deffn primitive max x1 x2 @dots{}
720 Return the maximum of all parameter values.
723 @c begin (texi-doc-string "guile" "min")
724 @deffn primitive min x1 x2 @dots{}
725 Return the minium of all parameter values.
728 @c begin (texi-doc-string "guile" "truncate")
729 @deffn primitive truncate
730 Round the inexact number @var{x} towards zero.
733 @c begin (texi-doc-string "guile" "round")
734 @deffn primitive round x
735 Round the inexact number @var{x} towards zero.
738 @c begin (texi-doc-string "guile" "floor")
739 @deffn primitive floor x
740 Round the number @var{x} towards minus infinity.
743 @c begin (texi-doc-string "guile" "ceiling")
744 @deffn primitive ceiling x
745 Round the number @var{x} towards infinity.
750 @subsection Scientific Functions
752 The following procedures accept any kind of number as arguments,
753 including complex numbers.
756 @c begin (texi-doc-string "guile" "sqrt")
757 @deffn procedure sqrt z
758 Return the square root of @var{z}.
762 @c begin (texi-doc-string "guile" "expt")
763 @deffn procedure expt z1 z2
764 Return @var{z1} raised to the power of @var{z2}.
768 @c begin (texi-doc-string "guile" "sin")
769 @deffn procedure sin z
770 Return the sine of @var{z}.
774 @c begin (texi-doc-string "guile" "cos")
775 @deffn procedure cos z
776 Return the cosine of @var{z}.
780 @c begin (texi-doc-string "guile" "tan")
781 @deffn procedure tan z
782 Return the tangent of @var{z}.
786 @c begin (texi-doc-string "guile" "asin")
787 @deffn procedure asin z
788 Return the arcsine of @var{z}.
792 @c begin (texi-doc-string "guile" "acos")
793 @deffn procedure acos z
794 Return the arccosine of @var{z}.
798 @c begin (texi-doc-string "guile" "atan")
799 @deffn procedure atan z
800 Return the arctangent of @var{z}.
804 @c begin (texi-doc-string "guile" "exp")
805 @deffn procedure exp z
806 Return e to the power of @var{z}, where e is the base of natural
807 logarithms (2.71828@dots{}).
811 @c begin (texi-doc-string "guile" "log")
812 @deffn procedure log z
813 Return the natural logarithm of @var{z}.
816 @c begin (texi-doc-string "guile" "log10")
817 @deffn procedure log10 z
818 Return the base 10 logarithm of @var{z}.
821 @c begin (texi-doc-string "guile" "sinh")
822 @deffn procedure sinh z
823 Return the hyperbolic sine of @var{z}.
826 @c begin (texi-doc-string "guile" "cosh")
827 @deffn procedure cosh z
828 Return the hyperbolic cosine of @var{z}.
831 @c begin (texi-doc-string "guile" "tanh")
832 @deffn procedure tanh z
833 Return the hyperbolic tangent of @var{z}.
836 @c begin (texi-doc-string "guile" "asinh")
837 @deffn procedure asinh z
838 Return the hyperbolic arcsine of @var{z}.
841 @c begin (texi-doc-string "guile" "acosh")
842 @deffn procedure acosh z
843 Return the hyperbolic arccosine of @var{z}.
846 @c begin (texi-doc-string "guile" "atanh")
847 @deffn procedure atanh z
848 Return the hyperbolic arctangent of @var{z}.
852 @node Primitive Numerics
853 @subsection Primitive Numeric Functions
855 Many of Guile's numeric procedures which accept any kind of numbers as
856 arguments, including complex numbers, are implemented as Scheme
857 procedures that use the following real number-based primitives. These
858 primitives signal an error if they are called with complex arguments.
860 @c begin (texi-doc-string "guile" "$abs")
861 @deffn primitive $abs x
862 Return the absolute value of @var{x}.
865 @c begin (texi-doc-string "guile" "$sqrt")
866 @deffn primitive $sqrt x
867 Return the square root of @var{x}.
870 @deffn primitive $expt x y
871 Return @var{x} raised to the power of @var{y}. This
872 procedure does not accept complex arguments.
875 @c begin (texi-doc-string "guile" "$sin")
876 @deffn primitive $sin x
877 Return the sine of @var{x}.
880 @c begin (texi-doc-string "guile" "$cos")
881 @deffn primitive $cos x
882 Return the cosine of @var{x}.
885 @c begin (texi-doc-string "guile" "$tan")
886 @deffn primitive $tan x
887 Return the tangent of @var{x}.
890 @c begin (texi-doc-string "guile" "$asin")
891 @deffn primitive $asin x
892 Return the arcsine of @var{x}.
895 @c begin (texi-doc-string "guile" "$acos")
896 @deffn primitive $acos x
897 Return the arccosine of @var{x}.
900 @c begin (texi-doc-string "guile" "$atan")
901 @deffn primitive $atan x
902 Return the arctangent of @var{x} in the range -PI/2 to PI/2.
905 @deffn primitive $atan2 x y
906 Return the arc tangent of the two arguments @var{x} and
907 @var{y}. This is similar to calculating the arc tangent of
908 @var{x} / @var{y}, except that the signs of both arguments
909 are used to determine the quadrant of the result. This
910 procedure does not accept complex arguments.
913 @c begin (texi-doc-string "guile" "$exp")
914 @deffn primitive $exp x
915 Return e to the power of @var{x}, where e is the base of natural
916 logarithms (2.71828@dots{}).
919 @c begin (texi-doc-string "guile" "$log")
920 @deffn primitive $log x
921 Return the natural logarithm of @var{x}.
924 @c begin (texi-doc-string "guile" "$sinh")
925 @deffn primitive $sinh x
926 Return the hyperbolic sine of @var{x}.
929 @c begin (texi-doc-string "guile" "$cosh")
930 @deffn primitive $cosh x
931 Return the hyperbolic cosine of @var{x}.
934 @c begin (texi-doc-string "guile" "$tanh")
935 @deffn primitive $tanh x
936 Return the hyperbolic tangent of @var{x}.
939 @c begin (texi-doc-string "guile" "$asinh")
940 @deffn primitive $asinh x
941 Return the hyperbolic arcsine of @var{x}.
944 @c begin (texi-doc-string "guile" "$acosh")
945 @deffn primitive $acosh x
946 Return the hyperbolic arccosine of @var{x}.
949 @c begin (texi-doc-string "guile" "$atanh")
950 @deffn primitive $atanh x
951 Return the hyperbolic arctangent of @var{x}.
955 @node Bitwise Operations
956 @subsection Bitwise Operations
958 @deffn primitive logand n1 n2
959 Return the integer which is the bit-wise AND of the two integer
963 (number->string (logand #b1100 #b1010) 2)
968 @deffn primitive logior n1 n2
969 Return the integer which is the bit-wise OR of the two integer
973 (number->string (logior #b1100 #b1010) 2)
978 @deffn primitive logxor n1 n2
979 Return the integer which is the bit-wise XOR of the two integer
983 (number->string (logxor #b1100 #b1010) 2)
988 @deffn primitive lognot n
989 Return the integer which is the 2s-complement of the integer
993 (number->string (lognot #b10000000) 2)
994 @result{} "-10000001"
995 (number->string (lognot #b0) 2)
1000 @deffn primitive logtest j k
1002 (logtest j k) @equiv{} (not (zero? (logand j k)))
1004 (logtest #b0100 #b1011) @result{} #f
1005 (logtest #b0100 #b0111) @result{} #t
1009 @deffn primitive logbit? index j
1011 (logbit? index j) @equiv{} (logtest (integer-expt 2 index) j)
1013 (logbit? 0 #b1101) @result{} #t
1014 (logbit? 1 #b1101) @result{} #f
1015 (logbit? 2 #b1101) @result{} #t
1016 (logbit? 3 #b1101) @result{} #t
1017 (logbit? 4 #b1101) @result{} #f
1021 @deffn primitive ash n cnt
1022 The function ash performs an arithmetic shift left by @var{cnt}
1023 bits (or shift right, if @var{cnt} is negative). 'Arithmetic'
1024 means, that the function does not guarantee to keep the bit
1025 structure of @var{n}, but rather guarantees that the result
1026 will always be rounded towards minus infinity. Therefore, the
1027 results of ash and a corresponding bitwise shift will differ if
1028 @var{n} is negative.
1030 Formally, the function returns an integer equivalent to
1031 @code{(inexact->exact (floor (* @var{n} (expt 2 @var{cnt}))))}.
1034 (number->string (ash #b1 3) 2) @result{} "1000"
1035 (number->string (ash #b1010 -1) 2) @result{} "101"
1039 @deffn primitive logcount n
1040 Return the number of bits in integer @var{n}. If integer is
1041 positive, the 1-bits in its binary representation are counted.
1042 If negative, the 0-bits in its two's-complement binary
1043 representation are counted. If 0, 0 is returned.
1046 (logcount #b10101010)
1055 @deffn primitive integer-length n
1056 Return the number of bits neccessary to represent @var{n}.
1059 (integer-length #b10101010)
1063 (integer-length #b1111)
1068 @deffn primitive integer-expt n k
1069 Return @var{n} raised to the non-negative integer exponent
1080 @deffn primitive bit-extract n start end
1081 Return the integer composed of the @var{start} (inclusive)
1082 through @var{end} (exclusive) bits of @var{n}. The
1083 @var{start}th bit becomes the 0-th bit in the result.
1086 (number->string (bit-extract #b1101101010 0 4) 2)
1088 (number->string (bit-extract #b1101101010 4 9) 2)
1095 @subsection Random Number Generation
1097 @deffn primitive copy-random-state [state]
1098 Return a copy of the random state @var{state}.
1101 @deffn primitive random n [state]
1102 Return a number in [0,N).
1104 Accepts a positive integer or real n and returns a
1105 number of the same type between zero (inclusive) and
1106 N (exclusive). The values returned have a uniform
1109 The optional argument @var{state} must be of the type produced
1110 by @code{seed->random-state}. It defaults to the value of the
1111 variable @var{*random-state*}. This object is used to maintain
1112 the state of the pseudo-random-number generator and is altered
1113 as a side effect of the random operation.
1116 @deffn primitive random:exp [state]
1117 Return an inexact real in an exponential distribution with mean
1118 1. For an exponential distribution with mean u use (* u
1122 @deffn primitive random:hollow-sphere! v [state]
1123 Fills vect with inexact real random numbers
1124 the sum of whose squares is equal to 1.0.
1125 Thinking of vect as coordinates in space of
1126 dimension n = (vector-length vect), the coordinates
1127 are uniformly distributed over the surface of the
1131 @deffn primitive random:normal [state]
1132 Return an inexact real in a normal distribution. The
1133 distribution used has mean 0 and standard deviation 1. For a
1134 normal distribution with mean m and standard deviation d use
1135 @code{(+ m (* d (random:normal)))}.
1138 @deffn primitive random:normal-vector! v [state]
1139 Fills vect with inexact real random numbers that are
1140 independent and standard normally distributed
1141 (i.e., with mean 0 and variance 1).
1144 @deffn primitive random:solid-sphere! v [state]
1145 Fills vect with inexact real random numbers
1146 the sum of whose squares is less than 1.0.
1147 Thinking of vect as coordinates in space of
1148 dimension n = (vector-length vect), the coordinates
1149 are uniformly distributed within the unit n-shere.
1150 The sum of the squares of the numbers is returned.
1153 @deffn primitive random:uniform [state]
1154 Return a uniformly distributed inexact real random number in
1158 @deffn primitive seed->random-state seed
1159 Return a new random state using @var{seed}.
1167 Most of the characters in the ASCII character set may be referred to by
1168 name: for example, @code{#\tab}, @code{#\esc}, @code{#\stx}, and so on.
1169 The following table describes the ASCII names for each character.
1171 @multitable @columnfractions .25 .25 .25 .25
1172 @item 0 = @code{#\nul}
1173 @tab 1 = @code{#\soh}
1174 @tab 2 = @code{#\stx}
1175 @tab 3 = @code{#\etx}
1176 @item 4 = @code{#\eot}
1177 @tab 5 = @code{#\enq}
1178 @tab 6 = @code{#\ack}
1179 @tab 7 = @code{#\bel}
1180 @item 8 = @code{#\bs}
1181 @tab 9 = @code{#\ht}
1182 @tab 10 = @code{#\nl}
1183 @tab 11 = @code{#\vt}
1184 @item 12 = @code{#\np}
1185 @tab 13 = @code{#\cr}
1186 @tab 14 = @code{#\so}
1187 @tab 15 = @code{#\si}
1188 @item 16 = @code{#\dle}
1189 @tab 17 = @code{#\dc1}
1190 @tab 18 = @code{#\dc2}
1191 @tab 19 = @code{#\dc3}
1192 @item 20 = @code{#\dc4}
1193 @tab 21 = @code{#\nak}
1194 @tab 22 = @code{#\syn}
1195 @tab 23 = @code{#\etb}
1196 @item 24 = @code{#\can}
1197 @tab 25 = @code{#\em}
1198 @tab 26 = @code{#\sub}
1199 @tab 27 = @code{#\esc}
1200 @item 28 = @code{#\fs}
1201 @tab 29 = @code{#\gs}
1202 @tab 30 = @code{#\rs}
1203 @tab 31 = @code{#\us}
1204 @item 32 = @code{#\sp}
1207 The @code{delete} character (octal 177) may be referred to with the name
1210 Several characters have more than one name:
1214 @code{#\space}, @code{#\sp}
1216 @code{#\newline}, @code{#\nl}
1218 @code{#\tab}, @code{#\ht}
1220 @code{#\backspace}, @code{#\bs}
1222 @code{#\return}, @code{#\cr}
1224 @code{#\page}, @code{#\np}
1226 @code{#\null}, @code{#\nul}
1230 @deffn primitive char? x
1231 Return @code{#t} iff @var{x} is a character, else @code{#f}.
1235 @deffn primitive char=? x y
1236 Return @code{#t} iff @var{x} is the same character as @var{y}, else @code{#f}.
1240 @deffn primitive char<? x y
1241 Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence,
1246 @deffn primitive char<=? x y
1247 Return @code{#t} iff @var{x} is less than or equal to @var{y} in the
1248 ASCII sequence, else @code{#f}.
1252 @deffn primitive char>? x y
1253 Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII
1254 sequence, else @code{#f}.
1258 @deffn primitive char>=? x y
1259 Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the
1260 ASCII sequence, else @code{#f}.
1264 @deffn primitive char-ci=? x y
1265 Return @code{#t} iff @var{x} is the same character as @var{y} ignoring
1266 case, else @code{#f}.
1270 @deffn primitive char-ci<? x y
1271 Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence
1272 ignoring case, else @code{#f}.
1276 @deffn primitive char-ci<=? x y
1277 Return @code{#t} iff @var{x} is less than or equal to @var{y} in the
1278 ASCII sequence ignoring case, else @code{#f}.
1282 @deffn primitive char-ci>? x y
1283 Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII
1284 sequence ignoring case, else @code{#f}.
1288 @deffn primitive char-ci>=? x y
1289 Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the
1290 ASCII sequence ignoring case, else @code{#f}.
1293 @rnindex char-alphabetic?
1294 @deffn primitive char-alphabetic? chr
1295 Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.
1296 Alphabetic means the same thing as the isalpha C library function.
1299 @rnindex char-numeric?
1300 @deffn primitive char-numeric? chr
1301 Return @code{#t} iff @var{chr} is numeric, else @code{#f}.
1302 Numeric means the same thing as the isdigit C library function.
1305 @rnindex char-whitespace?
1306 @deffn primitive char-whitespace? chr
1307 Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.
1308 Whitespace means the same thing as the isspace C library function.
1311 @rnindex char-upper-case?
1312 @deffn primitive char-upper-case? chr
1313 Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.
1314 Uppercase means the same thing as the isupper C library function.
1317 @rnindex char-lower-case?
1318 @deffn primitive char-lower-case? chr
1319 Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.
1320 Lowercase means the same thing as the islower C library function.
1323 @deffn primitive char-is-both? chr
1324 Return @code{#t} iff @var{chr} is either uppercase or lowercase, else @code{#f}.
1325 Uppercase and lowercase are as defined by the isupper and islower
1326 C library functions.
1329 @rnindex char->integer
1330 @deffn primitive char->integer chr
1331 Return the number corresponding to ordinal position of @var{chr} in the
1335 @rnindex integer->char
1336 @deffn primitive integer->char n
1337 Return the character at position @var{n} in the ASCII sequence.
1340 @rnindex char-upcase
1341 @deffn primitive char-upcase chr
1342 Return the uppercase character version of @var{chr}.
1345 @rnindex char-downcase
1346 @deffn primitive char-downcase chr
1347 Return the lowercase character version of @var{chr}.
1355 Strings are fixed-length sequences of characters. They can be created
1356 by calling constructor procedures, but they can also literally get
1357 entered at the REPL or in Scheme source files.
1359 Guile provides a rich set of string processing procedures, because text
1360 handling is very important when Guile is used as a scripting language.
1362 Strings always carry the information about how many characters they are
1363 composed of with them, so there is no special end-of-string character,
1364 like in C. That means that Scheme strings can contain any character,
1365 even the NUL character @code{'\0'}. But note: Since most operating
1366 system calls dealing with strings (such as for file operations) expect
1367 strings to be zero-terminated, they might do unexpected things when
1368 called with string containing unusal characters.
1371 * String Syntax:: Read syntax for strings.
1372 * String Predicates:: Testing strings for certain properties.
1373 * String Constructors:: Creating new string objects.
1374 * List/String Conversion:: Converting from/to lists of characters.
1375 * String Selection:: Select portions from strings.
1376 * String Modification:: Modify parts or whole strings.
1377 * String Comparison:: Lexicographic ordering predicates.
1378 * String Searching:: Searching in strings.
1379 * Alphabetic Case Mapping:: Convert the alphabetic case of strings.
1380 * Appending Strings:: Appending strings to form a new string.
1381 * String Miscellanea:: Miscellaneous string procedures.
1385 @subsection String Read Syntax
1387 The read syntax for strings is an arbitrarily long sequence of
1388 characters enclosed in double quotes (@code{"}). @footnote{Actually, the
1389 current implementation restricts strings to a length of 2^24
1390 characters.} If you want to insert a double quote character into a
1391 string literal, it must be prefixed with a backslash @code{\} character
1392 (called an @emph{escape character}).
1394 The following are examples of string literals:
1403 @c FIXME::martin: What about escape sequences like \r, \n etc.?
1405 @node String Predicates
1406 @subsection String Predicates
1408 The following procedures can be used to check whether a given string
1409 fulfills some specified property.
1412 @deffn primitive string? obj
1413 Return @code{#t} iff @var{obj} is a string, else returns
1417 @deffn primitive string-null? str
1418 Return @code{#t} if @var{str}'s length is nonzero, and
1419 @code{#f} otherwise.
1421 (string-null? "") @result{} #t
1423 (string-null? y) @result{} #f
1427 @node String Constructors
1428 @subsection String Constructors
1430 The string constructor procedures create new string objects, possibly
1431 initializing them with some specified character data.
1433 @c FIXME::martin: list->string belongs into `List/String Conversion'
1436 @rnindex list->string
1437 @deffn primitive string . chrs
1438 @deffnx primitive list->string chrs
1439 Return a newly allocated string composed of the arguments,
1443 @rnindex make-string
1444 @deffn primitive make-string k [chr]
1445 Return a newly allocated string of
1446 length @var{k}. If @var{chr} is given, then all elements of
1447 the string are initialized to @var{chr}, otherwise the contents
1448 of the @var{string} are unspecified.
1451 @node List/String Conversion
1452 @subsection List/String conversion
1454 When processing strings, it is often convenient to first convert them
1455 into a list representation by using the procedure @code{string->list},
1456 work with the resulting list, and then convert it back into a string.
1457 These procedures are useful for similar tasks.
1459 @rnindex string->list
1460 @deffn primitive string->list str
1461 Return a newly allocated list of the characters that make up
1462 the given string @var{str}. @code{string->list} and
1463 @code{list->string} are inverses as far as @samp{equal?} is
1467 @deffn primitive string-split str chr
1468 Split the string @var{str} into the a list of the substrings delimited
1469 by appearances of the character @var{chr}. Note that an empty substring
1470 between separator characters will result in an empty string in the
1473 (string-split "root:x:0:0:root:/root:/bin/bash" #\:)
1475 ("root" "x" "0" "0" "root" "/root" "/bin/bash")
1477 (string-split "::" #\:)
1481 (string-split "" #\:)
1488 @node String Selection
1489 @subsection String Selection
1491 Portions of strings can be extracted by these procedures.
1492 @code{string-ref} delivers individual characters whereas
1493 @code{substring} can be used to extract substrings from longer strings.
1495 @rnindex string-length
1496 @deffn primitive string-length string
1497 Return the number of characters in @var{string}.
1501 @deffn primitive string-ref str k
1502 Return character @var{k} of @var{str} using zero-origin
1503 indexing. @var{k} must be a valid index of @var{str}.
1506 @rnindex string-copy
1507 @deffn primitive string-copy str
1508 Return a newly allocated copy of the given @var{string}.
1512 @deffn primitive substring str start [end]
1513 Return a newly allocated string formed from the characters
1514 of @var{str} beginning with index @var{start} (inclusive) and
1515 ending with index @var{end} (exclusive).
1516 @var{str} must be a string, @var{start} and @var{end} must be
1517 exact integers satisfying:
1519 0 <= @var{start} <= @var{end} <= (string-length @var{str}).
1522 @node String Modification
1523 @subsection String Modification
1525 These procedures are for modifying strings in-place. That means, that
1526 not a new string is the result of a string operation, but that the
1527 actual memory representation of a string is modified.
1529 @rnindex string-set!
1530 @deffn primitive string-set! str k chr
1531 Store @var{chr} in element @var{k} of @var{str} and return
1532 an unspecified value. @var{k} must be a valid index of
1536 @rnindex string-fill!
1537 @deffn primitive string-fill! str chr
1538 Store @var{char} in every element of the given @var{string} and
1539 return an unspecified value.
1542 @deffn primitive substring-fill! str start end fill
1543 Change every character in @var{str} between @var{start} and
1544 @var{end} to @var{fill}.
1547 (define y "abcdefg")
1548 (substring-fill! y 1 3 #\r)
1554 @deffn primitive substring-move! str1 start1 end1 str2 start2
1555 @deffnx primitive substring-move-left! str1 start1 end1 str2 start2
1556 @deffnx primitive substring-move-right! str1 start1 end1 str2 start2
1557 Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}
1558 into @var{str2} beginning at position @var{end2}.
1559 @code{substring-move-right!} begins copying from the rightmost character
1560 and moves left, and @code{substring-move-left!} copies from the leftmost
1561 character moving right.
1563 It is useful to have two functions that copy in different directions so
1564 that substrings can be copied back and forth within a single string. If
1565 you wish to copy text from the left-hand side of a string to the
1566 right-hand side of the same string, and the source and destination
1567 overlap, you must be careful to copy the rightmost characters of the
1568 text first, to avoid clobbering your data. Hence, when @var{str1} and
1569 @var{str2} are the same string, you should use
1570 @code{substring-move-right!} when moving text from left to right, and
1571 @code{substring-move-left!} otherwise. If @code{str1} and @samp{str2}
1572 are different strings, it does not matter which function you use.
1575 (define x (make-string 10 #\a))
1577 (substring-move-left! x 2 5 y 0)
1582 @result{} "aaaaaaaaaa"
1585 (substring-move-left! x 2 5 y 0)
1589 (define y "abcdefg")
1590 (substring-move-left! y 2 5 y 3)
1594 (define y "abcdefg")
1595 (substring-move-right! y 2 5 y 0)
1599 (define y "abcdefg")
1600 (substring-move-right! y 2 5 y 3)
1607 @node String Comparison
1608 @subsection String Comparison
1610 The procedures in this section are similar to the character ordering
1611 predicates (@pxref{Characters}), but are defined on character sequences.
1612 They all return @code{#t} on success and @code{#f} on failure. The
1613 predicates ending in @code{-ci} ignore the character case when comparing
1618 @deffn primitive string=? s1 s2
1619 Lexicographic equality predicate; return @code{#t} if the two
1620 strings are the same length and contain the same characters in
1621 the same positions, otherwise return @code{#f}.
1623 The procedure @code{string-ci=?} treats upper and lower case
1624 letters as though they were the same character, but
1625 @code{string=?} treats upper and lower case as distinct
1630 @deffn primitive string<? s1 s2
1631 Lexicographic ordering predicate; return @code{#t} if @var{s1}
1632 is lexicographically less than @var{s2}.
1636 @deffn primitive string<=? s1 s2
1637 Lexicographic ordering predicate; return @code{#t} if @var{s1}
1638 is lexicographically less than or equal to @var{s2}.
1642 @deffn primitive string>? s1 s2
1643 Lexicographic ordering predicate; return @code{#t} if @var{s1}
1644 is lexicographically greater than @var{s2}.
1648 @deffn primitive string>=? s1 s2
1649 Lexicographic ordering predicate; return @code{#t} if @var{s1}
1650 is lexicographically greater than or equal to @var{s2}.
1653 @rnindex string-ci=?
1654 @deffn primitive string-ci=? s1 s2
1655 Case-insensitive string equality predicate; return @code{#t} if
1656 the two strings are the same length and their component
1657 characters match (ignoring case) at each position; otherwise
1662 @deffn primitive string-ci<? s1 s2
1663 Case insensitive lexicographic ordering predicate; return
1664 @code{#t} if @var{s1} is lexicographically less than @var{s2}
1669 @deffn primitive string-ci<=? s1 s2
1670 Case insensitive lexicographic ordering predicate; return
1671 @code{#t} if @var{s1} is lexicographically less than or equal
1672 to @var{s2} regardless of case.
1675 @rnindex string-ci>?
1676 @deffn primitive string-ci>? s1 s2
1677 Case insensitive lexicographic ordering predicate; return
1678 @code{#t} if @var{s1} is lexicographically greater than
1679 @var{s2} regardless of case.
1682 @rnindex string-ci>=?
1683 @deffn primitive string-ci>=? s1 s2
1684 Case insensitive lexicographic ordering predicate; return
1685 @code{#t} if @var{s1} is lexicographically greater than or
1686 equal to @var{s2} regardless of case.
1690 @node String Searching
1691 @subsection String Searching
1693 When searching the index of a character in a string, these procedures
1696 @deffn primitive string-index str chr [frm [to]]
1697 Return the index of the first occurrence of @var{chr} in
1698 @var{str}. The optional integer arguments @var{frm} and
1699 @var{to} limit the search to a portion of the string. This
1700 procedure essentially implements the @code{index} or
1701 @code{strchr} functions from the C library.
1704 (string-index "weiner" #\e)
1707 (string-index "weiner" #\e 2)
1710 (string-index "weiner" #\e 2 4)
1715 @deffn primitive string-rindex str chr [frm [to]]
1716 Like @code{string-index}, but search from the right of the
1717 string rather than from the left. This procedure essentially
1718 implements the @code{rindex} or @code{strrchr} functions from
1722 (string-rindex "weiner" #\e)
1725 (string-rindex "weiner" #\e 2 4)
1728 (string-rindex "weiner" #\e 2 5)
1733 @node Alphabetic Case Mapping
1734 @subsection Alphabetic Case Mapping
1736 These are procedures for mapping strings to their upper- or lower-case
1737 equivalents, respectively, or for capitalizing strings.
1739 @deffn primitive string-upcase str
1740 Return a freshly allocated string containing the characters of
1741 @var{str} in upper case.
1744 @deffn primitive string-upcase! str
1745 Destructively upcase every character in @var{str} and return
1748 y @result{} "arrdefg"
1749 (string-upcase! y) @result{} "ARRDEFG"
1750 y @result{} "ARRDEFG"
1754 @deffn primitive string-downcase str
1755 Return a freshly allocation string containing the characters in
1756 @var{str} in lower case.
1759 @deffn primitive string-downcase! str
1760 Destructively downcase every character in @var{str} and return
1763 y @result{} "ARRDEFG"
1764 (string-downcase! y) @result{} "arrdefg"
1765 y @result{} "arrdefg"
1769 @deffn primitive string-capitalize str
1770 Return a freshly allocated string with the characters in
1771 @var{str}, where the first character of every word is
1775 @deffn primitive string-capitalize! str
1776 Upcase the first character of every word in @var{str}
1777 destructively and return @var{str}.
1780 y @result{} "hello world"
1781 (string-capitalize! y) @result{} "Hello World"
1782 y @result{} "Hello World"
1787 @node Appending Strings
1788 @subsection Appending Strings
1790 The procedure @code{string-append} appends several strings together to
1791 form a longer result string.
1793 @rnindex string-append
1794 @deffn primitive string-append string1 @dots{}
1795 Return a newly allocated string whose characters form the
1796 concatenation of the given strings.
1800 @node String Miscellanea
1801 @subsection String Miscellanea
1803 This section contains all remaining string procedures.
1805 @deffn primitive string-ci->symbol str
1806 Return the symbol whose name is @var{str}. @var{str} is
1807 converted to lowercase before the conversion is done, if Guile
1808 is currently reading symbols case-insensitively.
1812 @node Regular Expressions
1813 @section Regular Expressions
1814 @tpindex Regular expressions
1816 @cindex regular expressions
1818 @cindex emacs regexp
1820 A @dfn{regular expression} (or @dfn{regexp}) is a pattern that
1821 describes a whole class of strings. A full description of regular
1822 expressions and their syntax is beyond the scope of this manual;
1823 an introduction can be found in the Emacs manual (@pxref{Regexps,
1824 , Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, or
1825 in many general Unix reference books.
1827 If your system does not include a POSIX regular expression library, and
1828 you have not linked Guile with a third-party regexp library such as Rx,
1829 these functions will not be available. You can tell whether your Guile
1830 installation includes regular expression support by checking whether the
1831 @code{*features*} list includes the @code{regex} symbol.
1834 * Regexp Functions:: Functions that create and match regexps.
1835 * Match Structures:: Finding what was matched by a regexp.
1836 * Backslash Escapes:: Removing the special meaning of regexp metacharacters.
1837 * Rx Interface:: Tom Lord's Rx library does things differently.
1840 [FIXME: it may be useful to include an Examples section. Parts of this
1841 interface are bewildering on first glance.]
1843 @node Regexp Functions
1844 @subsection Regexp Functions
1846 By default, Guile supports POSIX extended regular expressions.
1847 That means that the characters @samp{(}, @samp{)}, @samp{+} and
1848 @samp{?} are special, and must be escaped if you wish to match the
1851 This regular expression interface was modeled after that
1852 implemented by SCSH, the Scheme Shell. It is intended to be
1853 upwardly compatible with SCSH regular expressions.
1855 @c begin (scm-doc-string "regex.scm" "string-match")
1856 @deffn procedure string-match pattern str [start]
1857 Compile the string @var{pattern} into a regular expression and compare
1858 it with @var{str}. The optional numeric argument @var{start} specifies
1859 the position of @var{str} at which to begin matching.
1861 @code{string-match} returns a @dfn{match structure} which
1862 describes what, if anything, was matched by the regular
1863 expression. @xref{Match Structures}. If @var{str} does not match
1864 @var{pattern} at all, @code{string-match} returns @code{#f}.
1867 Each time @code{string-match} is called, it must compile its
1868 @var{pattern} argument into a regular expression structure. This
1869 operation is expensive, which makes @code{string-match} inefficient if
1870 the same regular expression is used several times (for example, in a
1871 loop). For better performance, you can compile a regular expression in
1872 advance and then match strings against the compiled regexp.
1874 @deffn primitive make-regexp pat . flags
1875 Compile the regular expression described by @var{pat}, and
1876 return the compiled regexp structure. If @var{pat} does not
1877 describe a legal regular expression, @code{make-regexp} throws
1878 a @code{regular-expression-syntax} error.
1880 The @var{flags} arguments change the behavior of the compiled
1881 regular expression. The following flags may be supplied:
1885 Consider uppercase and lowercase letters to be the same when
1887 @item regexp/newline
1888 If a newline appears in the target string, then permit the
1889 @samp{^} and @samp{$} operators to match immediately after or
1890 immediately before the newline, respectively. Also, the
1891 @samp{.} and @samp{[^...]} operators will never match a newline
1892 character. The intent of this flag is to treat the target
1893 string as a buffer containing many lines of text, and the
1894 regular expression as a pattern that may match a single one of
1897 Compile a basic (``obsolete'') regexp instead of the extended
1898 (``modern'') regexps that are the default. Basic regexps do
1899 not consider @samp{|}, @samp{+} or @samp{?} to be special
1900 characters, and require the @samp{@{...@}} and @samp{(...)}
1901 metacharacters to be backslash-escaped (@pxref{Backslash
1902 Escapes}). There are several other differences between basic
1903 and extended regular expressions, but these are the most
1905 @item regexp/extended
1906 Compile an extended regular expression rather than a basic
1907 regexp. This is the default behavior; this flag will not
1908 usually be needed. If a call to @code{make-regexp} includes
1909 both @code{regexp/basic} and @code{regexp/extended} flags, the
1910 one which comes last will override the earlier one.
1914 @deffn primitive regexp-exec rx str [start [flags]]
1915 Match the compiled regular expression @var{rx} against
1916 @code{str}. If the optional integer @var{start} argument is
1917 provided, begin matching from that position in the string.
1918 Return a match structure describing the results of the match,
1919 or @code{#f} if no match could be found.
1922 @deffn primitive regexp? obj
1923 Return @code{#t} if @var{obj} is a compiled regular expression,
1924 or @code{#f} otherwise.
1927 Regular expressions are commonly used to find patterns in one string and
1928 replace them with the contents of another string.
1930 @c begin (scm-doc-string "regex.scm" "regexp-substitute")
1931 @deffn procedure regexp-substitute port match [item@dots{}]
1932 Write to the output port @var{port} selected contents of the match
1933 structure @var{match}. Each @var{item} specifies what should be
1934 written, and may be one of the following arguments:
1938 A string. String arguments are written out verbatim.
1941 An integer. The submatch with that number is written.
1944 The symbol @samp{pre}. The portion of the matched string preceding
1945 the regexp match is written.
1948 The symbol @samp{post}. The portion of the matched string following
1949 the regexp match is written.
1952 @var{port} may be @code{#f}, in which case nothing is written; instead,
1953 @code{regexp-substitute} constructs a string from the specified
1954 @var{item}s and returns that.
1957 @c begin (scm-doc-string "regex.scm" "regexp-substitute")
1958 @deffn procedure regexp-substitute/global port regexp target [item@dots{}]
1959 Similar to @code{regexp-substitute}, but can be used to perform global
1960 substitutions on @var{str}. Instead of taking a match structure as an
1961 argument, @code{regexp-substitute/global} takes two string arguments: a
1962 @var{regexp} string describing a regular expression, and a @var{target}
1963 string which should be matched against this regular expression.
1965 Each @var{item} behaves as in @var{regexp-substitute}, with the
1966 following exceptions:
1970 A function may be supplied. When this function is called, it will be
1971 passed one argument: a match structure for a given regular expression
1972 match. It should return a string to be written out to @var{port}.
1975 The @samp{post} symbol causes @code{regexp-substitute/global} to recurse
1976 on the unmatched portion of @var{str}. This @emph{must} be supplied in
1977 order to perform global search-and-replace on @var{str}; if it is not
1978 present among the @var{item}s, then @code{regexp-substitute/global} will
1979 return after processing a single match.
1983 @node Match Structures
1984 @subsection Match Structures
1986 @cindex match structures
1988 A @dfn{match structure} is the object returned by @code{string-match} and
1989 @code{regexp-exec}. It describes which portion of a string, if any,
1990 matched the given regular expression. Match structures include: a
1991 reference to the string that was checked for matches; the starting and
1992 ending positions of the regexp match; and, if the regexp included any
1993 parenthesized subexpressions, the starting and ending positions of each
1996 In each of the regexp match functions described below, the @code{match}
1997 argument must be a match structure returned by a previous call to
1998 @code{string-match} or @code{regexp-exec}. Most of these functions
1999 return some information about the original target string that was
2000 matched against a regular expression; we will call that string
2001 @var{target} for easy reference.
2003 @c begin (scm-doc-string "regex.scm" "regexp-match?")
2004 @deffn procedure regexp-match? obj
2005 Return @code{#t} if @var{obj} is a match structure returned by a
2006 previous call to @code{regexp-exec}, or @code{#f} otherwise.
2009 @c begin (scm-doc-string "regex.scm" "match:substring")
2010 @deffn procedure match:substring match [n]
2011 Return the portion of @var{target} matched by subexpression number
2012 @var{n}. Submatch 0 (the default) represents the entire regexp match.
2013 If the regular expression as a whole matched, but the subexpression
2014 number @var{n} did not match, return @code{#f}.
2017 @c begin (scm-doc-string "regex.scm" "match:start")
2018 @deffn procedure match:start match [n]
2019 Return the starting position of submatch number @var{n}.
2022 @c begin (scm-doc-string "regex.scm" "match:end")
2023 @deffn procedure match:end match [n]
2024 Return the ending position of submatch number @var{n}.
2027 @c begin (scm-doc-string "regex.scm" "match:prefix")
2028 @deffn procedure match:prefix match
2029 Return the unmatched portion of @var{target} preceding the regexp match.
2032 @c begin (scm-doc-string "regex.scm" "match:suffix")
2033 @deffn procedure match:suffix match
2034 Return the unmatched portion of @var{target} following the regexp match.
2037 @c begin (scm-doc-string "regex.scm" "match:count")
2038 @deffn procedure match:count match
2039 Return the number of parenthesized subexpressions from @var{match}.
2040 Note that the entire regular expression match itself counts as a
2041 subexpression, and failed submatches are included in the count.
2044 @c begin (scm-doc-string "regex.scm" "match:string")
2045 @deffn procedure match:string match
2046 Return the original @var{target} string.
2049 @node Backslash Escapes
2050 @subsection Backslash Escapes
2052 Sometimes you will want a regexp to match characters like @samp{*} or
2053 @samp{$} exactly. For example, to check whether a particular string
2054 represents a menu entry from an Info node, it would be useful to match
2055 it against a regexp like @samp{^* [^:]*::}. However, this won't work;
2056 because the asterisk is a metacharacter, it won't match the @samp{*} at
2057 the beginning of the string. In this case, we want to make the first
2060 You can do this by preceding the metacharacter with a backslash
2061 character @samp{\}. (This is also called @dfn{quoting} the
2062 metacharacter, and is known as a @dfn{backslash escape}.) When Guile
2063 sees a backslash in a regular expression, it considers the following
2064 glyph to be an ordinary character, no matter what special meaning it
2065 would ordinarily have. Therefore, we can make the above example work by
2066 changing the regexp to @samp{^\* [^:]*::}. The @samp{\*} sequence tells
2067 the regular expression engine to match only a single asterisk in the
2070 Since the backslash is itself a metacharacter, you may force a regexp to
2071 match a backslash in the target string by preceding the backslash with
2072 itself. For example, to find variable references in a @TeX{} program,
2073 you might want to find occurrences of the string @samp{\let\} followed
2074 by any number of alphabetic characters. The regular expression
2075 @samp{\\let\\[A-Za-z]*} would do this: the double backslashes in the
2076 regexp each match a single backslash in the target string.
2078 @c begin (scm-doc-string "regex.scm" "regexp-quote")
2079 @deffn procedure regexp-quote str
2080 Quote each special character found in @var{str} with a backslash, and
2081 return the resulting string.
2084 @strong{Very important:} Using backslash escapes in Guile source code
2085 (as in Emacs Lisp or C) can be tricky, because the backslash character
2086 has special meaning for the Guile reader. For example, if Guile
2087 encounters the character sequence @samp{\n} in the middle of a string
2088 while processing Scheme code, it replaces those characters with a
2089 newline character. Similarly, the character sequence @samp{\t} is
2090 replaced by a horizontal tab. Several of these @dfn{escape sequences}
2091 are processed by the Guile reader before your code is executed.
2092 Unrecognized escape sequences are ignored: if the characters @samp{\*}
2093 appear in a string, they will be translated to the single character
2096 This translation is obviously undesirable for regular expressions, since
2097 we want to be able to include backslashes in a string in order to
2098 escape regexp metacharacters. Therefore, to make sure that a backslash
2099 is preserved in a string in your Guile program, you must use @emph{two}
2100 consecutive backslashes:
2103 (define Info-menu-entry-pattern (make-regexp "^\\* [^:]*"))
2106 The string in this example is preprocessed by the Guile reader before
2107 any code is executed. The resulting argument to @code{make-regexp} is
2108 the string @samp{^\* [^:]*}, which is what we really want.
2110 This also means that in order to write a regular expression that matches
2111 a single backslash character, the regular expression string in the
2112 source code must include @emph{four} backslashes. Each consecutive pair
2113 of backslashes gets translated by the Guile reader to a single
2114 backslash, and the resulting double-backslash is interpreted by the
2115 regexp engine as matching a single backslash character. Hence:
2118 (define tex-variable-pattern (make-regexp "\\\\let\\\\=[A-Za-z]*"))
2121 The reason for the unwieldiness of this syntax is historical. Both
2122 regular expression pattern matchers and Unix string processing systems
2123 have traditionally used backslashes with the special meanings
2124 described above. The POSIX regular expression specification and ANSI C
2125 standard both require these semantics. Attempting to abandon either
2126 convention would cause other kinds of compatibility problems, possibly
2127 more severe ones. Therefore, without extending the Scheme reader to
2128 support strings with different quoting conventions (an ungainly and
2129 confusing extension when implemented in other languages), we must adhere
2130 to this cumbersome escape syntax.
2133 @subsection Rx Interface
2135 @c FIXME::martin: Shouldn't this be removed or moved to the
2136 @c ``Guile Modules'' chapter? The functions are not available in
2139 [FIXME: this is taken from Gary and Mark's quick summaries and should be
2140 reviewed and expanded. Rx is pretty stable, so could already be done!]
2143 @cindex finite automaton
2145 Guile includes an interface to Tom Lord's Rx library (currently only to
2146 POSIX regular expressions). Use of the library requires a two step
2147 process: compile a regular expression into an efficient structure, then
2148 use the structure in any number of string comparisons.
2150 For example, given the
2151 regular expression @samp{abc.} (which matches any string containing
2152 @samp{abc} followed by any single character):
2155 guile> @kbd{(define r (regcomp "abc."))}
2158 guile> @kbd{(regexec r "abc")}
2160 guile> @kbd{(regexec r "abcd")}
2165 The definitions of @code{regcomp} and @code{regexec} are as follows:
2167 @c NJFIXME not in libguile!
2168 @deffn primitive regcomp pattern [flags]
2169 Compile the regular expression pattern using POSIX rules. Flags is
2170 optional and should be specified using symbolic names:
2171 @defvar REG_EXTENDED
2172 use extended POSIX syntax
2175 use case-insensitive matching
2178 allow anchors to match after newline characters in the
2179 string and prevents @code{.} or @code{[^...]} from matching newlines.
2182 The @code{logior} procedure can be used to combine multiple flags.
2183 The default is to use
2184 POSIX basic syntax, which makes @code{+} and @code{?} literals and @code{\+}
2186 operators. Backslashes in @var{pattern} must be escaped if specified in a
2187 literal string e.g., @code{"\\(a\\)\\?"}.
2190 @c NJFIXME not in libguile!
2191 @deffn primitive regexec regex string [match-pick] [flags]
2193 Match @var{string} against the compiled POSIX regular expression
2195 @var{match-pick} and @var{flags} are optional. Possible flags (which can be
2196 combined using the logior procedure) are:
2199 The beginning of line operator won't match the beginning of
2200 @var{string} (presumably because it's not the beginning of a line)
2204 Similar to REG_NOTBOL, but prevents the end of line operator
2205 from matching the end of @var{string}.
2208 If no match is possible, regexec returns #f. Otherwise @var{match-pick}
2209 determines the return value:
2211 @code{#t} or unspecified: a newly-allocated vector is returned,
2212 containing pairs with the indices of the matched part of @var{string} and any
2215 @code{""}: a list is returned: the first element contains a nested list
2216 with the matched part of @var{string} surrounded by the the unmatched parts.
2217 Remaining elements are matched substrings (if any). All returned
2218 substrings share memory with @var{string}.
2220 @code{#f}: regexec returns #t if a match is made, otherwise #f.
2222 vector: the supplied vector is returned, with the first element replaced
2223 by a pair containing the indices of the matched portion of @var{string} and
2224 further elements replaced by pairs containing the indices of matched
2225 substrings (if any).
2227 list: a list will be returned, with each member of the list
2228 specified by a code in the corresponding position of the supplied list:
2230 a number: the numbered matching substring (0 for the entire match).
2232 @code{#\<}: the beginning of @var{string} to the beginning of the part matched
2235 @code{#\>}: the end of the matched part of @var{string} to the end of
2238 @code{#\c}: the "final tag", which seems to be associated with the "cut
2239 operator", which doesn't seem to be available through the posix
2242 e.g., @code{(list #\< 0 1 #\>)}. The returned substrings share memory with
2246 Here are some other procedures that might be used when using regular
2249 @c NJFIXME not in libguile!
2250 @deffn primitive compiled-regexp? obj
2251 Test whether obj is a compiled regular expression.
2254 @c NJFIXME not in libguile!
2255 @deffn primitive regexp->dfa regex [flags]
2258 @c NJFIXME not in libguile!
2259 @deffn primitive dfa-fork dfa
2262 @c NJFIXME not in libguile!
2263 @deffn primitive reset-dfa! dfa
2266 @c NJFIXME not in libguile!
2267 @deffn primitive dfa-final-tag dfa
2270 @c NJFIXME not in libguile!
2271 @deffn primitive dfa-continuable? dfa
2274 @c NJFIXME not in libguile!
2275 @deffn primitive advance-dfa! dfa string
2279 @node Symbols and Variables
2280 @section Symbols and Variables
2282 @c FIXME::martin: Review me!
2284 Symbols are a data type with a special property. On the one hand,
2285 symbols are used for denoting variables in a Scheme program, on the
2286 other they can be used as literal data as well.
2288 The association between symbols and values is maintained in special data
2289 structures, the symbol tables.
2291 In addition, Guile offers variables as first-class objects. They can
2292 be used for interacting with the module system.
2295 * Symbols:: All about symbols as a data type.
2296 * Symbol Tables:: Tables for mapping symbols to values.
2297 * Variables:: First-class variables.
2304 @c FIXME::martin: Review me!
2306 Symbols are especially useful because two symbols which are spelled the
2307 same way are equivalent in the sense of @code{eq?}. That means that
2308 they are actually the same Scheme object. The advantage is that symbols
2309 can be compared extremely efficiently, although they carry more
2310 information for the human reader than, say, numbers.
2312 It is very common in Scheme programs to use symbols as keys in
2313 association lists (@pxref{Association Lists}) or hash tables
2314 (@pxref{Hash Tables}), because this usage improves the readability a
2315 lot, and does not cause any performance loss.
2317 The read syntax for symbols is a sequence of letters, digits, and
2318 @emph{extended alphabetic characters} that begins with a character that
2319 cannot begin a number is an identifier. In addition, @code{+},
2320 @code{-}, and @code{...} are identifiers.
2322 Extended alphabetic characters may be used within identifiers as if
2323 they were letters. The following are extended alphabetic characters:
2326 ! $ % & * + - . / : < = > ? @@ ^ _ ~
2329 In addition to the read syntax defined above (which is taken from R5RS
2330 (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on Scheme})), Guile
2331 provides a method for writing symbols with unusual characters, such as
2332 space characters. If you (for whatever reason) need to write a symbol
2333 containing characters not mentioned above, you write symbols as follows:
2337 Begin the symbol with the two character @code{#@{},
2340 write the characters of the symbol and
2343 finish the symbol with the characters @code{@}#}.
2346 Here are a few examples of this form of read syntax; the first
2347 containing a space character, the second containing a line break and the
2348 last one looks like a number.
2357 Usage of this form of read syntax is discouraged, because it is not
2358 portable at all, and is not very readable.
2361 @deffn primitive symbol? obj
2362 Return @code{#t} if @var{obj} is a symbol, otherwise return
2366 @rnindex string->symbol
2367 @deffn primitive string->symbol string
2368 Return the symbol whose name is @var{string}. This procedure
2369 can create symbols with names containing special characters or
2370 letters in the non-standard case, but it is usually a bad idea
2371 to create such symbols because in some implementations of
2372 Scheme they cannot be read as themselves. See
2373 @code{symbol->string}.
2375 The following examples assume that the implementation's
2376 standard case is lower case:
2379 (eq? 'mISSISSIppi 'mississippi) @result{} #t
2380 (string->symbol "mISSISSIppi") @result{} @r{the symbol with name "mISSISSIppi"}
2381 (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
2383 (string->symbol (symbol->string 'JollyWog))) @result{} #t
2384 (string=? "K. Harper, M.D."
2386 (string->symbol "K. Harper, M.D."))) @result{}#t
2390 @rnindex symbol->string
2391 @deffn primitive symbol->string s
2392 Return the name of @var{symbol} as a string. If the symbol was
2393 part of an object returned as the value of a literal expression
2394 (section @pxref{Literal expressions,,,r5rs, The Revised^5
2395 Report on Scheme}) or by a call to the @code{read} procedure,
2396 and its name contains alphabetic characters, then the string
2397 returned will contain characters in the implementation's
2398 preferred standard case--some implementations will prefer
2399 upper case, others lower case. If the symbol was returned by
2400 @code{string->symbol}, the case of characters in the string
2401 returned will be the same as the case in the string that was
2402 passed to @code{string->symbol}. It is an error to apply
2403 mutation procedures like @code{string-set!} to strings returned
2406 The following examples assume that the implementation's
2407 standard case is lower case:
2410 (symbol->string 'flying-fish) @result{} "flying-fish"
2411 (symbol->string 'Martin) @result{} "martin"
2413 (string->symbol "Malvina")) @result{} "Malvina"
2418 @subsection Symbol Tables
2420 @c FIXME::martin: Review me!
2422 @c FIXME::martin: Are all these procedures still relevant?
2424 Guile symbol tables are hash tables. Each hash table, also called an
2425 @dfn{obarray} (for `object array'), is a vector of association lists.
2426 Each entry in the alists is a pair (@var{SYMBOL} . @var{VALUE}). To
2427 @dfn{intern} a symbol in a symbol table means to return its
2428 (@var{SYMBOL} . @var{VALUE}) pair, adding a new entry to the symbol
2429 table (with an undefined value) if none is yet present.
2431 @c FIXME::martin: According to NEWS, removed. Remove here too, or
2432 @c leave for compatibility?
2433 @c @c docstring begin (texi-doc-string "guile" "builtin-bindings")
2434 @c @deffn primitive builtin-bindings
2435 @c Create and return a copy of the global symbol table, removing all
2439 @deffn primitive gensym [prefix]
2440 Create a new symbol with a name constructed from a prefix and
2441 a counter value. The string @var{prefix} can be specified as
2442 an optional argument. Default prefix is @code{g}. The counter
2443 is increased by 1 at each call. There is no provision for
2444 resetting the counter.
2447 @deffn primitive gentemp [prefix [obarray]]
2448 Create a new symbol with a name unique in an obarray.
2449 The name is constructed from an optional string @var{prefix}
2450 and a counter value. The default prefix is @code{t}. The
2451 @var{obarray} is specified as a second optional argument.
2452 Default is the system obarray where all normal symbols are
2453 interned. The counter is increased by 1 at each
2454 call. There is no provision for resetting the counter.
2457 @deffn primitive intern-symbol obarray string
2458 Add a new symbol to @var{obarray} with name @var{string}, bound to an
2459 unspecified initial value. The symbol table is not modified if a symbol
2460 with this name is already present.
2463 @deffn primitive string->obarray-symbol obarray string [soft?]
2464 Intern a new symbol in @var{obarray}, a symbol table, with name
2468 @deffn primitive symbol-binding obarray string
2469 Look up in @var{obarray} the symbol whose name is @var{string}, and
2470 return the value to which it is bound. If @var{obarray} is @code{#f},
2471 use the global symbol table. If @var{string} is not interned in
2472 @var{obarray}, an error is signalled.
2475 @deffn primitive symbol-bound? obarray string
2476 Return @code{#t} if @var{obarray} contains a symbol with name
2477 @var{string} bound to a defined value. This differs from
2478 @var{symbol-interned?} in that the mere mention of a symbol
2479 usually causes it to be interned; @code{symbol-bound?}
2480 determines whether a symbol has been given any meaningful
2484 @deffn primitive symbol-fref symbol
2485 Return the contents of @var{symbol}'s @dfn{function slot}.
2488 @deffn primitive symbol-fset! symbol value
2489 Change the binding of @var{symbol}'s function slot.
2492 @deffn primitive symbol-hash symbol
2493 Return a hash value for @var{symbol}.
2496 @deffn primitive symbol-interned? obarray string
2497 Return @code{#t} if @var{obarray} contains a symbol with name
2498 @var{string}, and @code{#f} otherwise.
2501 @deffn primitive symbol-pref symbol
2502 Return the @dfn{property list} currently associated with @var{symbol}.
2505 @deffn primitive symbol-pset! symbol value
2506 Change the binding of @var{symbol}'s property slot.
2509 @deffn primitive symbol-set! obarray string value
2510 Find the symbol in @var{obarray} whose name is @var{string}, and rebind
2511 it to @var{value}. An error is signalled if @var{string} is not present
2515 @deffn primitive unintern-symbol obarray string
2516 Remove the symbol with name @var{string} from @var{obarray}. This
2517 function returns @code{#t} if the symbol was present and @code{#f}
2522 @subsection Variables
2525 @c FIXME::martin: Review me!
2527 Variables are objects with two fields. They contain a value and they
2528 can contain a symbol, which is the name of the variable. A variable is
2529 said to be bound if it does not contain the object denoting unbound
2530 variables in the value slot.
2532 Variables do not have a read syntax, they have to be created by calling
2533 one of the constructor procedures @code{make-variable} or
2534 @code{make-undefined-variable} or retrieved by @code{builtin-variable}.
2536 First-class variables are especially useful for interacting with the
2537 current module system (@pxref{The Guile module system}).
2539 @deffn primitive builtin-variable name
2540 Return the built-in variable with the name @var{name}.
2541 @var{name} must be a symbol (not a string).
2542 Then use @code{variable-ref} to access its value.
2545 @deffn primitive make-undefined-variable [name-hint]
2546 Return a variable object initialized to an undefined value.
2547 If given, uses @var{name-hint} as its internal (debugging)
2548 name, otherwise just treat it as an anonymous variable.
2549 Remember, of course, that multiple bindings to the same
2550 variable may exist, so @var{name-hint} is just that---a hint.
2553 @deffn primitive make-variable init [name-hint]
2554 Return a variable object initialized to value @var{init}.
2555 If given, uses @var{name-hint} as its internal (debugging)
2556 name, otherwise just treat it as an anonymous variable.
2557 Remember, of course, that multiple bindings to the same
2558 variable may exist, so @var{name-hint} is just that---a hint.
2561 @deffn primitive variable-bound? var
2562 Return @code{#t} iff @var{var} is bound to a value.
2563 Throws an error if @var{var} is not a variable object.
2566 @deffn primitive variable-ref var
2567 Dereference @var{var} and return its value.
2568 @var{var} must be a variable object; see @code{make-variable}
2569 and @code{make-undefined-variable}.
2572 @deffn primitive variable-set! var val
2573 Set the value of the variable @var{var} to @var{val}.
2574 @var{var} must be a variable object, @var{val} can be any
2575 value. Return an unspecified value.
2578 @deffn primitive variable? obj
2579 Return @code{#t} iff @var{obj} is a variable object, else
2588 Keywords are self-evaluating objects with a convenient read syntax that
2589 makes them easy to type.
2591 Guile's keyword support conforms to R5RS, and adds a (switchable) read
2592 syntax extension to permit keywords to begin with @code{:} as well as
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 Procedures:: Procedures for dealing with keywords.
2600 * Keyword Primitives:: The underlying primitive procedures.
2603 @node Why Use Keywords?
2604 @subsection Why Use Keywords?
2606 Keywords are useful in contexts where a program or procedure wants to be
2607 able to accept a large number of optional arguments without making its
2608 interface unmanageable.
2610 To illustrate this, consider a hypothetical @code{make-window}
2611 procedure, which creates a new window on the screen for drawing into
2612 using some graphical toolkit. There are many parameters that the caller
2613 might like to specify, but which could also be sensibly defaulted, for
2618 colour depth -- Default: the colour depth for the screen
2621 background colour -- Default: white
2624 width -- Default: 600
2627 height -- Default: 400
2630 If @code{make-window} did not use keywords, the caller would have to
2631 pass in a value for each possible argument, remembering the correct
2632 argument order and using a special value to indicate the default value
2636 (make-window 'default ;; Colour depth
2637 'default ;; Background colour
2640 @dots{}) ;; More make-window arguments
2643 With keywords, on the other hand, defaulted arguments are omitted, and
2644 non-default arguments are clearly tagged by the appropriate keyword. As
2645 a result, the invocation becomes much clearer:
2648 (make-window #:width 800 #:height 100)
2651 On the other hand, for a simpler procedure with few arguments, the use
2652 of keywords would be a hindrance rather than a help. The primitive
2653 procedure @code{cons}, for example, would not be improved if it had to
2657 (cons #:car x #:cdr y)
2660 So the decision whether to use keywords or not is purely pragmatic: use
2661 them if they will clarify the procedure invocation at point of call.
2663 @node Coding With Keywords
2664 @subsection Coding With Keywords
2666 If a procedure wants to support keywords, it should take a rest argument
2667 and then use whatever means is convenient to extract keywords and their
2668 corresponding arguments from the contents of that rest argument.
2670 The following example illustrates the principle: the code for
2671 @code{make-window} uses a helper procedure called
2672 @code{get-keyword-value} to extract individual keyword arguments from
2676 (define (get-keyword-value args keyword default)
2677 (let ((kv (memq keyword args)))
2678 (if (and kv (>= (length kv) 2))
2682 (define (make-window . args)
2683 (let ((depth (get-keyword-value args #:depth screen-depth))
2684 (bg (get-keyword-value args #:bg "white"))
2685 (width (get-keyword-value args #:width 800))
2686 (height (get-keyword-value args #:height 100))
2691 But you don't need to write @code{get-keyword-value}. The @code{(ice-9
2692 optargs)} module provides a set of powerful macros that you can use to
2693 implement keyword-supporting procedures like this:
2696 (use-modules (ice-9 optargs))
2698 (define (make-window . args)
2699 (let-keywords args #f ((depth screen-depth)
2707 Or, even more economically, like this:
2710 (use-modules (ice-9 optargs))
2712 (define* (make-window #:key (depth screen-depth)
2719 For further details on @code{let-keywords}, @code{define*} and other
2720 facilities provided by the @code{(ice-9 optargs)} module, @ref{Optional
2724 @node Keyword Read Syntax
2725 @subsection Keyword Read Syntax
2727 Guile, by default, only recognizes the keyword syntax specified by R5RS.
2728 A token of the form @code{#:NAME}, where @code{NAME} has the same syntax
2729 as a Scheme symbol, is the external representation of the keyword named
2730 @code{NAME}. Keyword objects print using this syntax as well, so values
2731 containing keyword objects can be read back into Guile. When used in an
2732 expression, keywords are self-quoting objects.
2734 If the @code{keyword} read option is set to @code{'prefix}, Guile also
2735 recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
2736 of the form @code{:NAME} are read as symbols, as required by R5RS.
2738 To enable and disable the alternative non-R5RS keyword syntax, you use
2739 the @code{read-options} procedure documented in @ref{General option
2740 interface} and @ref{Reader options}.
2743 (read-set! keywords 'prefix)
2753 (read-set! keywords #f)
2761 ERROR: In expression :type:
2762 ERROR: Unbound variable: :type
2763 ABORT: (unbound-variable)
2766 @node Keyword Procedures
2767 @subsection Keyword Procedures
2769 @c FIXME::martin: Review me!
2771 The following procedures can be used for converting symbols to keywords
2774 @deffn procedure symbol->keyword sym
2775 Return a keyword with the same characters as in @var{sym}.
2778 @deffn procedure keyword->symbol kw
2779 Return a symbol with the same characters as in @var{kw}.
2783 @node Keyword Primitives
2784 @subsection Keyword Primitives
2786 Internally, a keyword is implemented as something like a tagged symbol,
2787 where the tag identifies the keyword as being self-evaluating, and the
2788 symbol, known as the keyword's @dfn{dash symbol} has the same name as
2789 the keyword name but prefixed by a single dash. For example, the
2790 keyword @code{#:name} has the corresponding dash symbol @code{-name}.
2792 Most keyword objects are constructed automatically by the reader when it
2793 reads a token beginning with @code{#:}. However, if you need to
2794 construct a keyword object programmatically, you can do so by calling
2795 @code{make-keyword-from-dash-symbol} with the corresponding dash symbol
2796 (as the reader does). The dash symbol for a keyword object can be
2797 retrieved using the @code{keyword-dash-symbol} procedure.
2799 @deffn primitive make-keyword-from-dash-symbol symbol
2800 Make a keyword object from a @var{symbol} that starts with a dash.
2803 @deffn primitive keyword? obj
2804 Return @code{#t} if the argument @var{obj} is a keyword, else
2808 @deffn primitive keyword-dash-symbol keyword
2809 Return the dash symbol for @var{keyword}.
2810 This is the inverse of @code{make-keyword-from-dash-symbol}.
2817 @c FIXME::martin: Review me!
2819 Pairs are used to combine two Scheme objects into one compound object.
2820 Hence the name: A pair stores a pair of objects.
2822 The data type @emph{pair} is extremely important in Scheme, just like in
2823 any other Lisp dialect. The reason is that pairs are not only used to
2824 make two values available as one object, but that pairs are used for
2825 constructing lists of values. Because lists are so important in Scheme,
2826 they are described in a section of their own (@pxref{Lists}).
2828 Pairs can literally get entered in source code or at the REPL, in the
2829 so-called @dfn{dotted list} syntax. This syntax consists of an opening
2830 parentheses, the first element of the pair, a dot, the second element
2831 and a closing parentheses. The following example shows how a pair
2832 consisting of the two numbers 1 and 2, and a pair containing the symbols
2833 @code{foo} and @code{bar} can be entered. It is very important to write
2834 the whitespace before and after the dot, because otherwise the Scheme
2835 parser whould not be able to figure out where to split the tokens.
2842 But beware, if you want to try out these examples, you have to
2843 @dfn{quote} the expressions. More information about quotation is
2844 available in the section (REFFIXME). The correct way to try these
2845 examples is as follows.
2856 A new pair is made by calling the procedure @code{cons} with two
2857 arguments. Then the argument values are stored into a newly allocated
2858 pair, and the pair is returned. The name @code{cons} stands for
2859 @emph{construct}. Use the procedure @code{pair?} to test whether a
2860 given Scheme object is a pair or not.
2863 @deffn primitive cons x y
2864 Return a newly allocated pair whose car is @var{x} and whose
2865 cdr is @var{y}. The pair is guaranteed to be different (in the
2866 sense of @code{eq?}) from every previously existing object.
2870 @deffn primitive pair? x
2871 Return @code{#t} if @var{x} is a pair; otherwise return
2875 The two parts of a pair are traditionally called @emph{car} and
2876 @emph{cdr}. They can be retrieved with procedures of the same name
2877 (@code{car} and @code{cdr}), and can be modified with the procedures
2878 @code{set-car!} and @code{set-cdr!}. Since a very common operation in
2879 Scheme programs is to access the car of a pair, or the car of the cdr of
2880 a pair, etc., the procedures called @code{caar}, @code{cadr} and so on
2881 are also predefined.
2885 @deffn primitive car pair
2886 @deffnx primitive cdr pair
2887 Return the car or the cdr of @var{pair}, respectively.
2890 @deffn primitive caar pair
2891 @deffnx primitive cadr pair @dots{}
2892 @deffnx primitive cdddar pair
2893 @deffnx primitive cddddr pair
2894 These procedures are compositions of @code{car} and @code{cdr}, where
2895 for example @code{caddr} could be defined by
2898 (define caddr (lambda (x) (car (cdr (cdr x)))))
2903 @deffn primitive set-car! pair value
2904 Stores @var{value} in the car field of @var{pair}. The value returned
2905 by @code{set-car!} is unspecified.
2909 @deffn primitive set-cdr! pair value
2910 Stores @var{value} in the cdr field of @var{pair}. The value returned
2911 by @code{set-cdr!} is unspecified.
2919 @c FIXME::martin: Review me!
2921 A very important data type in Scheme---as well as in all other Lisp
2922 dialects---is the data type @dfn{list}.@footnote{Strictly speaking,
2923 Scheme does not have a real datatype @emph{list}. Lists are made up of
2924 chained @emph{pairs}, and only exist by definition---a list is a chain
2925 of pairs which looks like a list.}
2927 This is the short definition of what a list is:
2931 Either the empty list @code{()},
2934 or a pair which has a list in its cdr.
2937 @c FIXME::martin: Describe the pair chaining in more detail.
2939 @c FIXME::martin: What is a proper, what an improper list?
2940 @c What is a circular list?
2942 @c FIXME::martin: Maybe steal some graphics from the Elisp reference
2946 * List Syntax:: Writing literal lists.
2947 * List Predicates:: Testing lists.
2948 * List Constructors:: Creating new lists.
2949 * List Selection:: Selecting from lists, getting their length.
2950 * Append/Reverse:: Appending and reversing lists.
2951 * List Modifification:: Modifying list structure.
2952 * List Searching:: Searching for list elements
2953 * List Mapping:: Applying procedures to lists.
2957 @subsection List Read Syntax
2959 @c FIXME::martin: Review me!
2961 The syntax for lists is an opening parentheses, then all the elements of
2962 the list (separated by whitespace) and finally a closing
2963 parentheses.@footnote{Note that there is no separation character between
2964 the list elements, like a comma or a semicolon.}.
2967 (1 2 3) ; @r{a list of the numbers 1, 2 and 3}
2968 ("foo" bar 3.1415) ; @r{a string, a symbol and a real number}
2969 () ; @r{the empty list}
2972 The last example needs a bit more explanation. A list with no elements,
2973 called the @dfn{empty list}, is special in some ways. It is used for
2974 terminating lists by storing it into the cdr of the last pair that makes
2975 up a list. An example will clear that up:
2986 This example also shows that lists have to be quoted (REFFIXME) when
2987 written, because they would otherwise be mistakingly taken as procedure
2988 applications (@pxref{Simple Invocation}).
2991 @node List Predicates
2992 @subsection List Predicates
2994 @c FIXME::martin: Review me!
2996 Often it is useful to test whether a given Scheme object is a list or
2997 not. List-processing procedures could use this information to test
2998 whether their input is valid, or they could do different things
2999 depending on the datatype of their arguments.
3002 @deffn primitive list? x
3003 Return @code{#t} iff @var{x} is a proper list, else @code{#f}.
3006 The predicate @code{null?} is often used in list-processing code to
3007 tell whether a given list has run out of elements. That is, a loop
3008 somehow deals with the elements of a list until the list satisfies
3009 @code{null?}. Then, teh algorithm terminates.
3012 @deffn primitive null? x
3013 Return @code{#t} iff @var{x} is the empty list, else @code{#f}.
3016 @node List Constructors
3017 @subsection List Constructors
3019 This section describes the procedures for constructing new lists.
3020 @code{list} simply returns a list where the elements are the arguments,
3021 @code{cons*} is similar, but the last argument is stored in the cdr of
3022 the last pair of the list.
3025 @deffn primitive list arg1 @dots{}
3026 Return a list containing @var{objs}, the arguments to
3030 @deffn primitive cons* arg1 arg2 @dots{}
3031 Like @code{list}, but the last arg provides the tail of the
3032 constructed list, returning @code{(cons @var{arg1} (cons
3033 @var{arg2} (cons @dots{} @var{argn})))}. Requires at least one
3034 argument. If given one argument, that argument is returned as
3035 result. This function is called @code{list*} in some other
3036 Schemes and in Common LISP.
3039 @deffn primitive list-copy lst
3040 Return a (newly-created) copy of @var{lst}.
3043 @deffn procedure make-list n [init]
3044 Create a list containing of @var{n} elements, where each element is
3045 initialized to @var{init}. @var{init} defaults to the empty list
3046 @code{()} if not given.
3049 Note that @code{list-copy} only makes a copy of the pairs which make up
3050 the spine of the lists. The list elements are not copied, which means
3051 that modifying the elements of the new list also modyfies the elements
3052 of the old list. On the other hand, applying procedures like
3053 @code{set-cdr!} or @code{delv!} to the new list will not alter the old
3054 list. If you also need to copy the list elements (making a deep copy),
3055 use the procedure @code{copy-tree} (@pxref{Copying}).
3057 @node List Selection
3058 @subsection List Selection
3060 @c FIXME::martin: Review me!
3062 These procedures are used to get some information about a list, or to
3063 retrieve one or more elements of a list.
3066 @deffn primitive length lst
3067 Return the number of elements in list @var{lst}.
3070 @deffn primitive last-pair lst
3071 Return a pointer to the last pair in @var{lst}, signalling an error if
3072 @var{lst} is circular.
3076 @deffn primitive list-ref list k
3077 Return the @var{k}th element from @var{list}.
3081 @deffn primitive list-tail lst k
3082 @deffnx primitive list-cdr-ref lst k
3083 Return the "tail" of @var{lst} beginning with its @var{k}th element.
3084 The first element of the list is considered to be element 0.
3086 @code{list-tail} and @code{list-cdr-ref} are identical. It may help to
3087 think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,
3088 or returning the results of cdring @var{k} times down @var{lst}.
3091 @deffn primitive list-head lst k
3092 Copy the first @var{k} elements from @var{lst} into a new list, and
3096 @node Append/Reverse
3097 @subsection Append and Reverse
3099 @c FIXME::martin: Review me!
3101 @code{append} and @code{append!} are used to concatenate two or more
3102 lists in order to form a new list. @code{reverse} and @code{reverse!}
3103 return lists with the same elements as their arguments, but in reverse
3104 order. The procedure variants with an @code{!} directly modify the
3105 pairs which form the list, whereas the other procedures create new
3106 pairs. This is why you should be careful when using the side-effecting
3110 @deffn primitive append . args
3111 Return a list consisting of the elements the lists passed as
3114 (append '(x) '(y)) @result{} (x y)
3115 (append '(a) '(b c d)) @result{} (a b c d)
3116 (append '(a (b)) '((c))) @result{} (a (b) (c))
3118 The resulting list is always newly allocated, except that it
3119 shares structure with the last list argument. The last
3120 argument may actually be any object; an improper list results
3121 if the last argument is not a proper list.
3123 (append '(a b) '(c . d)) @result{} (a b c . d)
3124 (append '() 'a) @result{} a
3128 @deffn primitive append! . lists
3129 A destructive version of @code{append} (@pxref{Pairs and
3130 lists,,,r5rs, The Revised^5 Report on Scheme}). The cdr field
3131 of each list's final pair is changed to point to the head of
3132 the next list, so no consing is performed. Return a pointer to
3137 @deffn primitive reverse lst
3138 Return a new list that contains the elements of @var{lst} but
3142 @c NJFIXME explain new_tail
3143 @deffn primitive reverse! lst [new_tail]
3144 A destructive version of @code{reverse} (@pxref{Pairs and lists,,,r5rs,
3145 The Revised^5 Report on Scheme}). The cdr of each cell in @var{lst} is
3146 modified to point to the previous list element. Return a pointer to the
3147 head of the reversed list.
3149 Caveat: because the list is modified in place, the tail of the original
3150 list now becomes its head, and the head of the original list now becomes
3151 the tail. Therefore, the @var{lst} symbol to which the head of the
3152 original list was bound now points to the tail. To ensure that the head
3153 of the modified list is not lost, it is wise to save the return value of
3157 @node List Modifification
3158 @subsection List Modification
3160 @c FIXME::martin: Review me!
3162 The following procedures modify existing list. @code{list-set!} and
3163 @code{list-cdr-set!} change which elements a list contains, the various
3164 deletion procedures @code{delq}, @code{delv} etc.
3166 @deffn primitive list-set! list k val
3167 Set the @var{k}th element of @var{list} to @var{val}.
3170 @deffn primitive list-cdr-set! list k val
3171 Set the @var{k}th cdr of @var{list} to @var{val}.
3174 @deffn primitive delq item lst
3175 Return a newly-created copy of @var{lst} with elements
3176 @code{eq?} to @var{item} removed. This procedure mirrors
3177 @code{memq}: @code{delq} compares elements of @var{lst} against
3178 @var{item} with @code{eq?}.
3181 @deffn primitive delv item lst
3182 Return a newly-created copy of @var{lst} with elements
3183 @code{eqv?} to @var{item} removed. This procedure mirrors
3184 @code{memv}: @code{delv} compares elements of @var{lst} against
3185 @var{item} with @code{eqv?}.
3188 @deffn primitive delete item lst
3189 Return a newly-created copy of @var{lst} with elements
3190 @code{equal?} to @var{item} removed. This procedure mirrors
3191 @code{member}: @code{delete} compares elements of @var{lst}
3192 against @var{item} with @code{equal?}.
3195 @deffn primitive delq! item lst
3196 @deffnx primitive delv! item lst
3197 @deffnx primitive delete! item lst
3198 These procedures are destructive versions of @code{delq}, @code{delv}
3199 and @code{delete}: they modify the pointers in the existing @var{lst}
3200 rather than creating a new list. Caveat evaluator: Like other
3201 destructive list functions, these functions cannot modify the binding of
3202 @var{lst}, and so cannot be used to delete the first element of
3203 @var{lst} destructively.
3206 @deffn primitive delq1! item lst
3207 Like @code{delq!}, but only deletes the first occurrence of
3208 @var{item} from @var{lst}. Tests for equality using
3209 @code{eq?}. See also @code{delv1!} and @code{delete1!}.
3212 @deffn primitive delv1! item lst
3213 Like @code{delv!}, but only deletes the first occurrence of
3214 @var{item} from @var{lst}. Tests for equality using
3215 @code{eqv?}. See also @code{delq1!} and @code{delete1!}.
3218 @deffn primitive delete1! item lst
3219 Like @code{delete!}, but only deletes the first occurrence of
3220 @var{item} from @var{lst}. Tests for equality using
3221 @code{equal?}. See also @code{delq1!} and @code{delv1!}.
3224 @node List Searching
3225 @subsection List Searching
3227 @c FIXME::martin: Review me!
3229 The following procedures search lists for particular elements. They use
3230 different comparison predicates for comparing list elements with the
3231 object to be seached. When they fail, they return @code{#f}, otherwise
3232 they return the sublist whose car is equal to the search object, where
3233 equality depends on the equality predicate used.
3236 @deffn primitive memq x lst
3237 Return the first sublist of @var{lst} whose car is @code{eq?}
3238 to @var{x} where the sublists of @var{lst} are the non-empty
3239 lists returned by @code{(list-tail @var{lst} @var{k})} for
3240 @var{k} less than the length of @var{lst}. If @var{x} does not
3241 occur in @var{lst}, then @code{#f} (not the empty list) is
3246 @deffn primitive memv x lst
3247 Return the first sublist of @var{lst} whose car is @code{eqv?}
3248 to @var{x} where the sublists of @var{lst} are the non-empty
3249 lists returned by @code{(list-tail @var{lst} @var{k})} for
3250 @var{k} less than the length of @var{lst}. If @var{x} does not
3251 occur in @var{lst}, then @code{#f} (not the empty list) is
3256 @deffn primitive member x lst
3257 Return the first sublist of @var{lst} whose car is
3258 @code{equal?} to @var{x} where the sublists of @var{lst} are
3259 the non-empty lists returned by @code{(list-tail @var{lst}
3260 @var{k})} for @var{k} less than the length of @var{lst}. If
3261 @var{x} does not occur in @var{lst}, then @code{#f} (not the
3262 empty list) is returned.
3265 [FIXME: is there any reason to have the `sloppy' functions available at
3266 high level at all? Maybe these docs should be relegated to a "Guile
3267 Internals" node or something. -twp]
3269 @deffn primitive sloppy-memq x lst
3270 This procedure behaves like @code{memq}, but does no type or error checking.
3271 Its use is recommended only in writing Guile internals,
3272 not for high-level Scheme programs.
3275 @deffn primitive sloppy-memv x lst
3276 This procedure behaves like @code{memv}, but does no type or error checking.
3277 Its use is recommended only in writing Guile internals,
3278 not for high-level Scheme programs.
3281 @deffn primitive sloppy-member x lst
3282 This procedure behaves like @code{member}, but does no type or error checking.
3283 Its use is recommended only in writing Guile internals,
3284 not for high-level Scheme programs.
3288 @subsection List Mapping
3290 @c FIXME::martin: Review me!
3292 List processing is very convenient in Scheme because the process of
3293 iterating over the elements of a list can be highly abstracted. The
3294 procedures in this section are the most basic iterating procedures for
3295 lists. They take a procedure and one or more lists as arguments, and
3296 apply the procedure to each element of the list. They differ in what
3297 the result of the invocation is.
3300 @c begin (texi-doc-string "guile" "map")
3301 @deffn primitive map proc arg1 arg2 @dots{}
3302 @deffnx primitive map-in-order proc arg1 arg2 @dots{}
3303 Apply @var{proc} to each element of the list @var{arg1} (if only two
3304 arguments are given), or to the corresponding elements of the argument
3305 lists (if more than two arguments are given). The result(s) of the
3306 procedure applications are saved and returned in a list. For
3307 @code{map}, the order of procedure applications is not specified,
3308 @code{map-in-order} applies the procedure from left to right to the list
3313 @c begin (texi-doc-string "guile" "for-each")
3314 @deffn primitive for-each proc arg1 arg2 @dots{}
3315 Like @code{map}, but the procedure is always applied from left to right,
3316 and the result(s) of the procedure applications are thrown away. The
3317 return value is not specified.
3325 @c FIXME::martin: Review me!
3327 @c FIXME::martin: Should the subsections of this section be nodes
3328 @c of their own, or are the resulting nodes too short, then?
3330 Vectors are sequences of Scheme objects. Unlike lists, the length of a
3331 vector, once the vector is created, cannot be changed. The advantage of
3332 vectors over lists is that the time required to access one element of a
3333 vector is constant, whereas lists have an access time linear to the
3334 index of the accessed element in the list.
3336 Note that the vectors documented in this section can contain any kind of
3337 Scheme object, it is even possible to have different types of objects in
3340 @subsection Vector Read Syntax
3342 Vectors can literally be entered in source code, just like strings,
3343 characters or some of the other data types. The read syntax for vectors
3344 is as follows: A sharp sign (@code{#}), followed by an opening
3345 parentheses, all elements of the vector in their respective read syntax,
3346 and finally a closing parentheses. The following are examples of the
3347 read syntax for vectors; where the first vector only contains numbers
3348 and the second three different object types: a string, a symbol and a
3349 number in hexidecimal notation.
3353 #("Hello" foo #xdeadbeef)
3356 @subsection Vector Predicates
3359 @deffn primitive vector? obj
3360 Return @code{#t} if @var{obj} is a vector, otherwise return
3364 @subsection Vector Constructors
3366 @rnindex make-vector
3367 @deffn primitive make-vector k [fill]
3368 Return a newly allocated vector of @var{k} elements. If a
3369 second argument is given, then each element is initialized to
3370 @var{fill}. Otherwise the initial contents of each element is
3375 @rnindex list->vector
3376 @deffn primitive vector . l
3377 @deffnx primitive list->vector l
3378 Return a newly allocated vector whose elements contain the
3379 given arguments. Analogous to @code{list}.
3382 (vector 'a 'b 'c) @result{} #(a b c)
3386 @rnindex vector->list
3387 @deffn primitive vector->list v
3388 Return a newly allocated list of the objects contained in the
3389 elements of @var{vector}.
3392 (vector->list '#(dah dah didah)) @result{} (dah dah didah)
3393 (list->vector '(dididit dah)) @result{} #(dididit dah)
3397 @subsection Vector Modification
3399 A vector created by any of the vector constructor procedures
3400 (@pxref{Vectors}) documented above can be modified using the
3401 following procedures.
3403 According to R5RS, using any of these procedures on literally entered
3404 vectors is an error, because these vectors are considered to be
3405 constant, although Guile currently does not detect this error.
3407 @rnindex vector-set!
3408 @deffn primitive vector-set! vector k obj
3409 @var{k} must be a valid index of @var{vector}.
3410 @code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.
3411 The value returned by @samp{vector-set!} is unspecified.
3413 (let ((vec (vector 0 '(2 2 2 2) "Anna")))
3414 (vector-set! vec 1 '("Sue" "Sue"))
3415 vec) @result{} #(0 ("Sue" "Sue") "Anna")
3416 (vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector
3420 @rnindex vector-fill!
3421 @deffn primitive vector-fill! v fill
3422 Store @var{fill} in every element of @var{vector}. The value
3423 returned by @code{vector-fill!} is unspecified.
3426 @deffn primitive vector-move-left! vec1 start1 end1 vec2 start2
3427 Vector version of @code{substring-move-left!}.
3430 @deffn primitive vector-move-right! vec1 start1 end1 vec2 start2
3431 Vector version of @code{substring-move-right!}.
3434 @subsection Vector Selection
3436 These procedures return information about a given vector, such as the
3437 size or what elements are contained in the vector.
3439 @rnindex vector-length
3440 @deffn primitive vector-length vector
3441 Returns the number of elements in @var{vector} as an exact integer.
3445 @deffn primitive vector-ref vector k
3446 @var{k} must be a valid index of @var{vector}.
3447 @samp{Vector-ref} returns the contents of element @var{k} of
3450 (vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8
3451 (vector-ref '#(1 1 2 3 5 8 13 21)
3452 (let ((i (round (* 2 (acos -1)))))
3463 [FIXME: this is pasted in from Tom Lord's original guile.texi and should
3466 A @dfn{record type} is a first class object representing a user-defined
3467 data type. A @dfn{record} is an instance of a record type.
3469 @deffn procedure record? obj
3470 Returns @code{#t} if @var{obj} is a record of any type and @code{#f}
3473 Note that @code{record?} may be true of any Scheme value; there is no
3474 promise that records are disjoint with other Scheme types.
3477 @deffn procedure make-record-type type-name field-names
3478 Returns a @dfn{record-type descriptor}, a value representing a new data
3479 type disjoint from all others. The @var{type-name} argument must be a
3480 string, but is only used for debugging purposes (such as the printed
3481 representation of a record of the new type). The @var{field-names}
3482 argument is a list of symbols naming the @dfn{fields} of a record of the
3483 new type. It is an error if the list contains any duplicates. It is
3484 unspecified how record-type descriptors are represented.@refill
3487 @deffn procedure record-constructor rtd [field-names]
3488 Returns a procedure for constructing new members of the type represented
3489 by @var{rtd}. The returned procedure accepts exactly as many arguments
3490 as there are symbols in the given list, @var{field-names}; these are
3491 used, in order, as the initial values of those fields in a new record,
3492 which is returned by the constructor procedure. The values of any
3493 fields not named in that list are unspecified. The @var{field-names}
3494 argument defaults to the list of field names in the call to
3495 @code{make-record-type} that created the type represented by @var{rtd};
3496 if the @var{field-names} argument is provided, it is an error if it
3497 contains any duplicates or any symbols not in the default list.@refill
3500 @deffn procedure record-predicate rtd
3501 Returns a procedure for testing membership in the type represented by
3502 @var{rtd}. The returned procedure accepts exactly one argument and
3503 returns a true value if the argument is a member of the indicated record
3504 type; it returns a false value otherwise.@refill
3507 @deffn procedure record-accessor rtd field-name
3508 Returns a procedure for reading the value of a particular field of a
3509 member of the type represented by @var{rtd}. The returned procedure
3510 accepts exactly one argument which must be a record of the appropriate
3511 type; it returns the current value of the field named by the symbol
3512 @var{field-name} in that record. The symbol @var{field-name} must be a
3513 member of the list of field-names in the call to @code{make-record-type}
3514 that created the type represented by @var{rtd}.@refill
3517 @deffn procedure record-modifier rtd field-name
3518 Returns a procedure for writing the value of a particular field of a
3519 member of the type represented by @var{rtd}. The returned procedure
3520 accepts exactly two arguments: first, a record of the appropriate type,
3521 and second, an arbitrary Scheme value; it modifies the field named by
3522 the symbol @var{field-name} in that record to contain the given value.
3523 The returned value of the modifier procedure is unspecified. The symbol
3524 @var{field-name} must be a member of the list of field-names in the call
3525 to @code{make-record-type} that created the type represented by
3529 @deffn procedure record-type-descriptor record
3530 Returns a record-type descriptor representing the type of the given
3531 record. That is, for example, if the returned descriptor were passed to
3532 @code{record-predicate}, the resulting predicate would return a true
3533 value when passed the given record. Note that it is not necessarily the
3534 case that the returned descriptor is the one that was passed to
3535 @code{record-constructor} in the call that created the constructor
3536 procedure that created the given record.@refill
3539 @deffn procedure record-type-name rtd
3540 Returns the type-name associated with the type represented by rtd. The
3541 returned value is @code{eqv?} to the @var{type-name} argument given in
3542 the call to @code{make-record-type} that created the type represented by
3546 @deffn procedure record-type-fields rtd
3547 Returns a list of the symbols naming the fields in members of the type
3548 represented by @var{rtd}. The returned value is @code{equal?} to the
3549 field-names argument given in the call to @code{make-record-type} that
3550 created the type represented by @var{rtd}.@refill
3558 [FIXME: this is pasted in from Tom Lord's original guile.texi and should
3561 A @dfn{structure type} is a first class user-defined data type. A
3562 @dfn{structure} is an instance of a structure type. A structure type is
3565 Structures are less abstract and more general than traditional records.
3566 In fact, in Guile Scheme, records are implemented using structures.
3569 * Structure Concepts:: The structure of Structures
3570 * Structure Layout:: Defining the layout of structure types
3571 * Structure Basics:: make-, -ref and -set! procedures for structs
3572 * Vtables:: Accessing type-specific data
3575 @node Structure Concepts
3576 @subsection Structure Concepts
3578 A structure object consists of a handle, structure data, and a vtable.
3579 The handle is a Scheme value which points to both the vtable and the
3580 structure's data. Structure data is a dynamically allocated region of
3581 memory, private to the structure, divided up into typed fields. A
3582 vtable is another structure used to hold type-specific data. Multiple
3583 structures can share a common vtable.
3585 Three concepts are key to understanding structures.
3588 @item @dfn{layout specifications}
3590 Layout specifications determine how memory allocated to structures is
3591 divided up into fields. Programmers must write a layout specification
3592 whenever a new type of structure is defined.
3594 @item @dfn{structural accessors}
3596 Structure access is by field number. There is only one set of
3597 accessors common to all structure objects.
3601 Vtables, themselves structures, are first class representations of
3602 disjoint sub-types of structures in general. In most cases, when a
3603 new structure is created, programmers must specifiy a vtable for the
3604 new structure. Each vtable has a field describing the layout of its
3605 instances. Vtables can have additional, user-defined fields as well.
3610 @node Structure Layout
3611 @subsection Structure Layout
3613 When a structure is created, a region of memory is allocated to hold its
3614 state. The @dfn{layout} of the structure's type determines how that
3615 memory is divided into fields.
3617 Each field has a specified type. There are only three types allowed, each
3618 corresponding to a one letter code. The allowed types are:
3621 @item 'u' -- unprotected
3623 The field holds binary data that is not GC protected.
3625 @item 'p' -- protected
3627 The field holds a Scheme value and is GC protected.
3631 The field holds a Scheme value and is GC protected. When a structure is
3632 created with this type of field, the field is initialized to refer to
3633 the structure's own handle. This kind of field is mainly useful when
3634 mixing Scheme and C code in which the C code may need to compute a
3635 structure's handle given only the address of its malloced data.
3639 Each field also has an associated access protection. There are only
3640 three kinds of protection, each corresponding to a one letter code.
3641 The allowed protections are:
3644 @item 'w' -- writable
3646 The field can be read and written.
3648 @item 'r' -- readable
3650 The field can be read, but not written.
3654 The field can be neither read nor written. This kind
3655 of protection is for fields useful only to built-in routines.
3658 A layout specification is described by stringing together pairs
3659 of letters: one to specify a field type and one to specify a field
3660 protection. For example, a traditional cons pair type object could
3664 ; cons pairs have two writable fields of Scheme data
3668 A pair object in which the first field is held constant could be:
3674 Binary fields, (fields of type "u"), hold one @emph{word} each. The
3675 size of a word is a machine dependent value defined to be equal to the
3676 value of the C expression: @code{sizeof (long)}.
3678 The last field of a structure layout may specify a tail array.
3679 A tail array is indicated by capitalizing the field's protection
3680 code ('W', 'R' or 'O'). A tail-array field is replaced by
3681 a read-only binary data field containing an array size. The array
3682 size is determined at the time the structure is created. It is followed
3683 by a corresponding number of fields of the type specified for the
3684 tail array. For example, a conventional Scheme vector can be
3688 ; A vector is an arbitrary number of writable fields holding Scheme
3693 In the above example, field 0 contains the size of the vector and
3694 fields beginning at 1 contain the vector elements.
3696 A kind of tagged vector (a constant tag followed by conventioal
3697 vector elements) might be:
3704 Structure layouts are represented by specially interned symbols whose
3705 name is a string of type and protection codes. To create a new
3706 structure layout, use this procedure:
3708 @deffn primitive make-struct-layout fields
3709 Return a new structure layout object.
3711 @var{fields} must be a string made up of pairs of characters
3712 strung together. The first character of each pair describes a field
3713 type, the second a field protection. Allowed types are 'p' for
3714 GC-protected Scheme data, 'u' for unprotected binary data, and 's' for
3715 a field that points to the structure itself. Allowed protections
3716 are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque
3717 fields. The last field protection specification may be capitalized to
3718 indicate that the field is a tail-array.
3723 @node Structure Basics
3724 @subsection Structure Basics
3726 This section describes the basic procedures for creating and accessing
3729 @deffn primitive make-struct vtable tail_array_size . init
3730 Create a new structure.
3732 @var{type} must be a vtable structure (@pxref{Vtables}).
3734 @var{tail-elts} must be a non-negative integer. If the layout
3735 specification indicated by @var{type} includes a tail-array,
3736 this is the number of elements allocated to that array.
3738 The @var{init1}, @dots{} are optional arguments describing how
3739 successive fields of the structure should be initialized. Only fields
3740 with protection 'r' or 'w' can be initialized, except for fields of
3741 type 's', which are automatically initialized to point to the new
3742 structure itself; fields with protection 'o' can not be initialized by
3745 If fewer optional arguments than initializable fields are supplied,
3746 fields of type 'p' get default value #f while fields of type 'u' are
3749 Structs are currently the basic representation for record-like data
3750 structures in Guile. The plan is to eventually replace them with a
3751 new representation which will at the same time be easier to use and
3754 For more information, see the documentation for @code{make-vtable-vtable}.
3757 @deffn primitive struct? x
3758 Return @code{#t} iff @var{obj} is a structure object, else
3763 @deffn primitive struct-ref handle pos
3764 @deffnx primitive struct-set! struct n value
3765 Access (or modify) the @var{n}th field of @var{struct}.
3767 If the field is of type 'p', then it can be set to an arbitrary value.
3769 If the field is of type 'u', then it can only be set to a non-negative
3770 integer value small enough to fit in one machine word.
3778 Vtables are structures that are used to represent structure types. Each
3779 vtable contains a layout specification in field
3780 @code{vtable-index-layout} -- instances of the type are laid out
3781 according to that specification. Vtables contain additional fields
3782 which are used only internally to libguile. The variable
3783 @code{vtable-offset-user} is bound to a field number. Vtable fields
3784 at that position or greater are user definable.
3786 @deffn primitive struct-vtable handle
3787 Return the vtable structure that describes the type of @var{struct}.
3790 @deffn primitive struct-vtable? x
3791 Return @code{#t} iff obj is a vtable structure.
3794 If you have a vtable structure, @code{V}, you can create an instance of
3795 the type it describes by using @code{(make-struct V ...)}. But where
3796 does @code{V} itself come from? One possibility is that @code{V} is an
3797 instance of a user-defined vtable type, @code{V'}, so that @code{V} is
3798 created by using @code{(make-struct V' ...)}. Another possibility is
3799 that @code{V} is an instance of the type it itself describes. Vtable
3800 structures of the second sort are created by this procedure:
3802 @deffn primitive make-vtable-vtable user_fields tail_array_size . init
3803 Return a new, self-describing vtable structure.
3805 @var{user-fields} is a string describing user defined fields of the
3806 vtable beginning at index @code{vtable-offset-user}
3807 (see @code{make-struct-layout}).
3809 @var{tail-size} specifies the size of the tail-array (if any) of
3812 @var{init1}, @dots{} are the optional initializers for the fields of
3815 Vtables have one initializable system field---the struct printer.
3816 This field comes before the user fields in the initializers passed
3817 to @code{make-vtable-vtable} and @code{make-struct}, and thus works as
3818 a third optional argument to @code{make-vtable-vtable} and a fourth to
3819 @code{make-struct} when creating vtables:
3821 If the value is a procedure, it will be called instead of the standard
3822 printer whenever a struct described by this vtable is printed.
3823 The procedure will be called with arguments STRUCT and PORT.
3825 The structure of a struct is described by a vtable, so the vtable is
3826 in essence the type of the struct. The vtable is itself a struct with
3827 a vtable. This could go on forever if it weren't for the
3828 vtable-vtables which are self-describing vtables, and thus terminate
3831 There are several potential ways of using structs, but the standard
3832 one is to use three kinds of structs, together building up a type
3833 sub-system: one vtable-vtable working as the root and one or several
3834 "types", each with a set of "instances". (The vtable-vtable should be
3835 compared to the class <class> which is the class of itself.)
3838 (define ball-root (make-vtable-vtable "pr" 0))
3840 (define (make-ball-type ball-color)
3841 (make-struct ball-root 0
3842 (make-struct-layout "pw")
3844 (format port "#<a ~A ball owned by ~A>"
3848 (define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))
3849 (define (owner ball) (struct-ref ball 0))
3851 (define red (make-ball-type 'red))
3852 (define green (make-ball-type 'green))
3854 (define (make-ball type owner) (make-struct type 0 owner))
3856 (define ball (make-ball green 'Nisse))
3857 ball @result{} #<a green ball owned by Nisse>
3861 @deffn primitive struct-vtable-name vtable
3862 Return the name of the vtable @var{vtable}.
3865 @deffn primitive set-struct-vtable-name! vtable name
3866 Set the name of the vtable @var{vtable} to @var{name}.
3869 @deffn primitive struct-vtable-tag handle
3870 Return the vtable tag of the structure @var{handle}.
3879 * Conventional Arrays:: Arrays with arbitrary data.
3880 * Array Mapping:: Applying a procedure to the contents of an array.
3881 * Uniform Arrays:: Arrays with data of a single type.
3882 * Bit Vectors:: Vectors of bits.
3885 @node Conventional Arrays
3886 @subsection Conventional Arrays
3888 @dfn{Conventional arrays} are a collection of cells organised into an
3889 arbitrary number of dimensions. Each cell can hold any kind of Scheme
3890 value and can be accessed in constant time by supplying an index for
3891 each dimension. This contrasts with uniform arrays, which use memory
3892 more efficiently but can hold data of only a single type, and lists
3893 where inserting and deleting cells is more efficient, but more time
3894 is usually required to access a particular cell.
3896 A conventional array is displayed as @code{#} followed by the @dfn{rank}
3897 (number of dimensions) followed by the cells, organised into dimensions
3898 using parentheses. The nesting depth of the parentheses is equal to
3901 When an array is created, the number of dimensions and range of each
3902 dimension must be specified, e.g., to create a 2x3 array with a
3906 (make-array 'ho 2 3) @result{}
3907 #2((ho ho ho) (ho ho ho))
3910 The range of each dimension can also be given explicitly, e.g., another
3911 way to create the same array:
3914 (make-array 'ho '(0 1) '(0 2)) @result{}
3915 #2((ho ho ho) (ho ho ho))
3918 A conventional array with one dimension based at zero is identical to
3922 (make-array 'ho 3) @result{}
3926 The following procedures can be used with conventional arrays (or vectors).
3928 @deffn primitive array? v [prot]
3929 Return @code{#t} if the @var{obj} is an array, and @code{#f} if
3930 not. The @var{prototype} argument is used with uniform arrays
3931 and is described elsewhere.
3934 @deffn procedure make-array initial-value bound1 bound2 @dots{}
3935 Creates and returns an array that has as many dimensions as there are
3936 @var{bound}s and fills it with @var{initial-value}.
3939 @c array-ref's type is `compiled-closure'. There's some weird stuff
3940 @c going on in array.c, too. Let's call it a primitive. -twp
3942 @deffn primitive uniform-vector-ref v args
3943 @deffnx primitive array-ref v . args
3944 Return the element at the @code{(index1, index2)} element in
3948 @deffn primitive array-in-bounds? v . args
3949 Return @code{#t} if its arguments would be acceptable to
3953 @deffn primitive array-set! v obj . args
3954 @deffnx primitive uniform-array-set1! v obj args
3955 Sets the element at the @code{(index1, index2)} element in @var{array} to
3956 @var{new-value}. The value returned by array-set! is unspecified.
3959 @deffn primitive make-shared-array oldra mapfunc . dims
3960 @code{make-shared-array} can be used to create shared subarrays of other
3961 arrays. The @var{mapper} is a function that translates coordinates in
3962 the new array into coordinates in the old array. A @var{mapper} must be
3963 linear, and its range must stay within the bounds of the old array, but
3964 it can be otherwise arbitrary. A simple example:
3966 (define fred (make-array #f 8 8))
3967 (define freds-diagonal
3968 (make-shared-array fred (lambda (i) (list i i)) 8))
3969 (array-set! freds-diagonal 'foo 3)
3970 (array-ref fred 3 3) @result{} foo
3971 (define freds-center
3972 (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))
3973 (array-ref freds-center 0 0) @result{} foo
3977 @deffn primitive shared-array-increments ra
3978 For each dimension, return the distance between elements in the root vector.
3981 @deffn primitive shared-array-offset ra
3982 Return the root vector index of the first element in the array.
3985 @deffn primitive shared-array-root ra
3986 Return the root vector of a shared array.
3989 @deffn primitive transpose-array ra . args
3990 Return an array sharing contents with @var{array}, but with
3991 dimensions arranged in a different order. There must be one
3992 @var{dim} argument for each dimension of @var{array}.
3993 @var{dim0}, @var{dim1}, @dots{} should be integers between 0
3994 and the rank of the array to be returned. Each integer in that
3995 range must appear at least once in the argument list.
3997 The values of @var{dim0}, @var{dim1}, @dots{} correspond to
3998 dimensions in the array to be returned, their positions in the
3999 argument list to dimensions of @var{array}. Several @var{dim}s
4000 may have the same value, in which case the returned array will
4001 have smaller rank than @var{array}.
4004 (transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))
4005 (transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)
4006 (transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}
4007 #2((a 4) (b 5) (c 6))
4011 @deffn primitive enclose-array ra . axes
4012 @var{dim0}, @var{dim1} @dots{} should be nonnegative integers less than
4013 the rank of @var{array}. @var{enclose-array} returns an array
4014 resembling an array of shared arrays. The dimensions of each shared
4015 array are the same as the @var{dim}th dimensions of the original array,
4016 the dimensions of the outer array are the same as those of the original
4017 array that did not match a @var{dim}.
4019 An enclosed array is not a general Scheme array. Its elements may not
4020 be set using @code{array-set!}. Two references to the same element of
4021 an enclosed array will be @code{equal?} but will not in general be
4022 @code{eq?}. The value returned by @var{array-prototype} when given an
4023 enclosed array is unspecified.
4027 (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1) @result{}
4028 #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))>
4030 (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0) @result{}
4031 #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))>
4035 @deffn procedure array-shape array
4036 Returns a list of inclusive bounds of integers.
4038 (array-shape (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) (0 4))
4042 @deffn primitive array-dimensions ra
4043 @code{Array-dimensions} is similar to @code{array-shape} but replaces
4044 elements with a @code{0} minimum with one greater than the maximum. So:
4046 (array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)
4050 @deffn primitive array-rank ra
4051 Return the number of dimensions of @var{obj}. If @var{obj} is
4052 not an array, @code{0} is returned.
4055 @deffn primitive array->list v
4056 Return a list consisting of all the elements, in order, of
4060 @deffn primitive array-copy! src dst
4061 @deffnx primitive array-copy-in-order! src dst
4062 Copies every element from vector or array @var{source} to the
4063 corresponding element of @var{destination}. @var{destination} must have
4064 the same rank as @var{source}, and be at least as large in each
4065 dimension. The order is unspecified.
4068 @deffn primitive array-fill! ra fill
4069 Stores @var{fill} in every element of @var{array}. The value returned
4073 @c begin (texi-doc-string "guile" "array-equal?")
4074 @deffn primitive array-equal? ra0 ra1
4075 Returns @code{#t} iff all arguments are arrays with the same shape, the
4076 same type, and have corresponding elements which are either
4077 @code{equal?} or @code{array-equal?}. This function differs from
4078 @code{equal?} in that a one dimensional shared array may be
4079 @var{array-equal?} but not @var{equal?} to a vector or uniform vector.
4082 @deffn primitive array-contents ra [strict]
4083 @deffnx primitive array-contents array strict
4084 If @var{array} may be @dfn{unrolled} into a one dimensional shared array
4085 without changing their order (last subscript changing fastest), then
4086 @code{array-contents} returns that shared array, otherwise it returns
4087 @code{#f}. All arrays made by @var{make-array} and
4088 @var{make-uniform-array} may be unrolled, some arrays made by
4089 @var{make-shared-array} may not be.
4091 If the optional argument @var{strict} is provided, a shared array will
4092 be returned only if its elements are stored internally contiguous in
4097 @subsection Array Mapping
4099 @deffn primitive array-map! ra0 proc . lra
4100 @deffnx primitive array-map-in-order! ra0 proc . lra
4101 @var{array1}, @dots{} must have the same number of dimensions as
4102 @var{array0} and have a range for each index which includes the range
4103 for the corresponding index in @var{array0}. @var{proc} is applied to
4104 each tuple of elements of @var{array1} @dots{} and the result is stored
4105 as the corresponding element in @var{array0}. The value returned is
4106 unspecified. The order of application is unspecified.
4109 @deffn primitive array-for-each proc ra0 . lra
4110 @var{proc} is applied to each tuple of elements of @var{array0} @dots{}
4111 in row-major order. The value returned is unspecified.
4114 @deffn primitive array-index-map! ra proc
4115 applies @var{proc} to the indices of each element of @var{array} in
4116 turn, storing the result in the corresponding element. The value
4117 returned and the order of application are unspecified.
4119 One can implement @var{array-indexes} as
4121 (define (array-indexes array)
4122 (let ((ra (apply make-array #f (array-shape array))))
4123 (array-index-map! ra (lambda x x))
4128 (define (apl:index-generator n)
4129 (let ((v (make-uniform-vector n 1)))
4130 (array-index-map! v (lambda (i) i))
4135 @node Uniform Arrays
4136 @subsection Uniform Arrays
4137 @tpindex Uniform Arrays
4140 @dfn{Uniform arrays} have elements all of the
4141 same type and occupy less storage than conventional
4142 arrays. Uniform arrays with a single zero-based dimension
4143 are also known as @dfn{uniform vectors}. The procedures in
4144 this section can also be used on conventional arrays, vectors,
4145 bit-vectors and strings.
4148 When creating a uniform array, the type of data to be stored
4149 is indicated with a @var{prototype} argument. The following table
4150 lists the types available and example prototypes:
4153 prototype type printing character
4155 #t boolean (bit-vector) b
4157 #\nul byte (integer) y
4158 's short (integer) h
4159 1 unsigned long (integer) u
4160 -1 signed long (integer) e
4161 'l signed long long (integer) l
4162 1.0 float (single precision) s
4163 1/3 double (double precision float) i
4164 0+i complex (double precision) c
4165 () conventional vector
4169 Unshared uniform arrays of characters with a single zero-based dimension
4170 are identical to strings:
4173 (make-uniform-array #\a 3) @result{}
4178 Unshared uniform arrays of booleans with a single zero-based dimension
4179 are identical to @ref{Bit Vectors, bit-vectors}.
4182 (make-uniform-array #t 3) @result{}
4187 Other uniform vectors are written in a form similar to that of vectors,
4188 except that a single character from the above table is put between
4189 @code{#} and @code{(}. For example, a uniform vector of signed
4190 long integers is displayed in the form @code{'#e(3 5 9)}.
4192 @deffn primitive array? v [prot]
4193 Returns @code{#t} if the @var{obj} is an array, and @code{#f} if not.
4195 The @var{prototype} argument is used with uniform arrays and is described
4199 @deffn procedure make-uniform-array prototype bound1 bound2 @dots{}
4200 Creates and returns a uniform array of type corresponding to
4201 @var{prototype} that has as many dimensions as there are @var{bound}s
4202 and fills it with @var{prototype}.
4205 @deffn primitive array-prototype ra
4206 Return an object that would produce an array of the same type
4207 as @var{array}, if used as the @var{prototype} for
4208 @code{make-uniform-array}.
4211 @deffn primitive list->uniform-array ndim prot lst
4212 @deffnx procedure list->uniform-vector prot lst
4213 Return a uniform array of the type indicated by prototype
4214 @var{prot} with elements the same as those of @var{lst}.
4215 Elements must be of the appropriate type, no coercions are
4219 @deffn primitive uniform-vector-fill! uve fill
4220 Stores @var{fill} in every element of @var{uve}. The value returned is
4224 @deffn primitive uniform-vector-length v
4225 Return the number of elements in @var{uve}.
4228 @deffn primitive dimensions->uniform-array dims prot [fill]
4229 @deffnx primitive make-uniform-vector length prototype [fill]
4230 Create and return a uniform array or vector of type
4231 corresponding to @var{prototype} with dimensions @var{dims} or
4232 length @var{length}. If @var{fill} is supplied, it's used to
4233 fill the array, otherwise @var{prototype} is used.
4236 @c Another compiled-closure. -twp
4238 @deffn primitive uniform-array-read! ra [port_or_fd [start [end]]]
4239 @deffnx primitive uniform-vector-read! uve [port-or-fdes] [start] [end]
4240 Attempts to read all elements of @var{ura}, in lexicographic order, as
4241 binary objects from @var{port-or-fdes}.
4242 If an end of file is encountered during
4243 uniform-array-read! the objects up to that point only are put into @var{ura}
4244 (starting at the beginning) and the remainder of the array is
4247 The optional arguments @var{start} and @var{end} allow
4248 a specified region of a vector (or linearized array) to be read,
4249 leaving the remainder of the vector unchanged.
4251 @code{uniform-array-read!} returns the number of objects read.
4252 @var{port-or-fdes} may be omitted, in which case it defaults to the value
4253 returned by @code{(current-input-port)}.
4256 @deffn primitive uniform-array-write v [port_or_fd [start [end]]]
4257 @deffnx primitive uniform-vector-write uve [port-or-fdes] [start] [end]
4258 Writes all elements of @var{ura} as binary objects to
4261 The optional arguments @var{start}
4263 a specified region of a vector (or linearized array) to be written.
4265 The number of objects actually written is returned.
4266 @var{port-or-fdes} may be
4267 omitted, in which case it defaults to the value returned by
4268 @code{(current-output-port)}.
4272 @subsection Bit Vectors
4275 Bit vectors are a specific type of uniform array: an array of booleans
4276 with a single zero-based index.
4279 They are displayed as a sequence of @code{0}s and
4280 @code{1}s prefixed by @code{#*}, e.g.,
4283 (make-uniform-vector 8 #t #f) @result{}
4286 #b(#t #f #t) @result{}
4290 @deffn primitive bit-count b bitvector
4291 Return the number of occurrences of the boolean @var{b} in
4295 @deffn primitive bit-position item v k
4296 Return the minimum index of an occurrence of @var{bool} in
4297 @var{bv} which is at least @var{k}. If no @var{bool} occurs
4298 within the specified range @code{#f} is returned.
4301 @deffn primitive bit-invert! v
4302 Modifies @var{bv} by replacing each element with its negation.
4305 @deffn primitive bit-set*! v kv obj
4306 If uve is a bit-vector @var{bv} and uve must be of the same
4307 length. If @var{bool} is @code{#t}, uve is OR'ed into
4308 @var{bv}; If @var{bool} is @code{#f}, the inversion of uve is
4309 AND'ed into @var{bv}.
4311 If uve is a unsigned long integer vector all the elements of uve
4312 must be between 0 and the @code{length} of @var{bv}. The bits
4313 of @var{bv} corresponding to the indexes in uve are set to
4314 @var{bool}. The return value is unspecified.
4317 @deffn primitive bit-count* v kv obj
4320 (bit-count (bit-set*! (if bool bv (bit-invert! bv)) uve #t) #t).
4322 @var{bv} is not modified.
4326 @node Association Lists and Hash Tables
4327 @section Association Lists and Hash Tables
4329 This chapter discusses dictionary objects: data structures that are
4330 useful for organizing and indexing large bodies of information.
4333 * Dictionary Types:: About dictionary types; what they're good for.
4334 * Association Lists:: List-based dictionaries.
4335 * Hash Tables:: Table-based dictionaries.
4338 @node Dictionary Types
4339 @subsection Dictionary Types
4341 A @dfn{dictionary} object is a data structure used to index
4342 information in a user-defined way. In standard Scheme, the main
4343 aggregate data types are lists and vectors. Lists are not really
4344 indexed at all, and vectors are indexed only by number
4345 (e.g. @code{(vector-ref foo 5)}). Often you will find it useful
4346 to index your data on some other type; for example, in a library
4347 catalog you might want to look up a book by the name of its
4348 author. Dictionaries are used to help you organize information in
4351 An @dfn{association list} (or @dfn{alist} for short) is a list of
4352 key-value pairs. Each pair represents a single quantity or
4353 object; the @code{car} of the pair is a key which is used to
4354 identify the object, and the @code{cdr} is the object's value.
4356 A @dfn{hash table} also permits you to index objects with
4357 arbitrary keys, but in a way that makes looking up any one object
4358 extremely fast. A well-designed hash system makes hash table
4359 lookups almost as fast as conventional array or vector references.
4361 Alists are popular among Lisp programmers because they use only
4362 the language's primitive operations (lists, @dfn{car}, @dfn{cdr}
4363 and the equality primitives). No changes to the language core are
4364 necessary. Therefore, with Scheme's built-in list manipulation
4365 facilities, it is very convenient to handle data stored in an
4366 association list. Also, alists are highly portable and can be
4367 easily implemented on even the most minimal Lisp systems.
4369 However, alists are inefficient, especially for storing large
4370 quantities of data. Because we want Guile to be useful for large
4371 software systems as well as small ones, Guile provides a rich set
4372 of tools for using either association lists or hash tables.
4374 @node Association Lists
4375 @subsection Association Lists
4376 @tpindex Association Lists
4379 @cindex Association List
4383 An association list is a conventional data structure that is often used
4384 to implement simple key-value databases. It consists of a list of
4385 entries in which each entry is a pair. The @dfn{key} of each entry is
4386 the @code{car} of the pair and the @dfn{value} of each entry is the
4390 ASSOCIATION LIST ::= '( (KEY1 . VALUE1)
4398 Association lists are also known, for short, as @dfn{alists}.
4400 The structure of an association list is just one example of the infinite
4401 number of possible structures that can be built using pairs and lists.
4402 As such, the keys and values in an association list can be manipulated
4403 using the general list structure procedures @code{cons}, @code{car},
4404 @code{cdr}, @code{set-car!}, @code{set-cdr!} and so on. However,
4405 because association lists are so useful, Guile also provides specific
4406 procedures for manipulating them.
4409 * Alist Key Equality::
4410 * Adding or Setting Alist Entries::
4411 * Retrieving Alist Entries::
4412 * Removing Alist Entries::
4413 * Sloppy Alist Functions::
4417 @node Alist Key Equality
4418 @subsubsection Alist Key Equality
4420 All of Guile's dedicated association list procedures, apart from
4421 @code{acons}, come in three flavours, depending on the level of equality
4422 that is required to decide whether an existing key in the association
4423 list is the same as the key that the procedure call uses to identify the
4428 Procedures with @dfn{assq} in their name use @code{eq?} to determine key
4432 Procedures with @dfn{assv} in their name use @code{eqv?} to determine
4436 Procedures with @dfn{assoc} in their name use @code{equal?} to
4437 determine key equality.
4440 @code{acons} is an exception because it is used to build association
4441 lists which do not require their entries' keys to be unique.
4443 @node Adding or Setting Alist Entries
4444 @subsubsection Adding or Setting Alist Entries
4446 @code{acons} adds a new entry to an association list and returns the
4447 combined association list. The combined alist is formed by consing the
4448 new entry onto the head of the alist specified in the @code{acons}
4449 procedure call. So the specified alist is not modified, but its
4450 contents become shared with the tail of the combined alist that
4451 @code{acons} returns.
4453 In the most common usage of @code{acons}, a variable holding the
4454 original association list is updated with the combined alist:
4457 (set! address-list (acons name address address-list))
4460 In such cases, it doesn't matter that the old and new values of
4461 @code{address-list} share some of their contents, since the old value is
4462 usually no longer independently accessible.
4464 Note that @code{acons} adds the specified new entry regardless of
4465 whether the alist may already contain entries with keys that are, in
4466 some sense, the same as that of the new entry. Thus @code{acons} is
4467 ideal for building alists where there is no concept of key uniqueness.
4470 (set! task-list (acons 3 "pay gas bill" '()))
4473 ((3 . "pay gas bill"))
4475 (set! task-list (acons 3 "tidy bedroom" task-list))
4478 ((3 . "tidy bedroom") (3 . "pay gas bill"))
4481 @code{assq-set!}, @code{assv-set!} and @code{assoc-set!} are used to add
4482 or replace an entry in an association list where there @emph{is} a
4483 concept of key uniqueness. If the specified association list already
4484 contains an entry whose key is the same as that specified in the
4485 procedure call, the existing entry is replaced by the new one.
4486 Otherwise, the new entry is consed onto the head of the old association
4487 list to create the combined alist. In all cases, these procedures
4488 return the combined alist.
4490 @code{assq-set!} and friends @emph{may} destructively modify the
4491 structure of the old association list in such a way that an existing
4492 variable is correctly updated without having to @code{set!} it to the
4498 (("mary" . "34 Elm Road") ("james" . "16 Bow Street"))
4500 (assoc-set! address-list "james" "1a London Road")
4502 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
4506 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
4512 (assoc-set! address-list "bob" "11 Newington Avenue")
4514 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
4515 ("james" . "1a London Road"))
4519 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
4522 The only safe way to update an association list variable when adding or
4523 replacing an entry like this is to @code{set!} the variable to the
4528 (assoc-set! address-list "bob" "11 Newington Avenue"))
4531 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
4532 ("james" . "1a London Road"))
4535 Because of this slight inconvenience, you may find it more convenient to
4536 use hash tables to store dictionary data. If your application will not
4537 be modifying the contents of an alist very often, this may not make much
4540 If you need to keep the old value of an association list in a form
4541 independent from the list that results from modification by
4542 @code{acons}, @code{assq-set!}, @code{assv-set!} or @code{assoc-set!},
4543 use @code{list-copy} to copy the old association list before modifying
4546 @deffn primitive acons key value alist
4547 Adds a new key-value pair to @var{alist}. A new pair is
4548 created whose car is @var{key} and whose cdr is @var{value}, and the
4549 pair is consed onto @var{alist}, and the new list is returned. This
4550 function is @emph{not} destructive; @var{alist} is not modified.
4553 @deffn primitive assq-set! alist key val
4554 @deffnx primitive assv-set! alist key value
4555 @deffnx primitive assoc-set! alist key value
4556 Reassociate @var{key} in @var{alist} with @var{value}: find any existing
4557 @var{alist} entry for @var{key} and associate it with the new
4558 @var{value}. If @var{alist} does not contain an entry for @var{key},
4559 add a new one. Return the (possibly new) alist.
4561 These functions do not attempt to verify the structure of @var{alist},
4562 and so may cause unusual results if passed an object that is not an
4566 @node Retrieving Alist Entries
4567 @subsubsection Retrieving Alist Entries
4572 @code{assq}, @code{assv} and @code{assoc} take an alist and a key as
4573 arguments and return the entry for that key if an entry exists, or
4574 @code{#f} if there is no entry for that key. Note that, in the cases
4575 where an entry exists, these procedures return the complete entry, that
4576 is @code{(KEY . VALUE)}, not just the value.
4578 @deffn primitive assq key alist
4579 @deffnx primitive assv key alist
4580 @deffnx primitive assoc key alist
4581 Fetches the entry in @var{alist} that is associated with @var{key}. To
4582 decide whether the argument @var{key} matches a particular entry in
4583 @var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}
4584 uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}
4585 cannot be found in @var{alist} (according to whichever equality
4586 predicate is in use), then @code{#f} is returned. These functions
4587 return the entire alist entry found (i.e. both the key and the value).
4590 @code{assq-ref}, @code{assv-ref} and @code{assoc-ref}, on the other
4591 hand, take an alist and a key and return @emph{just the value} for that
4592 key, if an entry exists. If there is no entry for the specified key,
4593 these procedures return @code{#f}.
4595 This creates an ambiguity: if the return value is @code{#f}, it means
4596 either that there is no entry with the specified key, or that there
4597 @emph{is} an entry for the specified key, with value @code{#f}.
4598 Consequently, @code{assq-ref} and friends should only be used where it
4599 is known that an entry exists, or where the ambiguity doesn't matter
4600 for some other reason.
4602 @deffn primitive assq-ref alist key
4603 @deffnx primitive assv-ref alist key
4604 @deffnx primitive assoc-ref alist key
4605 Like @code{assq}, @code{assv} and @code{assoc}, except that only the
4606 value associated with @var{key} in @var{alist} is returned. These
4607 functions are equivalent to
4610 (let ((ent (@var{associator} @var{key} @var{alist})))
4611 (and ent (cdr ent)))
4614 where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.
4617 @node Removing Alist Entries
4618 @subsubsection Removing Alist Entries
4620 To remove the element from an association list whose key matches a
4621 specified key, use @code{assq-remove!}, @code{assv-remove!} or
4622 @code{assoc-remove!} (depending, as usual, on the level of equality
4623 required between the key that you specify and the keys in the
4626 As with @code{assq-set!} and friends, the specified alist may or may not
4627 be modified destructively, and the only safe way to update a variable
4628 containing the alist is to @code{set!} it to the value that
4629 @code{assq-remove!} and friends return.
4634 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
4635 ("james" . "1a London Road"))
4637 (set! address-list (assoc-remove! address-list "mary"))
4640 (("bob" . "11 Newington Avenue") ("james" . "1a London Road"))
4643 Note that, when @code{assq/v/oc-remove!} is used to modify an
4644 association list that has been constructed only using the corresponding
4645 @code{assq/v/oc-set!}, there can be at most one matching entry in the
4646 alist, so the question of multiple entries being removed in one go does
4647 not arise. If @code{assq/v/oc-remove!} is applied to an association
4648 list that has been constructed using @code{acons}, or an
4649 @code{assq/v/oc-set!} with a different level of equality, or any mixture
4650 of these, it removes only the first matching entry from the alist, even
4651 if the alist might contain further matching entries. For example:
4654 (define address-list '())
4655 (set! address-list (assq-set! address-list "mary" "11 Elm Street"))
4656 (set! address-list (assq-set! address-list "mary" "57 Pine Drive"))
4659 (("mary" . "57 Pine Drive") ("mary" . "11 Elm Street"))
4661 (set! address-list (assoc-remove! address-list "mary"))
4664 (("mary" . "11 Elm Street"))
4667 In this example, the two instances of the string "mary" are not the same
4668 when compared using @code{eq?}, so the two @code{assq-set!} calls add
4669 two distinct entries to @code{address-list}. When compared using
4670 @code{equal?}, both "mary"s in @code{address-list} are the same as the
4671 "mary" in the @code{assoc-remove!} call, but @code{assoc-remove!} stops
4672 after removing the first matching entry that it finds, and so one of the
4673 "mary" entries is left in place.
4675 @deffn primitive assq-remove! alist key
4676 @deffnx primitive assv-remove! alist key
4677 @deffnx primitive assoc-remove! alist key
4678 Delete the first entry in @var{alist} associated with @var{key}, and return
4679 the resulting alist.
4682 @node Sloppy Alist Functions
4683 @subsubsection Sloppy Alist Functions
4685 @code{sloppy-assq}, @code{sloppy-assv} and @code{sloppy-assoc} behave
4686 like the corresponding non-@code{sloppy-} procedures, except that they
4687 return @code{#f} when the specified association list is not well-formed,
4688 where the non-@code{sloppy-} versions would signal an error.
4690 Specifically, there are two conditions for which the non-@code{sloppy-}
4691 procedures signal an error, which the @code{sloppy-} procedures handle
4692 instead by returning @code{#f}. Firstly, if the specified alist as a
4693 whole is not a proper list:
4696 (assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
4698 ERROR: In procedure assoc in expression (assoc "mary" (quote #)):
4699 ERROR: Wrong type argument in position 2 (expecting NULLP): "open sesame"
4700 ABORT: (wrong-type-arg)
4702 (sloppy-assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
4708 Secondly, if one of the entries in the specified alist is not a pair:
4711 (assoc 2 '((1 . 1) 2 (3 . 9)))
4713 ERROR: In procedure assoc in expression (assoc 2 (quote #)):
4714 ERROR: Wrong type argument in position 2 (expecting CONSP): 2
4715 ABORT: (wrong-type-arg)
4717 (sloppy-assoc 2 '((1 . 1) 2 (3 . 9)))
4722 Unless you are explicitly working with badly formed association lists,
4723 it is much safer to use the non-@code{sloppy-} procedures, because they
4724 help to highlight coding and data errors that the @code{sloppy-}
4725 versions would silently cover up.
4727 @deffn primitive sloppy-assq key alist
4728 Behaves like @code{assq} but does not do any error checking.
4729 Recommended only for use in Guile internals.
4732 @deffn primitive sloppy-assv key alist
4733 Behaves like @code{assv} but does not do any error checking.
4734 Recommended only for use in Guile internals.
4737 @deffn primitive sloppy-assoc key alist
4738 Behaves like @code{assoc} but does not do any error checking.
4739 Recommended only for use in Guile internals.
4743 @subsubsection Alist Example
4745 Here is a longer example of how alists may be used in practice.
4748 (define capitals '(("New York" . "Albany")
4749 ("Oregon" . "Salem")
4750 ("Florida" . "Miami")))
4752 ;; What's the capital of Oregon?
4753 (assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
4754 (assoc-ref capitals "Oregon") @result{} "Salem"
4756 ;; We left out South Dakota.
4758 (assoc-set! capitals "South Dakota" "Bismarck"))
4760 @result{} (("South Dakota" . "Bismarck")
4761 ("New York" . "Albany")
4762 ("Oregon" . "Salem")
4763 ("Florida" . "Miami"))
4765 ;; And we got Florida wrong.
4767 (assoc-set! capitals "Florida" "Tallahassee"))
4769 @result{} (("South Dakota" . "Bismarck")
4770 ("New York" . "Albany")
4771 ("Oregon" . "Salem")
4772 ("Florida" . "Tallahassee"))
4774 ;; After Oregon secedes, we can remove it.
4776 (assoc-remove! capitals "Oregon"))
4778 @result{} (("South Dakota" . "Bismarck")
4779 ("New York" . "Albany")
4780 ("Florida" . "Tallahassee"))
4784 @subsection Hash Tables
4785 @tpindex Hash Tables
4787 Like the association list functions, the hash table functions come
4788 in several varieties: @code{hashq}, @code{hashv}, and @code{hash}.
4789 The @code{hashq} functions use @code{eq?} to determine whether two
4790 keys match. The @code{hashv} functions use @code{eqv?}, and the
4791 @code{hash} functions use @code{equal?}.
4793 In each of the functions that follow, the @var{table} argument
4794 must be a vector. The @var{key} and @var{value} arguments may be
4797 @deffn procedure make-hash-table size
4798 Create a new hash table of @var{size} slots. Note that the number of
4799 slots does not limit the size of the table, it just tells how large
4800 the underlying vector will be. The @var{size} should be similar to
4801 the expected number of elements which will be added to the table, but
4802 they need not match. For good performance, it might be a good idea to
4803 use a prime number as the @var{size}.
4806 @deffn primitive hashq-ref table key [dflt]
4807 Look up @var{key} in the hash table @var{table}, and return the
4808 value (if any) associated with it. If @var{key} is not found,
4809 return @var{default} (or @code{#f} if no @var{default} argument
4810 is supplied). Uses @code{eq?} for equality testing.
4813 @deffn primitive hashv-ref table key [dflt]
4814 Look up @var{key} in the hash table @var{table}, and return the
4815 value (if any) associated with it. If @var{key} is not found,
4816 return @var{default} (or @code{#f} if no @var{default} argument
4817 is supplied). Uses @code{eqv?} for equality testing.
4820 @deffn primitive hash-ref table key [dflt]
4821 Look up @var{key} in the hash table @var{table}, and return the
4822 value (if any) associated with it. If @var{key} is not found,
4823 return @var{default} (or @code{#f} if no @var{default} argument
4824 is supplied). Uses @code{equal?} for equality testing.
4827 @deffn primitive hashq-set! table key val
4828 Find the entry in @var{table} associated with @var{key}, and
4829 store @var{value} there. Uses @code{eq?} for equality testing.
4832 @deffn primitive hashv-set! table key val
4833 Find the entry in @var{table} associated with @var{key}, and
4834 store @var{value} there. Uses @code{eqv?} for equality testing.
4837 @deffn primitive hash-set! table key val
4838 Find the entry in @var{table} associated with @var{key}, and
4839 store @var{value} there. Uses @code{equal?} for equality
4843 @deffn primitive hashq-remove! table key
4844 Remove @var{key} (and any value associated with it) from
4845 @var{table}. Uses @code{eq?} for equality tests.
4848 @deffn primitive hashv-remove! table key
4849 Remove @var{key} (and any value associated with it) from
4850 @var{table}. Uses @code{eqv?} for equality tests.
4853 @deffn primitive hash-remove! table key
4854 Remove @var{key} (and any value associated with it) from
4855 @var{table}. Uses @code{equal?} for equality tests.
4858 The standard hash table functions may be too limited for some
4859 applications. For example, you may want a hash table to store
4860 strings in a case-insensitive manner, so that references to keys
4861 named ``foobar'', ``FOOBAR'' and ``FooBaR'' will all yield the
4862 same item. Guile provides you with @dfn{extended} hash tables
4863 that permit you to specify a hash function and associator function
4864 of your choosing. The functions described in the rest of this section
4865 can be used to implement such custom hash table structures.
4867 If you are unfamiliar with the inner workings of hash tables, then
4868 this facility will probably be a little too abstract for you to
4869 use comfortably. If you are interested in learning more, see an
4870 introductory textbook on data structures or algorithms for an
4871 explanation of how hash tables are implemented.
4873 @deffn primitive hashq key size
4874 Determine a hash value for @var{key} that is suitable for
4875 lookups in a hashtable of size @var{size}, where @code{eq?} is
4876 used as the equality predicate. The function returns an
4877 integer in the range 0 to @var{size} - 1. Note that
4878 @code{hashq} may use internal addresses. Thus two calls to
4879 hashq where the keys are @code{eq?} are not guaranteed to
4880 deliver the same value if the key object gets garbage collected
4881 in between. This can happen, for example with symbols:
4882 @code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two
4883 different values, since @code{foo} will be garbage collected.
4886 @deffn primitive hashv key size
4887 Determine a hash value for @var{key} that is suitable for
4888 lookups in a hashtable of size @var{size}, where @code{eqv?} is
4889 used as the equality predicate. The function returns an
4890 integer in the range 0 to @var{size} - 1. Note that
4891 @code{(hashv key)} may use internal addresses. Thus two calls
4892 to hashv where the keys are @code{eqv?} are not guaranteed to
4893 deliver the same value if the key object gets garbage collected
4894 in between. This can happen, for example with symbols:
4895 @code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two
4896 different values, since @code{foo} will be garbage collected.
4899 @deffn primitive hash key size
4900 Determine a hash value for @var{key} that is suitable for
4901 lookups in a hashtable of size @var{size}, where @code{equal?}
4902 is used as the equality predicate. The function returns an
4903 integer in the range 0 to @var{size} - 1.
4906 @deffn primitive hashx-ref hash assoc table key [dflt]
4907 This behaves the same way as the corresponding @code{ref}
4908 function, but uses @var{hash} as a hash function and
4909 @var{assoc} to compare keys. @code{hash} must be a function
4910 that takes two arguments, a key to be hashed and a table size.
4911 @code{assoc} must be an associator function, like @code{assoc},
4912 @code{assq} or @code{assv}.
4914 By way of illustration, @code{hashq-ref table key} is
4915 equivalent to @code{hashx-ref hashq assq table key}.
4918 @deffn primitive hashx-set! hash assoc table key val
4919 This behaves the same way as the corresponding @code{set!}
4920 function, but uses @var{hash} as a hash function and
4921 @var{assoc} to compare keys. @code{hash} must be a function
4922 that takes two arguments, a key to be hashed and a table size.
4923 @code{assoc} must be an associator function, like @code{assoc},
4924 @code{assq} or @code{assv}.
4926 By way of illustration, @code{hashq-set! table key} is
4927 equivalent to @code{hashx-set! hashq assq table key}.
4930 @deffn primitive hashq-get-handle table key
4931 This procedure returns the @code{(key . value)} pair from the
4932 hash table @var{table}. If @var{table} does not hold an
4933 associated value for @var{key}, @code{#f} is returned.
4934 Uses @code{eq?} for equality testing.
4937 @deffn primitive hashv-get-handle table key
4938 This procedure returns the @code{(key . value)} pair from the
4939 hash table @var{table}. If @var{table} does not hold an
4940 associated value for @var{key}, @code{#f} is returned.
4941 Uses @code{eqv?} for equality testing.
4944 @deffn primitive hash-get-handle table key
4945 This procedure returns the @code{(key . value)} pair from the
4946 hash table @var{table}. If @var{table} does not hold an
4947 associated value for @var{key}, @code{#f} is returned.
4948 Uses @code{equal?} for equality testing.
4951 @deffn primitive hashx-get-handle hash assoc table key
4952 This behaves the same way as the corresponding
4953 @code{-get-handle} function, but uses @var{hash} as a hash
4954 function and @var{assoc} to compare keys. @code{hash} must be
4955 a function that takes two arguments, a key to be hashed and a
4956 table size. @code{assoc} must be an associator function, like
4957 @code{assoc}, @code{assq} or @code{assv}.
4960 @deffn primitive hashq-create-handle! table key init
4961 This function looks up @var{key} in @var{table} and returns its handle.
4962 If @var{key} is not already present, a new handle is created which
4963 associates @var{key} with @var{init}.
4966 @deffn primitive hashv-create-handle! table key init
4967 This function looks up @var{key} in @var{table} and returns its handle.
4968 If @var{key} is not already present, a new handle is created which
4969 associates @var{key} with @var{init}.
4972 @deffn primitive hash-create-handle! table key init
4973 This function looks up @var{key} in @var{table} and returns its handle.
4974 If @var{key} is not already present, a new handle is created which
4975 associates @var{key} with @var{init}.
4978 @deffn primitive hashx-create-handle! hash assoc table key init
4979 This behaves the same way as the corresponding
4980 @code{-create-handle} function, but uses @var{hash} as a hash
4981 function and @var{assoc} to compare keys. @code{hash} must be
4982 a function that takes two arguments, a key to be hashed and a
4983 table size. @code{assoc} must be an associator function, like
4984 @code{assoc}, @code{assq} or @code{assv}.
4987 @deffn primitive hash-fold proc init table
4988 An iterator over hash-table elements.
4989 Accumulates and returns a result by applying PROC successively.
4990 The arguments to PROC are "(key value prior-result)" where key
4991 and value are successive pairs from the hash table TABLE, and
4992 prior-result is either INIT (for the first application of PROC)
4993 or the return value of the previous application of PROC.
4994 For example, @code{(hash-fold acons () tab)} will convert a hash
4995 table into an a-list of key-value pairs.
5003 @c FIXME::martin: Review me!
5005 A hook is basically a list of procedures to be called at well defined
5006 points in time. Hooks are used internally for several debugging
5007 facilities, but they can be used in user code, too.
5009 Hooks are created with @code{make-hook}, then procedures can be added to
5010 a hook with @code{add-hook!} or removed with @code{remove-hook!} or
5011 @code{reset-hook!}. The procedures stored in a hook can be invoked with
5015 * Hook Examples:: Hook usage by example.
5016 * Hook Reference:: Reference of all hook procedures.
5020 @subsection Hook Examples
5022 Hook usage is shown by some examples in this section. First, we will
5023 define a hook of arity 2 --- that is, the procedures stored in the hook
5024 will have to accept two arguments.
5027 (define hook (make-hook 2))
5029 @result{} #<hook 2 40286c90>
5032 Now we are ready to add some procedures to the newly created hook with
5033 @code{add-hook!}. In the following example, two procedures are added,
5034 which print different messages and do different things with their
5035 arguments. When the procedures have been added, we can invoke them
5036 using @code{run-hook}.
5039 (add-hook! hook (lambda (x y)
5043 (add-hook! hook (lambda (x y)
5052 Note that the procedures are called in reverse order than they were
5053 added. This can be changed by providing the optional third argument
5054 on the second call to @code{add-hook!}.
5057 (add-hook! hook (lambda (x y)
5061 (add-hook! hook (lambda (x y)
5065 #t) ; @r{<- Change here!}
5071 @node Hook Reference
5072 @subsection Hook Reference
5074 When a hook is created with @code{make-hook}, you can supply the arity
5075 of the procedures which can be added to the hook. The arity defaults to
5076 zero. All procedures of a hook must have the same arity, and when the
5077 procedures are invoked using @code{run-hook}, the number of arguments
5078 must match the arity of the procedures.
5080 The order in which procedures are added to a hook matters. If the third
5081 parameter to @var{add-hook!} is omitted or is equal to @code{#f}, the
5082 procedure is added in front of the procedures which might already be on
5083 that hook, otherwise the procedure is added at the end. The procedures
5084 are always called from first to last when they are invoked via
5087 When calling @code{hook->list}, the procedures in the resulting list are
5088 in the same order as they would have been called by @code{run-hook}.
5090 @deffn primitive make-hook-with-name name [n_args]
5091 Create a named hook with the name @var{name} for storing
5092 procedures of arity @var{n_args}. @var{n_args} defaults to
5096 @deffn primitive make-hook [n_args]
5097 Create a hook for storing procedure of arity
5098 @var{n_args}. @var{n_args} defaults to zero.
5101 @deffn primitive hook? x
5102 Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.
5105 @deffn primitive hook-empty? hook
5106 Return @code{#t} if @var{hook} is an empty hook, @code{#f}
5110 @deffn primitive add-hook! hook proc [append_p]
5111 Add the procedure @var{proc} to the hook @var{hook}. The
5112 procedure is added to the end if @var{append_p} is true,
5113 otherwise it is added to the front.
5116 @deffn primitive remove-hook! hook proc
5117 Remove the procedure @var{proc} from the hook @var{hook}.
5120 @deffn primitive reset-hook! hook
5121 Remove all procedures from the hook @var{hook}.
5124 @deffn primitive run-hook hook . args
5125 Apply all procedures from the hook @var{hook} to the arguments
5126 @var{args}. The order of the procedure application is first to
5130 @deffn primitive hook->list hook
5131 Convert the procedure list of @var{hook} to a list.
5135 @node Other Data Types
5136 @section Other Core Guile Data Types
5139 @c TeX-master: "guile.texi"