2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
8 @node Simple Data Types
9 @section Simple Generic Data Types
11 This chapter describes those of Guile's simple data types which are
12 primarily used for their role as items of generic data. By
13 @dfn{simple} we mean data types that are not primarily used as
14 containers to hold other data --- i.e.@: pairs, lists, vectors and so on.
15 For the documentation of such @dfn{compound} data types, see
16 @ref{Compound Data Types}.
18 @c One of the great strengths of Scheme is that there is no straightforward
19 @c distinction between ``data'' and ``functionality''. For example,
20 @c Guile's support for dynamic linking could be described:
24 @c either in a ``data-centric'' way, as the behaviour and properties of the
25 @c ``dynamically linked object'' data type, and the operations that may be
26 @c applied to instances of this type
29 @c or in a ``functionality-centric'' way, as the set of procedures that
30 @c constitute Guile's support for dynamic linking, in the context of the
34 @c The contents of this chapter are, therefore, a matter of judgment. By
35 @c @dfn{generic}, we mean to select those data types whose typical use as
36 @c @emph{data} in a wide variety of programming contexts is more important
37 @c than their use in the implementation of a particular piece of
38 @c @emph{functionality}. The last section of this chapter provides
39 @c references for all the data types that are documented not here but in a
40 @c ``functionality-centric'' way elsewhere in the manual.
43 * Booleans:: True/false values.
44 * Numbers:: Numerical data types.
45 * Characters:: Single characters.
46 * Character Sets:: Sets of characters.
47 * Strings:: Sequences of characters.
48 * Bytevectors:: Sequences of bytes.
49 * Regular Expressions:: Pattern matching and substitution.
51 * Keywords:: Self-quoting, customizable display keywords.
52 * Other Types:: "Functionality-centric" data types.
60 The two boolean values are @code{#t} for true and @code{#f} for false.
62 Boolean values are returned by predicate procedures, such as the general
63 equality predicates @code{eq?}, @code{eqv?} and @code{equal?}
64 (@pxref{Equality}) and numerical and string comparison operators like
65 @code{string=?} (@pxref{String Comparison}) and @code{<=}
75 (equal? "house" "houses")
83 In test condition contexts like @code{if} and @code{cond} (@pxref{if
84 cond case}), where a group of subexpressions will be evaluated only if a
85 @var{condition} expression evaluates to ``true'', ``true'' means any
86 value at all except @code{#f}.
99 A result of this asymmetry is that typical Scheme source code more often
100 uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to
101 represent an @code{if} or @code{cond} false value, whereas @code{#t} is
102 not necessary to represent an @code{if} or @code{cond} true value.
104 It is important to note that @code{#f} is @strong{not} equivalent to any
105 other Scheme value. In particular, @code{#f} is not the same as the
106 number 0 (like in C and C++), and not the same as the ``empty list''
107 (like in some Lisp dialects).
109 In C, the two Scheme boolean values are available as the two constants
110 @code{SCM_BOOL_T} for @code{#t} and @code{SCM_BOOL_F} for @code{#f}.
111 Care must be taken with the false value @code{SCM_BOOL_F}: it is not
112 false when used in C conditionals. In order to test for it, use
113 @code{scm_is_false} or @code{scm_is_true}.
116 @deffn {Scheme Procedure} not x
117 @deffnx {C Function} scm_not (x)
118 Return @code{#t} if @var{x} is @code{#f}, else return @code{#f}.
122 @deffn {Scheme Procedure} boolean? obj
123 @deffnx {C Function} scm_boolean_p (obj)
124 Return @code{#t} if @var{obj} is either @code{#t} or @code{#f}, else
128 @deftypevr {C Macro} SCM SCM_BOOL_T
129 The @code{SCM} representation of the Scheme object @code{#t}.
132 @deftypevr {C Macro} SCM SCM_BOOL_F
133 The @code{SCM} representation of the Scheme object @code{#f}.
136 @deftypefn {C Function} int scm_is_true (SCM obj)
137 Return @code{0} if @var{obj} is @code{#f}, else return @code{1}.
140 @deftypefn {C Function} int scm_is_false (SCM obj)
141 Return @code{1} if @var{obj} is @code{#f}, else return @code{0}.
144 @deftypefn {C Function} int scm_is_bool (SCM obj)
145 Return @code{1} if @var{obj} is either @code{#t} or @code{#f}, else
149 @deftypefn {C Function} SCM scm_from_bool (int val)
150 Return @code{#f} if @var{val} is @code{0}, else return @code{#t}.
153 @deftypefn {C Function} int scm_to_bool (SCM val)
154 Return @code{1} if @var{val} is @code{SCM_BOOL_T}, return @code{0}
155 when @var{val} is @code{SCM_BOOL_F}, else signal a `wrong type' error.
157 You should probably use @code{scm_is_true} instead of this function
158 when you just want to test a @code{SCM} value for trueness.
162 @subsection Numerical data types
165 Guile supports a rich ``tower'' of numerical types --- integer,
166 rational, real and complex --- and provides an extensive set of
167 mathematical and scientific functions for operating on numerical
168 data. This section of the manual documents those types and functions.
170 You may also find it illuminating to read R5RS's presentation of numbers
171 in Scheme, which is particularly clear and accessible: see
172 @ref{Numbers,,,r5rs,R5RS}.
175 * Numerical Tower:: Scheme's numerical "tower".
176 * Integers:: Whole numbers.
177 * Reals and Rationals:: Real and rational numbers.
178 * Complex Numbers:: Complex numbers.
179 * Exactness:: Exactness and inexactness.
180 * Number Syntax:: Read syntax for numerical data.
181 * Integer Operations:: Operations on integer values.
182 * Comparison:: Comparison predicates.
183 * Conversion:: Converting numbers to and from strings.
184 * Complex:: Complex number operations.
185 * Arithmetic:: Arithmetic functions.
186 * Scientific:: Scientific functions.
187 * Bitwise Operations:: Logical AND, OR, NOT, and so on.
188 * Random:: Random number generation.
192 @node Numerical Tower
193 @subsubsection Scheme's Numerical ``Tower''
196 Scheme's numerical ``tower'' consists of the following categories of
201 Whole numbers, positive or negative; e.g.@: --5, 0, 18.
204 The set of numbers that can be expressed as @math{@var{p}/@var{q}}
205 where @var{p} and @var{q} are integers; e.g.@: @math{9/16} works, but
206 pi (an irrational number) doesn't. These include integers
210 The set of numbers that describes all possible positions along a
211 one-dimensional line. This includes rationals as well as irrational
214 @item complex numbers
215 The set of numbers that describes all possible positions in a two
216 dimensional space. This includes real as well as imaginary numbers
217 (@math{@var{a}+@var{b}i}, where @var{a} is the @dfn{real part},
218 @var{b} is the @dfn{imaginary part}, and @math{i} is the square root of
222 It is called a tower because each category ``sits on'' the one that
223 follows it, in the sense that every integer is also a rational, every
224 rational is also real, and every real number is also a complex number
225 (but with zero imaginary part).
227 In addition to the classification into integers, rationals, reals and
228 complex numbers, Scheme also distinguishes between whether a number is
229 represented exactly or not. For example, the result of
230 @m{2\sin(\pi/4),2*sin(pi/4)} is exactly @m{\sqrt{2},2^(1/2)}, but Guile
231 can represent neither @m{\pi/4,pi/4} nor @m{\sqrt{2},2^(1/2)} exactly.
232 Instead, it stores an inexact approximation, using the C type
235 Guile can represent exact rationals of any magnitude, inexact
236 rationals that fit into a C @code{double}, and inexact complex numbers
237 with @code{double} real and imaginary parts.
239 The @code{number?} predicate may be applied to any Scheme value to
240 discover whether the value is any of the supported numerical types.
242 @deffn {Scheme Procedure} number? obj
243 @deffnx {C Function} scm_number_p (obj)
244 Return @code{#t} if @var{obj} is any kind of number, else @code{#f}.
253 (number? "hello there!")
256 (define pi 3.141592654)
261 @deftypefn {C Function} int scm_is_number (SCM obj)
262 This is equivalent to @code{scm_is_true (scm_number_p (obj))}.
265 The next few subsections document each of Guile's numerical data types
269 @subsubsection Integers
271 @tpindex Integer numbers
275 Integers are whole numbers, that is numbers with no fractional part,
276 such as 2, 83, and @minus{}3789.
278 Integers in Guile can be arbitrarily big, as shown by the following
282 (define (factorial n)
283 (let loop ((n n) (product 1))
286 (loop (- n 1) (* product n)))))
292 @result{} 2432902008176640000
295 @result{} -119622220865480194561963161495657715064383733760000000000
298 Readers whose background is in programming languages where integers are
299 limited by the need to fit into just 4 or 8 bytes of memory may find
300 this surprising, or suspect that Guile's representation of integers is
301 inefficient. In fact, Guile achieves a near optimal balance of
302 convenience and efficiency by using the host computer's native
303 representation of integers where possible, and a more general
304 representation where the required number does not fit in the native
305 form. Conversion between these two representations is automatic and
306 completely invisible to the Scheme level programmer.
308 The infinities @samp{+inf.0} and @samp{-inf.0} are considered to be
309 inexact integers. They are explained in detail in the next section,
310 together with reals and rationals.
312 C has a host of different integer types, and Guile offers a host of
313 functions to convert between them and the @code{SCM} representation.
314 For example, a C @code{int} can be handled with @code{scm_to_int} and
315 @code{scm_from_int}. Guile also defines a few C integer types of its
316 own, to help with differences between systems.
318 C integer types that are not covered can be handled with the generic
319 @code{scm_to_signed_integer} and @code{scm_from_signed_integer} for
320 signed types, or with @code{scm_to_unsigned_integer} and
321 @code{scm_from_unsigned_integer} for unsigned types.
323 Scheme integers can be exact and inexact. For example, a number
324 written as @code{3.0} with an explicit decimal-point is inexact, but
325 it is also an integer. The functions @code{integer?} and
326 @code{scm_is_integer} report true for such a number, but the functions
327 @code{scm_is_signed_integer} and @code{scm_is_unsigned_integer} only
328 allow exact integers and thus report false. Likewise, the conversion
329 functions like @code{scm_to_signed_integer} only accept exact
332 The motivation for this behavior is that the inexactness of a number
333 should not be lost silently. If you want to allow inexact integers,
334 you can explicitly insert a call to @code{inexact->exact} or to its C
335 equivalent @code{scm_inexact_to_exact}. (Only inexact integers will
336 be converted by this call into exact integers; inexact non-integers
337 will become exact fractions.)
339 @deffn {Scheme Procedure} integer? x
340 @deffnx {C Function} scm_integer_p (x)
341 Return @code{#t} if @var{x} is an exact or inexact integer number, else
359 @deftypefn {C Function} int scm_is_integer (SCM x)
360 This is equivalent to @code{scm_is_true (scm_integer_p (x))}.
363 @defvr {C Type} scm_t_int8
364 @defvrx {C Type} scm_t_uint8
365 @defvrx {C Type} scm_t_int16
366 @defvrx {C Type} scm_t_uint16
367 @defvrx {C Type} scm_t_int32
368 @defvrx {C Type} scm_t_uint32
369 @defvrx {C Type} scm_t_int64
370 @defvrx {C Type} scm_t_uint64
371 @defvrx {C Type} scm_t_intmax
372 @defvrx {C Type} scm_t_uintmax
373 The C types are equivalent to the corresponding ISO C types but are
374 defined on all platforms, with the exception of @code{scm_t_int64} and
375 @code{scm_t_uint64}, which are only defined when a 64-bit type is
376 available. For example, @code{scm_t_int8} is equivalent to
379 You can regard these definitions as a stop-gap measure until all
380 platforms provide these types. If you know that all the platforms
381 that you are interested in already provide these types, it is better
382 to use them directly instead of the types provided by Guile.
385 @deftypefn {C Function} int scm_is_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
386 @deftypefnx {C Function} int scm_is_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
387 Return @code{1} when @var{x} represents an exact integer that is
388 between @var{min} and @var{max}, inclusive.
390 These functions can be used to check whether a @code{SCM} value will
391 fit into a given range, such as the range of a given C integer type.
392 If you just want to convert a @code{SCM} value to a given C integer
393 type, use one of the conversion functions directly.
396 @deftypefn {C Function} scm_t_intmax scm_to_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
397 @deftypefnx {C Function} scm_t_uintmax scm_to_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
398 When @var{x} represents an exact integer that is between @var{min} and
399 @var{max} inclusive, return that integer. Else signal an error,
400 either a `wrong-type' error when @var{x} is not an exact integer, or
401 an `out-of-range' error when it doesn't fit the given range.
404 @deftypefn {C Function} SCM scm_from_signed_integer (scm_t_intmax x)
405 @deftypefnx {C Function} SCM scm_from_unsigned_integer (scm_t_uintmax x)
406 Return the @code{SCM} value that represents the integer @var{x}. This
407 function will always succeed and will always return an exact number.
410 @deftypefn {C Function} char scm_to_char (SCM x)
411 @deftypefnx {C Function} {signed char} scm_to_schar (SCM x)
412 @deftypefnx {C Function} {unsigned char} scm_to_uchar (SCM x)
413 @deftypefnx {C Function} short scm_to_short (SCM x)
414 @deftypefnx {C Function} {unsigned short} scm_to_ushort (SCM x)
415 @deftypefnx {C Function} int scm_to_int (SCM x)
416 @deftypefnx {C Function} {unsigned int} scm_to_uint (SCM x)
417 @deftypefnx {C Function} long scm_to_long (SCM x)
418 @deftypefnx {C Function} {unsigned long} scm_to_ulong (SCM x)
419 @deftypefnx {C Function} {long long} scm_to_long_long (SCM x)
420 @deftypefnx {C Function} {unsigned long long} scm_to_ulong_long (SCM x)
421 @deftypefnx {C Function} size_t scm_to_size_t (SCM x)
422 @deftypefnx {C Function} ssize_t scm_to_ssize_t (SCM x)
423 @deftypefnx {C Function} scm_t_int8 scm_to_int8 (SCM x)
424 @deftypefnx {C Function} scm_t_uint8 scm_to_uint8 (SCM x)
425 @deftypefnx {C Function} scm_t_int16 scm_to_int16 (SCM x)
426 @deftypefnx {C Function} scm_t_uint16 scm_to_uint16 (SCM x)
427 @deftypefnx {C Function} scm_t_int32 scm_to_int32 (SCM x)
428 @deftypefnx {C Function} scm_t_uint32 scm_to_uint32 (SCM x)
429 @deftypefnx {C Function} scm_t_int64 scm_to_int64 (SCM x)
430 @deftypefnx {C Function} scm_t_uint64 scm_to_uint64 (SCM x)
431 @deftypefnx {C Function} scm_t_intmax scm_to_intmax (SCM x)
432 @deftypefnx {C Function} scm_t_uintmax scm_to_uintmax (SCM x)
433 When @var{x} represents an exact integer that fits into the indicated
434 C type, return that integer. Else signal an error, either a
435 `wrong-type' error when @var{x} is not an exact integer, or an
436 `out-of-range' error when it doesn't fit the given range.
438 The functions @code{scm_to_long_long}, @code{scm_to_ulong_long},
439 @code{scm_to_int64}, and @code{scm_to_uint64} are only available when
440 the corresponding types are.
443 @deftypefn {C Function} SCM scm_from_char (char x)
444 @deftypefnx {C Function} SCM scm_from_schar (signed char x)
445 @deftypefnx {C Function} SCM scm_from_uchar (unsigned char x)
446 @deftypefnx {C Function} SCM scm_from_short (short x)
447 @deftypefnx {C Function} SCM scm_from_ushort (unsigned short x)
448 @deftypefnx {C Function} SCM scm_from_int (int x)
449 @deftypefnx {C Function} SCM scm_from_uint (unsigned int x)
450 @deftypefnx {C Function} SCM scm_from_long (long x)
451 @deftypefnx {C Function} SCM scm_from_ulong (unsigned long x)
452 @deftypefnx {C Function} SCM scm_from_long_long (long long x)
453 @deftypefnx {C Function} SCM scm_from_ulong_long (unsigned long long x)
454 @deftypefnx {C Function} SCM scm_from_size_t (size_t x)
455 @deftypefnx {C Function} SCM scm_from_ssize_t (ssize_t x)
456 @deftypefnx {C Function} SCM scm_from_int8 (scm_t_int8 x)
457 @deftypefnx {C Function} SCM scm_from_uint8 (scm_t_uint8 x)
458 @deftypefnx {C Function} SCM scm_from_int16 (scm_t_int16 x)
459 @deftypefnx {C Function} SCM scm_from_uint16 (scm_t_uint16 x)
460 @deftypefnx {C Function} SCM scm_from_int32 (scm_t_int32 x)
461 @deftypefnx {C Function} SCM scm_from_uint32 (scm_t_uint32 x)
462 @deftypefnx {C Function} SCM scm_from_int64 (scm_t_int64 x)
463 @deftypefnx {C Function} SCM scm_from_uint64 (scm_t_uint64 x)
464 @deftypefnx {C Function} SCM scm_from_intmax (scm_t_intmax x)
465 @deftypefnx {C Function} SCM scm_from_uintmax (scm_t_uintmax x)
466 Return the @code{SCM} value that represents the integer @var{x}.
467 These functions will always succeed and will always return an exact
471 @deftypefn {C Function} void scm_to_mpz (SCM val, mpz_t rop)
472 Assign @var{val} to the multiple precision integer @var{rop}.
473 @var{val} must be an exact integer, otherwise an error will be
474 signalled. @var{rop} must have been initialized with @code{mpz_init}
475 before this function is called. When @var{rop} is no longer needed
476 the occupied space must be freed with @code{mpz_clear}.
477 @xref{Initializing Integers,,, gmp, GNU MP Manual}, for details.
480 @deftypefn {C Function} SCM scm_from_mpz (mpz_t val)
481 Return the @code{SCM} value that represents @var{val}.
484 @node Reals and Rationals
485 @subsubsection Real and Rational Numbers
486 @tpindex Real numbers
487 @tpindex Rational numbers
492 Mathematically, the real numbers are the set of numbers that describe
493 all possible points along a continuous, infinite, one-dimensional line.
494 The rational numbers are the set of all numbers that can be written as
495 fractions @var{p}/@var{q}, where @var{p} and @var{q} are integers.
496 All rational numbers are also real, but there are real numbers that
497 are not rational, for example @m{\sqrt2, the square root of 2}, and
500 Guile can represent both exact and inexact rational numbers, but it
501 can not represent irrational numbers. Exact rationals are represented
502 by storing the numerator and denominator as two exact integers.
503 Inexact rationals are stored as floating point numbers using the C
506 Exact rationals are written as a fraction of integers. There must be
507 no whitespace around the slash:
514 Even though the actual encoding of inexact rationals is in binary, it
515 may be helpful to think of it as a decimal number with a limited
516 number of significant figures and a decimal point somewhere, since
517 this corresponds to the standard notation for non-whole numbers. For
523 -5648394822220000000000.0
527 The limited precision of Guile's encoding means that any ``real'' number
528 in Guile can be written in a rational form, by multiplying and then dividing
529 by sufficient powers of 10 (or in fact, 2). For example,
530 @samp{-0.00000142857931198} is the same as @minus{}142857931198 divided by
531 100000000000000000. In Guile's current incarnation, therefore, the
532 @code{rational?} and @code{real?} predicates are equivalent.
535 Dividing by an exact zero leads to a error message, as one might
536 expect. However, dividing by an inexact zero does not produce an
537 error. Instead, the result of the division is either plus or minus
538 infinity, depending on the sign of the divided number.
540 The infinities are written @samp{+inf.0} and @samp{-inf.0},
541 respectively. This syntax is also recognized by @code{read} as an
542 extension to the usual Scheme syntax.
544 Dividing zero by zero yields something that is not a number at all:
545 @samp{+nan.0}. This is the special `not a number' value.
547 On platforms that follow @acronym{IEEE} 754 for their floating point
548 arithmetic, the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values
549 are implemented using the corresponding @acronym{IEEE} 754 values.
550 They behave in arithmetic operations like @acronym{IEEE} 754 describes
551 it, i.e., @code{(= +nan.0 +nan.0)} @result{} @code{#f}.
553 The infinities are inexact integers and are considered to be both even
554 and odd. While @samp{+nan.0} is not @code{=} to itself, it is
555 @code{eqv?} to itself.
557 To test for the special values, use the functions @code{inf?} and
560 @deffn {Scheme Procedure} real? obj
561 @deffnx {C Function} scm_real_p (obj)
562 Return @code{#t} if @var{obj} is a real number, else @code{#f}. Note
563 that the sets of integer and rational values form subsets of the set
564 of real numbers, so the predicate will also be fulfilled if @var{obj}
565 is an integer number or a rational number.
568 @deffn {Scheme Procedure} rational? x
569 @deffnx {C Function} scm_rational_p (x)
570 Return @code{#t} if @var{x} is a rational number, @code{#f} otherwise.
571 Note that the set of integer values forms a subset of the set of
572 rational numbers, i. e. the predicate will also be fulfilled if
573 @var{x} is an integer number.
575 Since Guile can not represent irrational numbers, every number
576 satisfying @code{real?} also satisfies @code{rational?} in Guile.
579 @deffn {Scheme Procedure} rationalize x eps
580 @deffnx {C Function} scm_rationalize (x, eps)
581 Returns the @emph{simplest} rational number differing
582 from @var{x} by no more than @var{eps}.
584 As required by @acronym{R5RS}, @code{rationalize} only returns an
585 exact result when both its arguments are exact. Thus, you might need
586 to use @code{inexact->exact} on the arguments.
589 (rationalize (inexact->exact 1.2) 1/100)
595 @deffn {Scheme Procedure} inf? x
596 @deffnx {C Function} scm_inf_p (x)
597 Return @code{#t} if @var{x} is either @samp{+inf.0} or @samp{-inf.0},
601 @deffn {Scheme Procedure} nan? x
602 @deffnx {C Function} scm_nan_p (x)
603 Return @code{#t} if @var{x} is @samp{+nan.0}, @code{#f} otherwise.
606 @deffn {Scheme Procedure} nan
607 @deffnx {C Function} scm_nan ()
611 @deffn {Scheme Procedure} inf
612 @deffnx {C Function} scm_inf ()
616 @deffn {Scheme Procedure} numerator x
617 @deffnx {C Function} scm_numerator (x)
618 Return the numerator of the rational number @var{x}.
621 @deffn {Scheme Procedure} denominator x
622 @deffnx {C Function} scm_denominator (x)
623 Return the denominator of the rational number @var{x}.
626 @deftypefn {C Function} int scm_is_real (SCM val)
627 @deftypefnx {C Function} int scm_is_rational (SCM val)
628 Equivalent to @code{scm_is_true (scm_real_p (val))} and
629 @code{scm_is_true (scm_rational_p (val))}, respectively.
632 @deftypefn {C Function} double scm_to_double (SCM val)
633 Returns the number closest to @var{val} that is representable as a
634 @code{double}. Returns infinity for a @var{val} that is too large in
635 magnitude. The argument @var{val} must be a real number.
638 @deftypefn {C Function} SCM scm_from_double (double val)
639 Return the @code{SCM} value that represents @var{val}. The returned
640 value is inexact according to the predicate @code{inexact?}, but it
641 will be exactly equal to @var{val}.
644 @node Complex Numbers
645 @subsubsection Complex Numbers
646 @tpindex Complex numbers
650 Complex numbers are the set of numbers that describe all possible points
651 in a two-dimensional space. The two coordinates of a particular point
652 in this space are known as the @dfn{real} and @dfn{imaginary} parts of
653 the complex number that describes that point.
655 In Guile, complex numbers are written in rectangular form as the sum of
656 their real and imaginary parts, using the symbol @code{i} to indicate
671 Polar form can also be used, with an @samp{@@} between magnitude and
675 1@@3.141592 @result{} -1.0 (approx)
676 -1@@1.57079 @result{} 0.0-1.0i (approx)
679 Guile represents a complex number with a non-zero imaginary part as a
680 pair of inexact rationals, so the real and imaginary parts of a
681 complex number have the same properties of inexactness and limited
682 precision as single inexact rational numbers. Guile can not represent
683 exact complex numbers with non-zero imaginary parts.
685 @deffn {Scheme Procedure} complex? z
686 @deffnx {C Function} scm_complex_p (z)
687 Return @code{#t} if @var{x} is a complex number, @code{#f}
688 otherwise. Note that the sets of real, rational and integer
689 values form subsets of the set of complex numbers, i. e. the
690 predicate will also be fulfilled if @var{x} is a real,
691 rational or integer number.
694 @deftypefn {C Function} int scm_is_complex (SCM val)
695 Equivalent to @code{scm_is_true (scm_complex_p (val))}.
699 @subsubsection Exact and Inexact Numbers
700 @tpindex Exact numbers
701 @tpindex Inexact numbers
705 @rnindex exact->inexact
706 @rnindex inexact->exact
708 R5RS requires that a calculation involving inexact numbers always
709 produces an inexact result. To meet this requirement, Guile
710 distinguishes between an exact integer value such as @samp{5} and the
711 corresponding inexact real value which, to the limited precision
712 available, has no fractional part, and is printed as @samp{5.0}. Guile
713 will only convert the latter value to the former when forced to do so by
714 an invocation of the @code{inexact->exact} procedure.
716 @deffn {Scheme Procedure} exact? z
717 @deffnx {C Function} scm_exact_p (z)
718 Return @code{#t} if the number @var{z} is exact, @code{#f}
734 @deffn {Scheme Procedure} inexact? z
735 @deffnx {C Function} scm_inexact_p (z)
736 Return @code{#t} if the number @var{z} is inexact, @code{#f}
740 @deffn {Scheme Procedure} inexact->exact z
741 @deffnx {C Function} scm_inexact_to_exact (z)
742 Return an exact number that is numerically closest to @var{z}, when
743 there is one. For inexact rationals, Guile returns the exact rational
744 that is numerically equal to the inexact rational. Inexact complex
745 numbers with a non-zero imaginary part can not be made exact.
752 The following happens because 12/10 is not exactly representable as a
753 @code{double} (on most platforms). However, when reading a decimal
754 number that has been marked exact with the ``#e'' prefix, Guile is
755 able to represent it correctly.
759 @result{} 5404319552844595/4503599627370496
767 @c begin (texi-doc-string "guile" "exact->inexact")
768 @deffn {Scheme Procedure} exact->inexact z
769 @deffnx {C Function} scm_exact_to_inexact (z)
770 Convert the number @var{z} to its inexact representation.
775 @subsubsection Read Syntax for Numerical Data
777 The read syntax for integers is a string of digits, optionally
778 preceded by a minus or plus character, a code indicating the
779 base in which the integer is encoded, and a code indicating whether
780 the number is exact or inexact. The supported base codes are:
785 the integer is written in binary (base 2)
789 the integer is written in octal (base 8)
793 the integer is written in decimal (base 10)
797 the integer is written in hexadecimal (base 16)
800 If the base code is omitted, the integer is assumed to be decimal. The
801 following examples show how these base codes are used.
820 The codes for indicating exactness (which can, incidentally, be applied
821 to all numerical values) are:
830 the number is inexact.
833 If the exactness indicator is omitted, the number is exact unless it
834 contains a radix point. Since Guile can not represent exact complex
835 numbers, an error is signalled when asking for them.
845 ERROR: Wrong type argument
848 Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for
849 plus and minus infinity, respectively. The value must be written
850 exactly as shown, that is, they always must have a sign and exactly
851 one zero digit after the decimal point. It also understands
852 @samp{+nan.0} and @samp{-nan.0} for the special `not-a-number' value.
853 The sign is ignored for `not-a-number' and the value is always printed
856 @node Integer Operations
857 @subsubsection Operations on Integer Values
866 @deffn {Scheme Procedure} odd? n
867 @deffnx {C Function} scm_odd_p (n)
868 Return @code{#t} if @var{n} is an odd number, @code{#f}
872 @deffn {Scheme Procedure} even? n
873 @deffnx {C Function} scm_even_p (n)
874 Return @code{#t} if @var{n} is an even number, @code{#f}
878 @c begin (texi-doc-string "guile" "quotient")
879 @c begin (texi-doc-string "guile" "remainder")
880 @deffn {Scheme Procedure} quotient n d
881 @deffnx {Scheme Procedure} remainder n d
882 @deffnx {C Function} scm_quotient (n, d)
883 @deffnx {C Function} scm_remainder (n, d)
884 Return the quotient or remainder from @var{n} divided by @var{d}. The
885 quotient is rounded towards zero, and the remainder will have the same
886 sign as @var{n}. In all cases quotient and remainder satisfy
887 @math{@var{n} = @var{q}*@var{d} + @var{r}}.
890 (remainder 13 4) @result{} 1
891 (remainder -13 4) @result{} -1
895 @c begin (texi-doc-string "guile" "modulo")
896 @deffn {Scheme Procedure} modulo n d
897 @deffnx {C Function} scm_modulo (n, d)
898 Return the remainder from @var{n} divided by @var{d}, with the same
902 (modulo 13 4) @result{} 1
903 (modulo -13 4) @result{} 3
904 (modulo 13 -4) @result{} -3
905 (modulo -13 -4) @result{} -1
909 @c begin (texi-doc-string "guile" "gcd")
910 @deffn {Scheme Procedure} gcd x@dots{}
911 @deffnx {C Function} scm_gcd (x, y)
912 Return the greatest common divisor of all arguments.
913 If called without arguments, 0 is returned.
915 The C function @code{scm_gcd} always takes two arguments, while the
916 Scheme function can take an arbitrary number.
919 @c begin (texi-doc-string "guile" "lcm")
920 @deffn {Scheme Procedure} lcm x@dots{}
921 @deffnx {C Function} scm_lcm (x, y)
922 Return the least common multiple of the arguments.
923 If called without arguments, 1 is returned.
925 The C function @code{scm_lcm} always takes two arguments, while the
926 Scheme function can take an arbitrary number.
929 @deffn {Scheme Procedure} modulo-expt n k m
930 @deffnx {C Function} scm_modulo_expt (n, k, m)
931 Return @var{n} raised to the integer exponent
932 @var{k}, modulo @var{m}.
941 @subsubsection Comparison Predicates
946 The C comparison functions below always takes two arguments, while the
947 Scheme functions can take an arbitrary number. Also keep in mind that
948 the C functions return one of the Scheme boolean values
949 @code{SCM_BOOL_T} or @code{SCM_BOOL_F} which are both true as far as C
950 is concerned. Thus, always write @code{scm_is_true (scm_num_eq_p (x,
951 y))} when testing the two Scheme numbers @code{x} and @code{y} for
952 equality, for example.
954 @c begin (texi-doc-string "guile" "=")
955 @deffn {Scheme Procedure} =
956 @deffnx {C Function} scm_num_eq_p (x, y)
957 Return @code{#t} if all parameters are numerically equal.
960 @c begin (texi-doc-string "guile" "<")
961 @deffn {Scheme Procedure} <
962 @deffnx {C Function} scm_less_p (x, y)
963 Return @code{#t} if the list of parameters is monotonically
967 @c begin (texi-doc-string "guile" ">")
968 @deffn {Scheme Procedure} >
969 @deffnx {C Function} scm_gr_p (x, y)
970 Return @code{#t} if the list of parameters is monotonically
974 @c begin (texi-doc-string "guile" "<=")
975 @deffn {Scheme Procedure} <=
976 @deffnx {C Function} scm_leq_p (x, y)
977 Return @code{#t} if the list of parameters is monotonically
981 @c begin (texi-doc-string "guile" ">=")
982 @deffn {Scheme Procedure} >=
983 @deffnx {C Function} scm_geq_p (x, y)
984 Return @code{#t} if the list of parameters is monotonically
988 @c begin (texi-doc-string "guile" "zero?")
989 @deffn {Scheme Procedure} zero? z
990 @deffnx {C Function} scm_zero_p (z)
991 Return @code{#t} if @var{z} is an exact or inexact number equal to
995 @c begin (texi-doc-string "guile" "positive?")
996 @deffn {Scheme Procedure} positive? x
997 @deffnx {C Function} scm_positive_p (x)
998 Return @code{#t} if @var{x} is an exact or inexact number greater than
1002 @c begin (texi-doc-string "guile" "negative?")
1003 @deffn {Scheme Procedure} negative? x
1004 @deffnx {C Function} scm_negative_p (x)
1005 Return @code{#t} if @var{x} is an exact or inexact number less than
1011 @subsubsection Converting Numbers To and From Strings
1012 @rnindex number->string
1013 @rnindex string->number
1015 The following procedures read and write numbers according to their
1016 external representation as defined by R5RS (@pxref{Lexical structure,
1017 R5RS Lexical Structure,, r5rs, The Revised^5 Report on the Algorithmic
1018 Language Scheme}). @xref{Number Input and Output, the @code{(ice-9
1019 i18n)} module}, for locale-dependent number parsing.
1021 @deffn {Scheme Procedure} number->string n [radix]
1022 @deffnx {C Function} scm_number_to_string (n, radix)
1023 Return a string holding the external representation of the
1024 number @var{n} in the given @var{radix}. If @var{n} is
1025 inexact, a radix of 10 will be used.
1028 @deffn {Scheme Procedure} string->number string [radix]
1029 @deffnx {C Function} scm_string_to_number (string, radix)
1030 Return a number of the maximally precise representation
1031 expressed by the given @var{string}. @var{radix} must be an
1032 exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}
1033 is a default radix that may be overridden by an explicit radix
1034 prefix in @var{string} (e.g. "#o177"). If @var{radix} is not
1035 supplied, then the default radix is 10. If string is not a
1036 syntactically valid notation for a number, then
1037 @code{string->number} returns @code{#f}.
1040 @deftypefn {C Function} SCM scm_c_locale_stringn_to_number (const char *string, size_t len, unsigned radix)
1041 As per @code{string->number} above, but taking a C string, as pointer
1042 and length. The string characters should be in the current locale
1043 encoding (@code{locale} in the name refers only to that, there's no
1044 locale-dependent parsing).
1049 @subsubsection Complex Number Operations
1050 @rnindex make-rectangular
1057 @deffn {Scheme Procedure} make-rectangular real imaginary
1058 @deffnx {C Function} scm_make_rectangular (real, imaginary)
1059 Return a complex number constructed of the given @var{real} and
1060 @var{imaginary} parts.
1063 @deffn {Scheme Procedure} make-polar x y
1064 @deffnx {C Function} scm_make_polar (x, y)
1066 Return the complex number @var{x} * e^(i * @var{y}).
1069 @c begin (texi-doc-string "guile" "real-part")
1070 @deffn {Scheme Procedure} real-part z
1071 @deffnx {C Function} scm_real_part (z)
1072 Return the real part of the number @var{z}.
1075 @c begin (texi-doc-string "guile" "imag-part")
1076 @deffn {Scheme Procedure} imag-part z
1077 @deffnx {C Function} scm_imag_part (z)
1078 Return the imaginary part of the number @var{z}.
1081 @c begin (texi-doc-string "guile" "magnitude")
1082 @deffn {Scheme Procedure} magnitude z
1083 @deffnx {C Function} scm_magnitude (z)
1084 Return the magnitude of the number @var{z}. This is the same as
1085 @code{abs} for real arguments, but also allows complex numbers.
1088 @c begin (texi-doc-string "guile" "angle")
1089 @deffn {Scheme Procedure} angle z
1090 @deffnx {C Function} scm_angle (z)
1091 Return the angle of the complex number @var{z}.
1094 @deftypefn {C Function} SCM scm_c_make_rectangular (double re, double im)
1095 @deftypefnx {C Function} SCM scm_c_make_polar (double x, double y)
1096 Like @code{scm_make_rectangular} or @code{scm_make_polar},
1097 respectively, but these functions take @code{double}s as their
1101 @deftypefn {C Function} double scm_c_real_part (z)
1102 @deftypefnx {C Function} double scm_c_imag_part (z)
1103 Returns the real or imaginary part of @var{z} as a @code{double}.
1106 @deftypefn {C Function} double scm_c_magnitude (z)
1107 @deftypefnx {C Function} double scm_c_angle (z)
1108 Returns the magnitude or angle of @var{z} as a @code{double}.
1113 @subsubsection Arithmetic Functions
1128 The C arithmetic functions below always takes two arguments, while the
1129 Scheme functions can take an arbitrary number. When you need to
1130 invoke them with just one argument, for example to compute the
1131 equivalent od @code{(- x)}, pass @code{SCM_UNDEFINED} as the second
1132 one: @code{scm_difference (x, SCM_UNDEFINED)}.
1134 @c begin (texi-doc-string "guile" "+")
1135 @deffn {Scheme Procedure} + z1 @dots{}
1136 @deffnx {C Function} scm_sum (z1, z2)
1137 Return the sum of all parameter values. Return 0 if called without any
1141 @c begin (texi-doc-string "guile" "-")
1142 @deffn {Scheme Procedure} - z1 z2 @dots{}
1143 @deffnx {C Function} scm_difference (z1, z2)
1144 If called with one argument @var{z1}, -@var{z1} is returned. Otherwise
1145 the sum of all but the first argument are subtracted from the first
1149 @c begin (texi-doc-string "guile" "*")
1150 @deffn {Scheme Procedure} * z1 @dots{}
1151 @deffnx {C Function} scm_product (z1, z2)
1152 Return the product of all arguments. If called without arguments, 1 is
1156 @c begin (texi-doc-string "guile" "/")
1157 @deffn {Scheme Procedure} / z1 z2 @dots{}
1158 @deffnx {C Function} scm_divide (z1, z2)
1159 Divide the first argument by the product of the remaining arguments. If
1160 called with one argument @var{z1}, 1/@var{z1} is returned.
1163 @deffn {Scheme Procedure} 1+ z
1164 @deffnx {C Function} scm_oneplus (z)
1165 Return @math{@var{z} + 1}.
1168 @deffn {Scheme Procedure} 1- z
1169 @deffnx {C function} scm_oneminus (z)
1170 Return @math{@var{z} - 1}.
1173 @c begin (texi-doc-string "guile" "abs")
1174 @deffn {Scheme Procedure} abs x
1175 @deffnx {C Function} scm_abs (x)
1176 Return the absolute value of @var{x}.
1178 @var{x} must be a number with zero imaginary part. To calculate the
1179 magnitude of a complex number, use @code{magnitude} instead.
1182 @c begin (texi-doc-string "guile" "max")
1183 @deffn {Scheme Procedure} max x1 x2 @dots{}
1184 @deffnx {C Function} scm_max (x1, x2)
1185 Return the maximum of all parameter values.
1188 @c begin (texi-doc-string "guile" "min")
1189 @deffn {Scheme Procedure} min x1 x2 @dots{}
1190 @deffnx {C Function} scm_min (x1, x2)
1191 Return the minimum of all parameter values.
1194 @c begin (texi-doc-string "guile" "truncate")
1195 @deffn {Scheme Procedure} truncate x
1196 @deffnx {C Function} scm_truncate_number (x)
1197 Round the inexact number @var{x} towards zero.
1200 @c begin (texi-doc-string "guile" "round")
1201 @deffn {Scheme Procedure} round x
1202 @deffnx {C Function} scm_round_number (x)
1203 Round the inexact number @var{x} to the nearest integer. When exactly
1204 halfway between two integers, round to the even one.
1207 @c begin (texi-doc-string "guile" "floor")
1208 @deffn {Scheme Procedure} floor x
1209 @deffnx {C Function} scm_floor (x)
1210 Round the number @var{x} towards minus infinity.
1213 @c begin (texi-doc-string "guile" "ceiling")
1214 @deffn {Scheme Procedure} ceiling x
1215 @deffnx {C Function} scm_ceiling (x)
1216 Round the number @var{x} towards infinity.
1219 @deftypefn {C Function} double scm_c_truncate (double x)
1220 @deftypefnx {C Function} double scm_c_round (double x)
1221 Like @code{scm_truncate_number} or @code{scm_round_number},
1222 respectively, but these functions take and return @code{double}
1227 @subsubsection Scientific Functions
1229 The following procedures accept any kind of number as arguments,
1230 including complex numbers.
1233 @c begin (texi-doc-string "guile" "sqrt")
1234 @deffn {Scheme Procedure} sqrt z
1235 Return the square root of @var{z}. Of the two possible roots
1236 (positive and negative), the one with the a positive real part is
1237 returned, or if that's zero then a positive imaginary part. Thus,
1240 (sqrt 9.0) @result{} 3.0
1241 (sqrt -9.0) @result{} 0.0+3.0i
1242 (sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i
1243 (sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i
1248 @c begin (texi-doc-string "guile" "expt")
1249 @deffn {Scheme Procedure} expt z1 z2
1250 Return @var{z1} raised to the power of @var{z2}.
1254 @c begin (texi-doc-string "guile" "sin")
1255 @deffn {Scheme Procedure} sin z
1256 Return the sine of @var{z}.
1260 @c begin (texi-doc-string "guile" "cos")
1261 @deffn {Scheme Procedure} cos z
1262 Return the cosine of @var{z}.
1266 @c begin (texi-doc-string "guile" "tan")
1267 @deffn {Scheme Procedure} tan z
1268 Return the tangent of @var{z}.
1272 @c begin (texi-doc-string "guile" "asin")
1273 @deffn {Scheme Procedure} asin z
1274 Return the arcsine of @var{z}.
1278 @c begin (texi-doc-string "guile" "acos")
1279 @deffn {Scheme Procedure} acos z
1280 Return the arccosine of @var{z}.
1284 @c begin (texi-doc-string "guile" "atan")
1285 @deffn {Scheme Procedure} atan z
1286 @deffnx {Scheme Procedure} atan y x
1287 Return the arctangent of @var{z}, or of @math{@var{y}/@var{x}}.
1291 @c begin (texi-doc-string "guile" "exp")
1292 @deffn {Scheme Procedure} exp z
1293 Return e to the power of @var{z}, where e is the base of natural
1294 logarithms (2.71828@dots{}).
1298 @c begin (texi-doc-string "guile" "log")
1299 @deffn {Scheme Procedure} log z
1300 Return the natural logarithm of @var{z}.
1303 @c begin (texi-doc-string "guile" "log10")
1304 @deffn {Scheme Procedure} log10 z
1305 Return the base 10 logarithm of @var{z}.
1308 @c begin (texi-doc-string "guile" "sinh")
1309 @deffn {Scheme Procedure} sinh z
1310 Return the hyperbolic sine of @var{z}.
1313 @c begin (texi-doc-string "guile" "cosh")
1314 @deffn {Scheme Procedure} cosh z
1315 Return the hyperbolic cosine of @var{z}.
1318 @c begin (texi-doc-string "guile" "tanh")
1319 @deffn {Scheme Procedure} tanh z
1320 Return the hyperbolic tangent of @var{z}.
1323 @c begin (texi-doc-string "guile" "asinh")
1324 @deffn {Scheme Procedure} asinh z
1325 Return the hyperbolic arcsine of @var{z}.
1328 @c begin (texi-doc-string "guile" "acosh")
1329 @deffn {Scheme Procedure} acosh z
1330 Return the hyperbolic arccosine of @var{z}.
1333 @c begin (texi-doc-string "guile" "atanh")
1334 @deffn {Scheme Procedure} atanh z
1335 Return the hyperbolic arctangent of @var{z}.
1339 @node Bitwise Operations
1340 @subsubsection Bitwise Operations
1342 For the following bitwise functions, negative numbers are treated as
1343 infinite precision twos-complements. For instance @math{-6} is bits
1344 @math{@dots{}111010}, with infinitely many ones on the left. It can
1345 be seen that adding 6 (binary 110) to such a bit pattern gives all
1348 @deffn {Scheme Procedure} logand n1 n2 @dots{}
1349 @deffnx {C Function} scm_logand (n1, n2)
1350 Return the bitwise @sc{and} of the integer arguments.
1353 (logand) @result{} -1
1354 (logand 7) @result{} 7
1355 (logand #b111 #b011 #b001) @result{} 1
1359 @deffn {Scheme Procedure} logior n1 n2 @dots{}
1360 @deffnx {C Function} scm_logior (n1, n2)
1361 Return the bitwise @sc{or} of the integer arguments.
1364 (logior) @result{} 0
1365 (logior 7) @result{} 7
1366 (logior #b000 #b001 #b011) @result{} 3
1370 @deffn {Scheme Procedure} logxor n1 n2 @dots{}
1371 @deffnx {C Function} scm_loxor (n1, n2)
1372 Return the bitwise @sc{xor} of the integer arguments. A bit is
1373 set in the result if it is set in an odd number of arguments.
1376 (logxor) @result{} 0
1377 (logxor 7) @result{} 7
1378 (logxor #b000 #b001 #b011) @result{} 2
1379 (logxor #b000 #b001 #b011 #b011) @result{} 1
1383 @deffn {Scheme Procedure} lognot n
1384 @deffnx {C Function} scm_lognot (n)
1385 Return the integer which is the ones-complement of the integer
1386 argument, ie.@: each 0 bit is changed to 1 and each 1 bit to 0.
1389 (number->string (lognot #b10000000) 2)
1390 @result{} "-10000001"
1391 (number->string (lognot #b0) 2)
1396 @deffn {Scheme Procedure} logtest j k
1397 @deffnx {C Function} scm_logtest (j, k)
1398 Test whether @var{j} and @var{k} have any 1 bits in common. This is
1399 equivalent to @code{(not (zero? (logand j k)))}, but without actually
1400 calculating the @code{logand}, just testing for non-zero.
1403 (logtest #b0100 #b1011) @result{} #f
1404 (logtest #b0100 #b0111) @result{} #t
1408 @deffn {Scheme Procedure} logbit? index j
1409 @deffnx {C Function} scm_logbit_p (index, j)
1410 Test whether bit number @var{index} in @var{j} is set. @var{index}
1411 starts from 0 for the least significant bit.
1414 (logbit? 0 #b1101) @result{} #t
1415 (logbit? 1 #b1101) @result{} #f
1416 (logbit? 2 #b1101) @result{} #t
1417 (logbit? 3 #b1101) @result{} #t
1418 (logbit? 4 #b1101) @result{} #f
1422 @deffn {Scheme Procedure} ash n cnt
1423 @deffnx {C Function} scm_ash (n, cnt)
1424 Return @var{n} shifted left by @var{cnt} bits, or shifted right if
1425 @var{cnt} is negative. This is an ``arithmetic'' shift.
1427 This is effectively a multiplication by @m{2^{cnt}, 2^@var{cnt}}, and
1428 when @var{cnt} is negative it's a division, rounded towards negative
1429 infinity. (Note that this is not the same rounding as @code{quotient}
1432 With @var{n} viewed as an infinite precision twos complement,
1433 @code{ash} means a left shift introducing zero bits, or a right shift
1437 (number->string (ash #b1 3) 2) @result{} "1000"
1438 (number->string (ash #b1010 -1) 2) @result{} "101"
1440 ;; -23 is bits ...11101001, -6 is bits ...111010
1441 (ash -23 -2) @result{} -6
1445 @deffn {Scheme Procedure} logcount n
1446 @deffnx {C Function} scm_logcount (n)
1447 Return the number of bits in integer @var{n}. If @var{n} is
1448 positive, the 1-bits in its binary representation are counted.
1449 If negative, the 0-bits in its two's-complement binary
1450 representation are counted. If zero, 0 is returned.
1453 (logcount #b10101010)
1462 @deffn {Scheme Procedure} integer-length n
1463 @deffnx {C Function} scm_integer_length (n)
1464 Return the number of bits necessary to represent @var{n}.
1466 For positive @var{n} this is how many bits to the most significant one
1467 bit. For negative @var{n} it's how many bits to the most significant
1468 zero bit in twos complement form.
1471 (integer-length #b10101010) @result{} 8
1472 (integer-length #b1111) @result{} 4
1473 (integer-length 0) @result{} 0
1474 (integer-length -1) @result{} 0
1475 (integer-length -256) @result{} 8
1476 (integer-length -257) @result{} 9
1480 @deffn {Scheme Procedure} integer-expt n k
1481 @deffnx {C Function} scm_integer_expt (n, k)
1482 Return @var{n} raised to the power @var{k}. @var{k} must be an exact
1483 integer, @var{n} can be any number.
1485 Negative @var{k} is supported, and results in @m{1/n^|k|, 1/n^abs(k)}
1486 in the usual way. @math{@var{n}^0} is 1, as usual, and that includes
1490 (integer-expt 2 5) @result{} 32
1491 (integer-expt -3 3) @result{} -27
1492 (integer-expt 5 -3) @result{} 1/125
1493 (integer-expt 0 0) @result{} 1
1497 @deffn {Scheme Procedure} bit-extract n start end
1498 @deffnx {C Function} scm_bit_extract (n, start, end)
1499 Return the integer composed of the @var{start} (inclusive)
1500 through @var{end} (exclusive) bits of @var{n}. The
1501 @var{start}th bit becomes the 0-th bit in the result.
1504 (number->string (bit-extract #b1101101010 0 4) 2)
1506 (number->string (bit-extract #b1101101010 4 9) 2)
1513 @subsubsection Random Number Generation
1515 Pseudo-random numbers are generated from a random state object, which
1516 can be created with @code{seed->random-state}. The @var{state}
1517 parameter to the various functions below is optional, it defaults to
1518 the state object in the @code{*random-state*} variable.
1520 @deffn {Scheme Procedure} copy-random-state [state]
1521 @deffnx {C Function} scm_copy_random_state (state)
1522 Return a copy of the random state @var{state}.
1525 @deffn {Scheme Procedure} random n [state]
1526 @deffnx {C Function} scm_random (n, state)
1527 Return a number in [0, @var{n}).
1529 Accepts a positive integer or real n and returns a
1530 number of the same type between zero (inclusive) and
1531 @var{n} (exclusive). The values returned have a uniform
1535 @deffn {Scheme Procedure} random:exp [state]
1536 @deffnx {C Function} scm_random_exp (state)
1537 Return an inexact real in an exponential distribution with mean
1538 1. For an exponential distribution with mean @var{u} use @code{(*
1539 @var{u} (random:exp))}.
1542 @deffn {Scheme Procedure} random:hollow-sphere! vect [state]
1543 @deffnx {C Function} scm_random_hollow_sphere_x (vect, state)
1544 Fills @var{vect} with inexact real random numbers the sum of whose
1545 squares is equal to 1.0. Thinking of @var{vect} as coordinates in
1546 space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1547 the coordinates are uniformly distributed over the surface of the unit
1551 @deffn {Scheme Procedure} random:normal [state]
1552 @deffnx {C Function} scm_random_normal (state)
1553 Return an inexact real in a normal distribution. The distribution
1554 used has mean 0 and standard deviation 1. For a normal distribution
1555 with mean @var{m} and standard deviation @var{d} use @code{(+ @var{m}
1556 (* @var{d} (random:normal)))}.
1559 @deffn {Scheme Procedure} random:normal-vector! vect [state]
1560 @deffnx {C Function} scm_random_normal_vector_x (vect, state)
1561 Fills @var{vect} with inexact real random numbers that are
1562 independent and standard normally distributed
1563 (i.e., with mean 0 and variance 1).
1566 @deffn {Scheme Procedure} random:solid-sphere! vect [state]
1567 @deffnx {C Function} scm_random_solid_sphere_x (vect, state)
1568 Fills @var{vect} with inexact real random numbers the sum of whose
1569 squares is less than 1.0. Thinking of @var{vect} as coordinates in
1570 space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1571 the coordinates are uniformly distributed within the unit
1573 @c FIXME: What does this mean, particularly the n-sphere part?
1576 @deffn {Scheme Procedure} random:uniform [state]
1577 @deffnx {C Function} scm_random_uniform (state)
1578 Return a uniformly distributed inexact real random number in
1582 @deffn {Scheme Procedure} seed->random-state seed
1583 @deffnx {C Function} scm_seed_to_random_state (seed)
1584 Return a new random state using @var{seed}.
1587 @defvar *random-state*
1588 The global random state used by the above functions when the
1589 @var{state} parameter is not given.
1592 Note that the initial value of @code{*random-state*} is the same every
1593 time Guile starts up. Therefore, if you don't pass a @var{state}
1594 parameter to the above procedures, and you don't set
1595 @code{*random-state*} to @code{(seed->random-state your-seed)}, where
1596 @code{your-seed} is something that @emph{isn't} the same every time,
1597 you'll get the same sequence of ``random'' numbers on every run.
1599 For example, unless the relevant source code has changed, @code{(map
1600 random (cdr (iota 30)))}, if the first use of random numbers since
1601 Guile started up, will always give:
1604 (map random (cdr (iota 19)))
1606 (0 1 1 2 2 2 1 2 6 7 10 0 5 3 12 5 5 12)
1609 To use the time of day as the random seed, you can use code like this:
1612 (let ((time (gettimeofday)))
1613 (set! *random-state*
1614 (seed->random-state (+ (car time)
1619 And then (depending on the time of day, of course):
1622 (map random (cdr (iota 19)))
1624 (0 0 1 0 2 4 5 4 5 5 9 3 10 1 8 3 14 17)
1627 For security applications, such as password generation, you should use
1628 more bits of seed. Otherwise an open source password generator could
1629 be attacked by guessing the seed@dots{} but that's a subject for
1634 @subsection Characters
1637 In Scheme, there is a data type to describe a single character.
1639 Defining what exactly a character @emph{is} can be more complicated
1640 than it seems. Guile follows the advice of R6RS and uses The Unicode
1641 Standard to help define what a character is. So, for Guile, a
1642 character is anything in the Unicode Character Database.
1645 @cindex Unicode code point
1647 The Unicode Character Database is basically a table of characters
1648 indexed using integers called 'code points'. Valid code points are in
1649 the ranges 0 to @code{#xD7FF} inclusive or @code{#xE000} to
1650 @code{#x10FFFF} inclusive, which is about 1.1 million code points.
1652 @cindex designated code point
1653 @cindex code point, designated
1655 Any code point that has been assigned to a character or that has
1656 otherwise been given a meaning by Unicode is called a 'designated code
1657 point'. Most of the designated code points, about 200,000 of them,
1658 indicate characters, accents or other combining marks that modify
1659 other characters, symbols, whitespace, and control characters. Some
1660 are not characters but indicators that suggest how to format or
1661 display neighboring characters.
1663 @cindex reserved code point
1664 @cindex code point, reserved
1666 If a code point is not a designated code point -- if it has not been
1667 assigned to a character by The Unicode Standard -- it is a 'reserved
1668 code point', meaning that they are reserved for future use. Most of
1669 the code points, about 800,000, are 'reserved code points'.
1671 By convention, a Unicode code point is written as
1672 ``U+XXXX'' where ``XXXX'' is a hexadecimal number. Please note that
1673 this convenient notation is not valid code. Guile does not interpret
1674 ``U+XXXX'' as a character.
1676 In Scheme, a character literal is written as @code{#\@var{name}} where
1677 @var{name} is the name of the character that you want. Printable
1678 characters have their usual single character name; for example,
1679 @code{#\a} is a lower case @code{a}.
1681 Some of the code points are 'combining characters' that are not meant
1682 to be printed by themselves but are instead meant to modify the
1683 appearance of the previous character. For combining characters, an
1684 alternate form of the character literal is @code{#\} followed by
1685 U+25CC (a small, dotted circle), followed by the combining character.
1686 This allows the combining character to be drawn on the circle, not on
1687 the backslash of @code{#\}.
1689 Many of the non-printing characters, such as whitespace characters and
1690 control characters, also have names.
1692 The most commonly used non-printing characters are space and
1693 newline. Their character names are @code{#\space} and
1694 @code{#\newline}. There are also names for all of the ``C0 control
1695 characters'' (those with code points below 32). The following table
1696 describes the names for each character.
1698 @multitable @columnfractions .25 .25 .25 .25
1699 @item 0 = @code{#\nul}
1700 @tab 1 = @code{#\soh}
1701 @tab 2 = @code{#\stx}
1702 @tab 3 = @code{#\etx}
1703 @item 4 = @code{#\eot}
1704 @tab 5 = @code{#\enq}
1705 @tab 6 = @code{#\ack}
1706 @tab 7 = @code{#\bel}
1707 @item 8 = @code{#\bs}
1708 @tab 9 = @code{#\ht}
1709 @tab 10 = @code{#\lf}
1710 @tab 11 = @code{#\vt}
1711 @item 12 = @code{#\ff}
1712 @tab 13 = @code{#\cr}
1713 @tab 14 = @code{#\so}
1714 @tab 15 = @code{#\si}
1715 @item 16 = @code{#\dle}
1716 @tab 17 = @code{#\dc1}
1717 @tab 18 = @code{#\dc2}
1718 @tab 19 = @code{#\dc3}
1719 @item 20 = @code{#\dc4}
1720 @tab 21 = @code{#\nak}
1721 @tab 22 = @code{#\syn}
1722 @tab 23 = @code{#\etb}
1723 @item 24 = @code{#\can}
1724 @tab 25 = @code{#\em}
1725 @tab 26 = @code{#\sub}
1726 @tab 27 = @code{#\esc}
1727 @item 28 = @code{#\fs}
1728 @tab 29 = @code{#\gs}
1729 @tab 30 = @code{#\rs}
1730 @tab 31 = @code{#\us}
1731 @item 32 = @code{#\sp}
1734 The ``delete'' character (code point U+007F) may be referred to with the
1737 One might note that the space character has two names --
1738 @code{#\space} and @code{#\sp} -- as does the newline character.
1739 Several other non-printing characters have more than one name, for the
1740 sake of compatibility with previous versions.
1742 @multitable {@code{#\backspace}} {Preferred}
1743 @item Alternate @tab Standard
1744 @item @code{#\sp} @tab @code{#\space}
1745 @item @code{#\nl} @tab @code{#\newline}
1746 @item @code{#\lf} @tab @code{#\newline}
1747 @item @code{#\tab} @tab @code{#\ht}
1748 @item @code{#\backspace} @tab @code{#\bs}
1749 @item @code{#\return} @tab @code{#\cr}
1750 @item @code{#\page} @tab @code{#\ff}
1751 @item @code{#\np} @tab @code{#\ff}
1752 @item @code{#\null} @tab @code{#\nul}
1755 Characters may also be written using their code point values. They can
1756 be written with as an octal number, such as @code{#\10} for
1757 @code{#\bs} or @code{#\177} for @code{#\del}.
1760 @deffn {Scheme Procedure} char? x
1761 @deffnx {C Function} scm_char_p (x)
1762 Return @code{#t} iff @var{x} is a character, else @code{#f}.
1765 Fundamentally, the character comparison operations below are
1766 numeric comparisons of the character's code points.
1769 @deffn {Scheme Procedure} char=? x y
1770 Return @code{#t} iff code point of @var{x} is equal to the code point
1771 of @var{y}, else @code{#f}.
1775 @deffn {Scheme Procedure} char<? x y
1776 Return @code{#t} iff the code point of @var{x} is less than the code
1777 point of @var{y}, else @code{#f}.
1781 @deffn {Scheme Procedure} char<=? x y
1782 Return @code{#t} iff the code point of @var{x} is less than or equal
1783 to the code point of @var{y}, else @code{#f}.
1787 @deffn {Scheme Procedure} char>? x y
1788 Return @code{#t} iff the code point of @var{x} is greater than the
1789 code point of @var{y}, else @code{#f}.
1793 @deffn {Scheme Procedure} char>=? x y
1794 Return @code{#t} iff the code point of @var{x} is greater than or
1795 equal to the code point of @var{y}, else @code{#f}.
1798 @cindex case folding
1800 Case-insensitive character comparisons use @emph{Unicode case
1801 folding}. In case folding comparisons, if a character is lowercase
1802 and has an uppercase form that can be expressed as a single character,
1803 it is converted to uppercase before comparison. All other characters
1804 undergo no conversion before the comparison occurs. This includes the
1805 German sharp S (Eszett) which is not uppercased before conversion
1806 because its uppercase form has two characters. Unicode case folding
1807 is language independent: it uses rules that are generally true, but,
1808 it cannot cover all cases for all languages.
1811 @deffn {Scheme Procedure} char-ci=? x y
1812 Return @code{#t} iff the case-folded code point of @var{x} is the same
1813 as the case-folded code point of @var{y}, else @code{#f}.
1817 @deffn {Scheme Procedure} char-ci<? x y
1818 Return @code{#t} iff the case-folded code point of @var{x} is less
1819 than the case-folded code point of @var{y}, else @code{#f}.
1823 @deffn {Scheme Procedure} char-ci<=? x y
1824 Return @code{#t} iff the case-folded code point of @var{x} is less
1825 than or equal to the case-folded code point of @var{y}, else
1830 @deffn {Scheme Procedure} char-ci>? x y
1831 Return @code{#t} iff the case-folded code point of @var{x} is greater
1832 than the case-folded code point of @var{y}, else @code{#f}.
1836 @deffn {Scheme Procedure} char-ci>=? x y
1837 Return @code{#t} iff the case-folded code point of @var{x} is greater
1838 than or equal to the case-folded code point of @var{y}, else
1842 @rnindex char-alphabetic?
1843 @deffn {Scheme Procedure} char-alphabetic? chr
1844 @deffnx {C Function} scm_char_alphabetic_p (chr)
1845 Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.
1848 @rnindex char-numeric?
1849 @deffn {Scheme Procedure} char-numeric? chr
1850 @deffnx {C Function} scm_char_numeric_p (chr)
1851 Return @code{#t} iff @var{chr} is numeric, else @code{#f}.
1854 @rnindex char-whitespace?
1855 @deffn {Scheme Procedure} char-whitespace? chr
1856 @deffnx {C Function} scm_char_whitespace_p (chr)
1857 Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.
1860 @rnindex char-upper-case?
1861 @deffn {Scheme Procedure} char-upper-case? chr
1862 @deffnx {C Function} scm_char_upper_case_p (chr)
1863 Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.
1866 @rnindex char-lower-case?
1867 @deffn {Scheme Procedure} char-lower-case? chr
1868 @deffnx {C Function} scm_char_lower_case_p (chr)
1869 Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.
1872 @deffn {Scheme Procedure} char-is-both? chr
1873 @deffnx {C Function} scm_char_is_both_p (chr)
1874 Return @code{#t} iff @var{chr} is either uppercase or lowercase, else
1878 @rnindex char->integer
1879 @deffn {Scheme Procedure} char->integer chr
1880 @deffnx {C Function} scm_char_to_integer (chr)
1881 Return the code point of @var{chr}.
1884 @rnindex integer->char
1885 @deffn {Scheme Procedure} integer->char n
1886 @deffnx {C Function} scm_integer_to_char (n)
1887 Return the character that has code point @var{n}. The integer @var{n}
1888 must be a valid code point. Valid code points are in the ranges 0 to
1889 @code{#xD7FF} inclusive or @code{#xE000} to @code{#x10FFFF} inclusive.
1892 @rnindex char-upcase
1893 @deffn {Scheme Procedure} char-upcase chr
1894 @deffnx {C Function} scm_char_upcase (chr)
1895 Return the uppercase character version of @var{chr}.
1898 @rnindex char-downcase
1899 @deffn {Scheme Procedure} char-downcase chr
1900 @deffnx {C Function} scm_char_downcase (chr)
1901 Return the lowercase character version of @var{chr}.
1904 @node Character Sets
1905 @subsection Character Sets
1907 The features described in this section correspond directly to SRFI-14.
1909 The data type @dfn{charset} implements sets of characters
1910 (@pxref{Characters}). Because the internal representation of
1911 character sets is not visible to the user, a lot of procedures for
1912 handling them are provided.
1914 Character sets can be created, extended, tested for the membership of a
1915 characters and be compared to other character sets.
1918 * Character Set Predicates/Comparison::
1919 * Iterating Over Character Sets:: Enumerate charset elements.
1920 * Creating Character Sets:: Making new charsets.
1921 * Querying Character Sets:: Test charsets for membership etc.
1922 * Character-Set Algebra:: Calculating new charsets.
1923 * Standard Character Sets:: Variables containing predefined charsets.
1926 @node Character Set Predicates/Comparison
1927 @subsubsection Character Set Predicates/Comparison
1929 Use these procedures for testing whether an object is a character set,
1930 or whether several character sets are equal or subsets of each other.
1931 @code{char-set-hash} can be used for calculating a hash value, maybe for
1932 usage in fast lookup procedures.
1934 @deffn {Scheme Procedure} char-set? obj
1935 @deffnx {C Function} scm_char_set_p (obj)
1936 Return @code{#t} if @var{obj} is a character set, @code{#f}
1940 @deffn {Scheme Procedure} char-set= . char_sets
1941 @deffnx {C Function} scm_char_set_eq (char_sets)
1942 Return @code{#t} if all given character sets are equal.
1945 @deffn {Scheme Procedure} char-set<= . char_sets
1946 @deffnx {C Function} scm_char_set_leq (char_sets)
1947 Return @code{#t} if every character set @var{cs}i is a subset
1948 of character set @var{cs}i+1.
1951 @deffn {Scheme Procedure} char-set-hash cs [bound]
1952 @deffnx {C Function} scm_char_set_hash (cs, bound)
1953 Compute a hash value for the character set @var{cs}. If
1954 @var{bound} is given and non-zero, it restricts the
1955 returned value to the range 0 @dots{} @var{bound - 1}.
1958 @c ===================================================================
1960 @node Iterating Over Character Sets
1961 @subsubsection Iterating Over Character Sets
1963 Character set cursors are a means for iterating over the members of a
1964 character sets. After creating a character set cursor with
1965 @code{char-set-cursor}, a cursor can be dereferenced with
1966 @code{char-set-ref}, advanced to the next member with
1967 @code{char-set-cursor-next}. Whether a cursor has passed past the last
1968 element of the set can be checked with @code{end-of-char-set?}.
1970 Additionally, mapping and (un-)folding procedures for character sets are
1973 @deffn {Scheme Procedure} char-set-cursor cs
1974 @deffnx {C Function} scm_char_set_cursor (cs)
1975 Return a cursor into the character set @var{cs}.
1978 @deffn {Scheme Procedure} char-set-ref cs cursor
1979 @deffnx {C Function} scm_char_set_ref (cs, cursor)
1980 Return the character at the current cursor position
1981 @var{cursor} in the character set @var{cs}. It is an error to
1982 pass a cursor for which @code{end-of-char-set?} returns true.
1985 @deffn {Scheme Procedure} char-set-cursor-next cs cursor
1986 @deffnx {C Function} scm_char_set_cursor_next (cs, cursor)
1987 Advance the character set cursor @var{cursor} to the next
1988 character in the character set @var{cs}. It is an error if the
1989 cursor given satisfies @code{end-of-char-set?}.
1992 @deffn {Scheme Procedure} end-of-char-set? cursor
1993 @deffnx {C Function} scm_end_of_char_set_p (cursor)
1994 Return @code{#t} if @var{cursor} has reached the end of a
1995 character set, @code{#f} otherwise.
1998 @deffn {Scheme Procedure} char-set-fold kons knil cs
1999 @deffnx {C Function} scm_char_set_fold (kons, knil, cs)
2000 Fold the procedure @var{kons} over the character set @var{cs},
2001 initializing it with @var{knil}.
2004 @deffn {Scheme Procedure} char-set-unfold p f g seed [base_cs]
2005 @deffnx {C Function} scm_char_set_unfold (p, f, g, seed, base_cs)
2006 This is a fundamental constructor for character sets.
2008 @item @var{g} is used to generate a series of ``seed'' values
2009 from the initial seed: @var{seed}, (@var{g} @var{seed}),
2010 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
2011 @item @var{p} tells us when to stop -- when it returns true
2012 when applied to one of the seed values.
2013 @item @var{f} maps each seed value to a character. These
2014 characters are added to the base character set @var{base_cs} to
2015 form the result; @var{base_cs} defaults to the empty set.
2019 @deffn {Scheme Procedure} char-set-unfold! p f g seed base_cs
2020 @deffnx {C Function} scm_char_set_unfold_x (p, f, g, seed, base_cs)
2021 This is a fundamental constructor for character sets.
2023 @item @var{g} is used to generate a series of ``seed'' values
2024 from the initial seed: @var{seed}, (@var{g} @var{seed}),
2025 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
2026 @item @var{p} tells us when to stop -- when it returns true
2027 when applied to one of the seed values.
2028 @item @var{f} maps each seed value to a character. These
2029 characters are added to the base character set @var{base_cs} to
2030 form the result; @var{base_cs} defaults to the empty set.
2034 @deffn {Scheme Procedure} char-set-for-each proc cs
2035 @deffnx {C Function} scm_char_set_for_each (proc, cs)
2036 Apply @var{proc} to every character in the character set
2037 @var{cs}. The return value is not specified.
2040 @deffn {Scheme Procedure} char-set-map proc cs
2041 @deffnx {C Function} scm_char_set_map (proc, cs)
2042 Map the procedure @var{proc} over every character in @var{cs}.
2043 @var{proc} must be a character -> character procedure.
2046 @c ===================================================================
2048 @node Creating Character Sets
2049 @subsubsection Creating Character Sets
2051 New character sets are produced with these procedures.
2053 @deffn {Scheme Procedure} char-set-copy cs
2054 @deffnx {C Function} scm_char_set_copy (cs)
2055 Return a newly allocated character set containing all
2056 characters in @var{cs}.
2059 @deffn {Scheme Procedure} char-set . rest
2060 @deffnx {C Function} scm_char_set (rest)
2061 Return a character set containing all given characters.
2064 @deffn {Scheme Procedure} list->char-set list [base_cs]
2065 @deffnx {C Function} scm_list_to_char_set (list, base_cs)
2066 Convert the character list @var{list} to a character set. If
2067 the character set @var{base_cs} is given, the character in this
2068 set are also included in the result.
2071 @deffn {Scheme Procedure} list->char-set! list base_cs
2072 @deffnx {C Function} scm_list_to_char_set_x (list, base_cs)
2073 Convert the character list @var{list} to a character set. The
2074 characters are added to @var{base_cs} and @var{base_cs} is
2078 @deffn {Scheme Procedure} string->char-set str [base_cs]
2079 @deffnx {C Function} scm_string_to_char_set (str, base_cs)
2080 Convert the string @var{str} to a character set. If the
2081 character set @var{base_cs} is given, the characters in this
2082 set are also included in the result.
2085 @deffn {Scheme Procedure} string->char-set! str base_cs
2086 @deffnx {C Function} scm_string_to_char_set_x (str, base_cs)
2087 Convert the string @var{str} to a character set. The
2088 characters from the string are added to @var{base_cs}, and
2089 @var{base_cs} is returned.
2092 @deffn {Scheme Procedure} char-set-filter pred cs [base_cs]
2093 @deffnx {C Function} scm_char_set_filter (pred, cs, base_cs)
2094 Return a character set containing every character from @var{cs}
2095 so that it satisfies @var{pred}. If provided, the characters
2096 from @var{base_cs} are added to the result.
2099 @deffn {Scheme Procedure} char-set-filter! pred cs base_cs
2100 @deffnx {C Function} scm_char_set_filter_x (pred, cs, base_cs)
2101 Return a character set containing every character from @var{cs}
2102 so that it satisfies @var{pred}. The characters are added to
2103 @var{base_cs} and @var{base_cs} is returned.
2106 @deffn {Scheme Procedure} ucs-range->char-set lower upper [error [base_cs]]
2107 @deffnx {C Function} scm_ucs_range_to_char_set (lower, upper, error, base_cs)
2108 Return a character set containing all characters whose
2109 character codes lie in the half-open range
2110 [@var{lower},@var{upper}).
2112 If @var{error} is a true value, an error is signalled if the
2113 specified range contains characters which are not contained in
2114 the implemented character range. If @var{error} is @code{#f},
2115 these characters are silently left out of the resulting
2118 The characters in @var{base_cs} are added to the result, if
2122 @deffn {Scheme Procedure} ucs-range->char-set! lower upper error base_cs
2123 @deffnx {C Function} scm_ucs_range_to_char_set_x (lower, upper, error, base_cs)
2124 Return a character set containing all characters whose
2125 character codes lie in the half-open range
2126 [@var{lower},@var{upper}).
2128 If @var{error} is a true value, an error is signalled if the
2129 specified range contains characters which are not contained in
2130 the implemented character range. If @var{error} is @code{#f},
2131 these characters are silently left out of the resulting
2134 The characters are added to @var{base_cs} and @var{base_cs} is
2138 @deffn {Scheme Procedure} ->char-set x
2139 @deffnx {C Function} scm_to_char_set (x)
2140 Coerces x into a char-set. @var{x} may be a string, character or
2141 char-set. A string is converted to the set of its constituent
2142 characters; a character is converted to a singleton set; a char-set is
2146 @c ===================================================================
2148 @node Querying Character Sets
2149 @subsubsection Querying Character Sets
2151 Access the elements and other information of a character set with these
2154 @deffn {Scheme Procedure} %char-set-dump cs
2155 Returns an association list containing debugging information
2156 for @var{cs}. The association list has the following entries.
2161 The number of groups of contiguous code points the char-set
2164 A list of lists where each sublist is a range of code points
2165 and their associated characters
2167 The return value of this function cannot be relied upon to be
2168 consistent between versions of Guile and should not be used in code.
2171 @deffn {Scheme Procedure} char-set-size cs
2172 @deffnx {C Function} scm_char_set_size (cs)
2173 Return the number of elements in character set @var{cs}.
2176 @deffn {Scheme Procedure} char-set-count pred cs
2177 @deffnx {C Function} scm_char_set_count (pred, cs)
2178 Return the number of the elements int the character set
2179 @var{cs} which satisfy the predicate @var{pred}.
2182 @deffn {Scheme Procedure} char-set->list cs
2183 @deffnx {C Function} scm_char_set_to_list (cs)
2184 Return a list containing the elements of the character set
2188 @deffn {Scheme Procedure} char-set->string cs
2189 @deffnx {C Function} scm_char_set_to_string (cs)
2190 Return a string containing the elements of the character set
2191 @var{cs}. The order in which the characters are placed in the
2192 string is not defined.
2195 @deffn {Scheme Procedure} char-set-contains? cs ch
2196 @deffnx {C Function} scm_char_set_contains_p (cs, ch)
2197 Return @code{#t} iff the character @var{ch} is contained in the
2198 character set @var{cs}.
2201 @deffn {Scheme Procedure} char-set-every pred cs
2202 @deffnx {C Function} scm_char_set_every (pred, cs)
2203 Return a true value if every character in the character set
2204 @var{cs} satisfies the predicate @var{pred}.
2207 @deffn {Scheme Procedure} char-set-any pred cs
2208 @deffnx {C Function} scm_char_set_any (pred, cs)
2209 Return a true value if any character in the character set
2210 @var{cs} satisfies the predicate @var{pred}.
2213 @c ===================================================================
2215 @node Character-Set Algebra
2216 @subsubsection Character-Set Algebra
2218 Character sets can be manipulated with the common set algebra operation,
2219 such as union, complement, intersection etc. All of these procedures
2220 provide side-effecting variants, which modify their character set
2223 @deffn {Scheme Procedure} char-set-adjoin cs . rest
2224 @deffnx {C Function} scm_char_set_adjoin (cs, rest)
2225 Add all character arguments to the first argument, which must
2229 @deffn {Scheme Procedure} char-set-delete cs . rest
2230 @deffnx {C Function} scm_char_set_delete (cs, rest)
2231 Delete all character arguments from the first argument, which
2232 must be a character set.
2235 @deffn {Scheme Procedure} char-set-adjoin! cs . rest
2236 @deffnx {C Function} scm_char_set_adjoin_x (cs, rest)
2237 Add all character arguments to the first argument, which must
2241 @deffn {Scheme Procedure} char-set-delete! cs . rest
2242 @deffnx {C Function} scm_char_set_delete_x (cs, rest)
2243 Delete all character arguments from the first argument, which
2244 must be a character set.
2247 @deffn {Scheme Procedure} char-set-complement cs
2248 @deffnx {C Function} scm_char_set_complement (cs)
2249 Return the complement of the character set @var{cs}.
2252 Note that the complement of a character set is likely to contain many
2253 reserved code points (code points that are not associated with
2254 characters). It may be helpful to modify the output of
2255 @code{char-set-complement} by computing its intersection with the set
2256 of designated code points, @code{char-set:designated}.
2258 @deffn {Scheme Procedure} char-set-union . rest
2259 @deffnx {C Function} scm_char_set_union (rest)
2260 Return the union of all argument character sets.
2263 @deffn {Scheme Procedure} char-set-intersection . rest
2264 @deffnx {C Function} scm_char_set_intersection (rest)
2265 Return the intersection of all argument character sets.
2268 @deffn {Scheme Procedure} char-set-difference cs1 . rest
2269 @deffnx {C Function} scm_char_set_difference (cs1, rest)
2270 Return the difference of all argument character sets.
2273 @deffn {Scheme Procedure} char-set-xor . rest
2274 @deffnx {C Function} scm_char_set_xor (rest)
2275 Return the exclusive-or of all argument character sets.
2278 @deffn {Scheme Procedure} char-set-diff+intersection cs1 . rest
2279 @deffnx {C Function} scm_char_set_diff_plus_intersection (cs1, rest)
2280 Return the difference and the intersection of all argument
2284 @deffn {Scheme Procedure} char-set-complement! cs
2285 @deffnx {C Function} scm_char_set_complement_x (cs)
2286 Return the complement of the character set @var{cs}.
2289 @deffn {Scheme Procedure} char-set-union! cs1 . rest
2290 @deffnx {C Function} scm_char_set_union_x (cs1, rest)
2291 Return the union of all argument character sets.
2294 @deffn {Scheme Procedure} char-set-intersection! cs1 . rest
2295 @deffnx {C Function} scm_char_set_intersection_x (cs1, rest)
2296 Return the intersection of all argument character sets.
2299 @deffn {Scheme Procedure} char-set-difference! cs1 . rest
2300 @deffnx {C Function} scm_char_set_difference_x (cs1, rest)
2301 Return the difference of all argument character sets.
2304 @deffn {Scheme Procedure} char-set-xor! cs1 . rest
2305 @deffnx {C Function} scm_char_set_xor_x (cs1, rest)
2306 Return the exclusive-or of all argument character sets.
2309 @deffn {Scheme Procedure} char-set-diff+intersection! cs1 cs2 . rest
2310 @deffnx {C Function} scm_char_set_diff_plus_intersection_x (cs1, cs2, rest)
2311 Return the difference and the intersection of all argument
2315 @c ===================================================================
2317 @node Standard Character Sets
2318 @subsubsection Standard Character Sets
2320 In order to make the use of the character set data type and procedures
2321 useful, several predefined character set variables exist.
2327 These character sets are locale independent and are not recomputed
2328 upon a @code{setlocale} call. They contain characters from the whole
2329 range of Unicode code points. For instance, @code{char-set:letter}
2330 contains about 94,000 characters.
2332 @defvr {Scheme Variable} char-set:lower-case
2333 @defvrx {C Variable} scm_char_set_lower_case
2334 All lower-case characters.
2337 @defvr {Scheme Variable} char-set:upper-case
2338 @defvrx {C Variable} scm_char_set_upper_case
2339 All upper-case characters.
2342 @defvr {Scheme Variable} char-set:title-case
2343 @defvrx {C Variable} scm_char_set_title_case
2344 All single characters that function as if they were an upper-case
2345 letter followed by a lower-case letter.
2348 @defvr {Scheme Variable} char-set:letter
2349 @defvrx {C Variable} scm_char_set_letter
2350 All letters. This includes @code{char-set:lower-case},
2351 @code{char-set:upper-case}, @code{char-set:title-case}, and many
2352 letters that have no case at all. For example, Chinese and Japanese
2353 characters typically have no concept of case.
2356 @defvr {Scheme Variable} char-set:digit
2357 @defvrx {C Variable} scm_char_set_digit
2361 @defvr {Scheme Variable} char-set:letter+digit
2362 @defvrx {C Variable} scm_char_set_letter_and_digit
2363 The union of @code{char-set:letter} and @code{char-set:digit}.
2366 @defvr {Scheme Variable} char-set:graphic
2367 @defvrx {C Variable} scm_char_set_graphic
2368 All characters which would put ink on the paper.
2371 @defvr {Scheme Variable} char-set:printing
2372 @defvrx {C Variable} scm_char_set_printing
2373 The union of @code{char-set:graphic} and @code{char-set:whitespace}.
2376 @defvr {Scheme Variable} char-set:whitespace
2377 @defvrx {C Variable} scm_char_set_whitespace
2378 All whitespace characters.
2381 @defvr {Scheme Variable} char-set:blank
2382 @defvrx {C Variable} scm_char_set_blank
2383 All horizontal whitespace characters, which notably includes
2384 @code{#\space} and @code{#\tab}.
2387 @defvr {Scheme Variable} char-set:iso-control
2388 @defvrx {C Variable} scm_char_set_iso_control
2389 The ISO control characters are the C0 control characters (U+0000 to
2390 U+001F), delete (U+007F), and the C1 control characters (U+0080 to
2394 @defvr {Scheme Variable} char-set:punctuation
2395 @defvrx {C Variable} scm_char_set_punctuation
2396 All punctuation characters, such as the characters
2397 @code{!"#%&'()*,-./:;?@@[\\]_@{@}}
2400 @defvr {Scheme Variable} char-set:symbol
2401 @defvrx {C Variable} scm_char_set_symbol
2402 All symbol characters, such as the characters @code{$+<=>^`|~}.
2405 @defvr {Scheme Variable} char-set:hex-digit
2406 @defvrx {C Variable} scm_char_set_hex_digit
2407 The hexadecimal digits @code{0123456789abcdefABCDEF}.
2410 @defvr {Scheme Variable} char-set:ascii
2411 @defvrx {C Variable} scm_char_set_ascii
2412 All ASCII characters.
2415 @defvr {Scheme Variable} char-set:empty
2416 @defvrx {C Variable} scm_char_set_empty
2417 The empty character set.
2420 @defvr {Scheme Variable} char-set:designated
2421 @defvrx {C Variable} scm_char_set_designated
2422 This character set contains all designated code points. This includes
2423 all the code points to which Unicode has assigned a character or other
2427 @defvr {Scheme Variable} char-set:full
2428 @defvrx {C Variable} scm_char_set_full
2429 This character set contains all possible code points. This includes
2430 both designated and reserved code points.
2437 Strings are fixed-length sequences of characters. They can be created
2438 by calling constructor procedures, but they can also literally get
2439 entered at the @acronym{REPL} or in Scheme source files.
2441 @c Guile provides a rich set of string processing procedures, because text
2442 @c handling is very important when Guile is used as a scripting language.
2444 Strings always carry the information about how many characters they are
2445 composed of with them, so there is no special end-of-string character,
2446 like in C. That means that Scheme strings can contain any character,
2447 even the @samp{#\nul} character @samp{\0}.
2449 To use strings efficiently, you need to know a bit about how Guile
2450 implements them. In Guile, a string consists of two parts, a head and
2451 the actual memory where the characters are stored. When a string (or
2452 a substring of it) is copied, only a new head gets created, the memory
2453 is usually not copied. The two heads start out pointing to the same
2456 When one of these two strings is modified, as with @code{string-set!},
2457 their common memory does get copied so that each string has its own
2458 memory and modifying one does not accidentally modify the other as well.
2459 Thus, Guile's strings are `copy on write'; the actual copying of their
2460 memory is delayed until one string is written to.
2462 This implementation makes functions like @code{substring} very
2463 efficient in the common case that no modifications are done to the
2466 If you do know that your strings are getting modified right away, you
2467 can use @code{substring/copy} instead of @code{substring}. This
2468 function performs the copy immediately at the time of creation. This
2469 is more efficient, especially in a multi-threaded program. Also,
2470 @code{substring/copy} can avoid the problem that a short substring
2471 holds on to the memory of a very large original string that could
2472 otherwise be recycled.
2474 If you want to avoid the copy altogether, so that modifications of one
2475 string show up in the other, you can use @code{substring/shared}. The
2476 strings created by this procedure are called @dfn{mutation sharing
2477 substrings} since the substring and the original string share
2478 modifications to each other.
2480 If you want to prevent modifications, use @code{substring/read-only}.
2482 Guile provides all procedures of SRFI-13 and a few more.
2485 * String Syntax:: Read syntax for strings.
2486 * String Predicates:: Testing strings for certain properties.
2487 * String Constructors:: Creating new string objects.
2488 * List/String Conversion:: Converting from/to lists of characters.
2489 * String Selection:: Select portions from strings.
2490 * String Modification:: Modify parts or whole strings.
2491 * String Comparison:: Lexicographic ordering predicates.
2492 * String Searching:: Searching in strings.
2493 * Alphabetic Case Mapping:: Convert the alphabetic case of strings.
2494 * Reversing and Appending Strings:: Appending strings to form a new string.
2495 * Mapping Folding and Unfolding:: Iterating over strings.
2496 * Miscellaneous String Operations:: Replicating, insertion, parsing, ...
2497 * Conversion to/from C::
2501 @subsubsection String Read Syntax
2503 @c In the following @code is used to get a good font in TeX etc, but
2504 @c is omitted for Info format, so as not to risk any confusion over
2505 @c whether surrounding ` ' quotes are part of the escape or are
2506 @c special in a string (they're not).
2508 The read syntax for strings is an arbitrarily long sequence of
2509 characters enclosed in double quotes (@nicode{"}).
2511 Backslash is an escape character and can be used to insert the
2512 following special characters. @nicode{\"} and @nicode{\\} are R5RS
2513 standard, the rest are Guile extensions, notice they follow C string
2518 Backslash character.
2521 Double quote character (an unescaped @nicode{"} is otherwise the end
2525 NUL character (ASCII 0).
2528 Bell character (ASCII 7).
2531 Formfeed character (ASCII 12).
2534 Newline character (ASCII 10).
2537 Carriage return character (ASCII 13).
2540 Tab character (ASCII 9).
2543 Vertical tab character (ASCII 11).
2546 Character code given by two hexadecimal digits. For example
2547 @nicode{\x7f} for an ASCII DEL (127).
2549 @item @nicode{\uHHHH}
2550 Character code given by four hexadecimal digits. For example
2551 @nicode{\u0100} for a capital A with macron (U+0100).
2553 @item @nicode{\UHHHHHH}
2554 Character code given by six hexadecimal digits. For example
2559 The following are examples of string literals:
2569 @node String Predicates
2570 @subsubsection String Predicates
2572 The following procedures can be used to check whether a given string
2573 fulfills some specified property.
2576 @deffn {Scheme Procedure} string? obj
2577 @deffnx {C Function} scm_string_p (obj)
2578 Return @code{#t} if @var{obj} is a string, else @code{#f}.
2581 @deftypefn {C Function} int scm_is_string (SCM obj)
2582 Returns @code{1} if @var{obj} is a string, @code{0} otherwise.
2585 @deffn {Scheme Procedure} string-null? str
2586 @deffnx {C Function} scm_string_null_p (str)
2587 Return @code{#t} if @var{str}'s length is zero, and
2588 @code{#f} otherwise.
2590 (string-null? "") @result{} #t
2592 (string-null? y) @result{} #f
2596 @deffn {Scheme Procedure} string-any char_pred s [start [end]]
2597 @deffnx {C Function} scm_string_any (char_pred, s, start, end)
2598 Check if @var{char_pred} is true for any character in string @var{s}.
2600 @var{char_pred} can be a character to check for any equal to that, or
2601 a character set (@pxref{Character Sets}) to check for any in that set,
2602 or a predicate procedure to call.
2604 For a procedure, calls @code{(@var{char_pred} c)} are made
2605 successively on the characters from @var{start} to @var{end}. If
2606 @var{char_pred} returns true (ie.@: non-@code{#f}), @code{string-any}
2607 stops and that return value is the return from @code{string-any}. The
2608 call on the last character (ie.@: at @math{@var{end}-1}), if that
2609 point is reached, is a tail call.
2611 If there are no characters in @var{s} (ie.@: @var{start} equals
2612 @var{end}) then the return is @code{#f}.
2615 @deffn {Scheme Procedure} string-every char_pred s [start [end]]
2616 @deffnx {C Function} scm_string_every (char_pred, s, start, end)
2617 Check if @var{char_pred} is true for every character in string
2620 @var{char_pred} can be a character to check for every character equal
2621 to that, or a character set (@pxref{Character Sets}) to check for
2622 every character being in that set, or a predicate procedure to call.
2624 For a procedure, calls @code{(@var{char_pred} c)} are made
2625 successively on the characters from @var{start} to @var{end}. If
2626 @var{char_pred} returns @code{#f}, @code{string-every} stops and
2627 returns @code{#f}. The call on the last character (ie.@: at
2628 @math{@var{end}-1}), if that point is reached, is a tail call and the
2629 return from that call is the return from @code{string-every}.
2631 If there are no characters in @var{s} (ie.@: @var{start} equals
2632 @var{end}) then the return is @code{#t}.
2635 @node String Constructors
2636 @subsubsection String Constructors
2638 The string constructor procedures create new string objects, possibly
2639 initializing them with some specified character data. See also
2640 @xref{String Selection}, for ways to create strings from existing
2643 @c FIXME::martin: list->string belongs into `List/String Conversion'
2645 @deffn {Scheme Procedure} string char@dots{}
2647 Return a newly allocated string made from the given character
2651 (string #\x #\y #\z) @result{} "xyz"
2652 (string) @result{} ""
2656 @deffn {Scheme Procedure} list->string lst
2657 @deffnx {C Function} scm_string (lst)
2658 @rnindex list->string
2659 Return a newly allocated string made from a list of characters.
2662 (list->string '(#\a #\b #\c)) @result{} "abc"
2666 @deffn {Scheme Procedure} reverse-list->string lst
2667 @deffnx {C Function} scm_reverse_list_to_string (lst)
2668 Return a newly allocated string made from a list of characters, in
2672 (reverse-list->string '(#\a #\B #\c)) @result{} "cBa"
2676 @rnindex make-string
2677 @deffn {Scheme Procedure} make-string k [chr]
2678 @deffnx {C Function} scm_make_string (k, chr)
2679 Return a newly allocated string of
2680 length @var{k}. If @var{chr} is given, then all elements of
2681 the string are initialized to @var{chr}, otherwise the contents
2682 of the @var{string} are unspecified.
2685 @deftypefn {C Function} SCM scm_c_make_string (size_t len, SCM chr)
2686 Like @code{scm_make_string}, but expects the length as a
2690 @deffn {Scheme Procedure} string-tabulate proc len
2691 @deffnx {C Function} scm_string_tabulate (proc, len)
2692 @var{proc} is an integer->char procedure. Construct a string
2693 of size @var{len} by applying @var{proc} to each index to
2694 produce the corresponding string element. The order in which
2695 @var{proc} is applied to the indices is not specified.
2698 @deffn {Scheme Procedure} string-join ls [delimiter [grammar]]
2699 @deffnx {C Function} scm_string_join (ls, delimiter, grammar)
2700 Append the string in the string list @var{ls}, using the string
2701 @var{delim} as a delimiter between the elements of @var{ls}.
2702 @var{grammar} is a symbol which specifies how the delimiter is
2703 placed between the strings, and defaults to the symbol
2708 Insert the separator between list elements. An empty string
2709 will produce an empty list.
2711 Like @code{infix}, but will raise an error if given the empty
2714 Insert the separator after every list element.
2716 Insert the separator before each list element.
2720 @node List/String Conversion
2721 @subsubsection List/String conversion
2723 When processing strings, it is often convenient to first convert them
2724 into a list representation by using the procedure @code{string->list},
2725 work with the resulting list, and then convert it back into a string.
2726 These procedures are useful for similar tasks.
2728 @rnindex string->list
2729 @deffn {Scheme Procedure} string->list str [start [end]]
2730 @deffnx {C Function} scm_substring_to_list (str, start, end)
2731 @deffnx {C Function} scm_string_to_list (str)
2732 Convert the string @var{str} into a list of characters.
2735 @deffn {Scheme Procedure} string-split str chr
2736 @deffnx {C Function} scm_string_split (str, chr)
2737 Split the string @var{str} into the a list of the substrings delimited
2738 by appearances of the character @var{chr}. Note that an empty substring
2739 between separator characters will result in an empty string in the
2743 (string-split "root:x:0:0:root:/root:/bin/bash" #\:)
2745 ("root" "x" "0" "0" "root" "/root" "/bin/bash")
2747 (string-split "::" #\:)
2751 (string-split "" #\:)
2758 @node String Selection
2759 @subsubsection String Selection
2761 Portions of strings can be extracted by these procedures.
2762 @code{string-ref} delivers individual characters whereas
2763 @code{substring} can be used to extract substrings from longer strings.
2765 @rnindex string-length
2766 @deffn {Scheme Procedure} string-length string
2767 @deffnx {C Function} scm_string_length (string)
2768 Return the number of characters in @var{string}.
2771 @deftypefn {C Function} size_t scm_c_string_length (SCM str)
2772 Return the number of characters in @var{str} as a @code{size_t}.
2776 @deffn {Scheme Procedure} string-ref str k
2777 @deffnx {C Function} scm_string_ref (str, k)
2778 Return character @var{k} of @var{str} using zero-origin
2779 indexing. @var{k} must be a valid index of @var{str}.
2782 @deftypefn {C Function} SCM scm_c_string_ref (SCM str, size_t k)
2783 Return character @var{k} of @var{str} using zero-origin
2784 indexing. @var{k} must be a valid index of @var{str}.
2787 @rnindex string-copy
2788 @deffn {Scheme Procedure} string-copy str [start [end]]
2789 @deffnx {C Function} scm_substring_copy (str, start, end)
2790 @deffnx {C Function} scm_string_copy (str)
2791 Return a copy of the given string @var{str}.
2793 The returned string shares storage with @var{str} initially, but it is
2794 copied as soon as one of the two strings is modified.
2798 @deffn {Scheme Procedure} substring str start [end]
2799 @deffnx {C Function} scm_substring (str, start, end)
2800 Return a new string formed from the characters
2801 of @var{str} beginning with index @var{start} (inclusive) and
2802 ending with index @var{end} (exclusive).
2803 @var{str} must be a string, @var{start} and @var{end} must be
2804 exact integers satisfying:
2806 0 <= @var{start} <= @var{end} <= @code{(string-length @var{str})}.
2808 The returned string shares storage with @var{str} initially, but it is
2809 copied as soon as one of the two strings is modified.
2812 @deffn {Scheme Procedure} substring/shared str start [end]
2813 @deffnx {C Function} scm_substring_shared (str, start, end)
2814 Like @code{substring}, but the strings continue to share their storage
2815 even if they are modified. Thus, modifications to @var{str} show up
2816 in the new string, and vice versa.
2819 @deffn {Scheme Procedure} substring/copy str start [end]
2820 @deffnx {C Function} scm_substring_copy (str, start, end)
2821 Like @code{substring}, but the storage for the new string is copied
2825 @deffn {Scheme Procedure} substring/read-only str start [end]
2826 @deffnx {C Function} scm_substring_read_only (str, start, end)
2827 Like @code{substring}, but the resulting string can not be modified.
2830 @deftypefn {C Function} SCM scm_c_substring (SCM str, size_t start, size_t end)
2831 @deftypefnx {C Function} SCM scm_c_substring_shared (SCM str, size_t start, size_t end)
2832 @deftypefnx {C Function} SCM scm_c_substring_copy (SCM str, size_t start, size_t end)
2833 @deftypefnx {C Function} SCM scm_c_substring_read_only (SCM str, size_t start, size_t end)
2834 Like @code{scm_substring}, etc. but the bounds are given as a @code{size_t}.
2837 @deffn {Scheme Procedure} string-take s n
2838 @deffnx {C Function} scm_string_take (s, n)
2839 Return the @var{n} first characters of @var{s}.
2842 @deffn {Scheme Procedure} string-drop s n
2843 @deffnx {C Function} scm_string_drop (s, n)
2844 Return all but the first @var{n} characters of @var{s}.
2847 @deffn {Scheme Procedure} string-take-right s n
2848 @deffnx {C Function} scm_string_take_right (s, n)
2849 Return the @var{n} last characters of @var{s}.
2852 @deffn {Scheme Procedure} string-drop-right s n
2853 @deffnx {C Function} scm_string_drop_right (s, n)
2854 Return all but the last @var{n} characters of @var{s}.
2857 @deffn {Scheme Procedure} string-pad s len [chr [start [end]]]
2858 @deffnx {Scheme Procedure} string-pad-right s len [chr [start [end]]]
2859 @deffnx {C Function} scm_string_pad (s, len, chr, start, end)
2860 @deffnx {C Function} scm_string_pad_right (s, len, chr, start, end)
2861 Take characters @var{start} to @var{end} from the string @var{s} and
2862 either pad with @var{char} or truncate them to give @var{len}
2865 @code{string-pad} pads or truncates on the left, so for example
2868 (string-pad "x" 3) @result{} " x"
2869 (string-pad "abcde" 3) @result{} "cde"
2872 @code{string-pad-right} pads or truncates on the right, so for example
2875 (string-pad-right "x" 3) @result{} "x "
2876 (string-pad-right "abcde" 3) @result{} "abc"
2880 @deffn {Scheme Procedure} string-trim s [char_pred [start [end]]]
2881 @deffnx {Scheme Procedure} string-trim-right s [char_pred [start [end]]]
2882 @deffnx {Scheme Procedure} string-trim-both s [char_pred [start [end]]]
2883 @deffnx {C Function} scm_string_trim (s, char_pred, start, end)
2884 @deffnx {C Function} scm_string_trim_right (s, char_pred, start, end)
2885 @deffnx {C Function} scm_string_trim_both (s, char_pred, start, end)
2886 Trim occurrences of @var{char_pred} from the ends of @var{s}.
2888 @code{string-trim} trims @var{char_pred} characters from the left
2889 (start) of the string, @code{string-trim-right} trims them from the
2890 right (end) of the string, @code{string-trim-both} trims from both
2893 @var{char_pred} can be a character, a character set, or a predicate
2894 procedure to call on each character. If @var{char_pred} is not given
2895 the default is whitespace as per @code{char-set:whitespace}
2896 (@pxref{Standard Character Sets}).
2899 (string-trim " x ") @result{} "x "
2900 (string-trim-right "banana" #\a) @result{} "banan"
2901 (string-trim-both ".,xy:;" char-set:punctuation)
2903 (string-trim-both "xyzzy" (lambda (c)
2910 @node String Modification
2911 @subsubsection String Modification
2913 These procedures are for modifying strings in-place. This means that the
2914 result of the operation is not a new string; instead, the original string's
2915 memory representation is modified.
2917 @rnindex string-set!
2918 @deffn {Scheme Procedure} string-set! str k chr
2919 @deffnx {C Function} scm_string_set_x (str, k, chr)
2920 Store @var{chr} in element @var{k} of @var{str} and return
2921 an unspecified value. @var{k} must be a valid index of
2925 @deftypefn {C Function} void scm_c_string_set_x (SCM str, size_t k, SCM chr)
2926 Like @code{scm_string_set_x}, but the index is given as a @code{size_t}.
2929 @rnindex string-fill!
2930 @deffn {Scheme Procedure} string-fill! str chr [start [end]]
2931 @deffnx {C Function} scm_substring_fill_x (str, chr, start, end)
2932 @deffnx {C Function} scm_string_fill_x (str, chr)
2933 Stores @var{chr} in every element of the given @var{str} and
2934 returns an unspecified value.
2937 @deffn {Scheme Procedure} substring-fill! str start end fill
2938 @deffnx {C Function} scm_substring_fill_x (str, start, end, fill)
2939 Change every character in @var{str} between @var{start} and
2940 @var{end} to @var{fill}.
2943 (define y "abcdefg")
2944 (substring-fill! y 1 3 #\r)
2950 @deffn {Scheme Procedure} substring-move! str1 start1 end1 str2 start2
2951 @deffnx {C Function} scm_substring_move_x (str1, start1, end1, str2, start2)
2952 Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}
2953 into @var{str2} beginning at position @var{start2}.
2954 @var{str1} and @var{str2} can be the same string.
2957 @deffn {Scheme Procedure} string-copy! target tstart s [start [end]]
2958 @deffnx {C Function} scm_string_copy_x (target, tstart, s, start, end)
2959 Copy the sequence of characters from index range [@var{start},
2960 @var{end}) in string @var{s} to string @var{target}, beginning
2961 at index @var{tstart}. The characters are copied left-to-right
2962 or right-to-left as needed -- the copy is guaranteed to work,
2963 even if @var{target} and @var{s} are the same string. It is an
2964 error if the copy operation runs off the end of the target
2969 @node String Comparison
2970 @subsubsection String Comparison
2972 The procedures in this section are similar to the character ordering
2973 predicates (@pxref{Characters}), but are defined on character sequences.
2975 The first set is specified in R5RS and has names that end in @code{?}.
2976 The second set is specified in SRFI-13 and the names have not ending
2979 The predicates ending in @code{-ci} ignore the character case
2980 when comparing strings. For now, case-insensitive comparison is done
2981 using the R5RS rules, where every lower-case character that has a
2982 single character upper-case form is converted to uppercase before
2983 comparison. See @xref{Text Collation, the @code{(ice-9
2984 i18n)} module}, for locale-dependent string comparison.
2987 @deffn {Scheme Procedure} string=? s1 s2
2988 Lexicographic equality predicate; return @code{#t} if the two
2989 strings are the same length and contain the same characters in
2990 the same positions, otherwise return @code{#f}.
2992 The procedure @code{string-ci=?} treats upper and lower case
2993 letters as though they were the same character, but
2994 @code{string=?} treats upper and lower case as distinct
2999 @deffn {Scheme Procedure} string<? s1 s2
3000 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3001 is lexicographically less than @var{s2}.
3005 @deffn {Scheme Procedure} string<=? s1 s2
3006 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3007 is lexicographically less than or equal to @var{s2}.
3011 @deffn {Scheme Procedure} string>? s1 s2
3012 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3013 is lexicographically greater than @var{s2}.
3017 @deffn {Scheme Procedure} string>=? s1 s2
3018 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3019 is lexicographically greater than or equal to @var{s2}.
3022 @rnindex string-ci=?
3023 @deffn {Scheme Procedure} string-ci=? s1 s2
3024 Case-insensitive string equality predicate; return @code{#t} if
3025 the two strings are the same length and their component
3026 characters match (ignoring case) at each position; otherwise
3030 @rnindex string-ci<?
3031 @deffn {Scheme Procedure} string-ci<? s1 s2
3032 Case insensitive lexicographic ordering predicate; return
3033 @code{#t} if @var{s1} is lexicographically less than @var{s2}
3038 @deffn {Scheme Procedure} string-ci<=? s1 s2
3039 Case insensitive lexicographic ordering predicate; return
3040 @code{#t} if @var{s1} is lexicographically less than or equal
3041 to @var{s2} regardless of case.
3044 @rnindex string-ci>?
3045 @deffn {Scheme Procedure} string-ci>? s1 s2
3046 Case insensitive lexicographic ordering predicate; return
3047 @code{#t} if @var{s1} is lexicographically greater than
3048 @var{s2} regardless of case.
3051 @rnindex string-ci>=?
3052 @deffn {Scheme Procedure} string-ci>=? s1 s2
3053 Case insensitive lexicographic ordering predicate; return
3054 @code{#t} if @var{s1} is lexicographically greater than or
3055 equal to @var{s2} regardless of case.
3058 @deffn {Scheme Procedure} string-compare s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
3059 @deffnx {C Function} scm_string_compare (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
3060 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
3061 mismatch index, depending upon whether @var{s1} is less than,
3062 equal to, or greater than @var{s2}. The mismatch index is the
3063 largest index @var{i} such that for every 0 <= @var{j} <
3064 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
3065 @var{i} is the first position that does not match.
3068 @deffn {Scheme Procedure} string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
3069 @deffnx {C Function} scm_string_compare_ci (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
3070 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
3071 mismatch index, depending upon whether @var{s1} is less than,
3072 equal to, or greater than @var{s2}. The mismatch index is the
3073 largest index @var{i} such that for every 0 <= @var{j} <
3074 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
3075 @var{i} is the first position that does not match. The
3076 character comparison is done case-insensitively.
3079 @deffn {Scheme Procedure} string= s1 s2 [start1 [end1 [start2 [end2]]]]
3080 @deffnx {C Function} scm_string_eq (s1, s2, start1, end1, start2, end2)
3081 Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
3085 @deffn {Scheme Procedure} string<> s1 s2 [start1 [end1 [start2 [end2]]]]
3086 @deffnx {C Function} scm_string_neq (s1, s2, start1, end1, start2, end2)
3087 Return @code{#f} if @var{s1} and @var{s2} are equal, a true
3091 @deffn {Scheme Procedure} string< s1 s2 [start1 [end1 [start2 [end2]]]]
3092 @deffnx {C Function} scm_string_lt (s1, s2, start1, end1, start2, end2)
3093 Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
3094 true value otherwise.
3097 @deffn {Scheme Procedure} string> s1 s2 [start1 [end1 [start2 [end2]]]]
3098 @deffnx {C Function} scm_string_gt (s1, s2, start1, end1, start2, end2)
3099 Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
3100 true value otherwise.
3103 @deffn {Scheme Procedure} string<= s1 s2 [start1 [end1 [start2 [end2]]]]
3104 @deffnx {C Function} scm_string_le (s1, s2, start1, end1, start2, end2)
3105 Return @code{#f} if @var{s1} is greater to @var{s2}, a true
3109 @deffn {Scheme Procedure} string>= s1 s2 [start1 [end1 [start2 [end2]]]]
3110 @deffnx {C Function} scm_string_ge (s1, s2, start1, end1, start2, end2)
3111 Return @code{#f} if @var{s1} is less to @var{s2}, a true value
3115 @deffn {Scheme Procedure} string-ci= s1 s2 [start1 [end1 [start2 [end2]]]]
3116 @deffnx {C Function} scm_string_ci_eq (s1, s2, start1, end1, start2, end2)
3117 Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
3118 value otherwise. The character comparison is done
3122 @deffn {Scheme Procedure} string-ci<> s1 s2 [start1 [end1 [start2 [end2]]]]
3123 @deffnx {C Function} scm_string_ci_neq (s1, s2, start1, end1, start2, end2)
3124 Return @code{#f} if @var{s1} and @var{s2} are equal, a true
3125 value otherwise. The character comparison is done
3129 @deffn {Scheme Procedure} string-ci< s1 s2 [start1 [end1 [start2 [end2]]]]
3130 @deffnx {C Function} scm_string_ci_lt (s1, s2, start1, end1, start2, end2)
3131 Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
3132 true value otherwise. The character comparison is done
3136 @deffn {Scheme Procedure} string-ci> s1 s2 [start1 [end1 [start2 [end2]]]]
3137 @deffnx {C Function} scm_string_ci_gt (s1, s2, start1, end1, start2, end2)
3138 Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
3139 true value otherwise. The character comparison is done
3143 @deffn {Scheme Procedure} string-ci<= s1 s2 [start1 [end1 [start2 [end2]]]]
3144 @deffnx {C Function} scm_string_ci_le (s1, s2, start1, end1, start2, end2)
3145 Return @code{#f} if @var{s1} is greater to @var{s2}, a true
3146 value otherwise. The character comparison is done
3150 @deffn {Scheme Procedure} string-ci>= s1 s2 [start1 [end1 [start2 [end2]]]]
3151 @deffnx {C Function} scm_string_ci_ge (s1, s2, start1, end1, start2, end2)
3152 Return @code{#f} if @var{s1} is less to @var{s2}, a true value
3153 otherwise. The character comparison is done
3157 @deffn {Scheme Procedure} string-hash s [bound [start [end]]]
3158 @deffnx {C Function} scm_substring_hash (s, bound, start, end)
3159 Compute a hash value for @var{S}. the optional argument @var{bound} is a non-negative exact integer specifying the range of the hash function. A positive value restricts the return value to the range [0,bound).
3162 @deffn {Scheme Procedure} string-hash-ci s [bound [start [end]]]
3163 @deffnx {C Function} scm_substring_hash_ci (s, bound, start, end)
3164 Compute a hash value for @var{S}. the optional argument @var{bound} is a non-negative exact integer specifying the range of the hash function. A positive value restricts the return value to the range [0,bound).
3167 @node String Searching
3168 @subsubsection String Searching
3170 @deffn {Scheme Procedure} string-index s char_pred [start [end]]
3171 @deffnx {C Function} scm_string_index (s, char_pred, start, end)
3172 Search through the string @var{s} from left to right, returning
3173 the index of the first occurrence of a character which
3177 equals @var{char_pred}, if it is character,
3180 satisfies the predicate @var{char_pred}, if it is a procedure,
3183 is in the set @var{char_pred}, if it is a character set.
3187 @deffn {Scheme Procedure} string-rindex s char_pred [start [end]]
3188 @deffnx {C Function} scm_string_rindex (s, char_pred, start, end)
3189 Search through the string @var{s} from right to left, returning
3190 the index of the last occurrence of a character which
3194 equals @var{char_pred}, if it is character,
3197 satisfies the predicate @var{char_pred}, if it is a procedure,
3200 is in the set if @var{char_pred} is a character set.
3204 @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 [end1 [start2 [end2]]]]
3205 @deffnx {C Function} scm_string_prefix_length (s1, s2, start1, end1, start2, end2)
3206 Return the length of the longest common prefix of the two
3210 @deffn {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3211 @deffnx {C Function} scm_string_prefix_length_ci (s1, s2, start1, end1, start2, end2)
3212 Return the length of the longest common prefix of the two
3213 strings, ignoring character case.
3216 @deffn {Scheme Procedure} string-suffix-length s1 s2 [start1 [end1 [start2 [end2]]]]
3217 @deffnx {C Function} scm_string_suffix_length (s1, s2, start1, end1, start2, end2)
3218 Return the length of the longest common suffix of the two
3222 @deffn {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3223 @deffnx {C Function} scm_string_suffix_length_ci (s1, s2, start1, end1, start2, end2)
3224 Return the length of the longest common suffix of the two
3225 strings, ignoring character case.
3228 @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 [end1 [start2 [end2]]]]
3229 @deffnx {C Function} scm_string_prefix_p (s1, s2, start1, end1, start2, end2)
3230 Is @var{s1} a prefix of @var{s2}?
3233 @deffn {Scheme Procedure} string-prefix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
3234 @deffnx {C Function} scm_string_prefix_ci_p (s1, s2, start1, end1, start2, end2)
3235 Is @var{s1} a prefix of @var{s2}, ignoring character case?
3238 @deffn {Scheme Procedure} string-suffix? s1 s2 [start1 [end1 [start2 [end2]]]]
3239 @deffnx {C Function} scm_string_suffix_p (s1, s2, start1, end1, start2, end2)
3240 Is @var{s1} a suffix of @var{s2}?
3243 @deffn {Scheme Procedure} string-suffix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
3244 @deffnx {C Function} scm_string_suffix_ci_p (s1, s2, start1, end1, start2, end2)
3245 Is @var{s1} a suffix of @var{s2}, ignoring character case?
3248 @deffn {Scheme Procedure} string-index-right s char_pred [start [end]]
3249 @deffnx {C Function} scm_string_index_right (s, char_pred, start, end)
3250 Search through the string @var{s} from right to left, returning
3251 the index of the last occurrence of a character which
3255 equals @var{char_pred}, if it is character,
3258 satisfies the predicate @var{char_pred}, if it is a procedure,
3261 is in the set if @var{char_pred} is a character set.
3265 @deffn {Scheme Procedure} string-skip s char_pred [start [end]]
3266 @deffnx {C Function} scm_string_skip (s, char_pred, start, end)
3267 Search through the string @var{s} from left to right, returning
3268 the index of the first occurrence of a character which
3272 does not equal @var{char_pred}, if it is character,
3275 does not satisfy the predicate @var{char_pred}, if it is a
3279 is not in the set if @var{char_pred} is a character set.
3283 @deffn {Scheme Procedure} string-skip-right s char_pred [start [end]]
3284 @deffnx {C Function} scm_string_skip_right (s, char_pred, start, end)
3285 Search through the string @var{s} from right to left, returning
3286 the index of the last occurrence of a character which
3290 does not equal @var{char_pred}, if it is character,
3293 does not satisfy the predicate @var{char_pred}, if it is a
3297 is not in the set if @var{char_pred} is a character set.
3301 @deffn {Scheme Procedure} string-count s char_pred [start [end]]
3302 @deffnx {C Function} scm_string_count (s, char_pred, start, end)
3303 Return the count of the number of characters in the string
3308 equals @var{char_pred}, if it is character,
3311 satisfies the predicate @var{char_pred}, if it is a procedure.
3314 is in the set @var{char_pred}, if it is a character set.
3318 @deffn {Scheme Procedure} string-contains s1 s2 [start1 [end1 [start2 [end2]]]]
3319 @deffnx {C Function} scm_string_contains (s1, s2, start1, end1, start2, end2)
3320 Does string @var{s1} contain string @var{s2}? Return the index
3321 in @var{s1} where @var{s2} occurs as a substring, or false.
3322 The optional start/end indices restrict the operation to the
3323 indicated substrings.
3326 @deffn {Scheme Procedure} string-contains-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3327 @deffnx {C Function} scm_string_contains_ci (s1, s2, start1, end1, start2, end2)
3328 Does string @var{s1} contain string @var{s2}? Return the index
3329 in @var{s1} where @var{s2} occurs as a substring, or false.
3330 The optional start/end indices restrict the operation to the
3331 indicated substrings. Character comparison is done
3335 @node Alphabetic Case Mapping
3336 @subsubsection Alphabetic Case Mapping
3338 These are procedures for mapping strings to their upper- or lower-case
3339 equivalents, respectively, or for capitalizing strings.
3341 @deffn {Scheme Procedure} string-upcase str [start [end]]
3342 @deffnx {C Function} scm_substring_upcase (str, start, end)
3343 @deffnx {C Function} scm_string_upcase (str)
3344 Upcase every character in @code{str}.
3347 @deffn {Scheme Procedure} string-upcase! str [start [end]]
3348 @deffnx {C Function} scm_substring_upcase_x (str, start, end)
3349 @deffnx {C Function} scm_string_upcase_x (str)
3350 Destructively upcase every character in @code{str}.
3360 @deffn {Scheme Procedure} string-downcase str [start [end]]
3361 @deffnx {C Function} scm_substring_downcase (str, start, end)
3362 @deffnx {C Function} scm_string_downcase (str)
3363 Downcase every character in @var{str}.
3366 @deffn {Scheme Procedure} string-downcase! str [start [end]]
3367 @deffnx {C Function} scm_substring_downcase_x (str, start, end)
3368 @deffnx {C Function} scm_string_downcase_x (str)
3369 Destructively downcase every character in @var{str}.
3374 (string-downcase! y)
3381 @deffn {Scheme Procedure} string-capitalize str
3382 @deffnx {C Function} scm_string_capitalize (str)
3383 Return a freshly allocated string with the characters in
3384 @var{str}, where the first character of every word is
3388 @deffn {Scheme Procedure} string-capitalize! str
3389 @deffnx {C Function} scm_string_capitalize_x (str)
3390 Upcase the first character of every word in @var{str}
3391 destructively and return @var{str}.
3394 y @result{} "hello world"
3395 (string-capitalize! y) @result{} "Hello World"
3396 y @result{} "Hello World"
3400 @deffn {Scheme Procedure} string-titlecase str [start [end]]
3401 @deffnx {C Function} scm_string_titlecase (str, start, end)
3402 Titlecase every first character in a word in @var{str}.
3405 @deffn {Scheme Procedure} string-titlecase! str [start [end]]
3406 @deffnx {C Function} scm_string_titlecase_x (str, start, end)
3407 Destructively titlecase every first character in a word in
3411 @node Reversing and Appending Strings
3412 @subsubsection Reversing and Appending Strings
3414 @deffn {Scheme Procedure} string-reverse str [start [end]]
3415 @deffnx {C Function} scm_string_reverse (str, start, end)
3416 Reverse the string @var{str}. The optional arguments
3417 @var{start} and @var{end} delimit the region of @var{str} to
3421 @deffn {Scheme Procedure} string-reverse! str [start [end]]
3422 @deffnx {C Function} scm_string_reverse_x (str, start, end)
3423 Reverse the string @var{str} in-place. The optional arguments
3424 @var{start} and @var{end} delimit the region of @var{str} to
3425 operate on. The return value is unspecified.
3428 @rnindex string-append
3429 @deffn {Scheme Procedure} string-append . args
3430 @deffnx {C Function} scm_string_append (args)
3431 Return a newly allocated string whose characters form the
3432 concatenation of the given strings, @var{args}.
3436 (string-append h "world"))
3437 @result{} "hello world"
3441 @deffn {Scheme Procedure} string-append/shared . ls
3442 @deffnx {C Function} scm_string_append_shared (ls)
3443 Like @code{string-append}, but the result may share memory
3444 with the argument strings.
3447 @deffn {Scheme Procedure} string-concatenate ls
3448 @deffnx {C Function} scm_string_concatenate (ls)
3449 Append the elements of @var{ls} (which must be strings)
3450 together into a single string. Guaranteed to return a freshly
3454 @deffn {Scheme Procedure} string-concatenate-reverse ls [final_string [end]]
3455 @deffnx {C Function} scm_string_concatenate_reverse (ls, final_string, end)
3456 Without optional arguments, this procedure is equivalent to
3459 (string-concatenate (reverse ls))
3462 If the optional argument @var{final_string} is specified, it is
3463 consed onto the beginning to @var{ls} before performing the
3464 list-reverse and string-concatenate operations. If @var{end}
3465 is given, only the characters of @var{final_string} up to index
3468 Guaranteed to return a freshly allocated string.
3471 @deffn {Scheme Procedure} string-concatenate/shared ls
3472 @deffnx {C Function} scm_string_concatenate_shared (ls)
3473 Like @code{string-concatenate}, but the result may share memory
3474 with the strings in the list @var{ls}.
3477 @deffn {Scheme Procedure} string-concatenate-reverse/shared ls [final_string [end]]
3478 @deffnx {C Function} scm_string_concatenate_reverse_shared (ls, final_string, end)
3479 Like @code{string-concatenate-reverse}, but the result may
3480 share memory with the the strings in the @var{ls} arguments.
3483 @node Mapping Folding and Unfolding
3484 @subsubsection Mapping, Folding, and Unfolding
3486 @deffn {Scheme Procedure} string-map proc s [start [end]]
3487 @deffnx {C Function} scm_string_map (proc, s, start, end)
3488 @var{proc} is a char->char procedure, it is mapped over
3489 @var{s}. The order in which the procedure is applied to the
3490 string elements is not specified.
3493 @deffn {Scheme Procedure} string-map! proc s [start [end]]
3494 @deffnx {C Function} scm_string_map_x (proc, s, start, end)
3495 @var{proc} is a char->char procedure, it is mapped over
3496 @var{s}. The order in which the procedure is applied to the
3497 string elements is not specified. The string @var{s} is
3498 modified in-place, the return value is not specified.
3501 @deffn {Scheme Procedure} string-for-each proc s [start [end]]
3502 @deffnx {C Function} scm_string_for_each (proc, s, start, end)
3503 @var{proc} is mapped over @var{s} in left-to-right order. The
3504 return value is not specified.
3507 @deffn {Scheme Procedure} string-for-each-index proc s [start [end]]
3508 @deffnx {C Function} scm_string_for_each_index (proc, s, start, end)
3509 Call @code{(@var{proc} i)} for each index i in @var{s}, from left to
3512 For example, to change characters to alternately upper and lower case,
3515 (define str (string-copy "studly"))
3516 (string-for-each-index
3519 ((if (even? i) char-upcase char-downcase)
3520 (string-ref str i))))
3522 str @result{} "StUdLy"
3526 @deffn {Scheme Procedure} string-fold kons knil s [start [end]]
3527 @deffnx {C Function} scm_string_fold (kons, knil, s, start, end)
3528 Fold @var{kons} over the characters of @var{s}, with @var{knil}
3529 as the terminating element, from left to right. @var{kons}
3530 must expect two arguments: The actual character and the last
3531 result of @var{kons}' application.
3534 @deffn {Scheme Procedure} string-fold-right kons knil s [start [end]]
3535 @deffnx {C Function} scm_string_fold_right (kons, knil, s, start, end)
3536 Fold @var{kons} over the characters of @var{s}, with @var{knil}
3537 as the terminating element, from right to left. @var{kons}
3538 must expect two arguments: The actual character and the last
3539 result of @var{kons}' application.
3542 @deffn {Scheme Procedure} string-unfold p f g seed [base [make_final]]
3543 @deffnx {C Function} scm_string_unfold (p, f, g, seed, base, make_final)
3545 @item @var{g} is used to generate a series of @emph{seed}
3546 values from the initial @var{seed}: @var{seed}, (@var{g}
3547 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
3549 @item @var{p} tells us when to stop -- when it returns true
3550 when applied to one of these seed values.
3551 @item @var{f} maps each seed value to the corresponding
3552 character in the result string. These chars are assembled
3553 into the string in a left-to-right order.
3554 @item @var{base} is the optional initial/leftmost portion
3555 of the constructed string; it default to the empty
3557 @item @var{make_final} is applied to the terminal seed
3558 value (on which @var{p} returns true) to produce
3559 the final/rightmost portion of the constructed string.
3560 The default is nothing extra.
3564 @deffn {Scheme Procedure} string-unfold-right p f g seed [base [make_final]]
3565 @deffnx {C Function} scm_string_unfold_right (p, f, g, seed, base, make_final)
3567 @item @var{g} is used to generate a series of @emph{seed}
3568 values from the initial @var{seed}: @var{seed}, (@var{g}
3569 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
3571 @item @var{p} tells us when to stop -- when it returns true
3572 when applied to one of these seed values.
3573 @item @var{f} maps each seed value to the corresponding
3574 character in the result string. These chars are assembled
3575 into the string in a right-to-left order.
3576 @item @var{base} is the optional initial/rightmost portion
3577 of the constructed string; it default to the empty
3579 @item @var{make_final} is applied to the terminal seed
3580 value (on which @var{p} returns true) to produce
3581 the final/leftmost portion of the constructed string.
3582 It defaults to @code{(lambda (x) )}.
3586 @node Miscellaneous String Operations
3587 @subsubsection Miscellaneous String Operations
3589 @deffn {Scheme Procedure} xsubstring s from [to [start [end]]]
3590 @deffnx {C Function} scm_xsubstring (s, from, to, start, end)
3591 This is the @emph{extended substring} procedure that implements
3592 replicated copying of a substring of some string.
3594 @var{s} is a string, @var{start} and @var{end} are optional
3595 arguments that demarcate a substring of @var{s}, defaulting to
3596 0 and the length of @var{s}. Replicate this substring up and
3597 down index space, in both the positive and negative directions.
3598 @code{xsubstring} returns the substring of this string
3599 beginning at index @var{from}, and ending at @var{to}, which
3600 defaults to @var{from} + (@var{end} - @var{start}).
3603 @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto [start [end]]]
3604 @deffnx {C Function} scm_string_xcopy_x (target, tstart, s, sfrom, sto, start, end)
3605 Exactly the same as @code{xsubstring}, but the extracted text
3606 is written into the string @var{target} starting at index
3607 @var{tstart}. The operation is not defined if @code{(eq?
3608 @var{target} @var{s})} or these arguments share storage -- you
3609 cannot copy a string on top of itself.
3612 @deffn {Scheme Procedure} string-replace s1 s2 [start1 [end1 [start2 [end2]]]]
3613 @deffnx {C Function} scm_string_replace (s1, s2, start1, end1, start2, end2)
3614 Return the string @var{s1}, but with the characters
3615 @var{start1} @dots{} @var{end1} replaced by the characters
3616 @var{start2} @dots{} @var{end2} from @var{s2}.
3619 @deffn {Scheme Procedure} string-tokenize s [token_set [start [end]]]
3620 @deffnx {C Function} scm_string_tokenize (s, token_set, start, end)
3621 Split the string @var{s} into a list of substrings, where each
3622 substring is a maximal non-empty contiguous sequence of
3623 characters from the character set @var{token_set}, which
3624 defaults to @code{char-set:graphic}.
3625 If @var{start} or @var{end} indices are provided, they restrict
3626 @code{string-tokenize} to operating on the indicated substring
3630 @deffn {Scheme Procedure} string-filter s char_pred [start [end]]
3631 @deffnx {C Function} scm_string_filter (s, char_pred, start, end)
3632 Filter the string @var{s}, retaining only those characters which
3633 satisfy @var{char_pred}.
3635 If @var{char_pred} is a procedure, it is applied to each character as
3636 a predicate, if it is a character, it is tested for equality and if it
3637 is a character set, it is tested for membership.
3640 @deffn {Scheme Procedure} string-delete s char_pred [start [end]]
3641 @deffnx {C Function} scm_string_delete (s, char_pred, start, end)
3642 Delete characters satisfying @var{char_pred} from @var{s}.
3644 If @var{char_pred} is a procedure, it is applied to each character as
3645 a predicate, if it is a character, it is tested for equality and if it
3646 is a character set, it is tested for membership.
3649 @node Conversion to/from C
3650 @subsubsection Conversion to/from C
3652 When creating a Scheme string from a C string or when converting a
3653 Scheme string to a C string, the concept of character encoding becomes
3656 In C, a string is just a sequence of bytes, and the character encoding
3657 describes the relation between these bytes and the actual characters
3658 that make up the string. For Scheme strings, character encoding is
3659 not an issue (most of the time), since in Scheme you never get to see
3660 the bytes, only the characters.
3662 Well, ideally, anyway. Right now, Guile simply equates Scheme
3663 characters and bytes, ignoring the possibility of multi-byte encodings
3664 completely. This will change in the future, where Guile will use
3665 Unicode codepoints as its characters and UTF-8 or some other encoding
3666 as its internal encoding. When you exclusively use the functions
3667 listed in this section, you are `future-proof'.
3669 Converting a Scheme string to a C string will often allocate fresh
3670 memory to hold the result. You must take care that this memory is
3671 properly freed eventually. In many cases, this can be achieved by
3672 using @code{scm_dynwind_free} inside an appropriate dynwind context,
3673 @xref{Dynamic Wind}.
3675 @deftypefn {C Function} SCM scm_from_locale_string (const char *str)
3676 @deftypefnx {C Function} SCM scm_from_locale_stringn (const char *str, size_t len)
3677 Creates a new Scheme string that has the same contents as @var{str}
3678 when interpreted in the current locale character encoding.
3680 For @code{scm_from_locale_string}, @var{str} must be null-terminated.
3682 For @code{scm_from_locale_stringn}, @var{len} specifies the length of
3683 @var{str} in bytes, and @var{str} does not need to be null-terminated.
3684 If @var{len} is @code{(size_t)-1}, then @var{str} does need to be
3685 null-terminated and the real length will be found with @code{strlen}.
3688 @deftypefn {C Function} SCM scm_take_locale_string (char *str)
3689 @deftypefnx {C Function} SCM scm_take_locale_stringn (char *str, size_t len)
3690 Like @code{scm_from_locale_string} and @code{scm_from_locale_stringn},
3691 respectively, but also frees @var{str} with @code{free} eventually.
3692 Thus, you can use this function when you would free @var{str} anyway
3693 immediately after creating the Scheme string. In certain cases, Guile
3694 can then use @var{str} directly as its internal representation.
3697 @deftypefn {C Function} {char *} scm_to_locale_string (SCM str)
3698 @deftypefnx {C Function} {char *} scm_to_locale_stringn (SCM str, size_t *lenp)
3699 Returns a C string in the current locale encoding with the same
3700 contents as @var{str}. The C string must be freed with @code{free}
3701 eventually, maybe by using @code{scm_dynwind_free}, @xref{Dynamic
3704 For @code{scm_to_locale_string}, the returned string is
3705 null-terminated and an error is signalled when @var{str} contains
3706 @code{#\nul} characters.
3708 For @code{scm_to_locale_stringn} and @var{lenp} not @code{NULL},
3709 @var{str} might contain @code{#\nul} characters and the length of the
3710 returned string in bytes is stored in @code{*@var{lenp}}. The
3711 returned string will not be null-terminated in this case. If
3712 @var{lenp} is @code{NULL}, @code{scm_to_locale_stringn} behaves like
3713 @code{scm_to_locale_string}.
3716 @deftypefn {C Function} size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
3717 Puts @var{str} as a C string in the current locale encoding into the
3718 memory pointed to by @var{buf}. The buffer at @var{buf} has room for
3719 @var{max_len} bytes and @code{scm_to_local_stringbuf} will never store
3720 more than that. No terminating @code{'\0'} will be stored.
3722 The return value of @code{scm_to_locale_stringbuf} is the number of
3723 bytes that are needed for all of @var{str}, regardless of whether
3724 @var{buf} was large enough to hold them. Thus, when the return value
3725 is larger than @var{max_len}, only @var{max_len} bytes have been
3726 stored and you probably need to try again with a larger buffer.
3730 @subsection Bytevectors
3735 A @dfn{bytevector} is a raw bit string. The @code{(rnrs bytevector)}
3736 module provides the programming interface specified by the
3737 @uref{http://www.r6rs.org/, Revised^6 Report on the Algorithmic Language
3738 Scheme (R6RS)}. It contains procedures to manipulate bytevectors and
3739 interpret their contents in a number of ways: bytevector contents can be
3740 accessed as signed or unsigned integer of various sizes and endianness,
3741 as IEEE-754 floating point numbers, or as strings. It is a useful tool
3742 to encode and decode binary data.
3744 The R6RS (Section 4.3.4) specifies an external representation for
3745 bytevectors, whereby the octets (integers in the range 0--255) contained
3746 in the bytevector are represented as a list prefixed by @code{#vu8}:
3752 denotes a 3-byte bytevector containing the octets 1, 53, and 204. Like
3753 string literals, booleans, etc., bytevectors are ``self-quoting'', i.e.,
3754 they do not need to be quoted:
3758 @result{} #vu8(1 53 204)
3761 Bytevectors can be used with the binary input/output primitives of the
3762 R6RS (@pxref{R6RS I/O Ports}).
3765 * Bytevector Endianness:: Dealing with byte order.
3766 * Bytevector Manipulation:: Creating, copying, manipulating bytevectors.
3767 * Bytevectors as Integers:: Interpreting bytes as integers.
3768 * Bytevectors and Integer Lists:: Converting to/from an integer list.
3769 * Bytevectors as Floats:: Interpreting bytes as real numbers.
3770 * Bytevectors as Strings:: Interpreting bytes as Unicode strings.
3771 * Bytevectors as Generalized Vectors:: Guile extension to the bytevector API.
3774 @node Bytevector Endianness
3775 @subsubsection Endianness
3781 Some of the following procedures take an @var{endianness} parameter.
3782 The @dfn{endianness} is defined as the order of bytes in multi-byte
3783 numbers: numbers encoded in @dfn{big endian} have their most
3784 significant bytes written first, whereas numbers encoded in
3785 @dfn{little endian} have their least significant bytes
3786 first@footnote{Big-endian and little-endian are the most common
3787 ``endiannesses'', but others do exist. For instance, the GNU MP
3788 library allows @dfn{word order} to be specified independently of
3789 @dfn{byte order} (@pxref{Integer Import and Export,,, gmp, The GNU
3790 Multiple Precision Arithmetic Library Manual}).}.
3792 Little-endian is the native endianness of the IA32 architecture and
3793 its derivatives, while big-endian is native to SPARC and PowerPC,
3794 among others. The @code{native-endianness} procedure returns the
3795 native endianness of the machine it runs on.
3797 @deffn {Scheme Procedure} native-endianness
3798 @deffnx {C Function} scm_native_endianness ()
3799 Return a value denoting the native endianness of the host machine.
3802 @deffn {Scheme Macro} endianness symbol
3803 Return an object denoting the endianness specified by @var{symbol}. If
3804 @var{symbol} is neither @code{big} nor @code{little} then an error is
3805 raised at expand-time.
3808 @defvr {C Variable} scm_endianness_big
3809 @defvrx {C Variable} scm_endianness_little
3810 The objects denoting big- and little-endianness, respectively.
3814 @node Bytevector Manipulation
3815 @subsubsection Manipulating Bytevectors
3817 Bytevectors can be created, copied, and analyzed with the following
3818 procedures and C functions.
3820 @deffn {Scheme Procedure} make-bytevector len [fill]
3821 @deffnx {C Function} scm_make_bytevector (len, fill)
3822 @deffnx {C Function} scm_c_make_bytevector (size_t len)
3823 Return a new bytevector of @var{len} bytes. Optionally, if @var{fill}
3824 is given, fill it with @var{fill}; @var{fill} must be in the range
3828 @deffn {Scheme Procedure} bytevector? obj
3829 @deffnx {C Function} scm_bytevector_p (obj)
3830 Return true if @var{obj} is a bytevector.
3833 @deftypefn {C Function} int scm_is_bytevector (SCM obj)
3834 Equivalent to @code{scm_is_true (scm_bytevector_p (obj))}.
3837 @deffn {Scheme Procedure} bytevector-length bv
3838 @deffnx {C Function} scm_bytevector_length (bv)
3839 Return the length in bytes of bytevector @var{bv}.
3842 @deftypefn {C Function} size_t scm_c_bytevector_length (SCM bv)
3843 Likewise, return the length in bytes of bytevector @var{bv}.
3846 @deffn {Scheme Procedure} bytevector=? bv1 bv2
3847 @deffnx {C Function} scm_bytevector_eq_p (bv1, bv2)
3848 Return is @var{bv1} equals to @var{bv2}---i.e., if they have the same
3849 length and contents.
3852 @deffn {Scheme Procedure} bytevector-fill! bv fill
3853 @deffnx {C Function} scm_bytevector_fill_x (bv, fill)
3854 Fill bytevector @var{bv} with @var{fill}, a byte.
3857 @deffn {Scheme Procedure} bytevector-copy! source source-start target target-start len
3858 @deffnx {C Function} scm_bytevector_copy_x (source, source_start, target, target_start, len)
3859 Copy @var{len} bytes from @var{source} into @var{target}, starting
3860 reading from @var{source-start} (a positive index within @var{source})
3861 and start writing at @var{target-start}.
3864 @deffn {Scheme Procedure} bytevector-copy bv
3865 @deffnx {C Function} scm_bytevector_copy (bv)
3866 Return a newly allocated copy of @var{bv}.
3869 @deftypefn {C Function} scm_t_uint8 scm_c_bytevector_ref (SCM bv, size_t index)
3870 Return the byte at @var{index} in bytevector @var{bv}.
3873 @deftypefn {C Function} void scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
3874 Set the byte at @var{index} in @var{bv} to @var{value}.
3877 Low-level C macros are available. They do not perform any
3878 type-checking; as such they should be used with care.
3880 @deftypefn {C Macro} size_t SCM_BYTEVECTOR_LENGTH (bv)
3881 Return the length in bytes of bytevector @var{bv}.
3884 @deftypefn {C Macro} {signed char *} SCM_BYTEVECTOR_CONTENTS (bv)
3885 Return a pointer to the contents of bytevector @var{bv}.
3889 @node Bytevectors as Integers
3890 @subsubsection Interpreting Bytevector Contents as Integers
3892 The contents of a bytevector can be interpreted as a sequence of
3893 integers of any given size, sign, and endianness.
3896 (let ((bv (make-bytevector 4)))
3897 (bytevector-u8-set! bv 0 #x12)
3898 (bytevector-u8-set! bv 1 #x34)
3899 (bytevector-u8-set! bv 2 #x56)
3900 (bytevector-u8-set! bv 3 #x78)
3902 (map (lambda (number)
3903 (number->string number 16))
3904 (list (bytevector-u8-ref bv 0)
3905 (bytevector-u16-ref bv 0 (endianness big))
3906 (bytevector-u32-ref bv 0 (endianness little)))))
3908 @result{} ("12" "1234" "78563412")
3911 The most generic procedures to interpret bytevector contents as integers
3912 are described below.
3914 @deffn {Scheme Procedure} bytevector-uint-ref bv index endianness size
3915 @deffnx {Scheme Procedure} bytevector-sint-ref bv index endianness size
3916 @deffnx {C Function} scm_bytevector_uint_ref (bv, index, endianness, size)
3917 @deffnx {C Function} scm_bytevector_sint_ref (bv, index, endianness, size)
3918 Return the @var{size}-byte long unsigned (resp. signed) integer at
3919 index @var{index} in @var{bv}, decoded according to @var{endianness}.
3922 @deffn {Scheme Procedure} bytevector-uint-set! bv index value endianness size
3923 @deffnx {Scheme Procedure} bytevector-sint-set! bv index value endianness size
3924 @deffnx {C Function} scm_bytevector_uint_set_x (bv, index, value, endianness, size)
3925 @deffnx {C Function} scm_bytevector_sint_set_x (bv, index, value, endianness, size)
3926 Set the @var{size}-byte long unsigned (resp. signed) integer at
3927 @var{index} to @var{value}, encoded according to @var{endianness}.
3930 The following procedures are similar to the ones above, but specialized
3931 to a given integer size:
3933 @deffn {Scheme Procedure} bytevector-u8-ref bv index
3934 @deffnx {Scheme Procedure} bytevector-s8-ref bv index
3935 @deffnx {Scheme Procedure} bytevector-u16-ref bv index endianness
3936 @deffnx {Scheme Procedure} bytevector-s16-ref bv index endianness
3937 @deffnx {Scheme Procedure} bytevector-u32-ref bv index endianness
3938 @deffnx {Scheme Procedure} bytevector-s32-ref bv index endianness
3939 @deffnx {Scheme Procedure} bytevector-u64-ref bv index endianness
3940 @deffnx {Scheme Procedure} bytevector-s64-ref bv index endianness
3941 @deffnx {C Function} scm_bytevector_u8_ref (bv, index)
3942 @deffnx {C Function} scm_bytevector_s8_ref (bv, index)
3943 @deffnx {C Function} scm_bytevector_u16_ref (bv, index, endianness)
3944 @deffnx {C Function} scm_bytevector_s16_ref (bv, index, endianness)
3945 @deffnx {C Function} scm_bytevector_u32_ref (bv, index, endianness)
3946 @deffnx {C Function} scm_bytevector_s32_ref (bv, index, endianness)
3947 @deffnx {C Function} scm_bytevector_u64_ref (bv, index, endianness)
3948 @deffnx {C Function} scm_bytevector_s64_ref (bv, index, endianness)
3949 Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
3950 16, 32 or 64) from @var{bv} at @var{index}, decoded according to
3954 @deffn {Scheme Procedure} bytevector-u8-set! bv index value
3955 @deffnx {Scheme Procedure} bytevector-s8-set! bv index value
3956 @deffnx {Scheme Procedure} bytevector-u16-set! bv index value endianness
3957 @deffnx {Scheme Procedure} bytevector-s16-set! bv index value endianness
3958 @deffnx {Scheme Procedure} bytevector-u32-set! bv index value endianness
3959 @deffnx {Scheme Procedure} bytevector-s32-set! bv index value endianness
3960 @deffnx {Scheme Procedure} bytevector-u64-set! bv index value endianness
3961 @deffnx {Scheme Procedure} bytevector-s64-set! bv index value endianness
3962 @deffnx {C Function} scm_bytevector_u8_set_x (bv, index, value)
3963 @deffnx {C Function} scm_bytevector_s8_set_x (bv, index, value)
3964 @deffnx {C Function} scm_bytevector_u16_set_x (bv, index, value, endianness)
3965 @deffnx {C Function} scm_bytevector_s16_set_x (bv, index, value, endianness)
3966 @deffnx {C Function} scm_bytevector_u32_set_x (bv, index, value, endianness)
3967 @deffnx {C Function} scm_bytevector_s32_set_x (bv, index, value, endianness)
3968 @deffnx {C Function} scm_bytevector_u64_set_x (bv, index, value, endianness)
3969 @deffnx {C Function} scm_bytevector_s64_set_x (bv, index, value, endianness)
3970 Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
3971 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to
3975 Finally, a variant specialized for the host's endianness is available
3976 for each of these functions (with the exception of the @code{u8}
3977 accessors, for obvious reasons):
3979 @deffn {Scheme Procedure} bytevector-u16-native-ref bv index
3980 @deffnx {Scheme Procedure} bytevector-s16-native-ref bv index
3981 @deffnx {Scheme Procedure} bytevector-u32-native-ref bv index
3982 @deffnx {Scheme Procedure} bytevector-s32-native-ref bv index
3983 @deffnx {Scheme Procedure} bytevector-u64-native-ref bv index
3984 @deffnx {Scheme Procedure} bytevector-s64-native-ref bv index
3985 @deffnx {C Function} scm_bytevector_u16_native_ref (bv, index)
3986 @deffnx {C Function} scm_bytevector_s16_native_ref (bv, index)
3987 @deffnx {C Function} scm_bytevector_u32_native_ref (bv, index)
3988 @deffnx {C Function} scm_bytevector_s32_native_ref (bv, index)
3989 @deffnx {C Function} scm_bytevector_u64_native_ref (bv, index)
3990 @deffnx {C Function} scm_bytevector_s64_native_ref (bv, index)
3991 Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
3992 16, 32 or 64) from @var{bv} at @var{index}, decoded according to the
3993 host's native endianness.
3996 @deffn {Scheme Procedure} bytevector-u16-native-set! bv index value
3997 @deffnx {Scheme Procedure} bytevector-s16-native-set! bv index value
3998 @deffnx {Scheme Procedure} bytevector-u32-native-set! bv index value
3999 @deffnx {Scheme Procedure} bytevector-s32-native-set! bv index value
4000 @deffnx {Scheme Procedure} bytevector-u64-native-set! bv index value
4001 @deffnx {Scheme Procedure} bytevector-s64-native-set! bv index value
4002 @deffnx {C Function} scm_bytevector_u16_native_set_x (bv, index, value)
4003 @deffnx {C Function} scm_bytevector_s16_native_set_x (bv, index, value)
4004 @deffnx {C Function} scm_bytevector_u32_native_set_x (bv, index, value)
4005 @deffnx {C Function} scm_bytevector_s32_native_set_x (bv, index, value)
4006 @deffnx {C Function} scm_bytevector_u64_native_set_x (bv, index, value)
4007 @deffnx {C Function} scm_bytevector_s64_native_set_x (bv, index, value)
4008 Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
4009 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to the
4010 host's native endianness.
4014 @node Bytevectors and Integer Lists
4015 @subsubsection Converting Bytevectors to/from Integer Lists
4017 Bytevector contents can readily be converted to/from lists of signed or
4021 (bytevector->sint-list (u8-list->bytevector (make-list 4 255))
4022 (endianness little) 2)
4026 @deffn {Scheme Procedure} bytevector->u8-list bv
4027 @deffnx {C Function} scm_bytevector_to_u8_list (bv)
4028 Return a newly allocated list of unsigned 8-bit integers from the
4029 contents of @var{bv}.
4032 @deffn {Scheme Procedure} u8-list->bytevector lst
4033 @deffnx {C Function} scm_u8_list_to_bytevector (lst)
4034 Return a newly allocated bytevector consisting of the unsigned 8-bit
4035 integers listed in @var{lst}.
4038 @deffn {Scheme Procedure} bytevector->uint-list bv endianness size
4039 @deffnx {Scheme Procedure} bytevector->sint-list bv endianness size
4040 @deffnx {C Function} scm_bytevector_to_uint_list (bv, endianness, size)
4041 @deffnx {C Function} scm_bytevector_to_sint_list (bv, endianness, size)
4042 Return a list of unsigned (resp. signed) integers of @var{size} bytes
4043 representing the contents of @var{bv}, decoded according to
4047 @deffn {Scheme Procedure} uint-list->bytevector lst endianness size
4048 @deffnx {Scheme Procedure} sint-list->bytevector lst endianness size
4049 @deffnx {C Function} scm_uint_list_to_bytevector (lst, endianness, size)
4050 @deffnx {C Function} scm_sint_list_to_bytevector (lst, endianness, size)
4051 Return a new bytevector containing the unsigned (resp. signed) integers
4052 listed in @var{lst} and encoded on @var{size} bytes according to
4056 @node Bytevectors as Floats
4057 @subsubsection Interpreting Bytevector Contents as Floating Point Numbers
4059 @cindex IEEE-754 floating point numbers
4061 Bytevector contents can also be accessed as IEEE-754 single- or
4062 double-precision floating point numbers (respectively 32 and 64-bit
4063 long) using the procedures described here.
4065 @deffn {Scheme Procedure} bytevector-ieee-single-ref bv index endianness
4066 @deffnx {Scheme Procedure} bytevector-ieee-double-ref bv index endianness
4067 @deffnx {C Function} scm_bytevector_ieee_single_ref (bv, index, endianness)
4068 @deffnx {C Function} scm_bytevector_ieee_double_ref (bv, index, endianness)
4069 Return the IEEE-754 single-precision floating point number from @var{bv}
4070 at @var{index} according to @var{endianness}.
4073 @deffn {Scheme Procedure} bytevector-ieee-single-set! bv index value endianness
4074 @deffnx {Scheme Procedure} bytevector-ieee-double-set! bv index value endianness
4075 @deffnx {C Function} scm_bytevector_ieee_single_set_x (bv, index, value, endianness)
4076 @deffnx {C Function} scm_bytevector_ieee_double_set_x (bv, index, value, endianness)
4077 Store real number @var{value} in @var{bv} at @var{index} according to
4081 Specialized procedures are also available:
4083 @deffn {Scheme Procedure} bytevector-ieee-single-native-ref bv index
4084 @deffnx {Scheme Procedure} bytevector-ieee-double-native-ref bv index
4085 @deffnx {C Function} scm_bytevector_ieee_single_native_ref (bv, index)
4086 @deffnx {C Function} scm_bytevector_ieee_double_native_ref (bv, index)
4087 Return the IEEE-754 single-precision floating point number from @var{bv}
4088 at @var{index} according to the host's native endianness.
4091 @deffn {Scheme Procedure} bytevector-ieee-single-native-set! bv index value
4092 @deffnx {Scheme Procedure} bytevector-ieee-double-native-set! bv index value
4093 @deffnx {C Function} scm_bytevector_ieee_single_native_set_x (bv, index, value)
4094 @deffnx {C Function} scm_bytevector_ieee_double_native_set_x (bv, index, value)
4095 Store real number @var{value} in @var{bv} at @var{index} according to
4096 the host's native endianness.
4100 @node Bytevectors as Strings
4101 @subsubsection Interpreting Bytevector Contents as Unicode Strings
4103 @cindex Unicode string encoding
4105 Bytevector contents can also be interpreted as Unicode strings encoded
4106 in one of the most commonly available encoding formats@footnote{Guile
4107 1.8 does @emph{not} support Unicode strings. Therefore, the procedures
4108 described here assume that Guile strings are internally encoded
4109 according to the current locale. For instance, if @code{$LC_CTYPE} is
4110 @code{fr_FR.ISO-8859-1}, then @code{string->utf-8} @i{et al.} will
4111 assume that Guile strings are Latin-1-encoded.}.
4114 (utf8->string (u8-list->bytevector '(99 97 102 101)))
4117 (string->utf8 "caf@'e") ;; SMALL LATIN LETTER E WITH ACUTE ACCENT
4118 @result{} #vu8(99 97 102 195 169)
4121 @deffn {Scheme Procedure} string->utf8 str
4122 @deffnx {Scheme Procedure} string->utf16 str
4123 @deffnx {Scheme Procedure} string->utf32 str
4124 @deffnx {C Function} scm_string_to_utf8 (str)
4125 @deffnx {C Function} scm_string_to_utf16 (str)
4126 @deffnx {C Function} scm_string_to_utf32 (str)
4127 Return a newly allocated bytevector that contains the UTF-8, UTF-16, or
4128 UTF-32 (aka. UCS-4) encoding of @var{str}.
4131 @deffn {Scheme Procedure} utf8->string utf
4132 @deffnx {Scheme Procedure} utf16->string utf
4133 @deffnx {Scheme Procedure} utf32->string utf
4134 @deffnx {C Function} scm_utf8_to_string (utf)
4135 @deffnx {C Function} scm_utf16_to_string (utf)
4136 @deffnx {C Function} scm_utf32_to_string (utf)
4137 Return a newly allocated string that contains from the UTF-8-, UTF-16-,
4138 or UTF-32-decoded contents of bytevector @var{utf}.
4141 @node Bytevectors as Generalized Vectors
4142 @subsubsection Accessing Bytevectors with the Generalized Vector API
4144 As an extension to the R6RS, Guile allows bytevectors to be manipulated
4145 with the @dfn{generalized vector} procedures (@pxref{Generalized
4146 Vectors}). This also allows bytevectors to be accessed using the
4147 generic @dfn{array} procedures (@pxref{Array Procedures}). When using
4148 these APIs, bytes are accessed one at a time as 8-bit unsigned integers:
4151 (define bv #vu8(0 1 2 3))
4153 (generalized-vector? bv)
4156 (generalized-vector-ref bv 2)
4159 (generalized-vector-set! bv 2 77)
4168 @node Regular Expressions
4169 @subsection Regular Expressions
4170 @tpindex Regular expressions
4172 @cindex regular expressions
4174 @cindex emacs regexp
4176 A @dfn{regular expression} (or @dfn{regexp}) is a pattern that
4177 describes a whole class of strings. A full description of regular
4178 expressions and their syntax is beyond the scope of this manual;
4179 an introduction can be found in the Emacs manual (@pxref{Regexps,
4180 , Syntax of Regular Expressions, emacs, The GNU Emacs Manual}), or
4181 in many general Unix reference books.
4183 If your system does not include a POSIX regular expression library,
4184 and you have not linked Guile with a third-party regexp library such
4185 as Rx, these functions will not be available. You can tell whether
4186 your Guile installation includes regular expression support by
4187 checking whether @code{(provided? 'regex)} returns true.
4189 The following regexp and string matching features are provided by the
4190 @code{(ice-9 regex)} module. Before using the described functions,
4191 you should load this module by executing @code{(use-modules (ice-9
4195 * Regexp Functions:: Functions that create and match regexps.
4196 * Match Structures:: Finding what was matched by a regexp.
4197 * Backslash Escapes:: Removing the special meaning of regexp
4202 @node Regexp Functions
4203 @subsubsection Regexp Functions
4205 By default, Guile supports POSIX extended regular expressions.
4206 That means that the characters @samp{(}, @samp{)}, @samp{+} and
4207 @samp{?} are special, and must be escaped if you wish to match the
4210 This regular expression interface was modeled after that
4211 implemented by SCSH, the Scheme Shell. It is intended to be
4212 upwardly compatible with SCSH regular expressions.
4214 Zero bytes (@code{#\nul}) cannot be used in regex patterns or input
4215 strings, since the underlying C functions treat that as the end of
4216 string. If there's a zero byte an error is thrown.
4218 Patterns and input strings are treated as being in the locale
4219 character set if @code{setlocale} has been called (@pxref{Locales}),
4220 and in a multibyte locale this includes treating multi-byte sequences
4221 as a single character. (Guile strings are currently merely bytes,
4222 though this may change in the future, @xref{Conversion to/from C}.)
4224 @deffn {Scheme Procedure} string-match pattern str [start]
4225 Compile the string @var{pattern} into a regular expression and compare
4226 it with @var{str}. The optional numeric argument @var{start} specifies
4227 the position of @var{str} at which to begin matching.
4229 @code{string-match} returns a @dfn{match structure} which
4230 describes what, if anything, was matched by the regular
4231 expression. @xref{Match Structures}. If @var{str} does not match
4232 @var{pattern} at all, @code{string-match} returns @code{#f}.
4235 Two examples of a match follow. In the first example, the pattern
4236 matches the four digits in the match string. In the second, the pattern
4240 (string-match "[0-9][0-9][0-9][0-9]" "blah2002")
4241 @result{} #("blah2002" (4 . 8))
4243 (string-match "[A-Za-z]" "123456")
4247 Each time @code{string-match} is called, it must compile its
4248 @var{pattern} argument into a regular expression structure. This
4249 operation is expensive, which makes @code{string-match} inefficient if
4250 the same regular expression is used several times (for example, in a
4251 loop). For better performance, you can compile a regular expression in
4252 advance and then match strings against the compiled regexp.
4254 @deffn {Scheme Procedure} make-regexp pat flag@dots{}
4255 @deffnx {C Function} scm_make_regexp (pat, flaglst)
4256 Compile the regular expression described by @var{pat}, and
4257 return the compiled regexp structure. If @var{pat} does not
4258 describe a legal regular expression, @code{make-regexp} throws
4259 a @code{regular-expression-syntax} error.
4261 The @var{flag} arguments change the behavior of the compiled
4262 regular expression. The following values may be supplied:
4264 @defvar regexp/icase
4265 Consider uppercase and lowercase letters to be the same when
4269 @defvar regexp/newline
4270 If a newline appears in the target string, then permit the
4271 @samp{^} and @samp{$} operators to match immediately after or
4272 immediately before the newline, respectively. Also, the
4273 @samp{.} and @samp{[^...]} operators will never match a newline
4274 character. The intent of this flag is to treat the target
4275 string as a buffer containing many lines of text, and the
4276 regular expression as a pattern that may match a single one of
4280 @defvar regexp/basic
4281 Compile a basic (``obsolete'') regexp instead of the extended
4282 (``modern'') regexps that are the default. Basic regexps do
4283 not consider @samp{|}, @samp{+} or @samp{?} to be special
4284 characters, and require the @samp{@{...@}} and @samp{(...)}
4285 metacharacters to be backslash-escaped (@pxref{Backslash
4286 Escapes}). There are several other differences between basic
4287 and extended regular expressions, but these are the most
4291 @defvar regexp/extended
4292 Compile an extended regular expression rather than a basic
4293 regexp. This is the default behavior; this flag will not
4294 usually be needed. If a call to @code{make-regexp} includes
4295 both @code{regexp/basic} and @code{regexp/extended} flags, the
4296 one which comes last will override the earlier one.
4300 @deffn {Scheme Procedure} regexp-exec rx str [start [flags]]
4301 @deffnx {C Function} scm_regexp_exec (rx, str, start, flags)
4302 Match the compiled regular expression @var{rx} against
4303 @code{str}. If the optional integer @var{start} argument is
4304 provided, begin matching from that position in the string.
4305 Return a match structure describing the results of the match,
4306 or @code{#f} if no match could be found.
4308 The @var{flags} argument changes the matching behavior. The following
4309 flag values may be supplied, use @code{logior} (@pxref{Bitwise
4310 Operations}) to combine them,
4312 @defvar regexp/notbol
4313 Consider that the @var{start} offset into @var{str} is not the
4314 beginning of a line and should not match operator @samp{^}.
4316 If @var{rx} was created with the @code{regexp/newline} option above,
4317 @samp{^} will still match after a newline in @var{str}.
4320 @defvar regexp/noteol
4321 Consider that the end of @var{str} is not the end of a line and should
4322 not match operator @samp{$}.
4324 If @var{rx} was created with the @code{regexp/newline} option above,
4325 @samp{$} will still match before a newline in @var{str}.
4330 ;; Regexp to match uppercase letters
4331 (define r (make-regexp "[A-Z]*"))
4333 ;; Regexp to match letters, ignoring case
4334 (define ri (make-regexp "[A-Z]*" regexp/icase))
4336 ;; Search for bob using regexp r
4337 (match:substring (regexp-exec r "bob"))
4338 @result{} "" ; no match
4340 ;; Search for bob using regexp ri
4341 (match:substring (regexp-exec ri "Bob"))
4342 @result{} "Bob" ; matched case insensitive
4345 @deffn {Scheme Procedure} regexp? obj
4346 @deffnx {C Function} scm_regexp_p (obj)
4347 Return @code{#t} if @var{obj} is a compiled regular expression,
4348 or @code{#f} otherwise.
4352 @deffn {Scheme Procedure} list-matches regexp str [flags]
4353 Return a list of match structures which are the non-overlapping
4354 matches of @var{regexp} in @var{str}. @var{regexp} can be either a
4355 pattern string or a compiled regexp. The @var{flags} argument is as
4356 per @code{regexp-exec} above.
4359 (map match:substring (list-matches "[a-z]+" "abc 42 def 78"))
4360 @result{} ("abc" "def")
4364 @deffn {Scheme Procedure} fold-matches regexp str init proc [flags]
4365 Apply @var{proc} to the non-overlapping matches of @var{regexp} in
4366 @var{str}, to build a result. @var{regexp} can be either a pattern
4367 string or a compiled regexp. The @var{flags} argument is as per
4368 @code{regexp-exec} above.
4370 @var{proc} is called as @code{(@var{proc} match prev)} where
4371 @var{match} is a match structure and @var{prev} is the previous return
4372 from @var{proc}. For the first call @var{prev} is the given
4373 @var{init} parameter. @code{fold-matches} returns the final value
4376 For example to count matches,
4379 (fold-matches "[a-z][0-9]" "abc x1 def y2" 0
4380 (lambda (match count)
4387 Regular expressions are commonly used to find patterns in one string
4388 and replace them with the contents of another string. The following
4389 functions are convenient ways to do this.
4391 @c begin (scm-doc-string "regex.scm" "regexp-substitute")
4392 @deffn {Scheme Procedure} regexp-substitute port match [item@dots{}]
4393 Write to @var{port} selected parts of the match structure @var{match}.
4394 Or if @var{port} is @code{#f} then form a string from those parts and
4397 Each @var{item} specifies a part to be written, and may be one of the
4402 A string. String arguments are written out verbatim.
4405 An integer. The submatch with that number is written
4406 (@code{match:substring}). Zero is the entire match.
4409 The symbol @samp{pre}. The portion of the matched string preceding
4410 the regexp match is written (@code{match:prefix}).
4413 The symbol @samp{post}. The portion of the matched string following
4414 the regexp match is written (@code{match:suffix}).
4417 For example, changing a match and retaining the text before and after,
4420 (regexp-substitute #f (string-match "[0-9]+" "number 25 is good")
4422 @result{} "number 37 is good"
4425 Or matching a @sc{yyyymmdd} format date such as @samp{20020828} and
4426 re-ordering and hyphenating the fields.
4430 "([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])")
4431 (define s "Date 20020429 12am.")
4432 (regexp-substitute #f (string-match date-regex s)
4433 'pre 2 "-" 3 "-" 1 'post " (" 0 ")")
4434 @result{} "Date 04-29-2002 12am. (20020429)"
4439 @c begin (scm-doc-string "regex.scm" "regexp-substitute")
4440 @deffn {Scheme Procedure} regexp-substitute/global port regexp target [item@dots{}]
4441 @cindex search and replace
4442 Write to @var{port} selected parts of matches of @var{regexp} in
4443 @var{target}. If @var{port} is @code{#f} then form a string from
4444 those parts and return that. @var{regexp} can be a string or a
4447 This is similar to @code{regexp-substitute}, but allows global
4448 substitutions on @var{target}. Each @var{item} behaves as per
4449 @code{regexp-substitute}, with the following differences,
4453 A function. Called as @code{(@var{item} match)} with the match
4454 structure for the @var{regexp} match, it should return a string to be
4455 written to @var{port}.
4458 The symbol @samp{post}. This doesn't output anything, but instead
4459 causes @code{regexp-substitute/global} to recurse on the unmatched
4460 portion of @var{target}.
4462 This @emph{must} be supplied to perform a global search and replace on
4463 @var{target}; without it @code{regexp-substitute/global} returns after
4464 a single match and output.
4467 For example, to collapse runs of tabs and spaces to a single hyphen
4471 (regexp-substitute/global #f "[ \t]+" "this is the text"
4473 @result{} "this-is-the-text"
4476 Or using a function to reverse the letters in each word,
4479 (regexp-substitute/global #f "[a-z]+" "to do and not-do"
4480 'pre (lambda (m) (string-reverse (match:substring m))) 'post)
4481 @result{} "ot od dna ton-od"
4484 Without the @code{post} symbol, just one regexp match is made. For
4485 example the following is the date example from
4486 @code{regexp-substitute} above, without the need for the separate
4487 @code{string-match} call.
4491 "([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])")
4492 (define s "Date 20020429 12am.")
4493 (regexp-substitute/global #f date-regex s
4494 'pre 2 "-" 3 "-" 1 'post " (" 0 ")")
4496 @result{} "Date 04-29-2002 12am. (20020429)"
4501 @node Match Structures
4502 @subsubsection Match Structures
4504 @cindex match structures
4506 A @dfn{match structure} is the object returned by @code{string-match} and
4507 @code{regexp-exec}. It describes which portion of a string, if any,
4508 matched the given regular expression. Match structures include: a
4509 reference to the string that was checked for matches; the starting and
4510 ending positions of the regexp match; and, if the regexp included any
4511 parenthesized subexpressions, the starting and ending positions of each
4514 In each of the regexp match functions described below, the @code{match}
4515 argument must be a match structure returned by a previous call to
4516 @code{string-match} or @code{regexp-exec}. Most of these functions
4517 return some information about the original target string that was
4518 matched against a regular expression; we will call that string
4519 @var{target} for easy reference.
4521 @c begin (scm-doc-string "regex.scm" "regexp-match?")
4522 @deffn {Scheme Procedure} regexp-match? obj
4523 Return @code{#t} if @var{obj} is a match structure returned by a
4524 previous call to @code{regexp-exec}, or @code{#f} otherwise.
4527 @c begin (scm-doc-string "regex.scm" "match:substring")
4528 @deffn {Scheme Procedure} match:substring match [n]
4529 Return the portion of @var{target} matched by subexpression number
4530 @var{n}. Submatch 0 (the default) represents the entire regexp match.
4531 If the regular expression as a whole matched, but the subexpression
4532 number @var{n} did not match, return @code{#f}.
4536 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4540 ;; match starting at offset 6 in the string
4542 (string-match "[0-9][0-9][0-9][0-9]" "blah987654" 6))
4546 @c begin (scm-doc-string "regex.scm" "match:start")
4547 @deffn {Scheme Procedure} match:start match [n]
4548 Return the starting position of submatch number @var{n}.
4551 In the following example, the result is 4, since the match starts at
4555 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4560 @c begin (scm-doc-string "regex.scm" "match:end")
4561 @deffn {Scheme Procedure} match:end match [n]
4562 Return the ending position of submatch number @var{n}.
4565 In the following example, the result is 8, since the match runs between
4566 characters 4 and 8 (i.e. the ``2002'').
4569 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4574 @c begin (scm-doc-string "regex.scm" "match:prefix")
4575 @deffn {Scheme Procedure} match:prefix match
4576 Return the unmatched portion of @var{target} preceding the regexp match.
4579 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4585 @c begin (scm-doc-string "regex.scm" "match:suffix")
4586 @deffn {Scheme Procedure} match:suffix match
4587 Return the unmatched portion of @var{target} following the regexp match.
4591 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4596 @c begin (scm-doc-string "regex.scm" "match:count")
4597 @deffn {Scheme Procedure} match:count match
4598 Return the number of parenthesized subexpressions from @var{match}.
4599 Note that the entire regular expression match itself counts as a
4600 subexpression, and failed submatches are included in the count.
4603 @c begin (scm-doc-string "regex.scm" "match:string")
4604 @deffn {Scheme Procedure} match:string match
4605 Return the original @var{target} string.
4609 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4611 @result{} "blah2002foo"
4615 @node Backslash Escapes
4616 @subsubsection Backslash Escapes
4618 Sometimes you will want a regexp to match characters like @samp{*} or
4619 @samp{$} exactly. For example, to check whether a particular string
4620 represents a menu entry from an Info node, it would be useful to match
4621 it against a regexp like @samp{^* [^:]*::}. However, this won't work;
4622 because the asterisk is a metacharacter, it won't match the @samp{*} at
4623 the beginning of the string. In this case, we want to make the first
4626 You can do this by preceding the metacharacter with a backslash
4627 character @samp{\}. (This is also called @dfn{quoting} the
4628 metacharacter, and is known as a @dfn{backslash escape}.) When Guile
4629 sees a backslash in a regular expression, it considers the following
4630 glyph to be an ordinary character, no matter what special meaning it
4631 would ordinarily have. Therefore, we can make the above example work by
4632 changing the regexp to @samp{^\* [^:]*::}. The @samp{\*} sequence tells
4633 the regular expression engine to match only a single asterisk in the
4636 Since the backslash is itself a metacharacter, you may force a regexp to
4637 match a backslash in the target string by preceding the backslash with
4638 itself. For example, to find variable references in a @TeX{} program,
4639 you might want to find occurrences of the string @samp{\let\} followed
4640 by any number of alphabetic characters. The regular expression
4641 @samp{\\let\\[A-Za-z]*} would do this: the double backslashes in the
4642 regexp each match a single backslash in the target string.
4644 @c begin (scm-doc-string "regex.scm" "regexp-quote")
4645 @deffn {Scheme Procedure} regexp-quote str
4646 Quote each special character found in @var{str} with a backslash, and
4647 return the resulting string.
4650 @strong{Very important:} Using backslash escapes in Guile source code
4651 (as in Emacs Lisp or C) can be tricky, because the backslash character
4652 has special meaning for the Guile reader. For example, if Guile
4653 encounters the character sequence @samp{\n} in the middle of a string
4654 while processing Scheme code, it replaces those characters with a
4655 newline character. Similarly, the character sequence @samp{\t} is
4656 replaced by a horizontal tab. Several of these @dfn{escape sequences}
4657 are processed by the Guile reader before your code is executed.
4658 Unrecognized escape sequences are ignored: if the characters @samp{\*}
4659 appear in a string, they will be translated to the single character
4662 This translation is obviously undesirable for regular expressions, since
4663 we want to be able to include backslashes in a string in order to
4664 escape regexp metacharacters. Therefore, to make sure that a backslash
4665 is preserved in a string in your Guile program, you must use @emph{two}
4666 consecutive backslashes:
4669 (define Info-menu-entry-pattern (make-regexp "^\\* [^:]*"))
4672 The string in this example is preprocessed by the Guile reader before
4673 any code is executed. The resulting argument to @code{make-regexp} is
4674 the string @samp{^\* [^:]*}, which is what we really want.
4676 This also means that in order to write a regular expression that matches
4677 a single backslash character, the regular expression string in the
4678 source code must include @emph{four} backslashes. Each consecutive pair
4679 of backslashes gets translated by the Guile reader to a single
4680 backslash, and the resulting double-backslash is interpreted by the
4681 regexp engine as matching a single backslash character. Hence:
4684 (define tex-variable-pattern (make-regexp "\\\\let\\\\=[A-Za-z]*"))
4687 The reason for the unwieldiness of this syntax is historical. Both
4688 regular expression pattern matchers and Unix string processing systems
4689 have traditionally used backslashes with the special meanings
4690 described above. The POSIX regular expression specification and ANSI C
4691 standard both require these semantics. Attempting to abandon either
4692 convention would cause other kinds of compatibility problems, possibly
4693 more severe ones. Therefore, without extending the Scheme reader to
4694 support strings with different quoting conventions (an ungainly and
4695 confusing extension when implemented in other languages), we must adhere
4696 to this cumbersome escape syntax.
4703 Symbols in Scheme are widely used in three ways: as items of discrete
4704 data, as lookup keys for alists and hash tables, and to denote variable
4707 A @dfn{symbol} is similar to a string in that it is defined by a
4708 sequence of characters. The sequence of characters is known as the
4709 symbol's @dfn{name}. In the usual case --- that is, where the symbol's
4710 name doesn't include any characters that could be confused with other
4711 elements of Scheme syntax --- a symbol is written in a Scheme program by
4712 writing the sequence of characters that make up the name, @emph{without}
4713 any quotation marks or other special syntax. For example, the symbol
4714 whose name is ``multiply-by-2'' is written, simply:
4720 Notice how this differs from a @emph{string} with contents
4721 ``multiply-by-2'', which is written with double quotation marks, like
4728 Looking beyond how they are written, symbols are different from strings
4729 in two important respects.
4731 The first important difference is uniqueness. If the same-looking
4732 string is read twice from two different places in a program, the result
4733 is two @emph{different} string objects whose contents just happen to be
4734 the same. If, on the other hand, the same-looking symbol is read twice
4735 from two different places in a program, the result is the @emph{same}
4736 symbol object both times.
4738 Given two read symbols, you can use @code{eq?} to test whether they are
4739 the same (that is, have the same name). @code{eq?} is the most
4740 efficient comparison operator in Scheme, and comparing two symbols like
4741 this is as fast as comparing, for example, two numbers. Given two
4742 strings, on the other hand, you must use @code{equal?} or
4743 @code{string=?}, which are much slower comparison operators, to
4744 determine whether the strings have the same contents.
4747 (define sym1 (quote hello))
4748 (define sym2 (quote hello))
4749 (eq? sym1 sym2) @result{} #t
4751 (define str1 "hello")
4752 (define str2 "hello")
4753 (eq? str1 str2) @result{} #f
4754 (equal? str1 str2) @result{} #t
4757 The second important difference is that symbols, unlike strings, are not
4758 self-evaluating. This is why we need the @code{(quote @dots{})}s in the
4759 example above: @code{(quote hello)} evaluates to the symbol named
4760 "hello" itself, whereas an unquoted @code{hello} is @emph{read} as the
4761 symbol named "hello" and evaluated as a variable reference @dots{} about
4762 which more below (@pxref{Symbol Variables}).
4765 * Symbol Data:: Symbols as discrete data.
4766 * Symbol Keys:: Symbols as lookup keys.
4767 * Symbol Variables:: Symbols as denoting variables.
4768 * Symbol Primitives:: Operations related to symbols.
4769 * Symbol Props:: Function slots and property lists.
4770 * Symbol Read Syntax:: Extended read syntax for symbols.
4771 * Symbol Uninterned:: Uninterned symbols.
4776 @subsubsection Symbols as Discrete Data
4778 Numbers and symbols are similar to the extent that they both lend
4779 themselves to @code{eq?} comparison. But symbols are more descriptive
4780 than numbers, because a symbol's name can be used directly to describe
4781 the concept for which that symbol stands.
4783 For example, imagine that you need to represent some colours in a
4784 computer program. Using numbers, you would have to choose arbitrarily
4785 some mapping between numbers and colours, and then take care to use that
4786 mapping consistently:
4789 ;; 1=red, 2=green, 3=purple
4791 (if (eq? (colour-of car) 1)
4796 You can make the mapping more explicit and the code more readable by
4804 (if (eq? (colour-of car) red)
4809 But the simplest and clearest approach is not to use numbers at all, but
4810 symbols whose names specify the colours that they refer to:
4813 (if (eq? (colour-of car) 'red)
4817 The descriptive advantages of symbols over numbers increase as the set
4818 of concepts that you want to describe grows. Suppose that a car object
4819 can have other properties as well, such as whether it has or uses:
4823 automatic or manual transmission
4825 leaded or unleaded fuel
4827 power steering (or not).
4831 Then a car's combined property set could be naturally represented and
4832 manipulated as a list of symbols:
4835 (properties-of car1)
4837 (red manual unleaded power-steering)
4839 (if (memq 'power-steering (properties-of car1))
4840 (display "Unfit people can drive this car.\n")
4841 (display "You'll need strong arms to drive this car!\n"))
4843 Unfit people can drive this car.
4846 Remember, the fundamental property of symbols that we are relying on
4847 here is that an occurrence of @code{'red} in one part of a program is an
4848 @emph{indistinguishable} symbol from an occurrence of @code{'red} in
4849 another part of a program; this means that symbols can usefully be
4850 compared using @code{eq?}. At the same time, symbols have naturally
4851 descriptive names. This combination of efficiency and descriptive power
4852 makes them ideal for use as discrete data.
4856 @subsubsection Symbols as Lookup Keys
4858 Given their efficiency and descriptive power, it is natural to use
4859 symbols as the keys in an association list or hash table.
4861 To illustrate this, consider a more structured representation of the car
4862 properties example from the preceding subsection. Rather than
4863 mixing all the properties up together in a flat list, we could use an
4864 association list like this:
4867 (define car1-properties '((colour . red)
4868 (transmission . manual)
4870 (steering . power-assisted)))
4873 Notice how this structure is more explicit and extensible than the flat
4874 list. For example it makes clear that @code{manual} refers to the
4875 transmission rather than, say, the windows or the locking of the car.
4876 It also allows further properties to use the same symbols among their
4877 possible values without becoming ambiguous:
4880 (define car1-properties '((colour . red)
4881 (transmission . manual)
4883 (steering . power-assisted)
4885 (locking . manual)))
4888 With a representation like this, it is easy to use the efficient
4889 @code{assq-XXX} family of procedures (@pxref{Association Lists}) to
4890 extract or change individual pieces of information:
4893 (assq-ref car1-properties 'fuel) @result{} unleaded
4894 (assq-ref car1-properties 'transmission) @result{} manual
4896 (assq-set! car1-properties 'seat-colour 'black)
4899 (transmission . manual)
4901 (steering . power-assisted)
4902 (seat-colour . black)
4903 (locking . manual)))
4906 Hash tables also have keys, and exactly the same arguments apply to the
4907 use of symbols in hash tables as in association lists. The hash value
4908 that Guile uses to decide where to add a symbol-keyed entry to a hash
4909 table can be obtained by calling the @code{symbol-hash} procedure:
4911 @deffn {Scheme Procedure} symbol-hash symbol
4912 @deffnx {C Function} scm_symbol_hash (symbol)
4913 Return a hash value for @var{symbol}.
4916 See @ref{Hash Tables} for information about hash tables in general, and
4917 for why you might choose to use a hash table rather than an association
4921 @node Symbol Variables
4922 @subsubsection Symbols as Denoting Variables
4924 When an unquoted symbol in a Scheme program is evaluated, it is
4925 interpreted as a variable reference, and the result of the evaluation is
4926 the appropriate variable's value.
4928 For example, when the expression @code{(string-length "abcd")} is read
4929 and evaluated, the sequence of characters @code{string-length} is read
4930 as the symbol whose name is "string-length". This symbol is associated
4931 with a variable whose value is the procedure that implements string
4932 length calculation. Therefore evaluation of the @code{string-length}
4933 symbol results in that procedure.
4935 The details of the connection between an unquoted symbol and the
4936 variable to which it refers are explained elsewhere. See @ref{Binding
4937 Constructs}, for how associations between symbols and variables are
4938 created, and @ref{Modules}, for how those associations are affected by
4939 Guile's module system.
4942 @node Symbol Primitives
4943 @subsubsection Operations Related to Symbols
4945 Given any Scheme value, you can determine whether it is a symbol using
4946 the @code{symbol?} primitive:
4949 @deffn {Scheme Procedure} symbol? obj
4950 @deffnx {C Function} scm_symbol_p (obj)
4951 Return @code{#t} if @var{obj} is a symbol, otherwise return
4955 @deftypefn {C Function} int scm_is_symbol (SCM val)
4956 Equivalent to @code{scm_is_true (scm_symbol_p (val))}.
4959 Once you know that you have a symbol, you can obtain its name as a
4960 string by calling @code{symbol->string}. Note that Guile differs by
4961 default from R5RS on the details of @code{symbol->string} as regards
4964 @rnindex symbol->string
4965 @deffn {Scheme Procedure} symbol->string s
4966 @deffnx {C Function} scm_symbol_to_string (s)
4967 Return the name of symbol @var{s} as a string. By default, Guile reads
4968 symbols case-sensitively, so the string returned will have the same case
4969 variation as the sequence of characters that caused @var{s} to be
4972 If Guile is set to read symbols case-insensitively (as specified by
4973 R5RS), and @var{s} comes into being as part of a literal expression
4974 (@pxref{Literal expressions,,,r5rs, The Revised^5 Report on Scheme}) or
4975 by a call to the @code{read} or @code{string-ci->symbol} procedures,
4976 Guile converts any alphabetic characters in the symbol's name to
4977 lower case before creating the symbol object, so the string returned
4978 here will be in lower case.
4980 If @var{s} was created by @code{string->symbol}, the case of characters
4981 in the string returned will be the same as that in the string that was
4982 passed to @code{string->symbol}, regardless of Guile's case-sensitivity
4983 setting at the time @var{s} was created.
4985 It is an error to apply mutation procedures like @code{string-set!} to
4986 strings returned by this procedure.
4989 Most symbols are created by writing them literally in code. However it
4990 is also possible to create symbols programmatically using the following
4991 @code{string->symbol} and @code{string-ci->symbol} procedures:
4993 @rnindex string->symbol
4994 @deffn {Scheme Procedure} string->symbol string
4995 @deffnx {C Function} scm_string_to_symbol (string)
4996 Return the symbol whose name is @var{string}. This procedure can create
4997 symbols with names containing special characters or letters in the
4998 non-standard case, but it is usually a bad idea to create such symbols
4999 because in some implementations of Scheme they cannot be read as
5003 @deffn {Scheme Procedure} string-ci->symbol str
5004 @deffnx {C Function} scm_string_ci_to_symbol (str)
5005 Return the symbol whose name is @var{str}. If Guile is currently
5006 reading symbols case-insensitively, @var{str} is converted to lowercase
5007 before the returned symbol is looked up or created.
5010 The following examples illustrate Guile's detailed behaviour as regards
5011 the case-sensitivity of symbols:
5014 (read-enable 'case-insensitive) ; R5RS compliant behaviour
5016 (symbol->string 'flying-fish) @result{} "flying-fish"
5017 (symbol->string 'Martin) @result{} "martin"
5019 (string->symbol "Malvina")) @result{} "Malvina"
5021 (eq? 'mISSISSIppi 'mississippi) @result{} #t
5022 (string->symbol "mISSISSIppi") @result{} mISSISSIppi
5023 (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
5025 (string->symbol (symbol->string 'LolliPop))) @result{} #t
5026 (string=? "K. Harper, M.D."
5028 (string->symbol "K. Harper, M.D."))) @result{} #t
5030 (read-disable 'case-insensitive) ; Guile default behaviour
5032 (symbol->string 'flying-fish) @result{} "flying-fish"
5033 (symbol->string 'Martin) @result{} "Martin"
5035 (string->symbol "Malvina")) @result{} "Malvina"
5037 (eq? 'mISSISSIppi 'mississippi) @result{} #f
5038 (string->symbol "mISSISSIppi") @result{} mISSISSIppi
5039 (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #t
5041 (string->symbol (symbol->string 'LolliPop))) @result{} #t
5042 (string=? "K. Harper, M.D."
5044 (string->symbol "K. Harper, M.D."))) @result{} #t
5047 From C, there are lower level functions that construct a Scheme symbol
5048 from a C string in the current locale encoding.
5050 When you want to do more from C, you should convert between symbols
5051 and strings using @code{scm_symbol_to_string} and
5052 @code{scm_string_to_symbol} and work with the strings.
5054 @deffn {C Function} scm_from_locale_symbol (const char *name)
5055 @deffnx {C Function} scm_from_locale_symboln (const char *name, size_t len)
5056 Construct and return a Scheme symbol whose name is specified by
5057 @var{name}. For @code{scm_from_locale_symbol}, @var{name} must be null
5058 terminated; for @code{scm_from_locale_symboln} the length of @var{name} is
5059 specified explicitly by @var{len}.
5062 @deftypefn {C Function} SCM scm_take_locale_symbol (char *str)
5063 @deftypefnx {C Function} SCM scm_take_locale_symboln (char *str, size_t len)
5064 Like @code{scm_from_locale_symbol} and @code{scm_from_locale_symboln},
5065 respectively, but also frees @var{str} with @code{free} eventually.
5066 Thus, you can use this function when you would free @var{str} anyway
5067 immediately after creating the Scheme string. In certain cases, Guile
5068 can then use @var{str} directly as its internal representation.
5071 The size of a symbol can also be obtained from C:
5073 @deftypefn {C Function} size_t scm_c_symbol_length (SCM sym)
5074 Return the number of characters in @var{sym}.
5077 Finally, some applications, especially those that generate new Scheme
5078 code dynamically, need to generate symbols for use in the generated
5079 code. The @code{gensym} primitive meets this need:
5081 @deffn {Scheme Procedure} gensym [prefix]
5082 @deffnx {C Function} scm_gensym (prefix)
5083 Create a new symbol with a name constructed from a prefix and a counter
5084 value. The string @var{prefix} can be specified as an optional
5085 argument. Default prefix is @samp{@w{ g}}. The counter is increased by 1
5086 at each call. There is no provision for resetting the counter.
5089 The symbols generated by @code{gensym} are @emph{likely} to be unique,
5090 since their names begin with a space and it is only otherwise possible
5091 to generate such symbols if a programmer goes out of their way to do
5092 so. Uniqueness can be guaranteed by instead using uninterned symbols
5093 (@pxref{Symbol Uninterned}), though they can't be usefully written out
5098 @subsubsection Function Slots and Property Lists
5100 In traditional Lisp dialects, symbols are often understood as having
5101 three kinds of value at once:
5105 a @dfn{variable} value, which is used when the symbol appears in
5106 code in a variable reference context
5109 a @dfn{function} value, which is used when the symbol appears in
5110 code in a function name position (i.e. as the first element in an
5114 a @dfn{property list} value, which is used when the symbol is given as
5115 the first argument to Lisp's @code{put} or @code{get} functions.
5118 Although Scheme (as one of its simplifications with respect to Lisp)
5119 does away with the distinction between variable and function namespaces,
5120 Guile currently retains some elements of the traditional structure in
5121 case they turn out to be useful when implementing translators for other
5122 languages, in particular Emacs Lisp.
5124 Specifically, Guile symbols have two extra slots. for a symbol's
5125 property list, and for its ``function value.'' The following procedures
5126 are provided to access these slots.
5128 @deffn {Scheme Procedure} symbol-fref symbol
5129 @deffnx {C Function} scm_symbol_fref (symbol)
5130 Return the contents of @var{symbol}'s @dfn{function slot}.
5133 @deffn {Scheme Procedure} symbol-fset! symbol value
5134 @deffnx {C Function} scm_symbol_fset_x (symbol, value)
5135 Set the contents of @var{symbol}'s function slot to @var{value}.
5138 @deffn {Scheme Procedure} symbol-pref symbol
5139 @deffnx {C Function} scm_symbol_pref (symbol)
5140 Return the @dfn{property list} currently associated with @var{symbol}.
5143 @deffn {Scheme Procedure} symbol-pset! symbol value
5144 @deffnx {C Function} scm_symbol_pset_x (symbol, value)
5145 Set @var{symbol}'s property list to @var{value}.
5148 @deffn {Scheme Procedure} symbol-property sym prop
5149 From @var{sym}'s property list, return the value for property
5150 @var{prop}. The assumption is that @var{sym}'s property list is an
5151 association list whose keys are distinguished from each other using
5152 @code{equal?}; @var{prop} should be one of the keys in that list. If
5153 the property list has no entry for @var{prop}, @code{symbol-property}
5157 @deffn {Scheme Procedure} set-symbol-property! sym prop val
5158 In @var{sym}'s property list, set the value for property @var{prop} to
5159 @var{val}, or add a new entry for @var{prop}, with value @var{val}, if
5160 none already exists. For the structure of the property list, see
5161 @code{symbol-property}.
5164 @deffn {Scheme Procedure} symbol-property-remove! sym prop
5165 From @var{sym}'s property list, remove the entry for property
5166 @var{prop}, if there is one. For the structure of the property list,
5167 see @code{symbol-property}.
5170 Support for these extra slots may be removed in a future release, and it
5171 is probably better to avoid using them. For a more modern and Schemely
5172 approach to properties, see @ref{Object Properties}.
5175 @node Symbol Read Syntax
5176 @subsubsection Extended Read Syntax for Symbols
5178 The read syntax for a symbol is a sequence of letters, digits, and
5179 @dfn{extended alphabetic characters}, beginning with a character that
5180 cannot begin a number. In addition, the special cases of @code{+},
5181 @code{-}, and @code{...} are read as symbols even though numbers can
5182 begin with @code{+}, @code{-} or @code{.}.
5184 Extended alphabetic characters may be used within identifiers as if
5185 they were letters. The set of extended alphabetic characters is:
5188 ! $ % & * + - . / : < = > ? @@ ^ _ ~
5191 In addition to the standard read syntax defined above (which is taken
5192 from R5RS (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on
5193 Scheme})), Guile provides an extended symbol read syntax that allows the
5194 inclusion of unusual characters such as space characters, newlines and
5195 parentheses. If (for whatever reason) you need to write a symbol
5196 containing characters not mentioned above, you can do so as follows.
5200 Begin the symbol with the characters @code{#@{},
5203 write the characters of the symbol and
5206 finish the symbol with the characters @code{@}#}.
5209 Here are a few examples of this form of read syntax. The first symbol
5210 needs to use extended syntax because it contains a space character, the
5211 second because it contains a line break, and the last because it looks
5223 Although Guile provides this extended read syntax for symbols,
5224 widespread usage of it is discouraged because it is not portable and not
5228 @node Symbol Uninterned
5229 @subsubsection Uninterned Symbols
5231 What makes symbols useful is that they are automatically kept unique.
5232 There are no two symbols that are distinct objects but have the same
5233 name. But of course, there is no rule without exception. In addition
5234 to the normal symbols that have been discussed up to now, you can also
5235 create special @dfn{uninterned} symbols that behave slightly
5238 To understand what is different about them and why they might be useful,
5239 we look at how normal symbols are actually kept unique.
5241 Whenever Guile wants to find the symbol with a specific name, for
5242 example during @code{read} or when executing @code{string->symbol}, it
5243 first looks into a table of all existing symbols to find out whether a
5244 symbol with the given name already exists. When this is the case, Guile
5245 just returns that symbol. When not, a new symbol with the name is
5246 created and entered into the table so that it can be found later.
5248 Sometimes you might want to create a symbol that is guaranteed `fresh',
5249 i.e. a symbol that did not exist previously. You might also want to
5250 somehow guarantee that no one else will ever unintentionally stumble
5251 across your symbol in the future. These properties of a symbol are
5252 often needed when generating code during macro expansion. When
5253 introducing new temporary variables, you want to guarantee that they
5254 don't conflict with variables in other people's code.
5256 The simplest way to arrange for this is to create a new symbol but
5257 not enter it into the global table of all symbols. That way, no one
5258 will ever get access to your symbol by chance. Symbols that are not in
5259 the table are called @dfn{uninterned}. Of course, symbols that
5260 @emph{are} in the table are called @dfn{interned}.
5262 You create new uninterned symbols with the function @code{make-symbol}.
5263 You can test whether a symbol is interned or not with
5264 @code{symbol-interned?}.
5266 Uninterned symbols break the rule that the name of a symbol uniquely
5267 identifies the symbol object. Because of this, they can not be written
5268 out and read back in like interned symbols. Currently, Guile has no
5269 support for reading uninterned symbols. Note that the function
5270 @code{gensym} does not return uninterned symbols for this reason.
5272 @deffn {Scheme Procedure} make-symbol name
5273 @deffnx {C Function} scm_make_symbol (name)
5274 Return a new uninterned symbol with the name @var{name}. The returned
5275 symbol is guaranteed to be unique and future calls to
5276 @code{string->symbol} will not return it.
5279 @deffn {Scheme Procedure} symbol-interned? symbol
5280 @deffnx {C Function} scm_symbol_interned_p (symbol)
5281 Return @code{#t} if @var{symbol} is interned, otherwise return
5288 (define foo-1 (string->symbol "foo"))
5289 (define foo-2 (string->symbol "foo"))
5290 (define foo-3 (make-symbol "foo"))
5291 (define foo-4 (make-symbol "foo"))
5295 ; Two interned symbols with the same name are the same object,
5299 ; but a call to make-symbol with the same name returns a
5304 ; A call to make-symbol always returns a new object, even for
5308 @result{} #<uninterned-symbol foo 8085290>
5309 ; Uninterned symbols print differently from interned symbols,
5313 ; but they are still symbols,
5315 (symbol-interned? foo-3)
5317 ; just not interned.
5322 @subsection Keywords
5325 Keywords are self-evaluating objects with a convenient read syntax that
5326 makes them easy to type.
5328 Guile's keyword support conforms to R5RS, and adds a (switchable) read
5329 syntax extension to permit keywords to begin with @code{:} as well as
5330 @code{#:}, or to end with @code{:}.
5333 * Why Use Keywords?:: Motivation for keyword usage.
5334 * Coding With Keywords:: How to use keywords.
5335 * Keyword Read Syntax:: Read syntax for keywords.
5336 * Keyword Procedures:: Procedures for dealing with keywords.
5339 @node Why Use Keywords?
5340 @subsubsection Why Use Keywords?
5342 Keywords are useful in contexts where a program or procedure wants to be
5343 able to accept a large number of optional arguments without making its
5344 interface unmanageable.
5346 To illustrate this, consider a hypothetical @code{make-window}
5347 procedure, which creates a new window on the screen for drawing into
5348 using some graphical toolkit. There are many parameters that the caller
5349 might like to specify, but which could also be sensibly defaulted, for
5354 color depth -- Default: the color depth for the screen
5357 background color -- Default: white
5360 width -- Default: 600
5363 height -- Default: 400
5366 If @code{make-window} did not use keywords, the caller would have to
5367 pass in a value for each possible argument, remembering the correct
5368 argument order and using a special value to indicate the default value
5372 (make-window 'default ;; Color depth
5373 'default ;; Background color
5376 @dots{}) ;; More make-window arguments
5379 With keywords, on the other hand, defaulted arguments are omitted, and
5380 non-default arguments are clearly tagged by the appropriate keyword. As
5381 a result, the invocation becomes much clearer:
5384 (make-window #:width 800 #:height 100)
5387 On the other hand, for a simpler procedure with few arguments, the use
5388 of keywords would be a hindrance rather than a help. The primitive
5389 procedure @code{cons}, for example, would not be improved if it had to
5393 (cons #:car x #:cdr y)
5396 So the decision whether to use keywords or not is purely pragmatic: use
5397 them if they will clarify the procedure invocation at point of call.
5399 @node Coding With Keywords
5400 @subsubsection Coding With Keywords
5402 If a procedure wants to support keywords, it should take a rest argument
5403 and then use whatever means is convenient to extract keywords and their
5404 corresponding arguments from the contents of that rest argument.
5406 The following example illustrates the principle: the code for
5407 @code{make-window} uses a helper procedure called
5408 @code{get-keyword-value} to extract individual keyword arguments from
5412 (define (get-keyword-value args keyword default)
5413 (let ((kv (memq keyword args)))
5414 (if (and kv (>= (length kv) 2))
5418 (define (make-window . args)
5419 (let ((depth (get-keyword-value args #:depth screen-depth))
5420 (bg (get-keyword-value args #:bg "white"))
5421 (width (get-keyword-value args #:width 800))
5422 (height (get-keyword-value args #:height 100))
5427 But you don't need to write @code{get-keyword-value}. The @code{(ice-9
5428 optargs)} module provides a set of powerful macros that you can use to
5429 implement keyword-supporting procedures like this:
5432 (use-modules (ice-9 optargs))
5434 (define (make-window . args)
5435 (let-keywords args #f ((depth screen-depth)
5443 Or, even more economically, like this:
5446 (use-modules (ice-9 optargs))
5448 (define* (make-window #:key (depth screen-depth)
5455 For further details on @code{let-keywords}, @code{define*} and other
5456 facilities provided by the @code{(ice-9 optargs)} module, see
5457 @ref{Optional Arguments}.
5460 @node Keyword Read Syntax
5461 @subsubsection Keyword Read Syntax
5463 Guile, by default, only recognizes a keyword syntax that is compatible
5464 with R5RS. A token of the form @code{#:NAME}, where @code{NAME} has the
5465 same syntax as a Scheme symbol (@pxref{Symbol Read Syntax}), is the
5466 external representation of the keyword named @code{NAME}. Keyword
5467 objects print using this syntax as well, so values containing keyword
5468 objects can be read back into Guile. When used in an expression,
5469 keywords are self-quoting objects.
5471 If the @code{keyword} read option is set to @code{'prefix}, Guile also
5472 recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
5473 of the form @code{:NAME} are read as symbols, as required by R5RS.
5475 @cindex SRFI-88 keyword syntax
5477 If the @code{keyword} read option is set to @code{'postfix}, Guile
5478 recognizes the SRFI-88 read syntax @code{NAME:} (@pxref{SRFI-88}).
5479 Otherwise, tokens of this form are read as symbols.
5481 To enable and disable the alternative non-R5RS keyword syntax, you use
5482 the @code{read-set!} procedure documented in @ref{User level options
5483 interfaces} and @ref{Reader options}. Note that the @code{prefix} and
5484 @code{postfix} syntax are mutually exclusive.
5487 (read-set! keywords 'prefix)
5497 (read-set! keywords 'postfix)
5507 (read-set! keywords #f)
5515 ERROR: In expression :type:
5516 ERROR: Unbound variable: :type
5517 ABORT: (unbound-variable)
5520 @node Keyword Procedures
5521 @subsubsection Keyword Procedures
5523 @deffn {Scheme Procedure} keyword? obj
5524 @deffnx {C Function} scm_keyword_p (obj)
5525 Return @code{#t} if the argument @var{obj} is a keyword, else
5529 @deffn {Scheme Procedure} keyword->symbol keyword
5530 @deffnx {C Function} scm_keyword_to_symbol (keyword)
5531 Return the symbol with the same name as @var{keyword}.
5534 @deffn {Scheme Procedure} symbol->keyword symbol
5535 @deffnx {C Function} scm_symbol_to_keyword (symbol)
5536 Return the keyword with the same name as @var{symbol}.
5539 @deftypefn {C Function} int scm_is_keyword (SCM obj)
5540 Equivalent to @code{scm_is_true (scm_keyword_p (@var{obj}))}.
5543 @deftypefn {C Function} SCM scm_from_locale_keyword (const char *str)
5544 @deftypefnx {C Function} SCM scm_from_locale_keywordn (const char *str, size_t len)
5545 Equivalent to @code{scm_symbol_to_keyword (scm_from_locale_symbol
5546 (@var{str}))} and @code{scm_symbol_to_keyword (scm_from_locale_symboln
5547 (@var{str}, @var{len}))}, respectively.
5551 @subsection ``Functionality-Centric'' Data Types
5553 Procedures and macros are documented in their own chapter: see
5554 @ref{Procedures and Macros}.
5556 Variable objects are documented as part of the description of Guile's
5557 module system: see @ref{Variables}.
5559 Asyncs, dynamic roots and fluids are described in the chapter on
5560 scheduling: see @ref{Scheduling}.
5562 Hooks are documented in the chapter on general utility functions: see
5565 Ports are described in the chapter on I/O: see @ref{Input and Output}.
5569 @c TeX-master: "guile.texi"