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, 2010, 2011
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
7 @node Simple Data Types
8 @section Simple Generic Data Types
10 This chapter describes those of Guile's simple data types which are
11 primarily used for their role as items of generic data. By
12 @dfn{simple} we mean data types that are not primarily used as
13 containers to hold other data --- i.e.@: pairs, lists, vectors and so on.
14 For the documentation of such @dfn{compound} data types, see
15 @ref{Compound Data Types}.
17 @c One of the great strengths of Scheme is that there is no straightforward
18 @c distinction between ``data'' and ``functionality''. For example,
19 @c Guile's support for dynamic linking could be described:
23 @c either in a ``data-centric'' way, as the behaviour and properties of the
24 @c ``dynamically linked object'' data type, and the operations that may be
25 @c applied to instances of this type
28 @c or in a ``functionality-centric'' way, as the set of procedures that
29 @c constitute Guile's support for dynamic linking, in the context of the
33 @c The contents of this chapter are, therefore, a matter of judgment. By
34 @c @dfn{generic}, we mean to select those data types whose typical use as
35 @c @emph{data} in a wide variety of programming contexts is more important
36 @c than their use in the implementation of a particular piece of
37 @c @emph{functionality}. The last section of this chapter provides
38 @c references for all the data types that are documented not here but in a
39 @c ``functionality-centric'' way elsewhere in the manual.
42 * Booleans:: True/false values.
43 * Numbers:: Numerical data types.
44 * Characters:: Single characters.
45 * Character Sets:: Sets of characters.
46 * Strings:: Sequences of characters.
47 * Bytevectors:: Sequences of bytes.
49 * Keywords:: Self-quoting, customizable display keywords.
50 * Other Types:: "Functionality-centric" data types.
58 The two boolean values are @code{#t} for true and @code{#f} for false.
60 Boolean values are returned by predicate procedures, such as the general
61 equality predicates @code{eq?}, @code{eqv?} and @code{equal?}
62 (@pxref{Equality}) and numerical and string comparison operators like
63 @code{string=?} (@pxref{String Comparison}) and @code{<=}
73 (equal? "house" "houses")
81 In test condition contexts like @code{if} and @code{cond} (@pxref{if
82 cond case}), where a group of subexpressions will be evaluated only if a
83 @var{condition} expression evaluates to ``true'', ``true'' means any
84 value at all except @code{#f}.
97 A result of this asymmetry is that typical Scheme source code more often
98 uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to
99 represent an @code{if} or @code{cond} false value, whereas @code{#t} is
100 not necessary to represent an @code{if} or @code{cond} true value.
102 It is important to note that @code{#f} is @strong{not} equivalent to any
103 other Scheme value. In particular, @code{#f} is not the same as the
104 number 0 (like in C and C++), and not the same as the ``empty list''
105 (like in some Lisp dialects).
107 In C, the two Scheme boolean values are available as the two constants
108 @code{SCM_BOOL_T} for @code{#t} and @code{SCM_BOOL_F} for @code{#f}.
109 Care must be taken with the false value @code{SCM_BOOL_F}: it is not
110 false when used in C conditionals. In order to test for it, use
111 @code{scm_is_false} or @code{scm_is_true}.
114 @deffn {Scheme Procedure} not x
115 @deffnx {C Function} scm_not (x)
116 Return @code{#t} if @var{x} is @code{#f}, else return @code{#f}.
120 @deffn {Scheme Procedure} boolean? obj
121 @deffnx {C Function} scm_boolean_p (obj)
122 Return @code{#t} if @var{obj} is either @code{#t} or @code{#f}, else
126 @deftypevr {C Macro} SCM SCM_BOOL_T
127 The @code{SCM} representation of the Scheme object @code{#t}.
130 @deftypevr {C Macro} SCM SCM_BOOL_F
131 The @code{SCM} representation of the Scheme object @code{#f}.
134 @deftypefn {C Function} int scm_is_true (SCM obj)
135 Return @code{0} if @var{obj} is @code{#f}, else return @code{1}.
138 @deftypefn {C Function} int scm_is_false (SCM obj)
139 Return @code{1} if @var{obj} is @code{#f}, else return @code{0}.
142 @deftypefn {C Function} int scm_is_bool (SCM obj)
143 Return @code{1} if @var{obj} is either @code{#t} or @code{#f}, else
147 @deftypefn {C Function} SCM scm_from_bool (int val)
148 Return @code{#f} if @var{val} is @code{0}, else return @code{#t}.
151 @deftypefn {C Function} int scm_to_bool (SCM val)
152 Return @code{1} if @var{val} is @code{SCM_BOOL_T}, return @code{0}
153 when @var{val} is @code{SCM_BOOL_F}, else signal a `wrong type' error.
155 You should probably use @code{scm_is_true} instead of this function
156 when you just want to test a @code{SCM} value for trueness.
160 @subsection Numerical data types
163 Guile supports a rich ``tower'' of numerical types --- integer,
164 rational, real and complex --- and provides an extensive set of
165 mathematical and scientific functions for operating on numerical
166 data. This section of the manual documents those types and functions.
168 You may also find it illuminating to read R5RS's presentation of numbers
169 in Scheme, which is particularly clear and accessible: see
170 @ref{Numbers,,,r5rs,R5RS}.
173 * Numerical Tower:: Scheme's numerical "tower".
174 * Integers:: Whole numbers.
175 * Reals and Rationals:: Real and rational numbers.
176 * Complex Numbers:: Complex numbers.
177 * Exactness:: Exactness and inexactness.
178 * Number Syntax:: Read syntax for numerical data.
179 * Integer Operations:: Operations on integer values.
180 * Comparison:: Comparison predicates.
181 * Conversion:: Converting numbers to and from strings.
182 * Complex:: Complex number operations.
183 * Arithmetic:: Arithmetic functions.
184 * Scientific:: Scientific functions.
185 * Bitwise Operations:: Logical AND, OR, NOT, and so on.
186 * Random:: Random number generation.
190 @node Numerical Tower
191 @subsubsection Scheme's Numerical ``Tower''
194 Scheme's numerical ``tower'' consists of the following categories of
199 Whole numbers, positive or negative; e.g.@: --5, 0, 18.
202 The set of numbers that can be expressed as @math{@var{p}/@var{q}}
203 where @var{p} and @var{q} are integers; e.g.@: @math{9/16} works, but
204 pi (an irrational number) doesn't. These include integers
208 The set of numbers that describes all possible positions along a
209 one-dimensional line. This includes rationals as well as irrational
212 @item complex numbers
213 The set of numbers that describes all possible positions in a two
214 dimensional space. This includes real as well as imaginary numbers
215 (@math{@var{a}+@var{b}i}, where @var{a} is the @dfn{real part},
216 @var{b} is the @dfn{imaginary part}, and @math{i} is the square root of
220 It is called a tower because each category ``sits on'' the one that
221 follows it, in the sense that every integer is also a rational, every
222 rational is also real, and every real number is also a complex number
223 (but with zero imaginary part).
225 In addition to the classification into integers, rationals, reals and
226 complex numbers, Scheme also distinguishes between whether a number is
227 represented exactly or not. For example, the result of
228 @m{2\sin(\pi/4),2*sin(pi/4)} is exactly @m{\sqrt{2},2^(1/2)}, but Guile
229 can represent neither @m{\pi/4,pi/4} nor @m{\sqrt{2},2^(1/2)} exactly.
230 Instead, it stores an inexact approximation, using the C type
233 Guile can represent exact rationals of any magnitude, inexact
234 rationals that fit into a C @code{double}, and inexact complex numbers
235 with @code{double} real and imaginary parts.
237 The @code{number?} predicate may be applied to any Scheme value to
238 discover whether the value is any of the supported numerical types.
240 @deffn {Scheme Procedure} number? obj
241 @deffnx {C Function} scm_number_p (obj)
242 Return @code{#t} if @var{obj} is any kind of number, else @code{#f}.
251 (number? "hello there!")
254 (define pi 3.141592654)
259 @deftypefn {C Function} int scm_is_number (SCM obj)
260 This is equivalent to @code{scm_is_true (scm_number_p (obj))}.
263 The next few subsections document each of Guile's numerical data types
267 @subsubsection Integers
269 @tpindex Integer numbers
273 Integers are whole numbers, that is numbers with no fractional part,
274 such as 2, 83, and @minus{}3789.
276 Integers in Guile can be arbitrarily big, as shown by the following
280 (define (factorial n)
281 (let loop ((n n) (product 1))
284 (loop (- n 1) (* product n)))))
290 @result{} 2432902008176640000
293 @result{} -119622220865480194561963161495657715064383733760000000000
296 Readers whose background is in programming languages where integers are
297 limited by the need to fit into just 4 or 8 bytes of memory may find
298 this surprising, or suspect that Guile's representation of integers is
299 inefficient. In fact, Guile achieves a near optimal balance of
300 convenience and efficiency by using the host computer's native
301 representation of integers where possible, and a more general
302 representation where the required number does not fit in the native
303 form. Conversion between these two representations is automatic and
304 completely invisible to the Scheme level programmer.
306 C has a host of different integer types, and Guile offers a host of
307 functions to convert between them and the @code{SCM} representation.
308 For example, a C @code{int} can be handled with @code{scm_to_int} and
309 @code{scm_from_int}. Guile also defines a few C integer types of its
310 own, to help with differences between systems.
312 C integer types that are not covered can be handled with the generic
313 @code{scm_to_signed_integer} and @code{scm_from_signed_integer} for
314 signed types, or with @code{scm_to_unsigned_integer} and
315 @code{scm_from_unsigned_integer} for unsigned types.
317 Scheme integers can be exact and inexact. For example, a number
318 written as @code{3.0} with an explicit decimal-point is inexact, but
319 it is also an integer. The functions @code{integer?} and
320 @code{scm_is_integer} report true for such a number, but the functions
321 @code{scm_is_signed_integer} and @code{scm_is_unsigned_integer} only
322 allow exact integers and thus report false. Likewise, the conversion
323 functions like @code{scm_to_signed_integer} only accept exact
326 The motivation for this behavior is that the inexactness of a number
327 should not be lost silently. If you want to allow inexact integers,
328 you can explicitly insert a call to @code{inexact->exact} or to its C
329 equivalent @code{scm_inexact_to_exact}. (Only inexact integers will
330 be converted by this call into exact integers; inexact non-integers
331 will become exact fractions.)
333 @deffn {Scheme Procedure} integer? x
334 @deffnx {C Function} scm_integer_p (x)
335 Return @code{#t} if @var{x} is an exact or inexact integer number, else
353 @deftypefn {C Function} int scm_is_integer (SCM x)
354 This is equivalent to @code{scm_is_true (scm_integer_p (x))}.
357 @defvr {C Type} scm_t_int8
358 @defvrx {C Type} scm_t_uint8
359 @defvrx {C Type} scm_t_int16
360 @defvrx {C Type} scm_t_uint16
361 @defvrx {C Type} scm_t_int32
362 @defvrx {C Type} scm_t_uint32
363 @defvrx {C Type} scm_t_int64
364 @defvrx {C Type} scm_t_uint64
365 @defvrx {C Type} scm_t_intmax
366 @defvrx {C Type} scm_t_uintmax
367 The C types are equivalent to the corresponding ISO C types but are
368 defined on all platforms, with the exception of @code{scm_t_int64} and
369 @code{scm_t_uint64}, which are only defined when a 64-bit type is
370 available. For example, @code{scm_t_int8} is equivalent to
373 You can regard these definitions as a stop-gap measure until all
374 platforms provide these types. If you know that all the platforms
375 that you are interested in already provide these types, it is better
376 to use them directly instead of the types provided by Guile.
379 @deftypefn {C Function} int scm_is_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
380 @deftypefnx {C Function} int scm_is_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
381 Return @code{1} when @var{x} represents an exact integer that is
382 between @var{min} and @var{max}, inclusive.
384 These functions can be used to check whether a @code{SCM} value will
385 fit into a given range, such as the range of a given C integer type.
386 If you just want to convert a @code{SCM} value to a given C integer
387 type, use one of the conversion functions directly.
390 @deftypefn {C Function} scm_t_intmax scm_to_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
391 @deftypefnx {C Function} scm_t_uintmax scm_to_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
392 When @var{x} represents an exact integer that is between @var{min} and
393 @var{max} inclusive, return that integer. Else signal an error,
394 either a `wrong-type' error when @var{x} is not an exact integer, or
395 an `out-of-range' error when it doesn't fit the given range.
398 @deftypefn {C Function} SCM scm_from_signed_integer (scm_t_intmax x)
399 @deftypefnx {C Function} SCM scm_from_unsigned_integer (scm_t_uintmax x)
400 Return the @code{SCM} value that represents the integer @var{x}. This
401 function will always succeed and will always return an exact number.
404 @deftypefn {C Function} char scm_to_char (SCM x)
405 @deftypefnx {C Function} {signed char} scm_to_schar (SCM x)
406 @deftypefnx {C Function} {unsigned char} scm_to_uchar (SCM x)
407 @deftypefnx {C Function} short scm_to_short (SCM x)
408 @deftypefnx {C Function} {unsigned short} scm_to_ushort (SCM x)
409 @deftypefnx {C Function} int scm_to_int (SCM x)
410 @deftypefnx {C Function} {unsigned int} scm_to_uint (SCM x)
411 @deftypefnx {C Function} long scm_to_long (SCM x)
412 @deftypefnx {C Function} {unsigned long} scm_to_ulong (SCM x)
413 @deftypefnx {C Function} {long long} scm_to_long_long (SCM x)
414 @deftypefnx {C Function} {unsigned long long} scm_to_ulong_long (SCM x)
415 @deftypefnx {C Function} size_t scm_to_size_t (SCM x)
416 @deftypefnx {C Function} ssize_t scm_to_ssize_t (SCM x)
417 @deftypefnx {C Function} scm_t_int8 scm_to_int8 (SCM x)
418 @deftypefnx {C Function} scm_t_uint8 scm_to_uint8 (SCM x)
419 @deftypefnx {C Function} scm_t_int16 scm_to_int16 (SCM x)
420 @deftypefnx {C Function} scm_t_uint16 scm_to_uint16 (SCM x)
421 @deftypefnx {C Function} scm_t_int32 scm_to_int32 (SCM x)
422 @deftypefnx {C Function} scm_t_uint32 scm_to_uint32 (SCM x)
423 @deftypefnx {C Function} scm_t_int64 scm_to_int64 (SCM x)
424 @deftypefnx {C Function} scm_t_uint64 scm_to_uint64 (SCM x)
425 @deftypefnx {C Function} scm_t_intmax scm_to_intmax (SCM x)
426 @deftypefnx {C Function} scm_t_uintmax scm_to_uintmax (SCM x)
427 When @var{x} represents an exact integer that fits into the indicated
428 C type, return that integer. Else signal an error, either a
429 `wrong-type' error when @var{x} is not an exact integer, or an
430 `out-of-range' error when it doesn't fit the given range.
432 The functions @code{scm_to_long_long}, @code{scm_to_ulong_long},
433 @code{scm_to_int64}, and @code{scm_to_uint64} are only available when
434 the corresponding types are.
437 @deftypefn {C Function} SCM scm_from_char (char x)
438 @deftypefnx {C Function} SCM scm_from_schar (signed char x)
439 @deftypefnx {C Function} SCM scm_from_uchar (unsigned char x)
440 @deftypefnx {C Function} SCM scm_from_short (short x)
441 @deftypefnx {C Function} SCM scm_from_ushort (unsigned short x)
442 @deftypefnx {C Function} SCM scm_from_int (int x)
443 @deftypefnx {C Function} SCM scm_from_uint (unsigned int x)
444 @deftypefnx {C Function} SCM scm_from_long (long x)
445 @deftypefnx {C Function} SCM scm_from_ulong (unsigned long x)
446 @deftypefnx {C Function} SCM scm_from_long_long (long long x)
447 @deftypefnx {C Function} SCM scm_from_ulong_long (unsigned long long x)
448 @deftypefnx {C Function} SCM scm_from_size_t (size_t x)
449 @deftypefnx {C Function} SCM scm_from_ssize_t (ssize_t x)
450 @deftypefnx {C Function} SCM scm_from_int8 (scm_t_int8 x)
451 @deftypefnx {C Function} SCM scm_from_uint8 (scm_t_uint8 x)
452 @deftypefnx {C Function} SCM scm_from_int16 (scm_t_int16 x)
453 @deftypefnx {C Function} SCM scm_from_uint16 (scm_t_uint16 x)
454 @deftypefnx {C Function} SCM scm_from_int32 (scm_t_int32 x)
455 @deftypefnx {C Function} SCM scm_from_uint32 (scm_t_uint32 x)
456 @deftypefnx {C Function} SCM scm_from_int64 (scm_t_int64 x)
457 @deftypefnx {C Function} SCM scm_from_uint64 (scm_t_uint64 x)
458 @deftypefnx {C Function} SCM scm_from_intmax (scm_t_intmax x)
459 @deftypefnx {C Function} SCM scm_from_uintmax (scm_t_uintmax x)
460 Return the @code{SCM} value that represents the integer @var{x}.
461 These functions will always succeed and will always return an exact
465 @deftypefn {C Function} void scm_to_mpz (SCM val, mpz_t rop)
466 Assign @var{val} to the multiple precision integer @var{rop}.
467 @var{val} must be an exact integer, otherwise an error will be
468 signalled. @var{rop} must have been initialized with @code{mpz_init}
469 before this function is called. When @var{rop} is no longer needed
470 the occupied space must be freed with @code{mpz_clear}.
471 @xref{Initializing Integers,,, gmp, GNU MP Manual}, for details.
474 @deftypefn {C Function} SCM scm_from_mpz (mpz_t val)
475 Return the @code{SCM} value that represents @var{val}.
478 @node Reals and Rationals
479 @subsubsection Real and Rational Numbers
480 @tpindex Real numbers
481 @tpindex Rational numbers
486 Mathematically, the real numbers are the set of numbers that describe
487 all possible points along a continuous, infinite, one-dimensional line.
488 The rational numbers are the set of all numbers that can be written as
489 fractions @var{p}/@var{q}, where @var{p} and @var{q} are integers.
490 All rational numbers are also real, but there are real numbers that
491 are not rational, for example @m{\sqrt{2}, the square root of 2}, and
494 Guile can represent both exact and inexact rational numbers, but it
495 cannot represent precise finite irrational numbers. Exact rationals are
496 represented by storing the numerator and denominator as two exact
497 integers. Inexact rationals are stored as floating point numbers using
498 the C type @code{double}.
500 Exact rationals are written as a fraction of integers. There must be
501 no whitespace around the slash:
508 Even though the actual encoding of inexact rationals is in binary, it
509 may be helpful to think of it as a decimal number with a limited
510 number of significant figures and a decimal point somewhere, since
511 this corresponds to the standard notation for non-whole numbers. For
517 -5648394822220000000000.0
521 The limited precision of Guile's encoding means that any finite ``real''
522 number in Guile can be written in a rational form, by multiplying and
523 then dividing by sufficient powers of 10 (or in fact, 2). For example,
524 @samp{-0.00000142857931198} is the same as @minus{}142857931198 divided
525 by 100000000000000000. In Guile's current incarnation, therefore, the
526 @code{rational?} and @code{real?} predicates are equivalent for finite
530 Dividing by an exact zero leads to a error message, as one might expect.
531 However, dividing by an inexact zero does not produce an error.
532 Instead, the result of the division is either plus or minus infinity,
533 depending on the sign of the divided number and the sign of the zero
534 divisor (some platforms support signed zeroes @samp{-0.0} and
535 @samp{+0.0}; @samp{0.0} is the same as @samp{+0.0}).
537 Dividing zero by an inexact zero yields a @acronym{NaN} (`not a number')
538 value, although they are actually considered numbers by Scheme.
539 Attempts to compare a @acronym{NaN} value with any number (including
540 itself) using @code{=}, @code{<}, @code{>}, @code{<=} or @code{>=}
541 always returns @code{#f}. Although a @acronym{NaN} value is not
542 @code{=} to itself, it is both @code{eqv?} and @code{equal?} to itself
543 and other @acronym{NaN} values. However, the preferred way to test for
544 them is by using @code{nan?}.
546 The real @acronym{NaN} values and infinities are written @samp{+nan.0},
547 @samp{+inf.0} and @samp{-inf.0}. This syntax is also recognized by
548 @code{read} as an extension to the usual Scheme syntax. These special
549 values are considered by Scheme to be inexact real numbers but not
550 rational. Note that non-real complex numbers may also contain
551 infinities or @acronym{NaN} values in their real or imaginary parts. To
552 test a real number to see if it is infinite, a @acronym{NaN} value, or
553 neither, use @code{inf?}, @code{nan?}, or @code{finite?}, respectively.
554 Every real number in Scheme belongs to precisely one of those three
557 On platforms that follow @acronym{IEEE} 754 for their floating point
558 arithmetic, the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values
559 are implemented using the corresponding @acronym{IEEE} 754 values.
560 They behave in arithmetic operations like @acronym{IEEE} 754 describes
561 it, i.e., @code{(= +nan.0 +nan.0)} @result{} @code{#f}.
563 @deffn {Scheme Procedure} real? obj
564 @deffnx {C Function} scm_real_p (obj)
565 Return @code{#t} if @var{obj} is a real number, else @code{#f}. Note
566 that the sets of integer and rational values form subsets of the set
567 of real numbers, so the predicate will also be fulfilled if @var{obj}
568 is an integer number or a rational number.
571 @deffn {Scheme Procedure} rational? x
572 @deffnx {C Function} scm_rational_p (x)
573 Return @code{#t} if @var{x} is a rational number, @code{#f} otherwise.
574 Note that the set of integer values forms a subset of the set of
575 rational numbers, i.e.@: the predicate will also be fulfilled if
576 @var{x} is an integer number.
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 the real number @var{x} is @samp{+inf.0} or
598 @samp{-inf.0}. Otherwise return @code{#f}.
601 @deffn {Scheme Procedure} nan? x
602 @deffnx {C Function} scm_nan_p (x)
603 Return @code{#t} if the real number @var{x} is @samp{+nan.0}, or
607 @deffn {Scheme Procedure} finite? x
608 @deffnx {C Function} scm_finite_p (x)
609 Return @code{#t} if the real number @var{x} is neither infinite nor a
610 NaN, @code{#f} otherwise.
613 @deffn {Scheme Procedure} nan
614 @deffnx {C Function} scm_nan ()
615 Return @samp{+nan.0}, a @acronym{NaN} value.
618 @deffn {Scheme Procedure} inf
619 @deffnx {C Function} scm_inf ()
620 Return @samp{+inf.0}, positive infinity.
623 @deffn {Scheme Procedure} numerator x
624 @deffnx {C Function} scm_numerator (x)
625 Return the numerator of the rational number @var{x}.
628 @deffn {Scheme Procedure} denominator x
629 @deffnx {C Function} scm_denominator (x)
630 Return the denominator of the rational number @var{x}.
633 @deftypefn {C Function} int scm_is_real (SCM val)
634 @deftypefnx {C Function} int scm_is_rational (SCM val)
635 Equivalent to @code{scm_is_true (scm_real_p (val))} and
636 @code{scm_is_true (scm_rational_p (val))}, respectively.
639 @deftypefn {C Function} double scm_to_double (SCM val)
640 Returns the number closest to @var{val} that is representable as a
641 @code{double}. Returns infinity for a @var{val} that is too large in
642 magnitude. The argument @var{val} must be a real number.
645 @deftypefn {C Function} SCM scm_from_double (double val)
646 Return the @code{SCM} value that represents @var{val}. The returned
647 value is inexact according to the predicate @code{inexact?}, but it
648 will be exactly equal to @var{val}.
651 @node Complex Numbers
652 @subsubsection Complex Numbers
653 @tpindex Complex numbers
657 Complex numbers are the set of numbers that describe all possible points
658 in a two-dimensional space. The two coordinates of a particular point
659 in this space are known as the @dfn{real} and @dfn{imaginary} parts of
660 the complex number that describes that point.
662 In Guile, complex numbers are written in rectangular form as the sum of
663 their real and imaginary parts, using the symbol @code{i} to indicate
678 Polar form can also be used, with an @samp{@@} between magnitude and
682 1@@3.141592 @result{} -1.0 (approx)
683 -1@@1.57079 @result{} 0.0-1.0i (approx)
686 Guile represents a complex number as a pair of inexact reals, so the
687 real and imaginary parts of a complex number have the same properties of
688 inexactness and limited precision as single inexact real numbers.
690 Note that each part of a complex number may contain any inexact real
691 value, including the special values @samp{+nan.0}, @samp{+inf.0} and
692 @samp{-inf.0}, as well as either of the signed zeroes @samp{0.0} or
696 @deffn {Scheme Procedure} complex? z
697 @deffnx {C Function} scm_complex_p (z)
698 Return @code{#t} if @var{x} is a complex number, @code{#f}
699 otherwise. Note that the sets of real, rational and integer
700 values form subsets of the set of complex numbers, i.e.@: the
701 predicate will also be fulfilled if @var{x} is a real,
702 rational or integer number.
705 @deftypefn {C Function} int scm_is_complex (SCM val)
706 Equivalent to @code{scm_is_true (scm_complex_p (val))}.
710 @subsubsection Exact and Inexact Numbers
711 @tpindex Exact numbers
712 @tpindex Inexact numbers
716 @rnindex exact->inexact
717 @rnindex inexact->exact
719 R5RS requires that, with few exceptions, a calculation involving inexact
720 numbers always produces an inexact result. To meet this requirement,
721 Guile distinguishes between an exact integer value such as @samp{5} and
722 the corresponding inexact integer value which, to the limited precision
723 available, has no fractional part, and is printed as @samp{5.0}. Guile
724 will only convert the latter value to the former when forced to do so by
725 an invocation of the @code{inexact->exact} procedure.
727 The only exception to the above requirement is when the values of the
728 inexact numbers do not affect the result. For example @code{(expt n 0)}
729 is @samp{1} for any value of @code{n}, therefore @code{(expt 5.0 0)} is
730 permitted to return an exact @samp{1}.
732 @deffn {Scheme Procedure} exact? z
733 @deffnx {C Function} scm_exact_p (z)
734 Return @code{#t} if the number @var{z} is exact, @code{#f}
750 @deffn {Scheme Procedure} inexact? z
751 @deffnx {C Function} scm_inexact_p (z)
752 Return @code{#t} if the number @var{z} is inexact, @code{#f}
756 @deffn {Scheme Procedure} inexact->exact z
757 @deffnx {C Function} scm_inexact_to_exact (z)
758 Return an exact number that is numerically closest to @var{z}, when
759 there is one. For inexact rationals, Guile returns the exact rational
760 that is numerically equal to the inexact rational. Inexact complex
761 numbers with a non-zero imaginary part can not be made exact.
768 The following happens because 12/10 is not exactly representable as a
769 @code{double} (on most platforms). However, when reading a decimal
770 number that has been marked exact with the ``#e'' prefix, Guile is
771 able to represent it correctly.
775 @result{} 5404319552844595/4503599627370496
783 @c begin (texi-doc-string "guile" "exact->inexact")
784 @deffn {Scheme Procedure} exact->inexact z
785 @deffnx {C Function} scm_exact_to_inexact (z)
786 Convert the number @var{z} to its inexact representation.
791 @subsubsection Read Syntax for Numerical Data
793 The read syntax for integers is a string of digits, optionally
794 preceded by a minus or plus character, a code indicating the
795 base in which the integer is encoded, and a code indicating whether
796 the number is exact or inexact. The supported base codes are:
801 the integer is written in binary (base 2)
805 the integer is written in octal (base 8)
809 the integer is written in decimal (base 10)
813 the integer is written in hexadecimal (base 16)
816 If the base code is omitted, the integer is assumed to be decimal. The
817 following examples show how these base codes are used.
836 The codes for indicating exactness (which can, incidentally, be applied
837 to all numerical values) are:
846 the number is inexact.
849 If the exactness indicator is omitted, the number is exact unless it
850 contains a radix point. Since Guile can not represent exact complex
851 numbers, an error is signalled when asking for them.
861 ERROR: Wrong type argument
864 Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for
865 plus and minus infinity, respectively. The value must be written
866 exactly as shown, that is, they always must have a sign and exactly
867 one zero digit after the decimal point. It also understands
868 @samp{+nan.0} and @samp{-nan.0} for the special `not-a-number' value.
869 The sign is ignored for `not-a-number' and the value is always printed
872 @node Integer Operations
873 @subsubsection Operations on Integer Values
882 @deffn {Scheme Procedure} odd? n
883 @deffnx {C Function} scm_odd_p (n)
884 Return @code{#t} if @var{n} is an odd number, @code{#f}
888 @deffn {Scheme Procedure} even? n
889 @deffnx {C Function} scm_even_p (n)
890 Return @code{#t} if @var{n} is an even number, @code{#f}
894 @c begin (texi-doc-string "guile" "quotient")
895 @c begin (texi-doc-string "guile" "remainder")
896 @deffn {Scheme Procedure} quotient n d
897 @deffnx {Scheme Procedure} remainder n d
898 @deffnx {C Function} scm_quotient (n, d)
899 @deffnx {C Function} scm_remainder (n, d)
900 Return the quotient or remainder from @var{n} divided by @var{d}. The
901 quotient is rounded towards zero, and the remainder will have the same
902 sign as @var{n}. In all cases quotient and remainder satisfy
903 @math{@var{n} = @var{q}*@var{d} + @var{r}}.
906 (remainder 13 4) @result{} 1
907 (remainder -13 4) @result{} -1
910 See also @code{truncate-quotient}, @code{truncate-remainder} and
911 related operations in @ref{Arithmetic}.
914 @c begin (texi-doc-string "guile" "modulo")
915 @deffn {Scheme Procedure} modulo n d
916 @deffnx {C Function} scm_modulo (n, d)
917 Return the remainder from @var{n} divided by @var{d}, with the same
921 (modulo 13 4) @result{} 1
922 (modulo -13 4) @result{} 3
923 (modulo 13 -4) @result{} -3
924 (modulo -13 -4) @result{} -1
927 See also @code{floor-quotient}, @code{floor-remainder} and
928 related operations in @ref{Arithmetic}.
931 @c begin (texi-doc-string "guile" "gcd")
932 @deffn {Scheme Procedure} gcd x@dots{}
933 @deffnx {C Function} scm_gcd (x, y)
934 Return the greatest common divisor of all arguments.
935 If called without arguments, 0 is returned.
937 The C function @code{scm_gcd} always takes two arguments, while the
938 Scheme function can take an arbitrary number.
941 @c begin (texi-doc-string "guile" "lcm")
942 @deffn {Scheme Procedure} lcm x@dots{}
943 @deffnx {C Function} scm_lcm (x, y)
944 Return the least common multiple of the arguments.
945 If called without arguments, 1 is returned.
947 The C function @code{scm_lcm} always takes two arguments, while the
948 Scheme function can take an arbitrary number.
951 @deffn {Scheme Procedure} modulo-expt n k m
952 @deffnx {C Function} scm_modulo_expt (n, k, m)
953 Return @var{n} raised to the integer exponent
954 @var{k}, modulo @var{m}.
962 @deftypefn {Scheme Procedure} {} exact-integer-sqrt @var{k}
963 @deftypefnx {C Function} void scm_exact_integer_sqrt (SCM @var{k}, SCM *@var{s}, SCM *@var{r})
964 Return two exact non-negative integers @var{s} and @var{r}
965 such that @math{@var{k} = @var{s}^2 + @var{r}} and
966 @math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.
967 An error is raised if @var{k} is not an exact non-negative integer.
970 (exact-integer-sqrt 10) @result{} 3 and 1
975 @subsubsection Comparison Predicates
980 The C comparison functions below always takes two arguments, while the
981 Scheme functions can take an arbitrary number. Also keep in mind that
982 the C functions return one of the Scheme boolean values
983 @code{SCM_BOOL_T} or @code{SCM_BOOL_F} which are both true as far as C
984 is concerned. Thus, always write @code{scm_is_true (scm_num_eq_p (x,
985 y))} when testing the two Scheme numbers @code{x} and @code{y} for
986 equality, for example.
988 @c begin (texi-doc-string "guile" "=")
989 @deffn {Scheme Procedure} =
990 @deffnx {C Function} scm_num_eq_p (x, y)
991 Return @code{#t} if all parameters are numerically equal.
994 @c begin (texi-doc-string "guile" "<")
995 @deffn {Scheme Procedure} <
996 @deffnx {C Function} scm_less_p (x, y)
997 Return @code{#t} if the list of parameters is monotonically
1001 @c begin (texi-doc-string "guile" ">")
1002 @deffn {Scheme Procedure} >
1003 @deffnx {C Function} scm_gr_p (x, y)
1004 Return @code{#t} if the list of parameters is monotonically
1008 @c begin (texi-doc-string "guile" "<=")
1009 @deffn {Scheme Procedure} <=
1010 @deffnx {C Function} scm_leq_p (x, y)
1011 Return @code{#t} if the list of parameters is monotonically
1015 @c begin (texi-doc-string "guile" ">=")
1016 @deffn {Scheme Procedure} >=
1017 @deffnx {C Function} scm_geq_p (x, y)
1018 Return @code{#t} if the list of parameters is monotonically
1022 @c begin (texi-doc-string "guile" "zero?")
1023 @deffn {Scheme Procedure} zero? z
1024 @deffnx {C Function} scm_zero_p (z)
1025 Return @code{#t} if @var{z} is an exact or inexact number equal to
1029 @c begin (texi-doc-string "guile" "positive?")
1030 @deffn {Scheme Procedure} positive? x
1031 @deffnx {C Function} scm_positive_p (x)
1032 Return @code{#t} if @var{x} is an exact or inexact number greater than
1036 @c begin (texi-doc-string "guile" "negative?")
1037 @deffn {Scheme Procedure} negative? x
1038 @deffnx {C Function} scm_negative_p (x)
1039 Return @code{#t} if @var{x} is an exact or inexact number less than
1045 @subsubsection Converting Numbers To and From Strings
1046 @rnindex number->string
1047 @rnindex string->number
1049 The following procedures read and write numbers according to their
1050 external representation as defined by R5RS (@pxref{Lexical structure,
1051 R5RS Lexical Structure,, r5rs, The Revised^5 Report on the Algorithmic
1052 Language Scheme}). @xref{Number Input and Output, the @code{(ice-9
1053 i18n)} module}, for locale-dependent number parsing.
1055 @deffn {Scheme Procedure} number->string n [radix]
1056 @deffnx {C Function} scm_number_to_string (n, radix)
1057 Return a string holding the external representation of the
1058 number @var{n} in the given @var{radix}. If @var{n} is
1059 inexact, a radix of 10 will be used.
1062 @deffn {Scheme Procedure} string->number string [radix]
1063 @deffnx {C Function} scm_string_to_number (string, radix)
1064 Return a number of the maximally precise representation
1065 expressed by the given @var{string}. @var{radix} must be an
1066 exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}
1067 is a default radix that may be overridden by an explicit radix
1068 prefix in @var{string} (e.g.@: "#o177"). If @var{radix} is not
1069 supplied, then the default radix is 10. If string is not a
1070 syntactically valid notation for a number, then
1071 @code{string->number} returns @code{#f}.
1074 @deftypefn {C Function} SCM scm_c_locale_stringn_to_number (const char *string, size_t len, unsigned radix)
1075 As per @code{string->number} above, but taking a C string, as pointer
1076 and length. The string characters should be in the current locale
1077 encoding (@code{locale} in the name refers only to that, there's no
1078 locale-dependent parsing).
1083 @subsubsection Complex Number Operations
1084 @rnindex make-rectangular
1091 @deffn {Scheme Procedure} make-rectangular real_part imaginary_part
1092 @deffnx {C Function} scm_make_rectangular (real_part, imaginary_part)
1093 Return a complex number constructed of the given @var{real-part} and @var{imaginary-part} parts.
1096 @deffn {Scheme Procedure} make-polar mag ang
1097 @deffnx {C Function} scm_make_polar (mag, ang)
1099 Return the complex number @var{mag} * e^(i * @var{ang}).
1102 @c begin (texi-doc-string "guile" "real-part")
1103 @deffn {Scheme Procedure} real-part z
1104 @deffnx {C Function} scm_real_part (z)
1105 Return the real part of the number @var{z}.
1108 @c begin (texi-doc-string "guile" "imag-part")
1109 @deffn {Scheme Procedure} imag-part z
1110 @deffnx {C Function} scm_imag_part (z)
1111 Return the imaginary part of the number @var{z}.
1114 @c begin (texi-doc-string "guile" "magnitude")
1115 @deffn {Scheme Procedure} magnitude z
1116 @deffnx {C Function} scm_magnitude (z)
1117 Return the magnitude of the number @var{z}. This is the same as
1118 @code{abs} for real arguments, but also allows complex numbers.
1121 @c begin (texi-doc-string "guile" "angle")
1122 @deffn {Scheme Procedure} angle z
1123 @deffnx {C Function} scm_angle (z)
1124 Return the angle of the complex number @var{z}.
1127 @deftypefn {C Function} SCM scm_c_make_rectangular (double re, double im)
1128 @deftypefnx {C Function} SCM scm_c_make_polar (double x, double y)
1129 Like @code{scm_make_rectangular} or @code{scm_make_polar},
1130 respectively, but these functions take @code{double}s as their
1134 @deftypefn {C Function} double scm_c_real_part (z)
1135 @deftypefnx {C Function} double scm_c_imag_part (z)
1136 Returns the real or imaginary part of @var{z} as a @code{double}.
1139 @deftypefn {C Function} double scm_c_magnitude (z)
1140 @deftypefnx {C Function} double scm_c_angle (z)
1141 Returns the magnitude or angle of @var{z} as a @code{double}.
1146 @subsubsection Arithmetic Functions
1161 @rnindex euclidean-quotient
1162 @rnindex euclidean-remainder
1164 @rnindex floor-quotient
1165 @rnindex floor-remainder
1167 @rnindex ceiling-quotient
1168 @rnindex ceiling-remainder
1170 @rnindex truncate-quotient
1171 @rnindex truncate-remainder
1173 @rnindex centered-quotient
1174 @rnindex centered-remainder
1176 @rnindex round-quotient
1177 @rnindex round-remainder
1179 The C arithmetic functions below always takes two arguments, while the
1180 Scheme functions can take an arbitrary number. When you need to
1181 invoke them with just one argument, for example to compute the
1182 equivalent of @code{(- x)}, pass @code{SCM_UNDEFINED} as the second
1183 one: @code{scm_difference (x, SCM_UNDEFINED)}.
1185 @c begin (texi-doc-string "guile" "+")
1186 @deffn {Scheme Procedure} + z1 @dots{}
1187 @deffnx {C Function} scm_sum (z1, z2)
1188 Return the sum of all parameter values. Return 0 if called without any
1192 @c begin (texi-doc-string "guile" "-")
1193 @deffn {Scheme Procedure} - z1 z2 @dots{}
1194 @deffnx {C Function} scm_difference (z1, z2)
1195 If called with one argument @var{z1}, -@var{z1} is returned. Otherwise
1196 the sum of all but the first argument are subtracted from the first
1200 @c begin (texi-doc-string "guile" "*")
1201 @deffn {Scheme Procedure} * z1 @dots{}
1202 @deffnx {C Function} scm_product (z1, z2)
1203 Return the product of all arguments. If called without arguments, 1 is
1207 @c begin (texi-doc-string "guile" "/")
1208 @deffn {Scheme Procedure} / z1 z2 @dots{}
1209 @deffnx {C Function} scm_divide (z1, z2)
1210 Divide the first argument by the product of the remaining arguments. If
1211 called with one argument @var{z1}, 1/@var{z1} is returned.
1214 @deffn {Scheme Procedure} 1+ z
1215 @deffnx {C Function} scm_oneplus (z)
1216 Return @math{@var{z} + 1}.
1219 @deffn {Scheme Procedure} 1- z
1220 @deffnx {C function} scm_oneminus (z)
1221 Return @math{@var{z} - 1}.
1224 @c begin (texi-doc-string "guile" "abs")
1225 @deffn {Scheme Procedure} abs x
1226 @deffnx {C Function} scm_abs (x)
1227 Return the absolute value of @var{x}.
1229 @var{x} must be a number with zero imaginary part. To calculate the
1230 magnitude of a complex number, use @code{magnitude} instead.
1233 @c begin (texi-doc-string "guile" "max")
1234 @deffn {Scheme Procedure} max x1 x2 @dots{}
1235 @deffnx {C Function} scm_max (x1, x2)
1236 Return the maximum of all parameter values.
1239 @c begin (texi-doc-string "guile" "min")
1240 @deffn {Scheme Procedure} min x1 x2 @dots{}
1241 @deffnx {C Function} scm_min (x1, x2)
1242 Return the minimum of all parameter values.
1245 @c begin (texi-doc-string "guile" "truncate")
1246 @deffn {Scheme Procedure} truncate x
1247 @deffnx {C Function} scm_truncate_number (x)
1248 Round the inexact number @var{x} towards zero.
1251 @c begin (texi-doc-string "guile" "round")
1252 @deffn {Scheme Procedure} round x
1253 @deffnx {C Function} scm_round_number (x)
1254 Round the inexact number @var{x} to the nearest integer. When exactly
1255 halfway between two integers, round to the even one.
1258 @c begin (texi-doc-string "guile" "floor")
1259 @deffn {Scheme Procedure} floor x
1260 @deffnx {C Function} scm_floor (x)
1261 Round the number @var{x} towards minus infinity.
1264 @c begin (texi-doc-string "guile" "ceiling")
1265 @deffn {Scheme Procedure} ceiling x
1266 @deffnx {C Function} scm_ceiling (x)
1267 Round the number @var{x} towards infinity.
1270 @deftypefn {C Function} double scm_c_truncate (double x)
1271 @deftypefnx {C Function} double scm_c_round (double x)
1272 Like @code{scm_truncate_number} or @code{scm_round_number},
1273 respectively, but these functions take and return @code{double}
1277 @deftypefn {Scheme Procedure} {} euclidean/ @var{x} @var{y}
1278 @deftypefnx {Scheme Procedure} {} euclidean-quotient @var{x} @var{y}
1279 @deftypefnx {Scheme Procedure} {} euclidean-remainder @var{x} @var{y}
1280 @deftypefnx {C Function} void scm_euclidean_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
1281 @deftypefnx {C Function} SCM scm_euclidean_quotient (SCM @var{x}, SCM @var{y})
1282 @deftypefnx {C Function} SCM scm_euclidean_remainder (SCM @var{x}, SCM @var{y})
1283 These procedures accept two real numbers @var{x} and @var{y}, where the
1284 divisor @var{y} must be non-zero. @code{euclidean-quotient} returns the
1285 integer @var{q} and @code{euclidean-remainder} returns the real number
1286 @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
1287 @math{0 <= @var{r} < |@var{y}|}. @code{euclidean/} returns both @var{q} and
1288 @var{r}, and is more efficient than computing each separately. Note
1289 that when @math{@var{y} > 0}, @code{euclidean-quotient} returns
1290 @math{floor(@var{x}/@var{y})}, otherwise it returns
1291 @math{ceiling(@var{x}/@var{y})}.
1293 Note that these operators are equivalent to the R6RS operators
1294 @code{div}, @code{mod}, and @code{div-and-mod}.
1297 (euclidean-quotient 123 10) @result{} 12
1298 (euclidean-remainder 123 10) @result{} 3
1299 (euclidean/ 123 10) @result{} 12 and 3
1300 (euclidean/ 123 -10) @result{} -12 and 3
1301 (euclidean/ -123 10) @result{} -13 and 7
1302 (euclidean/ -123 -10) @result{} 13 and 7
1303 (euclidean/ -123.2 -63.5) @result{} 2.0 and 3.8
1304 (euclidean/ 16/3 -10/7) @result{} -3 and 22/21
1308 @deftypefn {Scheme Procedure} {} floor/ @var{x} @var{y}
1309 @deftypefnx {Scheme Procedure} {} floor-quotient @var{x} @var{y}
1310 @deftypefnx {Scheme Procedure} {} floor-remainder @var{x} @var{y}
1311 @deftypefnx {C Function} void scm_floor_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
1312 @deftypefnx {C Function} SCM scm_floor_quotient (@var{x}, @var{y})
1313 @deftypefnx {C Function} SCM scm_floor_remainder (@var{x}, @var{y})
1314 These procedures accept two real numbers @var{x} and @var{y}, where the
1315 divisor @var{y} must be non-zero. @code{floor-quotient} returns the
1316 integer @var{q} and @code{floor-remainder} returns the real number
1317 @var{r} such that @math{@var{q} = floor(@var{x}/@var{y})} and
1318 @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{floor/} returns
1319 both @var{q} and @var{r}, and is more efficient than computing each
1320 separately. Note that @var{r}, if non-zero, will have the same sign
1323 When @var{x} and @var{y} are integers, @code{floor-remainder} is
1324 equivalent to the R5RS integer-only operator @code{modulo}.
1327 (floor-quotient 123 10) @result{} 12
1328 (floor-remainder 123 10) @result{} 3
1329 (floor/ 123 10) @result{} 12 and 3
1330 (floor/ 123 -10) @result{} -13 and -7
1331 (floor/ -123 10) @result{} -13 and 7
1332 (floor/ -123 -10) @result{} 12 and -3
1333 (floor/ -123.2 -63.5) @result{} 1.0 and -59.7
1334 (floor/ 16/3 -10/7) @result{} -4 and -8/21
1338 @deftypefn {Scheme Procedure} {} ceiling/ @var{x} @var{y}
1339 @deftypefnx {Scheme Procedure} {} ceiling-quotient @var{x} @var{y}
1340 @deftypefnx {Scheme Procedure} {} ceiling-remainder @var{x} @var{y}
1341 @deftypefnx {C Function} void scm_ceiling_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
1342 @deftypefnx {C Function} SCM scm_ceiling_quotient (@var{x}, @var{y})
1343 @deftypefnx {C Function} SCM scm_ceiling_remainder (@var{x}, @var{y})
1344 These procedures accept two real numbers @var{x} and @var{y}, where the
1345 divisor @var{y} must be non-zero. @code{ceiling-quotient} returns the
1346 integer @var{q} and @code{ceiling-remainder} returns the real number
1347 @var{r} such that @math{@var{q} = ceiling(@var{x}/@var{y})} and
1348 @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{ceiling/} returns
1349 both @var{q} and @var{r}, and is more efficient than computing each
1350 separately. Note that @var{r}, if non-zero, will have the opposite sign
1354 (ceiling-quotient 123 10) @result{} 13
1355 (ceiling-remainder 123 10) @result{} -7
1356 (ceiling/ 123 10) @result{} 13 and -7
1357 (ceiling/ 123 -10) @result{} -12 and 3
1358 (ceiling/ -123 10) @result{} -12 and -3
1359 (ceiling/ -123 -10) @result{} 13 and 7
1360 (ceiling/ -123.2 -63.5) @result{} 2.0 and 3.8
1361 (ceiling/ 16/3 -10/7) @result{} -3 and 22/21
1365 @deftypefn {Scheme Procedure} {} truncate/ @var{x} @var{y}
1366 @deftypefnx {Scheme Procedure} {} truncate-quotient @var{x} @var{y}
1367 @deftypefnx {Scheme Procedure} {} truncate-remainder @var{x} @var{y}
1368 @deftypefnx {C Function} void scm_truncate_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
1369 @deftypefnx {C Function} SCM scm_truncate_quotient (@var{x}, @var{y})
1370 @deftypefnx {C Function} SCM scm_truncate_remainder (@var{x}, @var{y})
1371 These procedures accept two real numbers @var{x} and @var{y}, where the
1372 divisor @var{y} must be non-zero. @code{truncate-quotient} returns the
1373 integer @var{q} and @code{truncate-remainder} returns the real number
1374 @var{r} such that @var{q} is @math{@var{x}/@var{y}} rounded toward zero,
1375 and @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{truncate/} returns
1376 both @var{q} and @var{r}, and is more efficient than computing each
1377 separately. Note that @var{r}, if non-zero, will have the same sign
1380 When @var{x} and @var{y} are integers, these operators are
1381 equivalent to the R5RS integer-only operators @code{quotient} and
1385 (truncate-quotient 123 10) @result{} 12
1386 (truncate-remainder 123 10) @result{} 3
1387 (truncate/ 123 10) @result{} 12 and 3
1388 (truncate/ 123 -10) @result{} -12 and 3
1389 (truncate/ -123 10) @result{} -12 and -3
1390 (truncate/ -123 -10) @result{} 12 and -3
1391 (truncate/ -123.2 -63.5) @result{} 1.0 and -59.7
1392 (truncate/ 16/3 -10/7) @result{} -3 and 22/21
1396 @deftypefn {Scheme Procedure} {} centered/ @var{x} @var{y}
1397 @deftypefnx {Scheme Procedure} {} centered-quotient @var{x} @var{y}
1398 @deftypefnx {Scheme Procedure} {} centered-remainder @var{x} @var{y}
1399 @deftypefnx {C Function} void scm_centered_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
1400 @deftypefnx {C Function} SCM scm_centered_quotient (SCM @var{x}, SCM @var{y})
1401 @deftypefnx {C Function} SCM scm_centered_remainder (SCM @var{x}, SCM @var{y})
1402 These procedures accept two real numbers @var{x} and @var{y}, where the
1403 divisor @var{y} must be non-zero. @code{centered-quotient} returns the
1404 integer @var{q} and @code{centered-remainder} returns the real number
1405 @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
1406 @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}. @code{centered/}
1407 returns both @var{q} and @var{r}, and is more efficient than computing
1410 Note that @code{centered-quotient} returns @math{@var{x}/@var{y}}
1411 rounded to the nearest integer. When @math{@var{x}/@var{y}} lies
1412 exactly half-way between two integers, the tie is broken according to
1413 the sign of @var{y}. If @math{@var{y} > 0}, ties are rounded toward
1414 positive infinity, otherwise they are rounded toward negative infinity.
1415 This is a consequence of the requirement that
1416 @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}.
1418 Note that these operators are equivalent to the R6RS operators
1419 @code{div0}, @code{mod0}, and @code{div0-and-mod0}.
1422 (centered-quotient 123 10) @result{} 12
1423 (centered-remainder 123 10) @result{} 3
1424 (centered/ 123 10) @result{} 12 and 3
1425 (centered/ 123 -10) @result{} -12 and 3
1426 (centered/ -123 10) @result{} -12 and -3
1427 (centered/ -123 -10) @result{} 12 and -3
1428 (centered/ 125 10) @result{} 13 and -5
1429 (centered/ 127 10) @result{} 13 and -3
1430 (centered/ 135 10) @result{} 14 and -5
1431 (centered/ -123.2 -63.5) @result{} 2.0 and 3.8
1432 (centered/ 16/3 -10/7) @result{} -4 and -8/21
1436 @deftypefn {Scheme Procedure} {} round/ @var{x} @var{y}
1437 @deftypefnx {Scheme Procedure} {} round-quotient @var{x} @var{y}
1438 @deftypefnx {Scheme Procedure} {} round-remainder @var{x} @var{y}
1439 @deftypefnx {C Function} void scm_round_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
1440 @deftypefnx {C Function} SCM scm_round_quotient (@var{x}, @var{y})
1441 @deftypefnx {C Function} SCM scm_round_remainder (@var{x}, @var{y})
1442 These procedures accept two real numbers @var{x} and @var{y}, where the
1443 divisor @var{y} must be non-zero. @code{round-quotient} returns the
1444 integer @var{q} and @code{round-remainder} returns the real number
1445 @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
1446 @var{q} is @math{@var{x}/@var{y}} rounded to the nearest integer,
1447 with ties going to the nearest even integer. @code{round/}
1448 returns both @var{q} and @var{r}, and is more efficient than computing
1451 Note that @code{round/} and @code{centered/} are almost equivalent, but
1452 their behavior differs when @math{@var{x}/@var{y}} lies exactly half-way
1453 between two integers. In this case, @code{round/} chooses the nearest
1454 even integer, whereas @code{centered/} chooses in such a way to satisfy
1455 the constraint @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}, which
1456 is stronger than the corresponding constraint for @code{round/},
1457 @math{-|@var{y}/2| <= @var{r} <= |@var{y}/2|}. In particular,
1458 when @var{x} and @var{y} are integers, the number of possible remainders
1459 returned by @code{centered/} is @math{|@var{y}|}, whereas the number of
1460 possible remainders returned by @code{round/} is @math{|@var{y}|+1} when
1464 (round-quotient 123 10) @result{} 12
1465 (round-remainder 123 10) @result{} 3
1466 (round/ 123 10) @result{} 12 and 3
1467 (round/ 123 -10) @result{} -12 and 3
1468 (round/ -123 10) @result{} -12 and -3
1469 (round/ -123 -10) @result{} 12 and -3
1470 (round/ 125 10) @result{} 12 and 5
1471 (round/ 127 10) @result{} 13 and -3
1472 (round/ 135 10) @result{} 14 and -5
1473 (round/ -123.2 -63.5) @result{} 2.0 and 3.8
1474 (round/ 16/3 -10/7) @result{} -4 and -8/21
1479 @subsubsection Scientific Functions
1481 The following procedures accept any kind of number as arguments,
1482 including complex numbers.
1485 @c begin (texi-doc-string "guile" "sqrt")
1486 @deffn {Scheme Procedure} sqrt z
1487 Return the square root of @var{z}. Of the two possible roots
1488 (positive and negative), the one with a positive real part is
1489 returned, or if that's zero then a positive imaginary part. Thus,
1492 (sqrt 9.0) @result{} 3.0
1493 (sqrt -9.0) @result{} 0.0+3.0i
1494 (sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i
1495 (sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i
1500 @c begin (texi-doc-string "guile" "expt")
1501 @deffn {Scheme Procedure} expt z1 z2
1502 Return @var{z1} raised to the power of @var{z2}.
1506 @c begin (texi-doc-string "guile" "sin")
1507 @deffn {Scheme Procedure} sin z
1508 Return the sine of @var{z}.
1512 @c begin (texi-doc-string "guile" "cos")
1513 @deffn {Scheme Procedure} cos z
1514 Return the cosine of @var{z}.
1518 @c begin (texi-doc-string "guile" "tan")
1519 @deffn {Scheme Procedure} tan z
1520 Return the tangent of @var{z}.
1524 @c begin (texi-doc-string "guile" "asin")
1525 @deffn {Scheme Procedure} asin z
1526 Return the arcsine of @var{z}.
1530 @c begin (texi-doc-string "guile" "acos")
1531 @deffn {Scheme Procedure} acos z
1532 Return the arccosine of @var{z}.
1536 @c begin (texi-doc-string "guile" "atan")
1537 @deffn {Scheme Procedure} atan z
1538 @deffnx {Scheme Procedure} atan y x
1539 Return the arctangent of @var{z}, or of @math{@var{y}/@var{x}}.
1543 @c begin (texi-doc-string "guile" "exp")
1544 @deffn {Scheme Procedure} exp z
1545 Return e to the power of @var{z}, where e is the base of natural
1546 logarithms (2.71828@dots{}).
1550 @c begin (texi-doc-string "guile" "log")
1551 @deffn {Scheme Procedure} log z
1552 Return the natural logarithm of @var{z}.
1555 @c begin (texi-doc-string "guile" "log10")
1556 @deffn {Scheme Procedure} log10 z
1557 Return the base 10 logarithm of @var{z}.
1560 @c begin (texi-doc-string "guile" "sinh")
1561 @deffn {Scheme Procedure} sinh z
1562 Return the hyperbolic sine of @var{z}.
1565 @c begin (texi-doc-string "guile" "cosh")
1566 @deffn {Scheme Procedure} cosh z
1567 Return the hyperbolic cosine of @var{z}.
1570 @c begin (texi-doc-string "guile" "tanh")
1571 @deffn {Scheme Procedure} tanh z
1572 Return the hyperbolic tangent of @var{z}.
1575 @c begin (texi-doc-string "guile" "asinh")
1576 @deffn {Scheme Procedure} asinh z
1577 Return the hyperbolic arcsine of @var{z}.
1580 @c begin (texi-doc-string "guile" "acosh")
1581 @deffn {Scheme Procedure} acosh z
1582 Return the hyperbolic arccosine of @var{z}.
1585 @c begin (texi-doc-string "guile" "atanh")
1586 @deffn {Scheme Procedure} atanh z
1587 Return the hyperbolic arctangent of @var{z}.
1591 @node Bitwise Operations
1592 @subsubsection Bitwise Operations
1594 For the following bitwise functions, negative numbers are treated as
1595 infinite precision twos-complements. For instance @math{-6} is bits
1596 @math{@dots{}111010}, with infinitely many ones on the left. It can
1597 be seen that adding 6 (binary 110) to such a bit pattern gives all
1600 @deffn {Scheme Procedure} logand n1 n2 @dots{}
1601 @deffnx {C Function} scm_logand (n1, n2)
1602 Return the bitwise @sc{and} of the integer arguments.
1605 (logand) @result{} -1
1606 (logand 7) @result{} 7
1607 (logand #b111 #b011 #b001) @result{} 1
1611 @deffn {Scheme Procedure} logior n1 n2 @dots{}
1612 @deffnx {C Function} scm_logior (n1, n2)
1613 Return the bitwise @sc{or} of the integer arguments.
1616 (logior) @result{} 0
1617 (logior 7) @result{} 7
1618 (logior #b000 #b001 #b011) @result{} 3
1622 @deffn {Scheme Procedure} logxor n1 n2 @dots{}
1623 @deffnx {C Function} scm_loxor (n1, n2)
1624 Return the bitwise @sc{xor} of the integer arguments. A bit is
1625 set in the result if it is set in an odd number of arguments.
1628 (logxor) @result{} 0
1629 (logxor 7) @result{} 7
1630 (logxor #b000 #b001 #b011) @result{} 2
1631 (logxor #b000 #b001 #b011 #b011) @result{} 1
1635 @deffn {Scheme Procedure} lognot n
1636 @deffnx {C Function} scm_lognot (n)
1637 Return the integer which is the ones-complement of the integer
1638 argument, ie.@: each 0 bit is changed to 1 and each 1 bit to 0.
1641 (number->string (lognot #b10000000) 2)
1642 @result{} "-10000001"
1643 (number->string (lognot #b0) 2)
1648 @deffn {Scheme Procedure} logtest j k
1649 @deffnx {C Function} scm_logtest (j, k)
1650 Test whether @var{j} and @var{k} have any 1 bits in common. This is
1651 equivalent to @code{(not (zero? (logand j k)))}, but without actually
1652 calculating the @code{logand}, just testing for non-zero.
1655 (logtest #b0100 #b1011) @result{} #f
1656 (logtest #b0100 #b0111) @result{} #t
1660 @deffn {Scheme Procedure} logbit? index j
1661 @deffnx {C Function} scm_logbit_p (index, j)
1662 Test whether bit number @var{index} in @var{j} is set. @var{index}
1663 starts from 0 for the least significant bit.
1666 (logbit? 0 #b1101) @result{} #t
1667 (logbit? 1 #b1101) @result{} #f
1668 (logbit? 2 #b1101) @result{} #t
1669 (logbit? 3 #b1101) @result{} #t
1670 (logbit? 4 #b1101) @result{} #f
1674 @deffn {Scheme Procedure} ash n cnt
1675 @deffnx {C Function} scm_ash (n, cnt)
1676 Return @var{n} shifted left by @var{cnt} bits, or shifted right if
1677 @var{cnt} is negative. This is an ``arithmetic'' shift.
1679 This is effectively a multiplication by @m{2^{cnt}, 2^@var{cnt}}, and
1680 when @var{cnt} is negative it's a division, rounded towards negative
1681 infinity. (Note that this is not the same rounding as @code{quotient}
1684 With @var{n} viewed as an infinite precision twos complement,
1685 @code{ash} means a left shift introducing zero bits, or a right shift
1689 (number->string (ash #b1 3) 2) @result{} "1000"
1690 (number->string (ash #b1010 -1) 2) @result{} "101"
1692 ;; -23 is bits ...11101001, -6 is bits ...111010
1693 (ash -23 -2) @result{} -6
1697 @deffn {Scheme Procedure} logcount n
1698 @deffnx {C Function} scm_logcount (n)
1699 Return the number of bits in integer @var{n}. If @var{n} is
1700 positive, the 1-bits in its binary representation are counted.
1701 If negative, the 0-bits in its two's-complement binary
1702 representation are counted. If zero, 0 is returned.
1705 (logcount #b10101010)
1714 @deffn {Scheme Procedure} integer-length n
1715 @deffnx {C Function} scm_integer_length (n)
1716 Return the number of bits necessary to represent @var{n}.
1718 For positive @var{n} this is how many bits to the most significant one
1719 bit. For negative @var{n} it's how many bits to the most significant
1720 zero bit in twos complement form.
1723 (integer-length #b10101010) @result{} 8
1724 (integer-length #b1111) @result{} 4
1725 (integer-length 0) @result{} 0
1726 (integer-length -1) @result{} 0
1727 (integer-length -256) @result{} 8
1728 (integer-length -257) @result{} 9
1732 @deffn {Scheme Procedure} integer-expt n k
1733 @deffnx {C Function} scm_integer_expt (n, k)
1734 Return @var{n} raised to the power @var{k}. @var{k} must be an exact
1735 integer, @var{n} can be any number.
1737 Negative @var{k} is supported, and results in @m{1/n^|k|, 1/n^abs(k)}
1738 in the usual way. @math{@var{n}^0} is 1, as usual, and that includes
1742 (integer-expt 2 5) @result{} 32
1743 (integer-expt -3 3) @result{} -27
1744 (integer-expt 5 -3) @result{} 1/125
1745 (integer-expt 0 0) @result{} 1
1749 @deffn {Scheme Procedure} bit-extract n start end
1750 @deffnx {C Function} scm_bit_extract (n, start, end)
1751 Return the integer composed of the @var{start} (inclusive)
1752 through @var{end} (exclusive) bits of @var{n}. The
1753 @var{start}th bit becomes the 0-th bit in the result.
1756 (number->string (bit-extract #b1101101010 0 4) 2)
1758 (number->string (bit-extract #b1101101010 4 9) 2)
1765 @subsubsection Random Number Generation
1767 Pseudo-random numbers are generated from a random state object, which
1768 can be created with @code{seed->random-state} or
1769 @code{datum->random-state}. An external representation (i.e.@: one
1770 which can written with @code{write} and read with @code{read}) of a
1771 random state object can be obtained via
1772 @code{random-state->datum}. The @var{state} parameter to the
1773 various functions below is optional, it defaults to the state object
1774 in the @code{*random-state*} variable.
1776 @deffn {Scheme Procedure} copy-random-state [state]
1777 @deffnx {C Function} scm_copy_random_state (state)
1778 Return a copy of the random state @var{state}.
1781 @deffn {Scheme Procedure} random n [state]
1782 @deffnx {C Function} scm_random (n, state)
1783 Return a number in [0, @var{n}).
1785 Accepts a positive integer or real n and returns a
1786 number of the same type between zero (inclusive) and
1787 @var{n} (exclusive). The values returned have a uniform
1791 @deffn {Scheme Procedure} random:exp [state]
1792 @deffnx {C Function} scm_random_exp (state)
1793 Return an inexact real in an exponential distribution with mean
1794 1. For an exponential distribution with mean @var{u} use @code{(*
1795 @var{u} (random:exp))}.
1798 @deffn {Scheme Procedure} random:hollow-sphere! vect [state]
1799 @deffnx {C Function} scm_random_hollow_sphere_x (vect, state)
1800 Fills @var{vect} with inexact real random numbers the sum of whose
1801 squares is equal to 1.0. Thinking of @var{vect} as coordinates in
1802 space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1803 the coordinates are uniformly distributed over the surface of the unit
1807 @deffn {Scheme Procedure} random:normal [state]
1808 @deffnx {C Function} scm_random_normal (state)
1809 Return an inexact real in a normal distribution. The distribution
1810 used has mean 0 and standard deviation 1. For a normal distribution
1811 with mean @var{m} and standard deviation @var{d} use @code{(+ @var{m}
1812 (* @var{d} (random:normal)))}.
1815 @deffn {Scheme Procedure} random:normal-vector! vect [state]
1816 @deffnx {C Function} scm_random_normal_vector_x (vect, state)
1817 Fills @var{vect} with inexact real random numbers that are
1818 independent and standard normally distributed
1819 (i.e., with mean 0 and variance 1).
1822 @deffn {Scheme Procedure} random:solid-sphere! vect [state]
1823 @deffnx {C Function} scm_random_solid_sphere_x (vect, state)
1824 Fills @var{vect} with inexact real random numbers the sum of whose
1825 squares is less than 1.0. Thinking of @var{vect} as coordinates in
1826 space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1827 the coordinates are uniformly distributed within the unit
1829 @c FIXME: What does this mean, particularly the n-sphere part?
1832 @deffn {Scheme Procedure} random:uniform [state]
1833 @deffnx {C Function} scm_random_uniform (state)
1834 Return a uniformly distributed inexact real random number in
1838 @deffn {Scheme Procedure} seed->random-state seed
1839 @deffnx {C Function} scm_seed_to_random_state (seed)
1840 Return a new random state using @var{seed}.
1843 @deffn {Scheme Procedure} datum->random-state datum
1844 @deffnx {C Function} scm_datum_to_random_state (datum)
1845 Return a new random state from @var{datum}, which should have been
1846 obtained by @code{random-state->datum}.
1849 @deffn {Scheme Procedure} random-state->datum state
1850 @deffnx {C Function} scm_random_state_to_datum (state)
1851 Return a datum representation of @var{state} that may be written out and
1852 read back with the Scheme reader.
1855 @defvar *random-state*
1856 The global random state used by the above functions when the
1857 @var{state} parameter is not given.
1860 Note that the initial value of @code{*random-state*} is the same every
1861 time Guile starts up. Therefore, if you don't pass a @var{state}
1862 parameter to the above procedures, and you don't set
1863 @code{*random-state*} to @code{(seed->random-state your-seed)}, where
1864 @code{your-seed} is something that @emph{isn't} the same every time,
1865 you'll get the same sequence of ``random'' numbers on every run.
1867 For example, unless the relevant source code has changed, @code{(map
1868 random (cdr (iota 30)))}, if the first use of random numbers since
1869 Guile started up, will always give:
1872 (map random (cdr (iota 19)))
1874 (0 1 1 2 2 2 1 2 6 7 10 0 5 3 12 5 5 12)
1877 To use the time of day as the random seed, you can use code like this:
1880 (let ((time (gettimeofday)))
1881 (set! *random-state*
1882 (seed->random-state (+ (car time)
1887 And then (depending on the time of day, of course):
1890 (map random (cdr (iota 19)))
1892 (0 0 1 0 2 4 5 4 5 5 9 3 10 1 8 3 14 17)
1895 For security applications, such as password generation, you should use
1896 more bits of seed. Otherwise an open source password generator could
1897 be attacked by guessing the seed@dots{} but that's a subject for
1902 @subsection Characters
1905 In Scheme, there is a data type to describe a single character.
1907 Defining what exactly a character @emph{is} can be more complicated
1908 than it seems. Guile follows the advice of R6RS and uses The Unicode
1909 Standard to help define what a character is. So, for Guile, a
1910 character is anything in the Unicode Character Database.
1913 @cindex Unicode code point
1915 The Unicode Character Database is basically a table of characters
1916 indexed using integers called 'code points'. Valid code points are in
1917 the ranges 0 to @code{#xD7FF} inclusive or @code{#xE000} to
1918 @code{#x10FFFF} inclusive, which is about 1.1 million code points.
1920 @cindex designated code point
1921 @cindex code point, designated
1923 Any code point that has been assigned to a character or that has
1924 otherwise been given a meaning by Unicode is called a 'designated code
1925 point'. Most of the designated code points, about 200,000 of them,
1926 indicate characters, accents or other combining marks that modify
1927 other characters, symbols, whitespace, and control characters. Some
1928 are not characters but indicators that suggest how to format or
1929 display neighboring characters.
1931 @cindex reserved code point
1932 @cindex code point, reserved
1934 If a code point is not a designated code point -- if it has not been
1935 assigned to a character by The Unicode Standard -- it is a 'reserved
1936 code point', meaning that they are reserved for future use. Most of
1937 the code points, about 800,000, are 'reserved code points'.
1939 By convention, a Unicode code point is written as
1940 ``U+XXXX'' where ``XXXX'' is a hexadecimal number. Please note that
1941 this convenient notation is not valid code. Guile does not interpret
1942 ``U+XXXX'' as a character.
1944 In Scheme, a character literal is written as @code{#\@var{name}} where
1945 @var{name} is the name of the character that you want. Printable
1946 characters have their usual single character name; for example,
1947 @code{#\a} is a lower case @code{a}.
1949 Some of the code points are 'combining characters' that are not meant
1950 to be printed by themselves but are instead meant to modify the
1951 appearance of the previous character. For combining characters, an
1952 alternate form of the character literal is @code{#\} followed by
1953 U+25CC (a small, dotted circle), followed by the combining character.
1954 This allows the combining character to be drawn on the circle, not on
1955 the backslash of @code{#\}.
1957 Many of the non-printing characters, such as whitespace characters and
1958 control characters, also have names.
1960 The most commonly used non-printing characters have long character
1961 names, described in the table below.
1963 @multitable {@code{#\backspace}} {Preferred}
1964 @item Character Name @tab Codepoint
1965 @item @code{#\nul} @tab U+0000
1966 @item @code{#\alarm} @tab u+0007
1967 @item @code{#\backspace} @tab U+0008
1968 @item @code{#\tab} @tab U+0009
1969 @item @code{#\linefeed} @tab U+000A
1970 @item @code{#\newline} @tab U+000A
1971 @item @code{#\vtab} @tab U+000B
1972 @item @code{#\page} @tab U+000C
1973 @item @code{#\return} @tab U+000D
1974 @item @code{#\esc} @tab U+001B
1975 @item @code{#\space} @tab U+0020
1976 @item @code{#\delete} @tab U+007F
1979 There are also short names for all of the ``C0 control characters''
1980 (those with code points below 32). The following table lists the short
1981 name for each character.
1983 @multitable @columnfractions .25 .25 .25 .25
1984 @item 0 = @code{#\nul}
1985 @tab 1 = @code{#\soh}
1986 @tab 2 = @code{#\stx}
1987 @tab 3 = @code{#\etx}
1988 @item 4 = @code{#\eot}
1989 @tab 5 = @code{#\enq}
1990 @tab 6 = @code{#\ack}
1991 @tab 7 = @code{#\bel}
1992 @item 8 = @code{#\bs}
1993 @tab 9 = @code{#\ht}
1994 @tab 10 = @code{#\lf}
1995 @tab 11 = @code{#\vt}
1996 @item 12 = @code{#\ff}
1997 @tab 13 = @code{#\cr}
1998 @tab 14 = @code{#\so}
1999 @tab 15 = @code{#\si}
2000 @item 16 = @code{#\dle}
2001 @tab 17 = @code{#\dc1}
2002 @tab 18 = @code{#\dc2}
2003 @tab 19 = @code{#\dc3}
2004 @item 20 = @code{#\dc4}
2005 @tab 21 = @code{#\nak}
2006 @tab 22 = @code{#\syn}
2007 @tab 23 = @code{#\etb}
2008 @item 24 = @code{#\can}
2009 @tab 25 = @code{#\em}
2010 @tab 26 = @code{#\sub}
2011 @tab 27 = @code{#\esc}
2012 @item 28 = @code{#\fs}
2013 @tab 29 = @code{#\gs}
2014 @tab 30 = @code{#\rs}
2015 @tab 31 = @code{#\us}
2016 @item 32 = @code{#\sp}
2019 The short name for the ``delete'' character (code point U+007F) is
2022 There are also a few alternative names left over for compatibility with
2023 previous versions of Guile.
2025 @multitable {@code{#\backspace}} {Preferred}
2026 @item Alternate @tab Standard
2027 @item @code{#\nl} @tab @code{#\newline}
2028 @item @code{#\np} @tab @code{#\page}
2029 @item @code{#\null} @tab @code{#\nul}
2032 Characters may also be written using their code point values. They can
2033 be written with as an octal number, such as @code{#\10} for
2034 @code{#\bs} or @code{#\177} for @code{#\del}.
2036 If one prefers hex to octal, there is an additional syntax for character
2037 escapes: @code{#\xHHHH} -- the letter 'x' followed by a hexadecimal
2038 number of one to eight digits.
2041 @deffn {Scheme Procedure} char? x
2042 @deffnx {C Function} scm_char_p (x)
2043 Return @code{#t} iff @var{x} is a character, else @code{#f}.
2046 Fundamentally, the character comparison operations below are
2047 numeric comparisons of the character's code points.
2050 @deffn {Scheme Procedure} char=? x y
2051 Return @code{#t} iff code point of @var{x} is equal to the code point
2052 of @var{y}, else @code{#f}.
2056 @deffn {Scheme Procedure} char<? x y
2057 Return @code{#t} iff the code point of @var{x} is less than the code
2058 point of @var{y}, else @code{#f}.
2062 @deffn {Scheme Procedure} char<=? x y
2063 Return @code{#t} iff the code point of @var{x} is less than or equal
2064 to the code point of @var{y}, else @code{#f}.
2068 @deffn {Scheme Procedure} char>? x y
2069 Return @code{#t} iff the code point of @var{x} is greater than the
2070 code point of @var{y}, else @code{#f}.
2074 @deffn {Scheme Procedure} char>=? x y
2075 Return @code{#t} iff the code point of @var{x} is greater than or
2076 equal to the code point of @var{y}, else @code{#f}.
2079 @cindex case folding
2081 Case-insensitive character comparisons use @emph{Unicode case
2082 folding}. In case folding comparisons, if a character is lowercase
2083 and has an uppercase form that can be expressed as a single character,
2084 it is converted to uppercase before comparison. All other characters
2085 undergo no conversion before the comparison occurs. This includes the
2086 German sharp S (Eszett) which is not uppercased before conversion
2087 because its uppercase form has two characters. Unicode case folding
2088 is language independent: it uses rules that are generally true, but,
2089 it cannot cover all cases for all languages.
2092 @deffn {Scheme Procedure} char-ci=? x y
2093 Return @code{#t} iff the case-folded code point of @var{x} is the same
2094 as the case-folded code point of @var{y}, else @code{#f}.
2098 @deffn {Scheme Procedure} char-ci<? x y
2099 Return @code{#t} iff the case-folded code point of @var{x} is less
2100 than the case-folded code point of @var{y}, else @code{#f}.
2104 @deffn {Scheme Procedure} char-ci<=? x y
2105 Return @code{#t} iff the case-folded code point of @var{x} is less
2106 than or equal to the case-folded code point of @var{y}, else
2111 @deffn {Scheme Procedure} char-ci>? x y
2112 Return @code{#t} iff the case-folded code point of @var{x} is greater
2113 than the case-folded code point of @var{y}, else @code{#f}.
2117 @deffn {Scheme Procedure} char-ci>=? x y
2118 Return @code{#t} iff the case-folded code point of @var{x} is greater
2119 than or equal to the case-folded code point of @var{y}, else
2123 @rnindex char-alphabetic?
2124 @deffn {Scheme Procedure} char-alphabetic? chr
2125 @deffnx {C Function} scm_char_alphabetic_p (chr)
2126 Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.
2129 @rnindex char-numeric?
2130 @deffn {Scheme Procedure} char-numeric? chr
2131 @deffnx {C Function} scm_char_numeric_p (chr)
2132 Return @code{#t} iff @var{chr} is numeric, else @code{#f}.
2135 @rnindex char-whitespace?
2136 @deffn {Scheme Procedure} char-whitespace? chr
2137 @deffnx {C Function} scm_char_whitespace_p (chr)
2138 Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.
2141 @rnindex char-upper-case?
2142 @deffn {Scheme Procedure} char-upper-case? chr
2143 @deffnx {C Function} scm_char_upper_case_p (chr)
2144 Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.
2147 @rnindex char-lower-case?
2148 @deffn {Scheme Procedure} char-lower-case? chr
2149 @deffnx {C Function} scm_char_lower_case_p (chr)
2150 Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.
2153 @deffn {Scheme Procedure} char-is-both? chr
2154 @deffnx {C Function} scm_char_is_both_p (chr)
2155 Return @code{#t} iff @var{chr} is either uppercase or lowercase, else
2159 @deffn {Scheme Procedure} char-general-category chr
2160 @deffnx {C Function} scm_char_general_category (chr)
2161 Return a symbol giving the two-letter name of the Unicode general
2162 category assigned to @var{chr} or @code{#f} if no named category is
2163 assigned. The following table provides a list of category names along
2164 with their meanings.
2166 @multitable @columnfractions .1 .4 .1 .4
2168 @tab Uppercase letter
2170 @tab Final quote punctuation
2172 @tab Lowercase letter
2174 @tab Other punctuation
2176 @tab Titlecase letter
2180 @tab Modifier letter
2182 @tab Currency symbol
2186 @tab Modifier symbol
2188 @tab Non-spacing mark
2192 @tab Combining spacing mark
2194 @tab Space separator
2200 @tab Decimal digit number
2202 @tab Paragraph separator
2212 @tab Connector punctuation
2216 @tab Dash punctuation
2220 @tab Open punctuation
2224 @tab Close punctuation
2228 @tab Initial quote punctuation
2234 @rnindex char->integer
2235 @deffn {Scheme Procedure} char->integer chr
2236 @deffnx {C Function} scm_char_to_integer (chr)
2237 Return the code point of @var{chr}.
2240 @rnindex integer->char
2241 @deffn {Scheme Procedure} integer->char n
2242 @deffnx {C Function} scm_integer_to_char (n)
2243 Return the character that has code point @var{n}. The integer @var{n}
2244 must be a valid code point. Valid code points are in the ranges 0 to
2245 @code{#xD7FF} inclusive or @code{#xE000} to @code{#x10FFFF} inclusive.
2248 @rnindex char-upcase
2249 @deffn {Scheme Procedure} char-upcase chr
2250 @deffnx {C Function} scm_char_upcase (chr)
2251 Return the uppercase character version of @var{chr}.
2254 @rnindex char-downcase
2255 @deffn {Scheme Procedure} char-downcase chr
2256 @deffnx {C Function} scm_char_downcase (chr)
2257 Return the lowercase character version of @var{chr}.
2260 @rnindex char-titlecase
2261 @deffn {Scheme Procedure} char-titlecase chr
2262 @deffnx {C Function} scm_char_titlecase (chr)
2263 Return the titlecase character version of @var{chr} if one exists;
2264 otherwise return the uppercase version.
2266 For most characters these will be the same, but the Unicode Standard
2267 includes certain digraph compatibility characters, such as @code{U+01F3}
2268 ``dz'', for which the uppercase and titlecase characters are different
2269 (@code{U+01F1} ``DZ'' and @code{U+01F2} ``Dz'' in this case,
2274 @deftypefn {C Function} scm_t_wchar scm_c_upcase (scm_t_wchar @var{c})
2275 @deftypefnx {C Function} scm_t_wchar scm_c_downcase (scm_t_wchar @var{c})
2276 @deftypefnx {C Function} scm_t_wchar scm_c_titlecase (scm_t_wchar @var{c})
2278 These C functions take an integer representation of a Unicode
2279 codepoint and return the codepoint corresponding to its uppercase,
2280 lowercase, and titlecase forms respectively. The type
2281 @code{scm_t_wchar} is a signed, 32-bit integer.
2284 @node Character Sets
2285 @subsection Character Sets
2287 The features described in this section correspond directly to SRFI-14.
2289 The data type @dfn{charset} implements sets of characters
2290 (@pxref{Characters}). Because the internal representation of
2291 character sets is not visible to the user, a lot of procedures for
2292 handling them are provided.
2294 Character sets can be created, extended, tested for the membership of a
2295 characters and be compared to other character sets.
2298 * Character Set Predicates/Comparison::
2299 * Iterating Over Character Sets:: Enumerate charset elements.
2300 * Creating Character Sets:: Making new charsets.
2301 * Querying Character Sets:: Test charsets for membership etc.
2302 * Character-Set Algebra:: Calculating new charsets.
2303 * Standard Character Sets:: Variables containing predefined charsets.
2306 @node Character Set Predicates/Comparison
2307 @subsubsection Character Set Predicates/Comparison
2309 Use these procedures for testing whether an object is a character set,
2310 or whether several character sets are equal or subsets of each other.
2311 @code{char-set-hash} can be used for calculating a hash value, maybe for
2312 usage in fast lookup procedures.
2314 @deffn {Scheme Procedure} char-set? obj
2315 @deffnx {C Function} scm_char_set_p (obj)
2316 Return @code{#t} if @var{obj} is a character set, @code{#f}
2320 @deffn {Scheme Procedure} char-set= . char_sets
2321 @deffnx {C Function} scm_char_set_eq (char_sets)
2322 Return @code{#t} if all given character sets are equal.
2325 @deffn {Scheme Procedure} char-set<= . char_sets
2326 @deffnx {C Function} scm_char_set_leq (char_sets)
2327 Return @code{#t} if every character set @var{cs}i is a subset
2328 of character set @var{cs}i+1.
2331 @deffn {Scheme Procedure} char-set-hash cs [bound]
2332 @deffnx {C Function} scm_char_set_hash (cs, bound)
2333 Compute a hash value for the character set @var{cs}. If
2334 @var{bound} is given and non-zero, it restricts the
2335 returned value to the range 0 @dots{} @var{bound - 1}.
2338 @c ===================================================================
2340 @node Iterating Over Character Sets
2341 @subsubsection Iterating Over Character Sets
2343 Character set cursors are a means for iterating over the members of a
2344 character sets. After creating a character set cursor with
2345 @code{char-set-cursor}, a cursor can be dereferenced with
2346 @code{char-set-ref}, advanced to the next member with
2347 @code{char-set-cursor-next}. Whether a cursor has passed past the last
2348 element of the set can be checked with @code{end-of-char-set?}.
2350 Additionally, mapping and (un-)folding procedures for character sets are
2353 @deffn {Scheme Procedure} char-set-cursor cs
2354 @deffnx {C Function} scm_char_set_cursor (cs)
2355 Return a cursor into the character set @var{cs}.
2358 @deffn {Scheme Procedure} char-set-ref cs cursor
2359 @deffnx {C Function} scm_char_set_ref (cs, cursor)
2360 Return the character at the current cursor position
2361 @var{cursor} in the character set @var{cs}. It is an error to
2362 pass a cursor for which @code{end-of-char-set?} returns true.
2365 @deffn {Scheme Procedure} char-set-cursor-next cs cursor
2366 @deffnx {C Function} scm_char_set_cursor_next (cs, cursor)
2367 Advance the character set cursor @var{cursor} to the next
2368 character in the character set @var{cs}. It is an error if the
2369 cursor given satisfies @code{end-of-char-set?}.
2372 @deffn {Scheme Procedure} end-of-char-set? cursor
2373 @deffnx {C Function} scm_end_of_char_set_p (cursor)
2374 Return @code{#t} if @var{cursor} has reached the end of a
2375 character set, @code{#f} otherwise.
2378 @deffn {Scheme Procedure} char-set-fold kons knil cs
2379 @deffnx {C Function} scm_char_set_fold (kons, knil, cs)
2380 Fold the procedure @var{kons} over the character set @var{cs},
2381 initializing it with @var{knil}.
2384 @deffn {Scheme Procedure} char-set-unfold p f g seed [base_cs]
2385 @deffnx {C Function} scm_char_set_unfold (p, f, g, seed, base_cs)
2386 This is a fundamental constructor for character sets.
2388 @item @var{g} is used to generate a series of ``seed'' values
2389 from the initial seed: @var{seed}, (@var{g} @var{seed}),
2390 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
2391 @item @var{p} tells us when to stop -- when it returns true
2392 when applied to one of the seed values.
2393 @item @var{f} maps each seed value to a character. These
2394 characters are added to the base character set @var{base_cs} to
2395 form the result; @var{base_cs} defaults to the empty set.
2399 @deffn {Scheme Procedure} char-set-unfold! p f g seed base_cs
2400 @deffnx {C Function} scm_char_set_unfold_x (p, f, g, seed, base_cs)
2401 This is a fundamental constructor for character sets.
2403 @item @var{g} is used to generate a series of ``seed'' values
2404 from the initial seed: @var{seed}, (@var{g} @var{seed}),
2405 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
2406 @item @var{p} tells us when to stop -- when it returns true
2407 when applied to one of the seed values.
2408 @item @var{f} maps each seed value to a character. These
2409 characters are added to the base character set @var{base_cs} to
2410 form the result; @var{base_cs} defaults to the empty set.
2414 @deffn {Scheme Procedure} char-set-for-each proc cs
2415 @deffnx {C Function} scm_char_set_for_each (proc, cs)
2416 Apply @var{proc} to every character in the character set
2417 @var{cs}. The return value is not specified.
2420 @deffn {Scheme Procedure} char-set-map proc cs
2421 @deffnx {C Function} scm_char_set_map (proc, cs)
2422 Map the procedure @var{proc} over every character in @var{cs}.
2423 @var{proc} must be a character -> character procedure.
2426 @c ===================================================================
2428 @node Creating Character Sets
2429 @subsubsection Creating Character Sets
2431 New character sets are produced with these procedures.
2433 @deffn {Scheme Procedure} char-set-copy cs
2434 @deffnx {C Function} scm_char_set_copy (cs)
2435 Return a newly allocated character set containing all
2436 characters in @var{cs}.
2439 @deffn {Scheme Procedure} char-set . rest
2440 @deffnx {C Function} scm_char_set (rest)
2441 Return a character set containing all given characters.
2444 @deffn {Scheme Procedure} list->char-set list [base_cs]
2445 @deffnx {C Function} scm_list_to_char_set (list, base_cs)
2446 Convert the character list @var{list} to a character set. If
2447 the character set @var{base_cs} is given, the character in this
2448 set are also included in the result.
2451 @deffn {Scheme Procedure} list->char-set! list base_cs
2452 @deffnx {C Function} scm_list_to_char_set_x (list, base_cs)
2453 Convert the character list @var{list} to a character set. The
2454 characters are added to @var{base_cs} and @var{base_cs} is
2458 @deffn {Scheme Procedure} string->char-set str [base_cs]
2459 @deffnx {C Function} scm_string_to_char_set (str, base_cs)
2460 Convert the string @var{str} to a character set. If the
2461 character set @var{base_cs} is given, the characters in this
2462 set are also included in the result.
2465 @deffn {Scheme Procedure} string->char-set! str base_cs
2466 @deffnx {C Function} scm_string_to_char_set_x (str, base_cs)
2467 Convert the string @var{str} to a character set. The
2468 characters from the string are added to @var{base_cs}, and
2469 @var{base_cs} is returned.
2472 @deffn {Scheme Procedure} char-set-filter pred cs [base_cs]
2473 @deffnx {C Function} scm_char_set_filter (pred, cs, base_cs)
2474 Return a character set containing every character from @var{cs}
2475 so that it satisfies @var{pred}. If provided, the characters
2476 from @var{base_cs} are added to the result.
2479 @deffn {Scheme Procedure} char-set-filter! pred cs base_cs
2480 @deffnx {C Function} scm_char_set_filter_x (pred, cs, base_cs)
2481 Return a character set containing every character from @var{cs}
2482 so that it satisfies @var{pred}. The characters are added to
2483 @var{base_cs} and @var{base_cs} is returned.
2486 @deffn {Scheme Procedure} ucs-range->char-set lower upper [error [base_cs]]
2487 @deffnx {C Function} scm_ucs_range_to_char_set (lower, upper, error, base_cs)
2488 Return a character set containing all characters whose
2489 character codes lie in the half-open range
2490 [@var{lower},@var{upper}).
2492 If @var{error} is a true value, an error is signalled if the
2493 specified range contains characters which are not contained in
2494 the implemented character range. If @var{error} is @code{#f},
2495 these characters are silently left out of the resulting
2498 The characters in @var{base_cs} are added to the result, if
2502 @deffn {Scheme Procedure} ucs-range->char-set! lower upper error base_cs
2503 @deffnx {C Function} scm_ucs_range_to_char_set_x (lower, upper, error, base_cs)
2504 Return a character set containing all characters whose
2505 character codes lie in the half-open range
2506 [@var{lower},@var{upper}).
2508 If @var{error} is a true value, an error is signalled if the
2509 specified range contains characters which are not contained in
2510 the implemented character range. If @var{error} is @code{#f},
2511 these characters are silently left out of the resulting
2514 The characters are added to @var{base_cs} and @var{base_cs} is
2518 @deffn {Scheme Procedure} ->char-set x
2519 @deffnx {C Function} scm_to_char_set (x)
2520 Coerces x into a char-set. @var{x} may be a string, character or
2521 char-set. A string is converted to the set of its constituent
2522 characters; a character is converted to a singleton set; a char-set is
2526 @c ===================================================================
2528 @node Querying Character Sets
2529 @subsubsection Querying Character Sets
2531 Access the elements and other information of a character set with these
2534 @deffn {Scheme Procedure} %char-set-dump cs
2535 Returns an association list containing debugging information
2536 for @var{cs}. The association list has the following entries.
2541 The number of groups of contiguous code points the char-set
2544 A list of lists where each sublist is a range of code points
2545 and their associated characters
2547 The return value of this function cannot be relied upon to be
2548 consistent between versions of Guile and should not be used in code.
2551 @deffn {Scheme Procedure} char-set-size cs
2552 @deffnx {C Function} scm_char_set_size (cs)
2553 Return the number of elements in character set @var{cs}.
2556 @deffn {Scheme Procedure} char-set-count pred cs
2557 @deffnx {C Function} scm_char_set_count (pred, cs)
2558 Return the number of the elements int the character set
2559 @var{cs} which satisfy the predicate @var{pred}.
2562 @deffn {Scheme Procedure} char-set->list cs
2563 @deffnx {C Function} scm_char_set_to_list (cs)
2564 Return a list containing the elements of the character set
2568 @deffn {Scheme Procedure} char-set->string cs
2569 @deffnx {C Function} scm_char_set_to_string (cs)
2570 Return a string containing the elements of the character set
2571 @var{cs}. The order in which the characters are placed in the
2572 string is not defined.
2575 @deffn {Scheme Procedure} char-set-contains? cs ch
2576 @deffnx {C Function} scm_char_set_contains_p (cs, ch)
2577 Return @code{#t} iff the character @var{ch} is contained in the
2578 character set @var{cs}.
2581 @deffn {Scheme Procedure} char-set-every pred cs
2582 @deffnx {C Function} scm_char_set_every (pred, cs)
2583 Return a true value if every character in the character set
2584 @var{cs} satisfies the predicate @var{pred}.
2587 @deffn {Scheme Procedure} char-set-any pred cs
2588 @deffnx {C Function} scm_char_set_any (pred, cs)
2589 Return a true value if any character in the character set
2590 @var{cs} satisfies the predicate @var{pred}.
2593 @c ===================================================================
2595 @node Character-Set Algebra
2596 @subsubsection Character-Set Algebra
2598 Character sets can be manipulated with the common set algebra operation,
2599 such as union, complement, intersection etc. All of these procedures
2600 provide side-effecting variants, which modify their character set
2603 @deffn {Scheme Procedure} char-set-adjoin cs . rest
2604 @deffnx {C Function} scm_char_set_adjoin (cs, rest)
2605 Add all character arguments to the first argument, which must
2609 @deffn {Scheme Procedure} char-set-delete cs . rest
2610 @deffnx {C Function} scm_char_set_delete (cs, rest)
2611 Delete all character arguments from the first argument, which
2612 must be a character set.
2615 @deffn {Scheme Procedure} char-set-adjoin! cs . rest
2616 @deffnx {C Function} scm_char_set_adjoin_x (cs, rest)
2617 Add all character arguments to the first argument, which must
2621 @deffn {Scheme Procedure} char-set-delete! cs . rest
2622 @deffnx {C Function} scm_char_set_delete_x (cs, rest)
2623 Delete all character arguments from the first argument, which
2624 must be a character set.
2627 @deffn {Scheme Procedure} char-set-complement cs
2628 @deffnx {C Function} scm_char_set_complement (cs)
2629 Return the complement of the character set @var{cs}.
2632 Note that the complement of a character set is likely to contain many
2633 reserved code points (code points that are not associated with
2634 characters). It may be helpful to modify the output of
2635 @code{char-set-complement} by computing its intersection with the set
2636 of designated code points, @code{char-set:designated}.
2638 @deffn {Scheme Procedure} char-set-union . rest
2639 @deffnx {C Function} scm_char_set_union (rest)
2640 Return the union of all argument character sets.
2643 @deffn {Scheme Procedure} char-set-intersection . rest
2644 @deffnx {C Function} scm_char_set_intersection (rest)
2645 Return the intersection of all argument character sets.
2648 @deffn {Scheme Procedure} char-set-difference cs1 . rest
2649 @deffnx {C Function} scm_char_set_difference (cs1, rest)
2650 Return the difference of all argument character sets.
2653 @deffn {Scheme Procedure} char-set-xor . rest
2654 @deffnx {C Function} scm_char_set_xor (rest)
2655 Return the exclusive-or of all argument character sets.
2658 @deffn {Scheme Procedure} char-set-diff+intersection cs1 . rest
2659 @deffnx {C Function} scm_char_set_diff_plus_intersection (cs1, rest)
2660 Return the difference and the intersection of all argument
2664 @deffn {Scheme Procedure} char-set-complement! cs
2665 @deffnx {C Function} scm_char_set_complement_x (cs)
2666 Return the complement of the character set @var{cs}.
2669 @deffn {Scheme Procedure} char-set-union! cs1 . rest
2670 @deffnx {C Function} scm_char_set_union_x (cs1, rest)
2671 Return the union of all argument character sets.
2674 @deffn {Scheme Procedure} char-set-intersection! cs1 . rest
2675 @deffnx {C Function} scm_char_set_intersection_x (cs1, rest)
2676 Return the intersection of all argument character sets.
2679 @deffn {Scheme Procedure} char-set-difference! cs1 . rest
2680 @deffnx {C Function} scm_char_set_difference_x (cs1, rest)
2681 Return the difference of all argument character sets.
2684 @deffn {Scheme Procedure} char-set-xor! cs1 . rest
2685 @deffnx {C Function} scm_char_set_xor_x (cs1, rest)
2686 Return the exclusive-or of all argument character sets.
2689 @deffn {Scheme Procedure} char-set-diff+intersection! cs1 cs2 . rest
2690 @deffnx {C Function} scm_char_set_diff_plus_intersection_x (cs1, cs2, rest)
2691 Return the difference and the intersection of all argument
2695 @c ===================================================================
2697 @node Standard Character Sets
2698 @subsubsection Standard Character Sets
2700 In order to make the use of the character set data type and procedures
2701 useful, several predefined character set variables exist.
2707 These character sets are locale independent and are not recomputed
2708 upon a @code{setlocale} call. They contain characters from the whole
2709 range of Unicode code points. For instance, @code{char-set:letter}
2710 contains about 94,000 characters.
2712 @defvr {Scheme Variable} char-set:lower-case
2713 @defvrx {C Variable} scm_char_set_lower_case
2714 All lower-case characters.
2717 @defvr {Scheme Variable} char-set:upper-case
2718 @defvrx {C Variable} scm_char_set_upper_case
2719 All upper-case characters.
2722 @defvr {Scheme Variable} char-set:title-case
2723 @defvrx {C Variable} scm_char_set_title_case
2724 All single characters that function as if they were an upper-case
2725 letter followed by a lower-case letter.
2728 @defvr {Scheme Variable} char-set:letter
2729 @defvrx {C Variable} scm_char_set_letter
2730 All letters. This includes @code{char-set:lower-case},
2731 @code{char-set:upper-case}, @code{char-set:title-case}, and many
2732 letters that have no case at all. For example, Chinese and Japanese
2733 characters typically have no concept of case.
2736 @defvr {Scheme Variable} char-set:digit
2737 @defvrx {C Variable} scm_char_set_digit
2741 @defvr {Scheme Variable} char-set:letter+digit
2742 @defvrx {C Variable} scm_char_set_letter_and_digit
2743 The union of @code{char-set:letter} and @code{char-set:digit}.
2746 @defvr {Scheme Variable} char-set:graphic
2747 @defvrx {C Variable} scm_char_set_graphic
2748 All characters which would put ink on the paper.
2751 @defvr {Scheme Variable} char-set:printing
2752 @defvrx {C Variable} scm_char_set_printing
2753 The union of @code{char-set:graphic} and @code{char-set:whitespace}.
2756 @defvr {Scheme Variable} char-set:whitespace
2757 @defvrx {C Variable} scm_char_set_whitespace
2758 All whitespace characters.
2761 @defvr {Scheme Variable} char-set:blank
2762 @defvrx {C Variable} scm_char_set_blank
2763 All horizontal whitespace characters, which notably includes
2764 @code{#\space} and @code{#\tab}.
2767 @defvr {Scheme Variable} char-set:iso-control
2768 @defvrx {C Variable} scm_char_set_iso_control
2769 The ISO control characters are the C0 control characters (U+0000 to
2770 U+001F), delete (U+007F), and the C1 control characters (U+0080 to
2774 @defvr {Scheme Variable} char-set:punctuation
2775 @defvrx {C Variable} scm_char_set_punctuation
2776 All punctuation characters, such as the characters
2777 @code{!"#%&'()*,-./:;?@@[\\]_@{@}}
2780 @defvr {Scheme Variable} char-set:symbol
2781 @defvrx {C Variable} scm_char_set_symbol
2782 All symbol characters, such as the characters @code{$+<=>^`|~}.
2785 @defvr {Scheme Variable} char-set:hex-digit
2786 @defvrx {C Variable} scm_char_set_hex_digit
2787 The hexadecimal digits @code{0123456789abcdefABCDEF}.
2790 @defvr {Scheme Variable} char-set:ascii
2791 @defvrx {C Variable} scm_char_set_ascii
2792 All ASCII characters.
2795 @defvr {Scheme Variable} char-set:empty
2796 @defvrx {C Variable} scm_char_set_empty
2797 The empty character set.
2800 @defvr {Scheme Variable} char-set:designated
2801 @defvrx {C Variable} scm_char_set_designated
2802 This character set contains all designated code points. This includes
2803 all the code points to which Unicode has assigned a character or other
2807 @defvr {Scheme Variable} char-set:full
2808 @defvrx {C Variable} scm_char_set_full
2809 This character set contains all possible code points. This includes
2810 both designated and reserved code points.
2817 Strings are fixed-length sequences of characters. They can be created
2818 by calling constructor procedures, but they can also literally get
2819 entered at the @acronym{REPL} or in Scheme source files.
2821 @c Guile provides a rich set of string processing procedures, because text
2822 @c handling is very important when Guile is used as a scripting language.
2824 Strings always carry the information about how many characters they are
2825 composed of with them, so there is no special end-of-string character,
2826 like in C. That means that Scheme strings can contain any character,
2827 even the @samp{#\nul} character @samp{\0}.
2829 To use strings efficiently, you need to know a bit about how Guile
2830 implements them. In Guile, a string consists of two parts, a head and
2831 the actual memory where the characters are stored. When a string (or
2832 a substring of it) is copied, only a new head gets created, the memory
2833 is usually not copied. The two heads start out pointing to the same
2836 When one of these two strings is modified, as with @code{string-set!},
2837 their common memory does get copied so that each string has its own
2838 memory and modifying one does not accidentally modify the other as well.
2839 Thus, Guile's strings are `copy on write'; the actual copying of their
2840 memory is delayed until one string is written to.
2842 This implementation makes functions like @code{substring} very
2843 efficient in the common case that no modifications are done to the
2846 If you do know that your strings are getting modified right away, you
2847 can use @code{substring/copy} instead of @code{substring}. This
2848 function performs the copy immediately at the time of creation. This
2849 is more efficient, especially in a multi-threaded program. Also,
2850 @code{substring/copy} can avoid the problem that a short substring
2851 holds on to the memory of a very large original string that could
2852 otherwise be recycled.
2854 If you want to avoid the copy altogether, so that modifications of one
2855 string show up in the other, you can use @code{substring/shared}. The
2856 strings created by this procedure are called @dfn{mutation sharing
2857 substrings} since the substring and the original string share
2858 modifications to each other.
2860 If you want to prevent modifications, use @code{substring/read-only}.
2862 Guile provides all procedures of SRFI-13 and a few more.
2865 * String Syntax:: Read syntax for strings.
2866 * String Predicates:: Testing strings for certain properties.
2867 * String Constructors:: Creating new string objects.
2868 * List/String Conversion:: Converting from/to lists of characters.
2869 * String Selection:: Select portions from strings.
2870 * String Modification:: Modify parts or whole strings.
2871 * String Comparison:: Lexicographic ordering predicates.
2872 * String Searching:: Searching in strings.
2873 * Alphabetic Case Mapping:: Convert the alphabetic case of strings.
2874 * Reversing and Appending Strings:: Appending strings to form a new string.
2875 * Mapping Folding and Unfolding:: Iterating over strings.
2876 * Miscellaneous String Operations:: Replicating, insertion, parsing, ...
2877 * Conversion to/from C::
2878 * String Internals:: The storage strategy for strings.
2882 @subsubsection String Read Syntax
2884 @c In the following @code is used to get a good font in TeX etc, but
2885 @c is omitted for Info format, so as not to risk any confusion over
2886 @c whether surrounding ` ' quotes are part of the escape or are
2887 @c special in a string (they're not).
2889 The read syntax for strings is an arbitrarily long sequence of
2890 characters enclosed in double quotes (@nicode{"}).
2892 Backslash is an escape character and can be used to insert the following
2893 special characters. @nicode{\"} and @nicode{\\} are R5RS standard, the
2894 next seven are R6RS standard --- notice they follow C syntax --- and the
2895 remaining four are Guile extensions.
2899 Backslash character.
2902 Double quote character (an unescaped @nicode{"} is otherwise the end
2906 Bell character (ASCII 7).
2909 Formfeed character (ASCII 12).
2912 Newline character (ASCII 10).
2915 Carriage return character (ASCII 13).
2918 Tab character (ASCII 9).
2921 Vertical tab character (ASCII 11).
2924 Backspace character (ASCII 8).
2927 NUL character (ASCII 0).
2929 @item @nicode{\} followed by newline (ASCII 10)
2930 Nothing. This way if @nicode{\} is the last character in a line, the
2931 string will continue with the first character from the next line,
2932 without a line break.
2934 If the @code{hungry-eol-escapes} reader option is enabled, which is not
2935 the case by default, leading whitespace on the next line is discarded.
2941 (read-enable 'hungry-eol-escapes)
2947 Character code given by two hexadecimal digits. For example
2948 @nicode{\x7f} for an ASCII DEL (127).
2950 @item @nicode{\uHHHH}
2951 Character code given by four hexadecimal digits. For example
2952 @nicode{\u0100} for a capital A with macron (U+0100).
2954 @item @nicode{\UHHHHHH}
2955 Character code given by six hexadecimal digits. For example
2960 The following are examples of string literals:
2969 The three escape sequences @code{\xHH}, @code{\uHHHH} and @code{\UHHHHHH} were
2970 chosen to not break compatibility with code written for previous versions of
2971 Guile. The R6RS specification suggests a different, incompatible syntax for hex
2972 escapes: @code{\xHHHH;} -- a character code followed by one to eight hexadecimal
2973 digits terminated with a semicolon. If this escape format is desired instead,
2974 it can be enabled with the reader option @code{r6rs-hex-escapes}.
2977 (read-enable 'r6rs-hex-escapes)
2980 For more on reader options, @xref{Scheme Read}.
2982 @node String Predicates
2983 @subsubsection String Predicates
2985 The following procedures can be used to check whether a given string
2986 fulfills some specified property.
2989 @deffn {Scheme Procedure} string? obj
2990 @deffnx {C Function} scm_string_p (obj)
2991 Return @code{#t} if @var{obj} is a string, else @code{#f}.
2994 @deftypefn {C Function} int scm_is_string (SCM obj)
2995 Returns @code{1} if @var{obj} is a string, @code{0} otherwise.
2998 @deffn {Scheme Procedure} string-null? str
2999 @deffnx {C Function} scm_string_null_p (str)
3000 Return @code{#t} if @var{str}'s length is zero, and
3001 @code{#f} otherwise.
3003 (string-null? "") @result{} #t
3005 (string-null? y) @result{} #f
3009 @deffn {Scheme Procedure} string-any char_pred s [start [end]]
3010 @deffnx {C Function} scm_string_any (char_pred, s, start, end)
3011 Check if @var{char_pred} is true for any character in string @var{s}.
3013 @var{char_pred} can be a character to check for any equal to that, or
3014 a character set (@pxref{Character Sets}) to check for any in that set,
3015 or a predicate procedure to call.
3017 For a procedure, calls @code{(@var{char_pred} c)} are made
3018 successively on the characters from @var{start} to @var{end}. If
3019 @var{char_pred} returns true (ie.@: non-@code{#f}), @code{string-any}
3020 stops and that return value is the return from @code{string-any}. The
3021 call on the last character (ie.@: at @math{@var{end}-1}), if that
3022 point is reached, is a tail call.
3024 If there are no characters in @var{s} (ie.@: @var{start} equals
3025 @var{end}) then the return is @code{#f}.
3028 @deffn {Scheme Procedure} string-every char_pred s [start [end]]
3029 @deffnx {C Function} scm_string_every (char_pred, s, start, end)
3030 Check if @var{char_pred} is true for every character in string
3033 @var{char_pred} can be a character to check for every character equal
3034 to that, or a character set (@pxref{Character Sets}) to check for
3035 every character being in that set, or a predicate procedure to call.
3037 For a procedure, calls @code{(@var{char_pred} c)} are made
3038 successively on the characters from @var{start} to @var{end}. If
3039 @var{char_pred} returns @code{#f}, @code{string-every} stops and
3040 returns @code{#f}. The call on the last character (ie.@: at
3041 @math{@var{end}-1}), if that point is reached, is a tail call and the
3042 return from that call is the return from @code{string-every}.
3044 If there are no characters in @var{s} (ie.@: @var{start} equals
3045 @var{end}) then the return is @code{#t}.
3048 @node String Constructors
3049 @subsubsection String Constructors
3051 The string constructor procedures create new string objects, possibly
3052 initializing them with some specified character data. See also
3053 @xref{String Selection}, for ways to create strings from existing
3056 @c FIXME::martin: list->string belongs into `List/String Conversion'
3058 @deffn {Scheme Procedure} string char@dots{}
3060 Return a newly allocated string made from the given character
3064 (string #\x #\y #\z) @result{} "xyz"
3065 (string) @result{} ""
3069 @deffn {Scheme Procedure} list->string lst
3070 @deffnx {C Function} scm_string (lst)
3071 @rnindex list->string
3072 Return a newly allocated string made from a list of characters.
3075 (list->string '(#\a #\b #\c)) @result{} "abc"
3079 @deffn {Scheme Procedure} reverse-list->string lst
3080 @deffnx {C Function} scm_reverse_list_to_string (lst)
3081 Return a newly allocated string made from a list of characters, in
3085 (reverse-list->string '(#\a #\B #\c)) @result{} "cBa"
3089 @rnindex make-string
3090 @deffn {Scheme Procedure} make-string k [chr]
3091 @deffnx {C Function} scm_make_string (k, chr)
3092 Return a newly allocated string of
3093 length @var{k}. If @var{chr} is given, then all elements of
3094 the string are initialized to @var{chr}, otherwise the contents
3095 of the @var{string} are unspecified.
3098 @deftypefn {C Function} SCM scm_c_make_string (size_t len, SCM chr)
3099 Like @code{scm_make_string}, but expects the length as a
3103 @deffn {Scheme Procedure} string-tabulate proc len
3104 @deffnx {C Function} scm_string_tabulate (proc, len)
3105 @var{proc} is an integer->char procedure. Construct a string
3106 of size @var{len} by applying @var{proc} to each index to
3107 produce the corresponding string element. The order in which
3108 @var{proc} is applied to the indices is not specified.
3111 @deffn {Scheme Procedure} string-join ls [delimiter [grammar]]
3112 @deffnx {C Function} scm_string_join (ls, delimiter, grammar)
3113 Append the string in the string list @var{ls}, using the string
3114 @var{delim} as a delimiter between the elements of @var{ls}.
3115 @var{grammar} is a symbol which specifies how the delimiter is
3116 placed between the strings, and defaults to the symbol
3121 Insert the separator between list elements. An empty string
3122 will produce an empty list.
3124 Like @code{infix}, but will raise an error if given the empty
3127 Insert the separator after every list element.
3129 Insert the separator before each list element.
3133 @node List/String Conversion
3134 @subsubsection List/String conversion
3136 When processing strings, it is often convenient to first convert them
3137 into a list representation by using the procedure @code{string->list},
3138 work with the resulting list, and then convert it back into a string.
3139 These procedures are useful for similar tasks.
3141 @rnindex string->list
3142 @deffn {Scheme Procedure} string->list str [start [end]]
3143 @deffnx {C Function} scm_substring_to_list (str, start, end)
3144 @deffnx {C Function} scm_string_to_list (str)
3145 Convert the string @var{str} into a list of characters.
3148 @deffn {Scheme Procedure} string-split str chr
3149 @deffnx {C Function} scm_string_split (str, chr)
3150 Split the string @var{str} into a list of substrings delimited
3151 by appearances of the character @var{chr}. Note that an empty substring
3152 between separator characters will result in an empty string in the
3156 (string-split "root:x:0:0:root:/root:/bin/bash" #\:)
3158 ("root" "x" "0" "0" "root" "/root" "/bin/bash")
3160 (string-split "::" #\:)
3164 (string-split "" #\:)
3171 @node String Selection
3172 @subsubsection String Selection
3174 Portions of strings can be extracted by these procedures.
3175 @code{string-ref} delivers individual characters whereas
3176 @code{substring} can be used to extract substrings from longer strings.
3178 @rnindex string-length
3179 @deffn {Scheme Procedure} string-length string
3180 @deffnx {C Function} scm_string_length (string)
3181 Return the number of characters in @var{string}.
3184 @deftypefn {C Function} size_t scm_c_string_length (SCM str)
3185 Return the number of characters in @var{str} as a @code{size_t}.
3189 @deffn {Scheme Procedure} string-ref str k
3190 @deffnx {C Function} scm_string_ref (str, k)
3191 Return character @var{k} of @var{str} using zero-origin
3192 indexing. @var{k} must be a valid index of @var{str}.
3195 @deftypefn {C Function} SCM scm_c_string_ref (SCM str, size_t k)
3196 Return character @var{k} of @var{str} using zero-origin
3197 indexing. @var{k} must be a valid index of @var{str}.
3200 @rnindex string-copy
3201 @deffn {Scheme Procedure} string-copy str [start [end]]
3202 @deffnx {C Function} scm_substring_copy (str, start, end)
3203 @deffnx {C Function} scm_string_copy (str)
3204 Return a copy of the given string @var{str}.
3206 The returned string shares storage with @var{str} initially, but it is
3207 copied as soon as one of the two strings is modified.
3211 @deffn {Scheme Procedure} substring str start [end]
3212 @deffnx {C Function} scm_substring (str, start, end)
3213 Return a new string formed from the characters
3214 of @var{str} beginning with index @var{start} (inclusive) and
3215 ending with index @var{end} (exclusive).
3216 @var{str} must be a string, @var{start} and @var{end} must be
3217 exact integers satisfying:
3219 0 <= @var{start} <= @var{end} <= @code{(string-length @var{str})}.
3221 The returned string shares storage with @var{str} initially, but it is
3222 copied as soon as one of the two strings is modified.
3225 @deffn {Scheme Procedure} substring/shared str start [end]
3226 @deffnx {C Function} scm_substring_shared (str, start, end)
3227 Like @code{substring}, but the strings continue to share their storage
3228 even if they are modified. Thus, modifications to @var{str} show up
3229 in the new string, and vice versa.
3232 @deffn {Scheme Procedure} substring/copy str start [end]
3233 @deffnx {C Function} scm_substring_copy (str, start, end)
3234 Like @code{substring}, but the storage for the new string is copied
3238 @deffn {Scheme Procedure} substring/read-only str start [end]
3239 @deffnx {C Function} scm_substring_read_only (str, start, end)
3240 Like @code{substring}, but the resulting string can not be modified.
3243 @deftypefn {C Function} SCM scm_c_substring (SCM str, size_t start, size_t end)
3244 @deftypefnx {C Function} SCM scm_c_substring_shared (SCM str, size_t start, size_t end)
3245 @deftypefnx {C Function} SCM scm_c_substring_copy (SCM str, size_t start, size_t end)
3246 @deftypefnx {C Function} SCM scm_c_substring_read_only (SCM str, size_t start, size_t end)
3247 Like @code{scm_substring}, etc. but the bounds are given as a @code{size_t}.
3250 @deffn {Scheme Procedure} string-take s n
3251 @deffnx {C Function} scm_string_take (s, n)
3252 Return the @var{n} first characters of @var{s}.
3255 @deffn {Scheme Procedure} string-drop s n
3256 @deffnx {C Function} scm_string_drop (s, n)
3257 Return all but the first @var{n} characters of @var{s}.
3260 @deffn {Scheme Procedure} string-take-right s n
3261 @deffnx {C Function} scm_string_take_right (s, n)
3262 Return the @var{n} last characters of @var{s}.
3265 @deffn {Scheme Procedure} string-drop-right s n
3266 @deffnx {C Function} scm_string_drop_right (s, n)
3267 Return all but the last @var{n} characters of @var{s}.
3270 @deffn {Scheme Procedure} string-pad s len [chr [start [end]]]
3271 @deffnx {Scheme Procedure} string-pad-right s len [chr [start [end]]]
3272 @deffnx {C Function} scm_string_pad (s, len, chr, start, end)
3273 @deffnx {C Function} scm_string_pad_right (s, len, chr, start, end)
3274 Take characters @var{start} to @var{end} from the string @var{s} and
3275 either pad with @var{char} or truncate them to give @var{len}
3278 @code{string-pad} pads or truncates on the left, so for example
3281 (string-pad "x" 3) @result{} " x"
3282 (string-pad "abcde" 3) @result{} "cde"
3285 @code{string-pad-right} pads or truncates on the right, so for example
3288 (string-pad-right "x" 3) @result{} "x "
3289 (string-pad-right "abcde" 3) @result{} "abc"
3293 @deffn {Scheme Procedure} string-trim s [char_pred [start [end]]]
3294 @deffnx {Scheme Procedure} string-trim-right s [char_pred [start [end]]]
3295 @deffnx {Scheme Procedure} string-trim-both s [char_pred [start [end]]]
3296 @deffnx {C Function} scm_string_trim (s, char_pred, start, end)
3297 @deffnx {C Function} scm_string_trim_right (s, char_pred, start, end)
3298 @deffnx {C Function} scm_string_trim_both (s, char_pred, start, end)
3299 Trim occurrences of @var{char_pred} from the ends of @var{s}.
3301 @code{string-trim} trims @var{char_pred} characters from the left
3302 (start) of the string, @code{string-trim-right} trims them from the
3303 right (end) of the string, @code{string-trim-both} trims from both
3306 @var{char_pred} can be a character, a character set, or a predicate
3307 procedure to call on each character. If @var{char_pred} is not given
3308 the default is whitespace as per @code{char-set:whitespace}
3309 (@pxref{Standard Character Sets}).
3312 (string-trim " x ") @result{} "x "
3313 (string-trim-right "banana" #\a) @result{} "banan"
3314 (string-trim-both ".,xy:;" char-set:punctuation)
3316 (string-trim-both "xyzzy" (lambda (c)
3323 @node String Modification
3324 @subsubsection String Modification
3326 These procedures are for modifying strings in-place. This means that the
3327 result of the operation is not a new string; instead, the original string's
3328 memory representation is modified.
3330 @rnindex string-set!
3331 @deffn {Scheme Procedure} string-set! str k chr
3332 @deffnx {C Function} scm_string_set_x (str, k, chr)
3333 Store @var{chr} in element @var{k} of @var{str} and return
3334 an unspecified value. @var{k} must be a valid index of
3338 @deftypefn {C Function} void scm_c_string_set_x (SCM str, size_t k, SCM chr)
3339 Like @code{scm_string_set_x}, but the index is given as a @code{size_t}.
3342 @rnindex string-fill!
3343 @deffn {Scheme Procedure} string-fill! str chr [start [end]]
3344 @deffnx {C Function} scm_substring_fill_x (str, chr, start, end)
3345 @deffnx {C Function} scm_string_fill_x (str, chr)
3346 Stores @var{chr} in every element of the given @var{str} and
3347 returns an unspecified value.
3350 @deffn {Scheme Procedure} substring-fill! str start end fill
3351 @deffnx {C Function} scm_substring_fill_x (str, start, end, fill)
3352 Change every character in @var{str} between @var{start} and
3353 @var{end} to @var{fill}.
3356 (define y "abcdefg")
3357 (substring-fill! y 1 3 #\r)
3363 @deffn {Scheme Procedure} substring-move! str1 start1 end1 str2 start2
3364 @deffnx {C Function} scm_substring_move_x (str1, start1, end1, str2, start2)
3365 Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}
3366 into @var{str2} beginning at position @var{start2}.
3367 @var{str1} and @var{str2} can be the same string.
3370 @deffn {Scheme Procedure} string-copy! target tstart s [start [end]]
3371 @deffnx {C Function} scm_string_copy_x (target, tstart, s, start, end)
3372 Copy the sequence of characters from index range [@var{start},
3373 @var{end}) in string @var{s} to string @var{target}, beginning
3374 at index @var{tstart}. The characters are copied left-to-right
3375 or right-to-left as needed -- the copy is guaranteed to work,
3376 even if @var{target} and @var{s} are the same string. It is an
3377 error if the copy operation runs off the end of the target
3382 @node String Comparison
3383 @subsubsection String Comparison
3385 The procedures in this section are similar to the character ordering
3386 predicates (@pxref{Characters}), but are defined on character sequences.
3388 The first set is specified in R5RS and has names that end in @code{?}.
3389 The second set is specified in SRFI-13 and the names have not ending
3392 The predicates ending in @code{-ci} ignore the character case
3393 when comparing strings. For now, case-insensitive comparison is done
3394 using the R5RS rules, where every lower-case character that has a
3395 single character upper-case form is converted to uppercase before
3396 comparison. See @xref{Text Collation, the @code{(ice-9
3397 i18n)} module}, for locale-dependent string comparison.
3400 @deffn {Scheme Procedure} string=? [s1 [s2 . rest]]
3401 @deffnx {C Function} scm_i_string_equal_p (s1, s2, rest)
3402 Lexicographic equality predicate; return @code{#t} if the two
3403 strings are the same length and contain the same characters in
3404 the same positions, otherwise return @code{#f}.
3406 The procedure @code{string-ci=?} treats upper and lower case
3407 letters as though they were the same character, but
3408 @code{string=?} treats upper and lower case as distinct
3413 @deffn {Scheme Procedure} string<? [s1 [s2 . rest]]
3414 @deffnx {C Function} scm_i_string_less_p (s1, s2, rest)
3415 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3416 is lexicographically less than @var{s2}.
3420 @deffn {Scheme Procedure} string<=? [s1 [s2 . rest]]
3421 @deffnx {C Function} scm_i_string_leq_p (s1, s2, rest)
3422 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3423 is lexicographically less than or equal to @var{s2}.
3427 @deffn {Scheme Procedure} string>? [s1 [s2 . rest]]
3428 @deffnx {C Function} scm_i_string_gr_p (s1, s2, rest)
3429 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3430 is lexicographically greater than @var{s2}.
3434 @deffn {Scheme Procedure} string>=? [s1 [s2 . rest]]
3435 @deffnx {C Function} scm_i_string_geq_p (s1, s2, rest)
3436 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3437 is lexicographically greater than or equal to @var{s2}.
3440 @rnindex string-ci=?
3441 @deffn {Scheme Procedure} string-ci=? [s1 [s2 . rest]]
3442 @deffnx {C Function} scm_i_string_ci_equal_p (s1, s2, rest)
3443 Case-insensitive string equality predicate; return @code{#t} if
3444 the two strings are the same length and their component
3445 characters match (ignoring case) at each position; otherwise
3449 @rnindex string-ci<?
3450 @deffn {Scheme Procedure} string-ci<? [s1 [s2 . rest]]
3451 @deffnx {C Function} scm_i_string_ci_less_p (s1, s2, rest)
3452 Case insensitive lexicographic ordering predicate; return
3453 @code{#t} if @var{s1} is lexicographically less than @var{s2}
3458 @deffn {Scheme Procedure} string-ci<=? [s1 [s2 . rest]]
3459 @deffnx {C Function} scm_i_string_ci_leq_p (s1, s2, rest)
3460 Case insensitive lexicographic ordering predicate; return
3461 @code{#t} if @var{s1} is lexicographically less than or equal
3462 to @var{s2} regardless of case.
3465 @rnindex string-ci>?
3466 @deffn {Scheme Procedure} string-ci>? [s1 [s2 . rest]]
3467 @deffnx {C Function} scm_i_string_ci_gr_p (s1, s2, rest)
3468 Case insensitive lexicographic ordering predicate; return
3469 @code{#t} if @var{s1} is lexicographically greater than
3470 @var{s2} regardless of case.
3473 @rnindex string-ci>=?
3474 @deffn {Scheme Procedure} string-ci>=? [s1 [s2 . rest]]
3475 @deffnx {C Function} scm_i_string_ci_geq_p (s1, s2, rest)
3476 Case insensitive lexicographic ordering predicate; return
3477 @code{#t} if @var{s1} is lexicographically greater than or
3478 equal to @var{s2} regardless of case.
3481 @deffn {Scheme Procedure} string-compare s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
3482 @deffnx {C Function} scm_string_compare (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
3483 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
3484 mismatch index, depending upon whether @var{s1} is less than,
3485 equal to, or greater than @var{s2}. The mismatch index is the
3486 largest index @var{i} such that for every 0 <= @var{j} <
3487 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
3488 @var{i} is the first position that does not match.
3491 @deffn {Scheme Procedure} string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
3492 @deffnx {C Function} scm_string_compare_ci (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
3493 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
3494 mismatch index, depending upon whether @var{s1} is less than,
3495 equal to, or greater than @var{s2}. The mismatch index is the
3496 largest index @var{i} such that for every 0 <= @var{j} <
3497 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
3498 @var{i} is the first position where the lowercased letters
3503 @deffn {Scheme Procedure} string= s1 s2 [start1 [end1 [start2 [end2]]]]
3504 @deffnx {C Function} scm_string_eq (s1, s2, start1, end1, start2, end2)
3505 Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
3509 @deffn {Scheme Procedure} string<> s1 s2 [start1 [end1 [start2 [end2]]]]
3510 @deffnx {C Function} scm_string_neq (s1, s2, start1, end1, start2, end2)
3511 Return @code{#f} if @var{s1} and @var{s2} are equal, a true
3515 @deffn {Scheme Procedure} string< s1 s2 [start1 [end1 [start2 [end2]]]]
3516 @deffnx {C Function} scm_string_lt (s1, s2, start1, end1, start2, end2)
3517 Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
3518 true value otherwise.
3521 @deffn {Scheme Procedure} string> s1 s2 [start1 [end1 [start2 [end2]]]]
3522 @deffnx {C Function} scm_string_gt (s1, s2, start1, end1, start2, end2)
3523 Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
3524 true value otherwise.
3527 @deffn {Scheme Procedure} string<= s1 s2 [start1 [end1 [start2 [end2]]]]
3528 @deffnx {C Function} scm_string_le (s1, s2, start1, end1, start2, end2)
3529 Return @code{#f} if @var{s1} is greater to @var{s2}, a true
3533 @deffn {Scheme Procedure} string>= s1 s2 [start1 [end1 [start2 [end2]]]]
3534 @deffnx {C Function} scm_string_ge (s1, s2, start1, end1, start2, end2)
3535 Return @code{#f} if @var{s1} is less to @var{s2}, a true value
3539 @deffn {Scheme Procedure} string-ci= s1 s2 [start1 [end1 [start2 [end2]]]]
3540 @deffnx {C Function} scm_string_ci_eq (s1, s2, start1, end1, start2, end2)
3541 Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
3542 value otherwise. The character comparison is done
3546 @deffn {Scheme Procedure} string-ci<> s1 s2 [start1 [end1 [start2 [end2]]]]
3547 @deffnx {C Function} scm_string_ci_neq (s1, s2, start1, end1, start2, end2)
3548 Return @code{#f} if @var{s1} and @var{s2} are equal, a true
3549 value otherwise. The character comparison is done
3553 @deffn {Scheme Procedure} string-ci< s1 s2 [start1 [end1 [start2 [end2]]]]
3554 @deffnx {C Function} scm_string_ci_lt (s1, s2, start1, end1, start2, end2)
3555 Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
3556 true value otherwise. The character comparison is done
3560 @deffn {Scheme Procedure} string-ci> s1 s2 [start1 [end1 [start2 [end2]]]]
3561 @deffnx {C Function} scm_string_ci_gt (s1, s2, start1, end1, start2, end2)
3562 Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
3563 true value otherwise. The character comparison is done
3567 @deffn {Scheme Procedure} string-ci<= s1 s2 [start1 [end1 [start2 [end2]]]]
3568 @deffnx {C Function} scm_string_ci_le (s1, s2, start1, end1, start2, end2)
3569 Return @code{#f} if @var{s1} is greater to @var{s2}, a true
3570 value otherwise. The character comparison is done
3574 @deffn {Scheme Procedure} string-ci>= s1 s2 [start1 [end1 [start2 [end2]]]]
3575 @deffnx {C Function} scm_string_ci_ge (s1, s2, start1, end1, start2, end2)
3576 Return @code{#f} if @var{s1} is less to @var{s2}, a true value
3577 otherwise. The character comparison is done
3581 @deffn {Scheme Procedure} string-hash s [bound [start [end]]]
3582 @deffnx {C Function} scm_substring_hash (s, bound, start, end)
3583 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).
3586 @deffn {Scheme Procedure} string-hash-ci s [bound [start [end]]]
3587 @deffnx {C Function} scm_substring_hash_ci (s, bound, start, end)
3588 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).
3591 Because the same visual appearance of an abstract Unicode character can
3592 be obtained via multiple sequences of Unicode characters, even the
3593 case-insensitive string comparison functions described above may return
3594 @code{#f} when presented with strings containing different
3595 representations of the same character. For example, the Unicode
3596 character ``LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE'' can be
3597 represented with a single character (U+1E69) or by the character ``LATIN
3598 SMALL LETTER S'' (U+0073) followed by the combining marks ``COMBINING
3599 DOT BELOW'' (U+0323) and ``COMBINING DOT ABOVE'' (U+0307).
3601 For this reason, it is often desirable to ensure that the strings
3602 to be compared are using a mutually consistent representation for every
3603 character. The Unicode standard defines two methods of normalizing the
3604 contents of strings: Decomposition, which breaks composite characters
3605 into a set of constituent characters with an ordering defined by the
3606 Unicode Standard; and composition, which performs the converse.
3608 There are two decomposition operations. ``Canonical decomposition''
3609 produces character sequences that share the same visual appearance as
3610 the original characters, while ``compatibility decomposition'' produces
3611 ones whose visual appearances may differ from the originals but which
3612 represent the same abstract character.
3614 These operations are encapsulated in the following set of normalization
3619 Characters are decomposed to their canonical forms.
3622 Characters are decomposed to their compatibility forms.
3625 Characters are decomposed to their canonical forms, then composed.
3628 Characters are decomposed to their compatibility forms, then composed.
3632 The functions below put their arguments into one of the forms described
3635 @deffn {Scheme Procedure} string-normalize-nfd s
3636 @deffnx {C Function} scm_string_normalize_nfd (s)
3637 Return the @code{NFD} normalized form of @var{s}.
3640 @deffn {Scheme Procedure} string-normalize-nfkd s
3641 @deffnx {C Function} scm_string_normalize_nfkd (s)
3642 Return the @code{NFKD} normalized form of @var{s}.
3645 @deffn {Scheme Procedure} string-normalize-nfc s
3646 @deffnx {C Function} scm_string_normalize_nfc (s)
3647 Return the @code{NFC} normalized form of @var{s}.
3650 @deffn {Scheme Procedure} string-normalize-nfkc s
3651 @deffnx {C Function} scm_string_normalize_nfkc (s)
3652 Return the @code{NFKC} normalized form of @var{s}.
3655 @node String Searching
3656 @subsubsection String Searching
3658 @deffn {Scheme Procedure} string-index s char_pred [start [end]]
3659 @deffnx {C Function} scm_string_index (s, char_pred, start, end)
3660 Search through the string @var{s} from left to right, returning
3661 the index of the first occurrence of a character which
3665 equals @var{char_pred}, if it is character,
3668 satisfies the predicate @var{char_pred}, if it is a procedure,
3671 is in the set @var{char_pred}, if it is a character set.
3674 Return @code{#f} if no match is found.
3677 @deffn {Scheme Procedure} string-rindex s char_pred [start [end]]
3678 @deffnx {C Function} scm_string_rindex (s, char_pred, start, end)
3679 Search through the string @var{s} from right to left, returning
3680 the index of the last occurrence of a character which
3684 equals @var{char_pred}, if it is character,
3687 satisfies the predicate @var{char_pred}, if it is a procedure,
3690 is in the set if @var{char_pred} is a character set.
3693 Return @code{#f} if no match is found.
3696 @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 [end1 [start2 [end2]]]]
3697 @deffnx {C Function} scm_string_prefix_length (s1, s2, start1, end1, start2, end2)
3698 Return the length of the longest common prefix of the two
3702 @deffn {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3703 @deffnx {C Function} scm_string_prefix_length_ci (s1, s2, start1, end1, start2, end2)
3704 Return the length of the longest common prefix of the two
3705 strings, ignoring character case.
3708 @deffn {Scheme Procedure} string-suffix-length s1 s2 [start1 [end1 [start2 [end2]]]]
3709 @deffnx {C Function} scm_string_suffix_length (s1, s2, start1, end1, start2, end2)
3710 Return the length of the longest common suffix of the two
3714 @deffn {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3715 @deffnx {C Function} scm_string_suffix_length_ci (s1, s2, start1, end1, start2, end2)
3716 Return the length of the longest common suffix of the two
3717 strings, ignoring character case.
3720 @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 [end1 [start2 [end2]]]]
3721 @deffnx {C Function} scm_string_prefix_p (s1, s2, start1, end1, start2, end2)
3722 Is @var{s1} a prefix of @var{s2}?
3725 @deffn {Scheme Procedure} string-prefix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
3726 @deffnx {C Function} scm_string_prefix_ci_p (s1, s2, start1, end1, start2, end2)
3727 Is @var{s1} a prefix of @var{s2}, ignoring character case?
3730 @deffn {Scheme Procedure} string-suffix? s1 s2 [start1 [end1 [start2 [end2]]]]
3731 @deffnx {C Function} scm_string_suffix_p (s1, s2, start1, end1, start2, end2)
3732 Is @var{s1} a suffix of @var{s2}?
3735 @deffn {Scheme Procedure} string-suffix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
3736 @deffnx {C Function} scm_string_suffix_ci_p (s1, s2, start1, end1, start2, end2)
3737 Is @var{s1} a suffix of @var{s2}, ignoring character case?
3740 @deffn {Scheme Procedure} string-index-right s char_pred [start [end]]
3741 @deffnx {C Function} scm_string_index_right (s, char_pred, start, end)
3742 Search through the string @var{s} from right to left, returning
3743 the index of the last occurrence of a character which
3747 equals @var{char_pred}, if it is character,
3750 satisfies the predicate @var{char_pred}, if it is a procedure,
3753 is in the set if @var{char_pred} is a character set.
3756 Return @code{#f} if no match is found.
3759 @deffn {Scheme Procedure} string-skip s char_pred [start [end]]
3760 @deffnx {C Function} scm_string_skip (s, char_pred, start, end)
3761 Search through the string @var{s} from left to right, returning
3762 the index of the first occurrence of a character which
3766 does not equal @var{char_pred}, if it is character,
3769 does not satisfy the predicate @var{char_pred}, if it is a
3773 is not in the set if @var{char_pred} is a character set.
3777 @deffn {Scheme Procedure} string-skip-right s char_pred [start [end]]
3778 @deffnx {C Function} scm_string_skip_right (s, char_pred, start, end)
3779 Search through the string @var{s} from right to left, returning
3780 the index of the last occurrence of a character which
3784 does not equal @var{char_pred}, if it is character,
3787 does not satisfy the predicate @var{char_pred}, if it is a
3791 is not in the set if @var{char_pred} is a character set.
3795 @deffn {Scheme Procedure} string-count s char_pred [start [end]]
3796 @deffnx {C Function} scm_string_count (s, char_pred, start, end)
3797 Return the count of the number of characters in the string
3802 equals @var{char_pred}, if it is character,
3805 satisfies the predicate @var{char_pred}, if it is a procedure.
3808 is in the set @var{char_pred}, if it is a character set.
3812 @deffn {Scheme Procedure} string-contains s1 s2 [start1 [end1 [start2 [end2]]]]
3813 @deffnx {C Function} scm_string_contains (s1, s2, start1, end1, start2, end2)
3814 Does string @var{s1} contain string @var{s2}? Return the index
3815 in @var{s1} where @var{s2} occurs as a substring, or false.
3816 The optional start/end indices restrict the operation to the
3817 indicated substrings.
3820 @deffn {Scheme Procedure} string-contains-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3821 @deffnx {C Function} scm_string_contains_ci (s1, s2, start1, end1, start2, end2)
3822 Does string @var{s1} contain string @var{s2}? Return the index
3823 in @var{s1} where @var{s2} occurs as a substring, or false.
3824 The optional start/end indices restrict the operation to the
3825 indicated substrings. Character comparison is done
3829 @node Alphabetic Case Mapping
3830 @subsubsection Alphabetic Case Mapping
3832 These are procedures for mapping strings to their upper- or lower-case
3833 equivalents, respectively, or for capitalizing strings.
3835 They use the basic case mapping rules for Unicode characters. No
3836 special language or context rules are considered. The resulting strings
3837 are guaranteed to be the same length as the input strings.
3839 @xref{Character Case Mapping, the @code{(ice-9
3840 i18n)} module}, for locale-dependent case conversions.
3842 @deffn {Scheme Procedure} string-upcase str [start [end]]
3843 @deffnx {C Function} scm_substring_upcase (str, start, end)
3844 @deffnx {C Function} scm_string_upcase (str)
3845 Upcase every character in @code{str}.
3848 @deffn {Scheme Procedure} string-upcase! str [start [end]]
3849 @deffnx {C Function} scm_substring_upcase_x (str, start, end)
3850 @deffnx {C Function} scm_string_upcase_x (str)
3851 Destructively upcase every character in @code{str}.
3861 @deffn {Scheme Procedure} string-downcase str [start [end]]
3862 @deffnx {C Function} scm_substring_downcase (str, start, end)
3863 @deffnx {C Function} scm_string_downcase (str)
3864 Downcase every character in @var{str}.
3867 @deffn {Scheme Procedure} string-downcase! str [start [end]]
3868 @deffnx {C Function} scm_substring_downcase_x (str, start, end)
3869 @deffnx {C Function} scm_string_downcase_x (str)
3870 Destructively downcase every character in @var{str}.
3875 (string-downcase! y)
3882 @deffn {Scheme Procedure} string-capitalize str
3883 @deffnx {C Function} scm_string_capitalize (str)
3884 Return a freshly allocated string with the characters in
3885 @var{str}, where the first character of every word is
3889 @deffn {Scheme Procedure} string-capitalize! str
3890 @deffnx {C Function} scm_string_capitalize_x (str)
3891 Upcase the first character of every word in @var{str}
3892 destructively and return @var{str}.
3895 y @result{} "hello world"
3896 (string-capitalize! y) @result{} "Hello World"
3897 y @result{} "Hello World"
3901 @deffn {Scheme Procedure} string-titlecase str [start [end]]
3902 @deffnx {C Function} scm_string_titlecase (str, start, end)
3903 Titlecase every first character in a word in @var{str}.
3906 @deffn {Scheme Procedure} string-titlecase! str [start [end]]
3907 @deffnx {C Function} scm_string_titlecase_x (str, start, end)
3908 Destructively titlecase every first character in a word in
3912 @node Reversing and Appending Strings
3913 @subsubsection Reversing and Appending Strings
3915 @deffn {Scheme Procedure} string-reverse str [start [end]]
3916 @deffnx {C Function} scm_string_reverse (str, start, end)
3917 Reverse the string @var{str}. The optional arguments
3918 @var{start} and @var{end} delimit the region of @var{str} to
3922 @deffn {Scheme Procedure} string-reverse! str [start [end]]
3923 @deffnx {C Function} scm_string_reverse_x (str, start, end)
3924 Reverse the string @var{str} in-place. The optional arguments
3925 @var{start} and @var{end} delimit the region of @var{str} to
3926 operate on. The return value is unspecified.
3929 @rnindex string-append
3930 @deffn {Scheme Procedure} string-append . args
3931 @deffnx {C Function} scm_string_append (args)
3932 Return a newly allocated string whose characters form the
3933 concatenation of the given strings, @var{args}.
3937 (string-append h "world"))
3938 @result{} "hello world"
3942 @deffn {Scheme Procedure} string-append/shared . rest
3943 @deffnx {C Function} scm_string_append_shared (rest)
3944 Like @code{string-append}, but the result may share memory
3945 with the argument strings.
3948 @deffn {Scheme Procedure} string-concatenate ls
3949 @deffnx {C Function} scm_string_concatenate (ls)
3950 Append the elements of @var{ls} (which must be strings)
3951 together into a single string. Guaranteed to return a freshly
3955 @deffn {Scheme Procedure} string-concatenate-reverse ls [final_string [end]]
3956 @deffnx {C Function} scm_string_concatenate_reverse (ls, final_string, end)
3957 Without optional arguments, this procedure is equivalent to
3960 (string-concatenate (reverse ls))
3963 If the optional argument @var{final_string} is specified, it is
3964 consed onto the beginning to @var{ls} before performing the
3965 list-reverse and string-concatenate operations. If @var{end}
3966 is given, only the characters of @var{final_string} up to index
3969 Guaranteed to return a freshly allocated string.
3972 @deffn {Scheme Procedure} string-concatenate/shared ls
3973 @deffnx {C Function} scm_string_concatenate_shared (ls)
3974 Like @code{string-concatenate}, but the result may share memory
3975 with the strings in the list @var{ls}.
3978 @deffn {Scheme Procedure} string-concatenate-reverse/shared ls [final_string [end]]
3979 @deffnx {C Function} scm_string_concatenate_reverse_shared (ls, final_string, end)
3980 Like @code{string-concatenate-reverse}, but the result may
3981 share memory with the strings in the @var{ls} arguments.
3984 @node Mapping Folding and Unfolding
3985 @subsubsection Mapping, Folding, and Unfolding
3987 @deffn {Scheme Procedure} string-map proc s [start [end]]
3988 @deffnx {C Function} scm_string_map (proc, s, start, end)
3989 @var{proc} is a char->char procedure, it is mapped over
3990 @var{s}. The order in which the procedure is applied to the
3991 string elements is not specified.
3994 @deffn {Scheme Procedure} string-map! proc s [start [end]]
3995 @deffnx {C Function} scm_string_map_x (proc, s, start, end)
3996 @var{proc} is a char->char procedure, it is mapped over
3997 @var{s}. The order in which the procedure is applied to the
3998 string elements is not specified. The string @var{s} is
3999 modified in-place, the return value is not specified.
4002 @deffn {Scheme Procedure} string-for-each proc s [start [end]]
4003 @deffnx {C Function} scm_string_for_each (proc, s, start, end)
4004 @var{proc} is mapped over @var{s} in left-to-right order. The
4005 return value is not specified.
4008 @deffn {Scheme Procedure} string-for-each-index proc s [start [end]]
4009 @deffnx {C Function} scm_string_for_each_index (proc, s, start, end)
4010 Call @code{(@var{proc} i)} for each index i in @var{s}, from left to
4013 For example, to change characters to alternately upper and lower case,
4016 (define str (string-copy "studly"))
4017 (string-for-each-index
4020 ((if (even? i) char-upcase char-downcase)
4021 (string-ref str i))))
4023 str @result{} "StUdLy"
4027 @deffn {Scheme Procedure} string-fold kons knil s [start [end]]
4028 @deffnx {C Function} scm_string_fold (kons, knil, s, start, end)
4029 Fold @var{kons} over the characters of @var{s}, with @var{knil}
4030 as the terminating element, from left to right. @var{kons}
4031 must expect two arguments: The actual character and the last
4032 result of @var{kons}' application.
4035 @deffn {Scheme Procedure} string-fold-right kons knil s [start [end]]
4036 @deffnx {C Function} scm_string_fold_right (kons, knil, s, start, end)
4037 Fold @var{kons} over the characters of @var{s}, with @var{knil}
4038 as the terminating element, from right to left. @var{kons}
4039 must expect two arguments: The actual character and the last
4040 result of @var{kons}' application.
4043 @deffn {Scheme Procedure} string-unfold p f g seed [base [make_final]]
4044 @deffnx {C Function} scm_string_unfold (p, f, g, seed, base, make_final)
4046 @item @var{g} is used to generate a series of @emph{seed}
4047 values from the initial @var{seed}: @var{seed}, (@var{g}
4048 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
4050 @item @var{p} tells us when to stop -- when it returns true
4051 when applied to one of these seed values.
4052 @item @var{f} maps each seed value to the corresponding
4053 character in the result string. These chars are assembled
4054 into the string in a left-to-right order.
4055 @item @var{base} is the optional initial/leftmost portion
4056 of the constructed string; it default to the empty
4058 @item @var{make_final} is applied to the terminal seed
4059 value (on which @var{p} returns true) to produce
4060 the final/rightmost portion of the constructed string.
4061 The default is nothing extra.
4065 @deffn {Scheme Procedure} string-unfold-right p f g seed [base [make_final]]
4066 @deffnx {C Function} scm_string_unfold_right (p, f, g, seed, base, make_final)
4068 @item @var{g} is used to generate a series of @emph{seed}
4069 values from the initial @var{seed}: @var{seed}, (@var{g}
4070 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
4072 @item @var{p} tells us when to stop -- when it returns true
4073 when applied to one of these seed values.
4074 @item @var{f} maps each seed value to the corresponding
4075 character in the result string. These chars are assembled
4076 into the string in a right-to-left order.
4077 @item @var{base} is the optional initial/rightmost portion
4078 of the constructed string; it default to the empty
4080 @item @var{make_final} is applied to the terminal seed
4081 value (on which @var{p} returns true) to produce
4082 the final/leftmost portion of the constructed string.
4083 It defaults to @code{(lambda (x) )}.
4087 @node Miscellaneous String Operations
4088 @subsubsection Miscellaneous String Operations
4090 @deffn {Scheme Procedure} xsubstring s from [to [start [end]]]
4091 @deffnx {C Function} scm_xsubstring (s, from, to, start, end)
4092 This is the @emph{extended substring} procedure that implements
4093 replicated copying of a substring of some string.
4095 @var{s} is a string, @var{start} and @var{end} are optional
4096 arguments that demarcate a substring of @var{s}, defaulting to
4097 0 and the length of @var{s}. Replicate this substring up and
4098 down index space, in both the positive and negative directions.
4099 @code{xsubstring} returns the substring of this string
4100 beginning at index @var{from}, and ending at @var{to}, which
4101 defaults to @var{from} + (@var{end} - @var{start}).
4104 @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto [start [end]]]
4105 @deffnx {C Function} scm_string_xcopy_x (target, tstart, s, sfrom, sto, start, end)
4106 Exactly the same as @code{xsubstring}, but the extracted text
4107 is written into the string @var{target} starting at index
4108 @var{tstart}. The operation is not defined if @code{(eq?
4109 @var{target} @var{s})} or these arguments share storage -- you
4110 cannot copy a string on top of itself.
4113 @deffn {Scheme Procedure} string-replace s1 s2 [start1 [end1 [start2 [end2]]]]
4114 @deffnx {C Function} scm_string_replace (s1, s2, start1, end1, start2, end2)
4115 Return the string @var{s1}, but with the characters
4116 @var{start1} @dots{} @var{end1} replaced by the characters
4117 @var{start2} @dots{} @var{end2} from @var{s2}.
4120 @deffn {Scheme Procedure} string-tokenize s [token_set [start [end]]]
4121 @deffnx {C Function} scm_string_tokenize (s, token_set, start, end)
4122 Split the string @var{s} into a list of substrings, where each
4123 substring is a maximal non-empty contiguous sequence of
4124 characters from the character set @var{token_set}, which
4125 defaults to @code{char-set:graphic}.
4126 If @var{start} or @var{end} indices are provided, they restrict
4127 @code{string-tokenize} to operating on the indicated substring
4131 @deffn {Scheme Procedure} string-filter char_pred s [start [end]]
4132 @deffnx {C Function} scm_string_filter (char_pred, s, start, end)
4133 Filter the string @var{s}, retaining only those characters which
4134 satisfy @var{char_pred}.
4136 If @var{char_pred} is a procedure, it is applied to each character as
4137 a predicate, if it is a character, it is tested for equality and if it
4138 is a character set, it is tested for membership.
4141 @deffn {Scheme Procedure} string-delete char_pred s [start [end]]
4142 @deffnx {C Function} scm_string_delete (char_pred, s, start, end)
4143 Delete characters satisfying @var{char_pred} from @var{s}.
4145 If @var{char_pred} is a procedure, it is applied to each character as
4146 a predicate, if it is a character, it is tested for equality and if it
4147 is a character set, it is tested for membership.
4150 @node Conversion to/from C
4151 @subsubsection Conversion to/from C
4153 When creating a Scheme string from a C string or when converting a
4154 Scheme string to a C string, the concept of character encoding becomes
4157 In C, a string is just a sequence of bytes, and the character encoding
4158 describes the relation between these bytes and the actual characters
4159 that make up the string. For Scheme strings, character encoding is
4160 not an issue (most of the time), since in Scheme you never get to see
4161 the bytes, only the characters.
4163 Converting to C and converting from C each have their own challenges.
4165 When converting from C to Scheme, it is important that the sequence of
4166 bytes in the C string be valid with respect to its encoding. ASCII
4167 strings, for example, can't have any bytes greater than 127. An ASCII
4168 byte greater than 127 is considered @emph{ill-formed} and cannot be
4169 converted into a Scheme character.
4171 Problems can occur in the reverse operation as well. Not all character
4172 encodings can hold all possible Scheme characters. Some encodings, like
4173 ASCII for example, can only describe a small subset of all possible
4174 characters. So, when converting to C, one must first decide what to do
4175 with Scheme characters that can't be represented in the C string.
4177 Converting a Scheme string to a C string will often allocate fresh
4178 memory to hold the result. You must take care that this memory is
4179 properly freed eventually. In many cases, this can be achieved by
4180 using @code{scm_dynwind_free} inside an appropriate dynwind context,
4181 @xref{Dynamic Wind}.
4183 @deftypefn {C Function} SCM scm_from_locale_string (const char *str)
4184 @deftypefnx {C Function} SCM scm_from_locale_stringn (const char *str, size_t len)
4185 Creates a new Scheme string that has the same contents as @var{str} when
4186 interpreted in the character encoding of the current locale.
4188 For @code{scm_from_locale_string}, @var{str} must be null-terminated.
4190 For @code{scm_from_locale_stringn}, @var{len} specifies the length of
4191 @var{str} in bytes, and @var{str} does not need to be null-terminated.
4192 If @var{len} is @code{(size_t)-1}, then @var{str} does need to be
4193 null-terminated and the real length will be found with @code{strlen}.
4195 If the C string is ill-formed, an error will be raised.
4197 Note that these functions should @emph{not} be used to convert C string
4198 constants, because there is no guarantee that the current locale will
4199 match that of the source code. To convert C string constants, use
4200 @code{scm_from_latin1_string}, @code{scm_from_utf8_string} or
4201 @code{scm_from_utf32_string}.
4204 @deftypefn {C Function} SCM scm_take_locale_string (char *str)
4205 @deftypefnx {C Function} SCM scm_take_locale_stringn (char *str, size_t len)
4206 Like @code{scm_from_locale_string} and @code{scm_from_locale_stringn},
4207 respectively, but also frees @var{str} with @code{free} eventually.
4208 Thus, you can use this function when you would free @var{str} anyway
4209 immediately after creating the Scheme string. In certain cases, Guile
4210 can then use @var{str} directly as its internal representation.
4213 @deftypefn {C Function} {char *} scm_to_locale_string (SCM str)
4214 @deftypefnx {C Function} {char *} scm_to_locale_stringn (SCM str, size_t *lenp)
4215 Returns a C string with the same contents as @var{str} in the character
4216 encoding of the current locale. The C string must be freed with
4217 @code{free} eventually, maybe by using @code{scm_dynwind_free},
4218 @xref{Dynamic Wind}.
4220 For @code{scm_to_locale_string}, the returned string is
4221 null-terminated and an error is signalled when @var{str} contains
4222 @code{#\nul} characters.
4224 For @code{scm_to_locale_stringn} and @var{lenp} not @code{NULL},
4225 @var{str} might contain @code{#\nul} characters and the length of the
4226 returned string in bytes is stored in @code{*@var{lenp}}. The
4227 returned string will not be null-terminated in this case. If
4228 @var{lenp} is @code{NULL}, @code{scm_to_locale_stringn} behaves like
4229 @code{scm_to_locale_string}.
4231 If a character in @var{str} cannot be represented in the character
4232 encoding of the current locale, the default port conversion strategy is
4233 used. @xref{Ports}, for more on conversion strategies.
4235 If the conversion strategy is @code{error}, an error will be raised. If
4236 it is @code{substitute}, a replacement character, such as a question
4237 mark, will be inserted in its place. If it is @code{escape}, a hex
4238 escape will be inserted in its place.
4241 @deftypefn {C Function} size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
4242 Puts @var{str} as a C string in the current locale encoding into the
4243 memory pointed to by @var{buf}. The buffer at @var{buf} has room for
4244 @var{max_len} bytes and @code{scm_to_local_stringbuf} will never store
4245 more than that. No terminating @code{'\0'} will be stored.
4247 The return value of @code{scm_to_locale_stringbuf} is the number of
4248 bytes that are needed for all of @var{str}, regardless of whether
4249 @var{buf} was large enough to hold them. Thus, when the return value
4250 is larger than @var{max_len}, only @var{max_len} bytes have been
4251 stored and you probably need to try again with a larger buffer.
4254 For most situations, string conversion should occur using the current
4255 locale, such as with the functions above. But there may be cases where
4256 one wants to convert strings from a character encoding other than the
4257 locale's character encoding. For these cases, the lower-level functions
4258 @code{scm_to_stringn} and @code{scm_from_stringn} are provided. These
4259 functions should seldom be necessary if one is properly using locales.
4261 @deftp {C Type} scm_t_string_failed_conversion_handler
4262 This is an enumerated type that can take one of three values:
4263 @code{SCM_FAILED_CONVERSION_ERROR},
4264 @code{SCM_FAILED_CONVERSION_QUESTION_MARK}, and
4265 @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}. They are used to indicate
4266 a strategy for handling characters that cannot be converted to or from a
4267 given character encoding. @code{SCM_FAILED_CONVERSION_ERROR} indicates
4268 that a conversion should throw an error if some characters cannot be
4269 converted. @code{SCM_FAILED_CONVERSION_QUESTION_MARK} indicates that a
4270 conversion should replace unconvertable characters with the question
4271 mark character. And, @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}
4272 requests that a conversion should replace an unconvertable character
4273 with an escape sequence.
4275 While all three strategies apply when converting Scheme strings to C,
4276 only @code{SCM_FAILED_CONVERSION_ERROR} and
4277 @code{SCM_FAILED_CONVERSION_QUESTION_MARK} can be used when converting C
4281 @deftypefn {C Function} char *scm_to_stringn (SCM str, size_t *lenp, const char *encoding, scm_t_string_failed_conversion_handler handler)
4282 This function returns a newly allocated C string from the Guile string
4283 @var{str}. The length of the returned string in bytes will be returned in
4284 @var{lenp}. The character encoding of the C string is passed as the ASCII,
4285 null-terminated C string @var{encoding}. The @var{handler} parameter
4286 gives a strategy for dealing with characters that cannot be converted
4287 into @var{encoding}.
4289 If @var{lenp} is @code{NULL}, this function will return a null-terminated C
4290 string. It will throw an error if the string contains a null
4294 @deftypefn {C Function} SCM scm_from_stringn (const char *str, size_t len, const char *encoding, scm_t_string_failed_conversion_handler handler)
4295 This function returns a scheme string from the C string @var{str}. The
4296 length of the C string is input as @var{len}. The encoding of the C
4297 string is passed as the ASCII, null-terminated C string @code{encoding}.
4298 The @var{handler} parameters suggests a strategy for dealing with
4299 unconvertable characters.
4302 The following conversion functions are provided as a convenience for the
4303 most commonly used encodings.
4305 @deftypefn {C Function} SCM scm_from_latin1_string (const char *str)
4306 @deftypefnx {C Function} SCM scm_from_utf8_string (const char *str)
4307 @deftypefnx {C Function} SCM scm_from_utf32_string (const scm_t_wchar *str)
4308 Return a scheme string from the null-terminated C string @var{str},
4309 which is ISO-8859-1-, UTF-8-, or UTF-32-encoded. These functions should
4310 be used to convert hard-coded C string constants into Scheme strings.
4313 @deftypefn {C Function} SCM scm_from_latin1_stringn (const char *str, size_t len)
4314 @deftypefnx {C Function} SCM scm_from_utf8_stringn (const char *str, size_t len)
4315 @deftypefnx {C Function} SCM scm_from_utf32_stringn (const scm_t_wchar *str, size_t len)
4316 Return a scheme string from C string @var{str}, which is ISO-8859-1-,
4317 UTF-8-, or UTF-32-encoded, of length @var{len}. @var{len} is the number
4318 of bytes pointed to by @var{str} for @code{scm_from_latin1_stringn} and
4319 @code{scm_from_utf8_stringn}; it is the number of elements (code points)
4320 in @var{str} in the case of @code{scm_from_utf32_stringn}.
4323 @deftypefn {C function} char *scm_to_latin1_stringn (SCM str, size_t *lenp)
4324 @deftypefnx {C function} char *scm_to_utf8_stringn (SCM str, size_t *lenp)
4325 @deftypefnx {C function} scm_t_wchar *scm_to_utf32_stringn (SCM str, size_t *lenp)
4326 Return a newly allocated, ISO-8859-1-, UTF-8-, or UTF-32-encoded C string
4327 from Scheme string @var{str}. An error is thrown when @var{str}
4328 cannot be converted to the specified encoding. If @var{lenp} is
4329 @code{NULL}, the returned C string will be null terminated, and an error
4330 will be thrown if the C string would otherwise contain null
4331 characters. If @var{lenp} is not @code{NULL}, the string is not null terminated,
4332 and the length of the returned string is returned in @var{lenp}. The length
4333 returned is the number of bytes for @code{scm_to_latin1_stringn} and
4334 @code{scm_to_utf8_stringn}; it is the number of elements (code points)
4335 for @code{scm_to_utf32_stringn}.
4338 @node String Internals
4339 @subsubsection String Internals
4341 Guile stores each string in memory as a contiguous array of Unicode code
4342 points along with an associated set of attributes. If all of the code
4343 points of a string have an integer range between 0 and 255 inclusive,
4344 the code point array is stored as one byte per code point: it is stored
4345 as an ISO-8859-1 (aka Latin-1) string. If any of the code points of the
4346 string has an integer value greater that 255, the code point array is
4347 stored as four bytes per code point: it is stored as a UTF-32 string.
4349 Conversion between the one-byte-per-code-point and
4350 four-bytes-per-code-point representations happens automatically as
4353 No API is provided to set the internal representation of strings;
4354 however, there are pair of procedures available to query it. These are
4355 debugging procedures. Using them in production code is discouraged,
4356 since the details of Guile's internal representation of strings may
4357 change from release to release.
4359 @deffn {Scheme Procedure} string-bytes-per-char str
4360 @deffnx {C Function} scm_string_bytes_per_char (str)
4361 Return the number of bytes used to encode a Unicode code point in string
4362 @var{str}. The result is one or four.
4365 @deffn {Scheme Procedure} %string-dump str
4366 @deffnx {C Function} scm_sys_string_dump (str)
4367 Returns an association list containing debugging information for
4368 @var{str}. The association list has the following entries.
4375 The start index of the string into its stringbuf
4378 The length of the string
4381 If this string is a substring, it returns its
4382 parent string. Otherwise, it returns @code{#f}
4385 @code{#t} if the string is read-only
4387 @item stringbuf-chars
4388 A new string containing this string's stringbuf's characters
4390 @item stringbuf-length
4391 The number of characters in this stringbuf
4393 @item stringbuf-shared
4394 @code{#t} if this stringbuf is shared
4396 @item stringbuf-wide
4397 @code{#t} if this stringbuf's characters are stored in a 32-bit buffer,
4398 or @code{#f} if they are stored in an 8-bit buffer
4404 @subsection Bytevectors
4409 A @dfn{bytevector} is a raw bit string. The @code{(rnrs bytevectors)}
4410 module provides the programming interface specified by the
4411 @uref{http://www.r6rs.org/, Revised^6 Report on the Algorithmic Language
4412 Scheme (R6RS)}. It contains procedures to manipulate bytevectors and
4413 interpret their contents in a number of ways: bytevector contents can be
4414 accessed as signed or unsigned integer of various sizes and endianness,
4415 as IEEE-754 floating point numbers, or as strings. It is a useful tool
4416 to encode and decode binary data.
4418 The R6RS (Section 4.3.4) specifies an external representation for
4419 bytevectors, whereby the octets (integers in the range 0--255) contained
4420 in the bytevector are represented as a list prefixed by @code{#vu8}:
4426 denotes a 3-byte bytevector containing the octets 1, 53, and 204. Like
4427 string literals, booleans, etc., bytevectors are ``self-quoting'', i.e.,
4428 they do not need to be quoted:
4432 @result{} #vu8(1 53 204)
4435 Bytevectors can be used with the binary input/output primitives of the
4436 R6RS (@pxref{R6RS I/O Ports}).
4439 * Bytevector Endianness:: Dealing with byte order.
4440 * Bytevector Manipulation:: Creating, copying, manipulating bytevectors.
4441 * Bytevectors as Integers:: Interpreting bytes as integers.
4442 * Bytevectors and Integer Lists:: Converting to/from an integer list.
4443 * Bytevectors as Floats:: Interpreting bytes as real numbers.
4444 * Bytevectors as Strings:: Interpreting bytes as Unicode strings.
4445 * Bytevectors as Generalized Vectors:: Guile extension to the bytevector API.
4446 * Bytevectors as Uniform Vectors:: Bytevectors and SRFI-4.
4449 @node Bytevector Endianness
4450 @subsubsection Endianness
4456 Some of the following procedures take an @var{endianness} parameter.
4457 The @dfn{endianness} is defined as the order of bytes in multi-byte
4458 numbers: numbers encoded in @dfn{big endian} have their most
4459 significant bytes written first, whereas numbers encoded in
4460 @dfn{little endian} have their least significant bytes
4461 first@footnote{Big-endian and little-endian are the most common
4462 ``endiannesses'', but others do exist. For instance, the GNU MP
4463 library allows @dfn{word order} to be specified independently of
4464 @dfn{byte order} (@pxref{Integer Import and Export,,, gmp, The GNU
4465 Multiple Precision Arithmetic Library Manual}).}.
4467 Little-endian is the native endianness of the IA32 architecture and
4468 its derivatives, while big-endian is native to SPARC and PowerPC,
4469 among others. The @code{native-endianness} procedure returns the
4470 native endianness of the machine it runs on.
4472 @deffn {Scheme Procedure} native-endianness
4473 @deffnx {C Function} scm_native_endianness ()
4474 Return a value denoting the native endianness of the host machine.
4477 @deffn {Scheme Macro} endianness symbol
4478 Return an object denoting the endianness specified by @var{symbol}. If
4479 @var{symbol} is neither @code{big} nor @code{little} then an error is
4480 raised at expand-time.
4483 @defvr {C Variable} scm_endianness_big
4484 @defvrx {C Variable} scm_endianness_little
4485 The objects denoting big- and little-endianness, respectively.
4489 @node Bytevector Manipulation
4490 @subsubsection Manipulating Bytevectors
4492 Bytevectors can be created, copied, and analyzed with the following
4493 procedures and C functions.
4495 @deffn {Scheme Procedure} make-bytevector len [fill]
4496 @deffnx {C Function} scm_make_bytevector (len, fill)
4497 @deffnx {C Function} scm_c_make_bytevector (size_t len)
4498 Return a new bytevector of @var{len} bytes. Optionally, if @var{fill}
4499 is given, fill it with @var{fill}; @var{fill} must be in the range
4503 @deffn {Scheme Procedure} bytevector? obj
4504 @deffnx {C Function} scm_bytevector_p (obj)
4505 Return true if @var{obj} is a bytevector.
4508 @deftypefn {C Function} int scm_is_bytevector (SCM obj)
4509 Equivalent to @code{scm_is_true (scm_bytevector_p (obj))}.
4512 @deffn {Scheme Procedure} bytevector-length bv
4513 @deffnx {C Function} scm_bytevector_length (bv)
4514 Return the length in bytes of bytevector @var{bv}.
4517 @deftypefn {C Function} size_t scm_c_bytevector_length (SCM bv)
4518 Likewise, return the length in bytes of bytevector @var{bv}.
4521 @deffn {Scheme Procedure} bytevector=? bv1 bv2
4522 @deffnx {C Function} scm_bytevector_eq_p (bv1, bv2)
4523 Return is @var{bv1} equals to @var{bv2}---i.e., if they have the same
4524 length and contents.
4527 @deffn {Scheme Procedure} bytevector-fill! bv fill
4528 @deffnx {C Function} scm_bytevector_fill_x (bv, fill)
4529 Fill bytevector @var{bv} with @var{fill}, a byte.
4532 @deffn {Scheme Procedure} bytevector-copy! source source-start target target-start len
4533 @deffnx {C Function} scm_bytevector_copy_x (source, source_start, target, target_start, len)
4534 Copy @var{len} bytes from @var{source} into @var{target}, starting
4535 reading from @var{source-start} (a positive index within @var{source})
4536 and start writing at @var{target-start}.
4539 @deffn {Scheme Procedure} bytevector-copy bv
4540 @deffnx {C Function} scm_bytevector_copy (bv)
4541 Return a newly allocated copy of @var{bv}.
4544 @deftypefn {C Function} scm_t_uint8 scm_c_bytevector_ref (SCM bv, size_t index)
4545 Return the byte at @var{index} in bytevector @var{bv}.
4548 @deftypefn {C Function} void scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
4549 Set the byte at @var{index} in @var{bv} to @var{value}.
4552 Low-level C macros are available. They do not perform any
4553 type-checking; as such they should be used with care.
4555 @deftypefn {C Macro} size_t SCM_BYTEVECTOR_LENGTH (bv)
4556 Return the length in bytes of bytevector @var{bv}.
4559 @deftypefn {C Macro} {signed char *} SCM_BYTEVECTOR_CONTENTS (bv)
4560 Return a pointer to the contents of bytevector @var{bv}.
4564 @node Bytevectors as Integers
4565 @subsubsection Interpreting Bytevector Contents as Integers
4567 The contents of a bytevector can be interpreted as a sequence of
4568 integers of any given size, sign, and endianness.
4571 (let ((bv (make-bytevector 4)))
4572 (bytevector-u8-set! bv 0 #x12)
4573 (bytevector-u8-set! bv 1 #x34)
4574 (bytevector-u8-set! bv 2 #x56)
4575 (bytevector-u8-set! bv 3 #x78)
4577 (map (lambda (number)
4578 (number->string number 16))
4579 (list (bytevector-u8-ref bv 0)
4580 (bytevector-u16-ref bv 0 (endianness big))
4581 (bytevector-u32-ref bv 0 (endianness little)))))
4583 @result{} ("12" "1234" "78563412")
4586 The most generic procedures to interpret bytevector contents as integers
4587 are described below.
4589 @deffn {Scheme Procedure} bytevector-uint-ref bv index endianness size
4590 @deffnx {C Function} scm_bytevector_uint_ref (bv, index, endianness, size)
4591 Return the @var{size}-byte long unsigned integer at index @var{index} in
4592 @var{bv}, decoded according to @var{endianness}.
4595 @deffn {Scheme Procedure} bytevector-sint-ref bv index endianness size
4596 @deffnx {C Function} scm_bytevector_sint_ref (bv, index, endianness, size)
4597 Return the @var{size}-byte long signed integer at index @var{index} in
4598 @var{bv}, decoded according to @var{endianness}.
4601 @deffn {Scheme Procedure} bytevector-uint-set! bv index value endianness size
4602 @deffnx {C Function} scm_bytevector_uint_set_x (bv, index, value, endianness, size)
4603 Set the @var{size}-byte long unsigned integer at @var{index} to
4604 @var{value}, encoded according to @var{endianness}.
4607 @deffn {Scheme Procedure} bytevector-sint-set! bv index value endianness size
4608 @deffnx {C Function} scm_bytevector_sint_set_x (bv, index, value, endianness, size)
4609 Set the @var{size}-byte long signed integer at @var{index} to
4610 @var{value}, encoded according to @var{endianness}.
4613 The following procedures are similar to the ones above, but specialized
4614 to a given integer size:
4616 @deffn {Scheme Procedure} bytevector-u8-ref bv index
4617 @deffnx {Scheme Procedure} bytevector-s8-ref bv index
4618 @deffnx {Scheme Procedure} bytevector-u16-ref bv index endianness
4619 @deffnx {Scheme Procedure} bytevector-s16-ref bv index endianness
4620 @deffnx {Scheme Procedure} bytevector-u32-ref bv index endianness
4621 @deffnx {Scheme Procedure} bytevector-s32-ref bv index endianness
4622 @deffnx {Scheme Procedure} bytevector-u64-ref bv index endianness
4623 @deffnx {Scheme Procedure} bytevector-s64-ref bv index endianness
4624 @deffnx {C Function} scm_bytevector_u8_ref (bv, index)
4625 @deffnx {C Function} scm_bytevector_s8_ref (bv, index)
4626 @deffnx {C Function} scm_bytevector_u16_ref (bv, index, endianness)
4627 @deffnx {C Function} scm_bytevector_s16_ref (bv, index, endianness)
4628 @deffnx {C Function} scm_bytevector_u32_ref (bv, index, endianness)
4629 @deffnx {C Function} scm_bytevector_s32_ref (bv, index, endianness)
4630 @deffnx {C Function} scm_bytevector_u64_ref (bv, index, endianness)
4631 @deffnx {C Function} scm_bytevector_s64_ref (bv, index, endianness)
4632 Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
4633 16, 32 or 64) from @var{bv} at @var{index}, decoded according to
4637 @deffn {Scheme Procedure} bytevector-u8-set! bv index value
4638 @deffnx {Scheme Procedure} bytevector-s8-set! bv index value
4639 @deffnx {Scheme Procedure} bytevector-u16-set! bv index value endianness
4640 @deffnx {Scheme Procedure} bytevector-s16-set! bv index value endianness
4641 @deffnx {Scheme Procedure} bytevector-u32-set! bv index value endianness
4642 @deffnx {Scheme Procedure} bytevector-s32-set! bv index value endianness
4643 @deffnx {Scheme Procedure} bytevector-u64-set! bv index value endianness
4644 @deffnx {Scheme Procedure} bytevector-s64-set! bv index value endianness
4645 @deffnx {C Function} scm_bytevector_u8_set_x (bv, index, value)
4646 @deffnx {C Function} scm_bytevector_s8_set_x (bv, index, value)
4647 @deffnx {C Function} scm_bytevector_u16_set_x (bv, index, value, endianness)
4648 @deffnx {C Function} scm_bytevector_s16_set_x (bv, index, value, endianness)
4649 @deffnx {C Function} scm_bytevector_u32_set_x (bv, index, value, endianness)
4650 @deffnx {C Function} scm_bytevector_s32_set_x (bv, index, value, endianness)
4651 @deffnx {C Function} scm_bytevector_u64_set_x (bv, index, value, endianness)
4652 @deffnx {C Function} scm_bytevector_s64_set_x (bv, index, value, endianness)
4653 Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
4654 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to
4658 Finally, a variant specialized for the host's endianness is available
4659 for each of these functions (with the exception of the @code{u8}
4660 accessors, for obvious reasons):
4662 @deffn {Scheme Procedure} bytevector-u16-native-ref bv index
4663 @deffnx {Scheme Procedure} bytevector-s16-native-ref bv index
4664 @deffnx {Scheme Procedure} bytevector-u32-native-ref bv index
4665 @deffnx {Scheme Procedure} bytevector-s32-native-ref bv index
4666 @deffnx {Scheme Procedure} bytevector-u64-native-ref bv index
4667 @deffnx {Scheme Procedure} bytevector-s64-native-ref bv index
4668 @deffnx {C Function} scm_bytevector_u16_native_ref (bv, index)
4669 @deffnx {C Function} scm_bytevector_s16_native_ref (bv, index)
4670 @deffnx {C Function} scm_bytevector_u32_native_ref (bv, index)
4671 @deffnx {C Function} scm_bytevector_s32_native_ref (bv, index)
4672 @deffnx {C Function} scm_bytevector_u64_native_ref (bv, index)
4673 @deffnx {C Function} scm_bytevector_s64_native_ref (bv, index)
4674 Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
4675 16, 32 or 64) from @var{bv} at @var{index}, decoded according to the
4676 host's native endianness.
4679 @deffn {Scheme Procedure} bytevector-u16-native-set! bv index value
4680 @deffnx {Scheme Procedure} bytevector-s16-native-set! bv index value
4681 @deffnx {Scheme Procedure} bytevector-u32-native-set! bv index value
4682 @deffnx {Scheme Procedure} bytevector-s32-native-set! bv index value
4683 @deffnx {Scheme Procedure} bytevector-u64-native-set! bv index value
4684 @deffnx {Scheme Procedure} bytevector-s64-native-set! bv index value
4685 @deffnx {C Function} scm_bytevector_u16_native_set_x (bv, index, value)
4686 @deffnx {C Function} scm_bytevector_s16_native_set_x (bv, index, value)
4687 @deffnx {C Function} scm_bytevector_u32_native_set_x (bv, index, value)
4688 @deffnx {C Function} scm_bytevector_s32_native_set_x (bv, index, value)
4689 @deffnx {C Function} scm_bytevector_u64_native_set_x (bv, index, value)
4690 @deffnx {C Function} scm_bytevector_s64_native_set_x (bv, index, value)
4691 Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
4692 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to the
4693 host's native endianness.
4697 @node Bytevectors and Integer Lists
4698 @subsubsection Converting Bytevectors to/from Integer Lists
4700 Bytevector contents can readily be converted to/from lists of signed or
4704 (bytevector->sint-list (u8-list->bytevector (make-list 4 255))
4705 (endianness little) 2)
4709 @deffn {Scheme Procedure} bytevector->u8-list bv
4710 @deffnx {C Function} scm_bytevector_to_u8_list (bv)
4711 Return a newly allocated list of unsigned 8-bit integers from the
4712 contents of @var{bv}.
4715 @deffn {Scheme Procedure} u8-list->bytevector lst
4716 @deffnx {C Function} scm_u8_list_to_bytevector (lst)
4717 Return a newly allocated bytevector consisting of the unsigned 8-bit
4718 integers listed in @var{lst}.
4721 @deffn {Scheme Procedure} bytevector->uint-list bv endianness size
4722 @deffnx {C Function} scm_bytevector_to_uint_list (bv, endianness, size)
4723 Return a list of unsigned integers of @var{size} bytes representing the
4724 contents of @var{bv}, decoded according to @var{endianness}.
4727 @deffn {Scheme Procedure} bytevector->sint-list bv endianness size
4728 @deffnx {C Function} scm_bytevector_to_sint_list (bv, endianness, size)
4729 Return a list of signed integers of @var{size} bytes representing the
4730 contents of @var{bv}, decoded according to @var{endianness}.
4733 @deffn {Scheme Procedure} uint-list->bytevector lst endianness size
4734 @deffnx {C Function} scm_uint_list_to_bytevector (lst, endianness, size)
4735 Return a new bytevector containing the unsigned integers listed in
4736 @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
4739 @deffn {Scheme Procedure} sint-list->bytevector lst endianness size
4740 @deffnx {C Function} scm_sint_list_to_bytevector (lst, endianness, size)
4741 Return a new bytevector containing the signed integers listed in
4742 @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
4745 @node Bytevectors as Floats
4746 @subsubsection Interpreting Bytevector Contents as Floating Point Numbers
4748 @cindex IEEE-754 floating point numbers
4750 Bytevector contents can also be accessed as IEEE-754 single- or
4751 double-precision floating point numbers (respectively 32 and 64-bit
4752 long) using the procedures described here.
4754 @deffn {Scheme Procedure} bytevector-ieee-single-ref bv index endianness
4755 @deffnx {Scheme Procedure} bytevector-ieee-double-ref bv index endianness
4756 @deffnx {C Function} scm_bytevector_ieee_single_ref (bv, index, endianness)
4757 @deffnx {C Function} scm_bytevector_ieee_double_ref (bv, index, endianness)
4758 Return the IEEE-754 single-precision floating point number from @var{bv}
4759 at @var{index} according to @var{endianness}.
4762 @deffn {Scheme Procedure} bytevector-ieee-single-set! bv index value endianness
4763 @deffnx {Scheme Procedure} bytevector-ieee-double-set! bv index value endianness
4764 @deffnx {C Function} scm_bytevector_ieee_single_set_x (bv, index, value, endianness)
4765 @deffnx {C Function} scm_bytevector_ieee_double_set_x (bv, index, value, endianness)
4766 Store real number @var{value} in @var{bv} at @var{index} according to
4770 Specialized procedures are also available:
4772 @deffn {Scheme Procedure} bytevector-ieee-single-native-ref bv index
4773 @deffnx {Scheme Procedure} bytevector-ieee-double-native-ref bv index
4774 @deffnx {C Function} scm_bytevector_ieee_single_native_ref (bv, index)
4775 @deffnx {C Function} scm_bytevector_ieee_double_native_ref (bv, index)
4776 Return the IEEE-754 single-precision floating point number from @var{bv}
4777 at @var{index} according to the host's native endianness.
4780 @deffn {Scheme Procedure} bytevector-ieee-single-native-set! bv index value
4781 @deffnx {Scheme Procedure} bytevector-ieee-double-native-set! bv index value
4782 @deffnx {C Function} scm_bytevector_ieee_single_native_set_x (bv, index, value)
4783 @deffnx {C Function} scm_bytevector_ieee_double_native_set_x (bv, index, value)
4784 Store real number @var{value} in @var{bv} at @var{index} according to
4785 the host's native endianness.
4789 @node Bytevectors as Strings
4790 @subsubsection Interpreting Bytevector Contents as Unicode Strings
4792 @cindex Unicode string encoding
4794 Bytevector contents can also be interpreted as Unicode strings encoded
4795 in one of the most commonly available encoding formats.
4798 (utf8->string (u8-list->bytevector '(99 97 102 101)))
4801 (string->utf8 "caf@'e") ;; SMALL LATIN LETTER E WITH ACUTE ACCENT
4802 @result{} #vu8(99 97 102 195 169)
4805 @deffn {Scheme Procedure} string->utf8 str
4806 @deffnx {Scheme Procedure} string->utf16 str [endianness]
4807 @deffnx {Scheme Procedure} string->utf32 str [endianness]
4808 @deffnx {C Function} scm_string_to_utf8 (str)
4809 @deffnx {C Function} scm_string_to_utf16 (str, endianness)
4810 @deffnx {C Function} scm_string_to_utf32 (str, endianness)
4811 Return a newly allocated bytevector that contains the UTF-8, UTF-16, or
4812 UTF-32 (aka. UCS-4) encoding of @var{str}. For UTF-16 and UTF-32,
4813 @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
4814 it defaults to big endian.
4817 @deffn {Scheme Procedure} utf8->string utf
4818 @deffnx {Scheme Procedure} utf16->string utf [endianness]
4819 @deffnx {Scheme Procedure} utf32->string utf [endianness]
4820 @deffnx {C Function} scm_utf8_to_string (utf)
4821 @deffnx {C Function} scm_utf16_to_string (utf, endianness)
4822 @deffnx {C Function} scm_utf32_to_string (utf, endianness)
4823 Return a newly allocated string that contains from the UTF-8-, UTF-16-,
4824 or UTF-32-decoded contents of bytevector @var{utf}. For UTF-16 and UTF-32,
4825 @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
4826 it defaults to big endian.
4829 @node Bytevectors as Generalized Vectors
4830 @subsubsection Accessing Bytevectors with the Generalized Vector API
4832 As an extension to the R6RS, Guile allows bytevectors to be manipulated
4833 with the @dfn{generalized vector} procedures (@pxref{Generalized
4834 Vectors}). This also allows bytevectors to be accessed using the
4835 generic @dfn{array} procedures (@pxref{Array Procedures}). When using
4836 these APIs, bytes are accessed one at a time as 8-bit unsigned integers:
4839 (define bv #vu8(0 1 2 3))
4841 (generalized-vector? bv)
4844 (generalized-vector-ref bv 2)
4847 (generalized-vector-set! bv 2 77)
4856 @node Bytevectors as Uniform Vectors
4857 @subsubsection Accessing Bytevectors with the SRFI-4 API
4859 Bytevectors may also be accessed with the SRFI-4 API. @xref{SRFI-4 and
4860 Bytevectors}, for more information.
4867 Symbols in Scheme are widely used in three ways: as items of discrete
4868 data, as lookup keys for alists and hash tables, and to denote variable
4871 A @dfn{symbol} is similar to a string in that it is defined by a
4872 sequence of characters. The sequence of characters is known as the
4873 symbol's @dfn{name}. In the usual case --- that is, where the symbol's
4874 name doesn't include any characters that could be confused with other
4875 elements of Scheme syntax --- a symbol is written in a Scheme program by
4876 writing the sequence of characters that make up the name, @emph{without}
4877 any quotation marks or other special syntax. For example, the symbol
4878 whose name is ``multiply-by-2'' is written, simply:
4884 Notice how this differs from a @emph{string} with contents
4885 ``multiply-by-2'', which is written with double quotation marks, like
4892 Looking beyond how they are written, symbols are different from strings
4893 in two important respects.
4895 The first important difference is uniqueness. If the same-looking
4896 string is read twice from two different places in a program, the result
4897 is two @emph{different} string objects whose contents just happen to be
4898 the same. If, on the other hand, the same-looking symbol is read twice
4899 from two different places in a program, the result is the @emph{same}
4900 symbol object both times.
4902 Given two read symbols, you can use @code{eq?} to test whether they are
4903 the same (that is, have the same name). @code{eq?} is the most
4904 efficient comparison operator in Scheme, and comparing two symbols like
4905 this is as fast as comparing, for example, two numbers. Given two
4906 strings, on the other hand, you must use @code{equal?} or
4907 @code{string=?}, which are much slower comparison operators, to
4908 determine whether the strings have the same contents.
4911 (define sym1 (quote hello))
4912 (define sym2 (quote hello))
4913 (eq? sym1 sym2) @result{} #t
4915 (define str1 "hello")
4916 (define str2 "hello")
4917 (eq? str1 str2) @result{} #f
4918 (equal? str1 str2) @result{} #t
4921 The second important difference is that symbols, unlike strings, are not
4922 self-evaluating. This is why we need the @code{(quote @dots{})}s in the
4923 example above: @code{(quote hello)} evaluates to the symbol named
4924 "hello" itself, whereas an unquoted @code{hello} is @emph{read} as the
4925 symbol named "hello" and evaluated as a variable reference @dots{} about
4926 which more below (@pxref{Symbol Variables}).
4929 * Symbol Data:: Symbols as discrete data.
4930 * Symbol Keys:: Symbols as lookup keys.
4931 * Symbol Variables:: Symbols as denoting variables.
4932 * Symbol Primitives:: Operations related to symbols.
4933 * Symbol Props:: Function slots and property lists.
4934 * Symbol Read Syntax:: Extended read syntax for symbols.
4935 * Symbol Uninterned:: Uninterned symbols.
4940 @subsubsection Symbols as Discrete Data
4942 Numbers and symbols are similar to the extent that they both lend
4943 themselves to @code{eq?} comparison. But symbols are more descriptive
4944 than numbers, because a symbol's name can be used directly to describe
4945 the concept for which that symbol stands.
4947 For example, imagine that you need to represent some colours in a
4948 computer program. Using numbers, you would have to choose arbitrarily
4949 some mapping between numbers and colours, and then take care to use that
4950 mapping consistently:
4953 ;; 1=red, 2=green, 3=purple
4955 (if (eq? (colour-of car) 1)
4960 You can make the mapping more explicit and the code more readable by
4968 (if (eq? (colour-of car) red)
4973 But the simplest and clearest approach is not to use numbers at all, but
4974 symbols whose names specify the colours that they refer to:
4977 (if (eq? (colour-of car) 'red)
4981 The descriptive advantages of symbols over numbers increase as the set
4982 of concepts that you want to describe grows. Suppose that a car object
4983 can have other properties as well, such as whether it has or uses:
4987 automatic or manual transmission
4989 leaded or unleaded fuel
4991 power steering (or not).
4995 Then a car's combined property set could be naturally represented and
4996 manipulated as a list of symbols:
4999 (properties-of car1)
5001 (red manual unleaded power-steering)
5003 (if (memq 'power-steering (properties-of car1))
5004 (display "Unfit people can drive this car.\n")
5005 (display "You'll need strong arms to drive this car!\n"))
5007 Unfit people can drive this car.
5010 Remember, the fundamental property of symbols that we are relying on
5011 here is that an occurrence of @code{'red} in one part of a program is an
5012 @emph{indistinguishable} symbol from an occurrence of @code{'red} in
5013 another part of a program; this means that symbols can usefully be
5014 compared using @code{eq?}. At the same time, symbols have naturally
5015 descriptive names. This combination of efficiency and descriptive power
5016 makes them ideal for use as discrete data.
5020 @subsubsection Symbols as Lookup Keys
5022 Given their efficiency and descriptive power, it is natural to use
5023 symbols as the keys in an association list or hash table.
5025 To illustrate this, consider a more structured representation of the car
5026 properties example from the preceding subsection. Rather than
5027 mixing all the properties up together in a flat list, we could use an
5028 association list like this:
5031 (define car1-properties '((colour . red)
5032 (transmission . manual)
5034 (steering . power-assisted)))
5037 Notice how this structure is more explicit and extensible than the flat
5038 list. For example it makes clear that @code{manual} refers to the
5039 transmission rather than, say, the windows or the locking of the car.
5040 It also allows further properties to use the same symbols among their
5041 possible values without becoming ambiguous:
5044 (define car1-properties '((colour . red)
5045 (transmission . manual)
5047 (steering . power-assisted)
5049 (locking . manual)))
5052 With a representation like this, it is easy to use the efficient
5053 @code{assq-XXX} family of procedures (@pxref{Association Lists}) to
5054 extract or change individual pieces of information:
5057 (assq-ref car1-properties 'fuel) @result{} unleaded
5058 (assq-ref car1-properties 'transmission) @result{} manual
5060 (assq-set! car1-properties 'seat-colour 'black)
5063 (transmission . manual)
5065 (steering . power-assisted)
5066 (seat-colour . black)
5067 (locking . manual)))
5070 Hash tables also have keys, and exactly the same arguments apply to the
5071 use of symbols in hash tables as in association lists. The hash value
5072 that Guile uses to decide where to add a symbol-keyed entry to a hash
5073 table can be obtained by calling the @code{symbol-hash} procedure:
5075 @deffn {Scheme Procedure} symbol-hash symbol
5076 @deffnx {C Function} scm_symbol_hash (symbol)
5077 Return a hash value for @var{symbol}.
5080 See @ref{Hash Tables} for information about hash tables in general, and
5081 for why you might choose to use a hash table rather than an association
5085 @node Symbol Variables
5086 @subsubsection Symbols as Denoting Variables
5088 When an unquoted symbol in a Scheme program is evaluated, it is
5089 interpreted as a variable reference, and the result of the evaluation is
5090 the appropriate variable's value.
5092 For example, when the expression @code{(string-length "abcd")} is read
5093 and evaluated, the sequence of characters @code{string-length} is read
5094 as the symbol whose name is "string-length". This symbol is associated
5095 with a variable whose value is the procedure that implements string
5096 length calculation. Therefore evaluation of the @code{string-length}
5097 symbol results in that procedure.
5099 The details of the connection between an unquoted symbol and the
5100 variable to which it refers are explained elsewhere. See @ref{Binding
5101 Constructs}, for how associations between symbols and variables are
5102 created, and @ref{Modules}, for how those associations are affected by
5103 Guile's module system.
5106 @node Symbol Primitives
5107 @subsubsection Operations Related to Symbols
5109 Given any Scheme value, you can determine whether it is a symbol using
5110 the @code{symbol?} primitive:
5113 @deffn {Scheme Procedure} symbol? obj
5114 @deffnx {C Function} scm_symbol_p (obj)
5115 Return @code{#t} if @var{obj} is a symbol, otherwise return
5119 @deftypefn {C Function} int scm_is_symbol (SCM val)
5120 Equivalent to @code{scm_is_true (scm_symbol_p (val))}.
5123 Once you know that you have a symbol, you can obtain its name as a
5124 string by calling @code{symbol->string}. Note that Guile differs by
5125 default from R5RS on the details of @code{symbol->string} as regards
5128 @rnindex symbol->string
5129 @deffn {Scheme Procedure} symbol->string s
5130 @deffnx {C Function} scm_symbol_to_string (s)
5131 Return the name of symbol @var{s} as a string. By default, Guile reads
5132 symbols case-sensitively, so the string returned will have the same case
5133 variation as the sequence of characters that caused @var{s} to be
5136 If Guile is set to read symbols case-insensitively (as specified by
5137 R5RS), and @var{s} comes into being as part of a literal expression
5138 (@pxref{Literal expressions,,,r5rs, The Revised^5 Report on Scheme}) or
5139 by a call to the @code{read} or @code{string-ci->symbol} procedures,
5140 Guile converts any alphabetic characters in the symbol's name to
5141 lower case before creating the symbol object, so the string returned
5142 here will be in lower case.
5144 If @var{s} was created by @code{string->symbol}, the case of characters
5145 in the string returned will be the same as that in the string that was
5146 passed to @code{string->symbol}, regardless of Guile's case-sensitivity
5147 setting at the time @var{s} was created.
5149 It is an error to apply mutation procedures like @code{string-set!} to
5150 strings returned by this procedure.
5153 Most symbols are created by writing them literally in code. However it
5154 is also possible to create symbols programmatically using the following
5157 @deffn {Scheme Procedure} symbol char@dots{}
5159 Return a newly allocated symbol made from the given character arguments.
5162 (symbol #\x #\y #\z) @result{} xyz
5166 @deffn {Scheme Procedure} list->symbol lst
5167 @rnindex list->symbol
5168 Return a newly allocated symbol made from a list of characters.
5171 (list->symbol '(#\a #\b #\c)) @result{} abc
5175 @rnindex symbol-append
5176 @deffn {Scheme Procedure} symbol-append . args
5177 Return a newly allocated symbol whose characters form the
5178 concatenation of the given symbols, @var{args}.
5182 (symbol-append h 'world))
5183 @result{} helloworld
5187 @rnindex string->symbol
5188 @deffn {Scheme Procedure} string->symbol string
5189 @deffnx {C Function} scm_string_to_symbol (string)
5190 Return the symbol whose name is @var{string}. This procedure can create
5191 symbols with names containing special characters or letters in the
5192 non-standard case, but it is usually a bad idea to create such symbols
5193 because in some implementations of Scheme they cannot be read as
5197 @deffn {Scheme Procedure} string-ci->symbol str
5198 @deffnx {C Function} scm_string_ci_to_symbol (str)
5199 Return the symbol whose name is @var{str}. If Guile is currently
5200 reading symbols case-insensitively, @var{str} is converted to lowercase
5201 before the returned symbol is looked up or created.
5204 The following examples illustrate Guile's detailed behaviour as regards
5205 the case-sensitivity of symbols:
5208 (read-enable 'case-insensitive) ; R5RS compliant behaviour
5210 (symbol->string 'flying-fish) @result{} "flying-fish"
5211 (symbol->string 'Martin) @result{} "martin"
5213 (string->symbol "Malvina")) @result{} "Malvina"
5215 (eq? 'mISSISSIppi 'mississippi) @result{} #t
5216 (string->symbol "mISSISSIppi") @result{} mISSISSIppi
5217 (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
5219 (string->symbol (symbol->string 'LolliPop))) @result{} #t
5220 (string=? "K. Harper, M.D."
5222 (string->symbol "K. Harper, M.D."))) @result{} #t
5224 (read-disable 'case-insensitive) ; Guile default behaviour
5226 (symbol->string 'flying-fish) @result{} "flying-fish"
5227 (symbol->string 'Martin) @result{} "Martin"
5229 (string->symbol "Malvina")) @result{} "Malvina"
5231 (eq? 'mISSISSIppi 'mississippi) @result{} #f
5232 (string->symbol "mISSISSIppi") @result{} mISSISSIppi
5233 (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #t
5235 (string->symbol (symbol->string 'LolliPop))) @result{} #t
5236 (string=? "K. Harper, M.D."
5238 (string->symbol "K. Harper, M.D."))) @result{} #t
5241 From C, there are lower level functions that construct a Scheme symbol
5242 from a C string in the current locale encoding.
5244 When you want to do more from C, you should convert between symbols
5245 and strings using @code{scm_symbol_to_string} and
5246 @code{scm_string_to_symbol} and work with the strings.
5248 @deffn {C Function} scm_from_latin1_symbol (const char *name)
5249 @deffnx {C Function} scm_from_utf8_symbol (const char *name)
5250 Construct and return a Scheme symbol whose name is specified by the
5251 null-terminated C string @var{name}. These are appropriate when
5252 the C string is hard-coded in the source code.
5255 @deffn {C Function} scm_from_locale_symbol (const char *name)
5256 @deffnx {C Function} scm_from_locale_symboln (const char *name, size_t len)
5257 Construct and return a Scheme symbol whose name is specified by
5258 @var{name}. For @code{scm_from_locale_symbol}, @var{name} must be null
5259 terminated; for @code{scm_from_locale_symboln} the length of @var{name} is
5260 specified explicitly by @var{len}.
5262 Note that these functions should @emph{not} be used when @var{name} is a
5263 C string constant, because there is no guarantee that the current locale
5264 will match that of the source code. In such cases, use
5265 @code{scm_from_latin1_symbol} or @code{scm_from_utf8_symbol}.
5268 @deftypefn {C Function} SCM scm_take_locale_symbol (char *str)
5269 @deftypefnx {C Function} SCM scm_take_locale_symboln (char *str, size_t len)
5270 Like @code{scm_from_locale_symbol} and @code{scm_from_locale_symboln},
5271 respectively, but also frees @var{str} with @code{free} eventually.
5272 Thus, you can use this function when you would free @var{str} anyway
5273 immediately after creating the Scheme string. In certain cases, Guile
5274 can then use @var{str} directly as its internal representation.
5277 The size of a symbol can also be obtained from C:
5279 @deftypefn {C Function} size_t scm_c_symbol_length (SCM sym)
5280 Return the number of characters in @var{sym}.
5283 Finally, some applications, especially those that generate new Scheme
5284 code dynamically, need to generate symbols for use in the generated
5285 code. The @code{gensym} primitive meets this need:
5287 @deffn {Scheme Procedure} gensym [prefix]
5288 @deffnx {C Function} scm_gensym (prefix)
5289 Create a new symbol with a name constructed from a prefix and a counter
5290 value. The string @var{prefix} can be specified as an optional
5291 argument. Default prefix is @samp{@w{ g}}. The counter is increased by 1
5292 at each call. There is no provision for resetting the counter.
5295 The symbols generated by @code{gensym} are @emph{likely} to be unique,
5296 since their names begin with a space and it is only otherwise possible
5297 to generate such symbols if a programmer goes out of their way to do
5298 so. Uniqueness can be guaranteed by instead using uninterned symbols
5299 (@pxref{Symbol Uninterned}), though they can't be usefully written out
5304 @subsubsection Function Slots and Property Lists
5306 In traditional Lisp dialects, symbols are often understood as having
5307 three kinds of value at once:
5311 a @dfn{variable} value, which is used when the symbol appears in
5312 code in a variable reference context
5315 a @dfn{function} value, which is used when the symbol appears in
5316 code in a function name position (i.e.@: as the first element in an
5320 a @dfn{property list} value, which is used when the symbol is given as
5321 the first argument to Lisp's @code{put} or @code{get} functions.
5324 Although Scheme (as one of its simplifications with respect to Lisp)
5325 does away with the distinction between variable and function namespaces,
5326 Guile currently retains some elements of the traditional structure in
5327 case they turn out to be useful when implementing translators for other
5328 languages, in particular Emacs Lisp.
5330 Specifically, Guile symbols have two extra slots, one for a symbol's
5331 property list, and one for its ``function value.'' The following procedures
5332 are provided to access these slots.
5334 @deffn {Scheme Procedure} symbol-fref symbol
5335 @deffnx {C Function} scm_symbol_fref (symbol)
5336 Return the contents of @var{symbol}'s @dfn{function slot}.
5339 @deffn {Scheme Procedure} symbol-fset! symbol value
5340 @deffnx {C Function} scm_symbol_fset_x (symbol, value)
5341 Set the contents of @var{symbol}'s function slot to @var{value}.
5344 @deffn {Scheme Procedure} symbol-pref symbol
5345 @deffnx {C Function} scm_symbol_pref (symbol)
5346 Return the @dfn{property list} currently associated with @var{symbol}.
5349 @deffn {Scheme Procedure} symbol-pset! symbol value
5350 @deffnx {C Function} scm_symbol_pset_x (symbol, value)
5351 Set @var{symbol}'s property list to @var{value}.
5354 @deffn {Scheme Procedure} symbol-property sym prop
5355 From @var{sym}'s property list, return the value for property
5356 @var{prop}. The assumption is that @var{sym}'s property list is an
5357 association list whose keys are distinguished from each other using
5358 @code{equal?}; @var{prop} should be one of the keys in that list. If
5359 the property list has no entry for @var{prop}, @code{symbol-property}
5363 @deffn {Scheme Procedure} set-symbol-property! sym prop val
5364 In @var{sym}'s property list, set the value for property @var{prop} to
5365 @var{val}, or add a new entry for @var{prop}, with value @var{val}, if
5366 none already exists. For the structure of the property list, see
5367 @code{symbol-property}.
5370 @deffn {Scheme Procedure} symbol-property-remove! sym prop
5371 From @var{sym}'s property list, remove the entry for property
5372 @var{prop}, if there is one. For the structure of the property list,
5373 see @code{symbol-property}.
5376 Support for these extra slots may be removed in a future release, and it
5377 is probably better to avoid using them. For a more modern and Schemely
5378 approach to properties, see @ref{Object Properties}.
5381 @node Symbol Read Syntax
5382 @subsubsection Extended Read Syntax for Symbols
5384 The read syntax for a symbol is a sequence of letters, digits, and
5385 @dfn{extended alphabetic characters}, beginning with a character that
5386 cannot begin a number. In addition, the special cases of @code{+},
5387 @code{-}, and @code{...} are read as symbols even though numbers can
5388 begin with @code{+}, @code{-} or @code{.}.
5390 Extended alphabetic characters may be used within identifiers as if
5391 they were letters. The set of extended alphabetic characters is:
5394 ! $ % & * + - . / : < = > ? @@ ^ _ ~
5397 In addition to the standard read syntax defined above (which is taken
5398 from R5RS (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on
5399 Scheme})), Guile provides an extended symbol read syntax that allows the
5400 inclusion of unusual characters such as space characters, newlines and
5401 parentheses. If (for whatever reason) you need to write a symbol
5402 containing characters not mentioned above, you can do so as follows.
5406 Begin the symbol with the characters @code{#@{},
5409 write the characters of the symbol and
5412 finish the symbol with the characters @code{@}#}.
5415 Here are a few examples of this form of read syntax. The first symbol
5416 needs to use extended syntax because it contains a space character, the
5417 second because it contains a line break, and the last because it looks
5429 Although Guile provides this extended read syntax for symbols,
5430 widespread usage of it is discouraged because it is not portable and not
5434 @node Symbol Uninterned
5435 @subsubsection Uninterned Symbols
5437 What makes symbols useful is that they are automatically kept unique.
5438 There are no two symbols that are distinct objects but have the same
5439 name. But of course, there is no rule without exception. In addition
5440 to the normal symbols that have been discussed up to now, you can also
5441 create special @dfn{uninterned} symbols that behave slightly
5444 To understand what is different about them and why they might be useful,
5445 we look at how normal symbols are actually kept unique.
5447 Whenever Guile wants to find the symbol with a specific name, for
5448 example during @code{read} or when executing @code{string->symbol}, it
5449 first looks into a table of all existing symbols to find out whether a
5450 symbol with the given name already exists. When this is the case, Guile
5451 just returns that symbol. When not, a new symbol with the name is
5452 created and entered into the table so that it can be found later.
5454 Sometimes you might want to create a symbol that is guaranteed `fresh',
5455 i.e.@: a symbol that did not exist previously. You might also want to
5456 somehow guarantee that no one else will ever unintentionally stumble
5457 across your symbol in the future. These properties of a symbol are
5458 often needed when generating code during macro expansion. When
5459 introducing new temporary variables, you want to guarantee that they
5460 don't conflict with variables in other people's code.
5462 The simplest way to arrange for this is to create a new symbol but
5463 not enter it into the global table of all symbols. That way, no one
5464 will ever get access to your symbol by chance. Symbols that are not in
5465 the table are called @dfn{uninterned}. Of course, symbols that
5466 @emph{are} in the table are called @dfn{interned}.
5468 You create new uninterned symbols with the function @code{make-symbol}.
5469 You can test whether a symbol is interned or not with
5470 @code{symbol-interned?}.
5472 Uninterned symbols break the rule that the name of a symbol uniquely
5473 identifies the symbol object. Because of this, they can not be written
5474 out and read back in like interned symbols. Currently, Guile has no
5475 support for reading uninterned symbols. Note that the function
5476 @code{gensym} does not return uninterned symbols for this reason.
5478 @deffn {Scheme Procedure} make-symbol name
5479 @deffnx {C Function} scm_make_symbol (name)
5480 Return a new uninterned symbol with the name @var{name}. The returned
5481 symbol is guaranteed to be unique and future calls to
5482 @code{string->symbol} will not return it.
5485 @deffn {Scheme Procedure} symbol-interned? symbol
5486 @deffnx {C Function} scm_symbol_interned_p (symbol)
5487 Return @code{#t} if @var{symbol} is interned, otherwise return
5494 (define foo-1 (string->symbol "foo"))
5495 (define foo-2 (string->symbol "foo"))
5496 (define foo-3 (make-symbol "foo"))
5497 (define foo-4 (make-symbol "foo"))
5501 ; Two interned symbols with the same name are the same object,
5505 ; but a call to make-symbol with the same name returns a
5510 ; A call to make-symbol always returns a new object, even for
5514 @result{} #<uninterned-symbol foo 8085290>
5515 ; Uninterned symbols print differently from interned symbols,
5519 ; but they are still symbols,
5521 (symbol-interned? foo-3)
5523 ; just not interned.
5528 @subsection Keywords
5531 Keywords are self-evaluating objects with a convenient read syntax that
5532 makes them easy to type.
5534 Guile's keyword support conforms to R5RS, and adds a (switchable) read
5535 syntax extension to permit keywords to begin with @code{:} as well as
5536 @code{#:}, or to end with @code{:}.
5539 * Why Use Keywords?:: Motivation for keyword usage.
5540 * Coding With Keywords:: How to use keywords.
5541 * Keyword Read Syntax:: Read syntax for keywords.
5542 * Keyword Procedures:: Procedures for dealing with keywords.
5545 @node Why Use Keywords?
5546 @subsubsection Why Use Keywords?
5548 Keywords are useful in contexts where a program or procedure wants to be
5549 able to accept a large number of optional arguments without making its
5550 interface unmanageable.
5552 To illustrate this, consider a hypothetical @code{make-window}
5553 procedure, which creates a new window on the screen for drawing into
5554 using some graphical toolkit. There are many parameters that the caller
5555 might like to specify, but which could also be sensibly defaulted, for
5560 color depth -- Default: the color depth for the screen
5563 background color -- Default: white
5566 width -- Default: 600
5569 height -- Default: 400
5572 If @code{make-window} did not use keywords, the caller would have to
5573 pass in a value for each possible argument, remembering the correct
5574 argument order and using a special value to indicate the default value
5578 (make-window 'default ;; Color depth
5579 'default ;; Background color
5582 @dots{}) ;; More make-window arguments
5585 With keywords, on the other hand, defaulted arguments are omitted, and
5586 non-default arguments are clearly tagged by the appropriate keyword. As
5587 a result, the invocation becomes much clearer:
5590 (make-window #:width 800 #:height 100)
5593 On the other hand, for a simpler procedure with few arguments, the use
5594 of keywords would be a hindrance rather than a help. The primitive
5595 procedure @code{cons}, for example, would not be improved if it had to
5599 (cons #:car x #:cdr y)
5602 So the decision whether to use keywords or not is purely pragmatic: use
5603 them if they will clarify the procedure invocation at point of call.
5605 @node Coding With Keywords
5606 @subsubsection Coding With Keywords
5608 If a procedure wants to support keywords, it should take a rest argument
5609 and then use whatever means is convenient to extract keywords and their
5610 corresponding arguments from the contents of that rest argument.
5612 The following example illustrates the principle: the code for
5613 @code{make-window} uses a helper procedure called
5614 @code{get-keyword-value} to extract individual keyword arguments from
5618 (define (get-keyword-value args keyword default)
5619 (let ((kv (memq keyword args)))
5620 (if (and kv (>= (length kv) 2))
5624 (define (make-window . args)
5625 (let ((depth (get-keyword-value args #:depth screen-depth))
5626 (bg (get-keyword-value args #:bg "white"))
5627 (width (get-keyword-value args #:width 800))
5628 (height (get-keyword-value args #:height 100))
5633 But you don't need to write @code{get-keyword-value}. The @code{(ice-9
5634 optargs)} module provides a set of powerful macros that you can use to
5635 implement keyword-supporting procedures like this:
5638 (use-modules (ice-9 optargs))
5640 (define (make-window . args)
5641 (let-keywords args #f ((depth screen-depth)
5649 Or, even more economically, like this:
5652 (use-modules (ice-9 optargs))
5654 (define* (make-window #:key (depth screen-depth)
5661 For further details on @code{let-keywords}, @code{define*} and other
5662 facilities provided by the @code{(ice-9 optargs)} module, see
5663 @ref{Optional Arguments}.
5666 @node Keyword Read Syntax
5667 @subsubsection Keyword Read Syntax
5669 Guile, by default, only recognizes a keyword syntax that is compatible
5670 with R5RS. A token of the form @code{#:NAME}, where @code{NAME} has the
5671 same syntax as a Scheme symbol (@pxref{Symbol Read Syntax}), is the
5672 external representation of the keyword named @code{NAME}. Keyword
5673 objects print using this syntax as well, so values containing keyword
5674 objects can be read back into Guile. When used in an expression,
5675 keywords are self-quoting objects.
5677 If the @code{keyword} read option is set to @code{'prefix}, Guile also
5678 recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
5679 of the form @code{:NAME} are read as symbols, as required by R5RS.
5681 @cindex SRFI-88 keyword syntax
5683 If the @code{keyword} read option is set to @code{'postfix}, Guile
5684 recognizes the SRFI-88 read syntax @code{NAME:} (@pxref{SRFI-88}).
5685 Otherwise, tokens of this form are read as symbols.
5687 To enable and disable the alternative non-R5RS keyword syntax, you use
5688 the @code{read-set!} procedure documented @ref{Scheme Read}. Note that
5689 the @code{prefix} and @code{postfix} syntax are mutually exclusive.
5692 (read-set! keywords 'prefix)
5702 (read-set! keywords 'postfix)
5712 (read-set! keywords #f)
5720 ERROR: In expression :type:
5721 ERROR: Unbound variable: :type
5722 ABORT: (unbound-variable)
5725 @node Keyword Procedures
5726 @subsubsection Keyword Procedures
5728 @deffn {Scheme Procedure} keyword? obj
5729 @deffnx {C Function} scm_keyword_p (obj)
5730 Return @code{#t} if the argument @var{obj} is a keyword, else
5734 @deffn {Scheme Procedure} keyword->symbol keyword
5735 @deffnx {C Function} scm_keyword_to_symbol (keyword)
5736 Return the symbol with the same name as @var{keyword}.
5739 @deffn {Scheme Procedure} symbol->keyword symbol
5740 @deffnx {C Function} scm_symbol_to_keyword (symbol)
5741 Return the keyword with the same name as @var{symbol}.
5744 @deftypefn {C Function} int scm_is_keyword (SCM obj)
5745 Equivalent to @code{scm_is_true (scm_keyword_p (@var{obj}))}.
5748 @deftypefn {C Function} SCM scm_from_locale_keyword (const char *name)
5749 @deftypefnx {C Function} SCM scm_from_locale_keywordn (const char *name, size_t len)
5750 Equivalent to @code{scm_symbol_to_keyword (scm_from_locale_symbol
5751 (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_locale_symboln
5752 (@var{name}, @var{len}))}, respectively.
5754 Note that these functions should @emph{not} be used when @var{name} is a
5755 C string constant, because there is no guarantee that the current locale
5756 will match that of the source code. In such cases, use
5757 @code{scm_from_latin1_keyword} or @code{scm_from_utf8_keyword}.
5760 @deftypefn {C Function} SCM scm_from_latin1_keyword (const char *name)
5761 @deftypefnx {C Function} SCM scm_from_utf8_keyword (const char *name)
5762 Equivalent to @code{scm_symbol_to_keyword (scm_from_latin1_symbol
5763 (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_utf8_symbol
5764 (@var{name}))}, respectively.
5768 @subsection ``Functionality-Centric'' Data Types
5770 Procedures and macros are documented in their own sections: see
5771 @ref{Procedures} and @ref{Macros}.
5773 Variable objects are documented as part of the description of Guile's
5774 module system: see @ref{Variables}.
5776 Asyncs, dynamic roots and fluids are described in the section on
5777 scheduling: see @ref{Scheduling}.
5779 Hooks are documented in the section on general utility functions: see
5782 Ports are described in the section on I/O: see @ref{Input and Output}.
5784 Regular expressions are described in their own section: see @ref{Regular
5788 @c TeX-master: "guile.texi"