Fix documentation of the `string->utf' and `utf->string' functions.
[bpt/guile.git] / doc / ref / api-data.texi
1 @c -*-texinfo-*-
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
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @page
8 @node Simple Data Types
9 @section Simple Generic Data Types
10
11 This chapter describes those of Guile's simple data types which are
12 primarily used for their role as items of generic data. By
13 @dfn{simple} we mean data types that are not primarily used as
14 containers to hold other data --- i.e.@: pairs, lists, vectors and so on.
15 For the documentation of such @dfn{compound} data types, see
16 @ref{Compound Data Types}.
17
18 @c One of the great strengths of Scheme is that there is no straightforward
19 @c distinction between ``data'' and ``functionality''. For example,
20 @c Guile's support for dynamic linking could be described:
21
22 @c @itemize @bullet
23 @c @item
24 @c either in a ``data-centric'' way, as the behaviour and properties of the
25 @c ``dynamically linked object'' data type, and the operations that may be
26 @c applied to instances of this type
27
28 @c @item
29 @c or in a ``functionality-centric'' way, as the set of procedures that
30 @c constitute Guile's support for dynamic linking, in the context of the
31 @c module system.
32 @c @end itemize
33
34 @c The contents of this chapter are, therefore, a matter of judgment. By
35 @c @dfn{generic}, we mean to select those data types whose typical use as
36 @c @emph{data} in a wide variety of programming contexts is more important
37 @c than their use in the implementation of a particular piece of
38 @c @emph{functionality}. The last section of this chapter provides
39 @c references for all the data types that are documented not here but in a
40 @c ``functionality-centric'' way elsewhere in the manual.
41
42 @menu
43 * Booleans:: True/false values.
44 * Numbers:: Numerical data types.
45 * Characters:: Single characters.
46 * Character Sets:: Sets of characters.
47 * Strings:: Sequences of characters.
48 * Bytevectors:: Sequences of bytes.
49 * Regular Expressions:: Pattern matching and substitution.
50 * Symbols:: Symbols.
51 * Keywords:: Self-quoting, customizable display keywords.
52 * Other Types:: "Functionality-centric" data types.
53 @end menu
54
55
56 @node Booleans
57 @subsection Booleans
58 @tpindex Booleans
59
60 The two boolean values are @code{#t} for true and @code{#f} for false.
61
62 Boolean values are returned by predicate procedures, such as the general
63 equality predicates @code{eq?}, @code{eqv?} and @code{equal?}
64 (@pxref{Equality}) and numerical and string comparison operators like
65 @code{string=?} (@pxref{String Comparison}) and @code{<=}
66 (@pxref{Comparison}).
67
68 @lisp
69 (<= 3 8)
70 @result{} #t
71
72 (<= 3 -3)
73 @result{} #f
74
75 (equal? "house" "houses")
76 @result{} #f
77
78 (eq? #f #f)
79 @result{}
80 #t
81 @end lisp
82
83 In test condition contexts like @code{if} and @code{cond} (@pxref{if
84 cond case}), where a group of subexpressions will be evaluated only if a
85 @var{condition} expression evaluates to ``true'', ``true'' means any
86 value at all except @code{#f}.
87
88 @lisp
89 (if #t "yes" "no")
90 @result{} "yes"
91
92 (if 0 "yes" "no")
93 @result{} "yes"
94
95 (if #f "yes" "no")
96 @result{} "no"
97 @end lisp
98
99 A result of this asymmetry is that typical Scheme source code more often
100 uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to
101 represent an @code{if} or @code{cond} false value, whereas @code{#t} is
102 not necessary to represent an @code{if} or @code{cond} true value.
103
104 It is important to note that @code{#f} is @strong{not} equivalent to any
105 other Scheme value. In particular, @code{#f} is not the same as the
106 number 0 (like in C and C++), and not the same as the ``empty list''
107 (like in some Lisp dialects).
108
109 In C, the two Scheme boolean values are available as the two constants
110 @code{SCM_BOOL_T} for @code{#t} and @code{SCM_BOOL_F} for @code{#f}.
111 Care must be taken with the false value @code{SCM_BOOL_F}: it is not
112 false when used in C conditionals. In order to test for it, use
113 @code{scm_is_false} or @code{scm_is_true}.
114
115 @rnindex not
116 @deffn {Scheme Procedure} not x
117 @deffnx {C Function} scm_not (x)
118 Return @code{#t} if @var{x} is @code{#f}, else return @code{#f}.
119 @end deffn
120
121 @rnindex boolean?
122 @deffn {Scheme Procedure} boolean? obj
123 @deffnx {C Function} scm_boolean_p (obj)
124 Return @code{#t} if @var{obj} is either @code{#t} or @code{#f}, else
125 return @code{#f}.
126 @end deffn
127
128 @deftypevr {C Macro} SCM SCM_BOOL_T
129 The @code{SCM} representation of the Scheme object @code{#t}.
130 @end deftypevr
131
132 @deftypevr {C Macro} SCM SCM_BOOL_F
133 The @code{SCM} representation of the Scheme object @code{#f}.
134 @end deftypevr
135
136 @deftypefn {C Function} int scm_is_true (SCM obj)
137 Return @code{0} if @var{obj} is @code{#f}, else return @code{1}.
138 @end deftypefn
139
140 @deftypefn {C Function} int scm_is_false (SCM obj)
141 Return @code{1} if @var{obj} is @code{#f}, else return @code{0}.
142 @end deftypefn
143
144 @deftypefn {C Function} int scm_is_bool (SCM obj)
145 Return @code{1} if @var{obj} is either @code{#t} or @code{#f}, else
146 return @code{0}.
147 @end deftypefn
148
149 @deftypefn {C Function} SCM scm_from_bool (int val)
150 Return @code{#f} if @var{val} is @code{0}, else return @code{#t}.
151 @end deftypefn
152
153 @deftypefn {C Function} int scm_to_bool (SCM val)
154 Return @code{1} if @var{val} is @code{SCM_BOOL_T}, return @code{0}
155 when @var{val} is @code{SCM_BOOL_F}, else signal a `wrong type' error.
156
157 You should probably use @code{scm_is_true} instead of this function
158 when you just want to test a @code{SCM} value for trueness.
159 @end deftypefn
160
161 @node Numbers
162 @subsection Numerical data types
163 @tpindex Numbers
164
165 Guile supports a rich ``tower'' of numerical types --- integer,
166 rational, real and complex --- and provides an extensive set of
167 mathematical and scientific functions for operating on numerical
168 data. This section of the manual documents those types and functions.
169
170 You may also find it illuminating to read R5RS's presentation of numbers
171 in Scheme, which is particularly clear and accessible: see
172 @ref{Numbers,,,r5rs,R5RS}.
173
174 @menu
175 * Numerical Tower:: Scheme's numerical "tower".
176 * Integers:: Whole numbers.
177 * Reals and Rationals:: Real and rational numbers.
178 * Complex Numbers:: Complex numbers.
179 * Exactness:: Exactness and inexactness.
180 * Number Syntax:: Read syntax for numerical data.
181 * Integer Operations:: Operations on integer values.
182 * Comparison:: Comparison predicates.
183 * Conversion:: Converting numbers to and from strings.
184 * Complex:: Complex number operations.
185 * Arithmetic:: Arithmetic functions.
186 * Scientific:: Scientific functions.
187 * Bitwise Operations:: Logical AND, OR, NOT, and so on.
188 * Random:: Random number generation.
189 @end menu
190
191
192 @node Numerical Tower
193 @subsubsection Scheme's Numerical ``Tower''
194 @rnindex number?
195
196 Scheme's numerical ``tower'' consists of the following categories of
197 numbers:
198
199 @table @dfn
200 @item integers
201 Whole numbers, positive or negative; e.g.@: --5, 0, 18.
202
203 @item rationals
204 The set of numbers that can be expressed as @math{@var{p}/@var{q}}
205 where @var{p} and @var{q} are integers; e.g.@: @math{9/16} works, but
206 pi (an irrational number) doesn't. These include integers
207 (@math{@var{n}/1}).
208
209 @item real numbers
210 The set of numbers that describes all possible positions along a
211 one-dimensional line. This includes rationals as well as irrational
212 numbers.
213
214 @item complex numbers
215 The set of numbers that describes all possible positions in a two
216 dimensional space. This includes real as well as imaginary numbers
217 (@math{@var{a}+@var{b}i}, where @var{a} is the @dfn{real part},
218 @var{b} is the @dfn{imaginary part}, and @math{i} is the square root of
219 @minus{}1.)
220 @end table
221
222 It is called a tower because each category ``sits on'' the one that
223 follows it, in the sense that every integer is also a rational, every
224 rational is also real, and every real number is also a complex number
225 (but with zero imaginary part).
226
227 In addition to the classification into integers, rationals, reals and
228 complex numbers, Scheme also distinguishes between whether a number is
229 represented exactly or not. For example, the result of
230 @m{2\sin(\pi/4),2*sin(pi/4)} is exactly @m{\sqrt{2},2^(1/2)}, but Guile
231 can represent neither @m{\pi/4,pi/4} nor @m{\sqrt{2},2^(1/2)} exactly.
232 Instead, it stores an inexact approximation, using the C type
233 @code{double}.
234
235 Guile can represent exact rationals of any magnitude, inexact
236 rationals that fit into a C @code{double}, and inexact complex numbers
237 with @code{double} real and imaginary parts.
238
239 The @code{number?} predicate may be applied to any Scheme value to
240 discover whether the value is any of the supported numerical types.
241
242 @deffn {Scheme Procedure} number? obj
243 @deffnx {C Function} scm_number_p (obj)
244 Return @code{#t} if @var{obj} is any kind of number, else @code{#f}.
245 @end deffn
246
247 For example:
248
249 @lisp
250 (number? 3)
251 @result{} #t
252
253 (number? "hello there!")
254 @result{} #f
255
256 (define pi 3.141592654)
257 (number? pi)
258 @result{} #t
259 @end lisp
260
261 @deftypefn {C Function} int scm_is_number (SCM obj)
262 This is equivalent to @code{scm_is_true (scm_number_p (obj))}.
263 @end deftypefn
264
265 The next few subsections document each of Guile's numerical data types
266 in detail.
267
268 @node Integers
269 @subsubsection Integers
270
271 @tpindex Integer numbers
272
273 @rnindex integer?
274
275 Integers are whole numbers, that is numbers with no fractional part,
276 such as 2, 83, and @minus{}3789.
277
278 Integers in Guile can be arbitrarily big, as shown by the following
279 example.
280
281 @lisp
282 (define (factorial n)
283 (let loop ((n n) (product 1))
284 (if (= n 0)
285 product
286 (loop (- n 1) (* product n)))))
287
288 (factorial 3)
289 @result{} 6
290
291 (factorial 20)
292 @result{} 2432902008176640000
293
294 (- (factorial 45))
295 @result{} -119622220865480194561963161495657715064383733760000000000
296 @end lisp
297
298 Readers whose background is in programming languages where integers are
299 limited by the need to fit into just 4 or 8 bytes of memory may find
300 this surprising, or suspect that Guile's representation of integers is
301 inefficient. In fact, Guile achieves a near optimal balance of
302 convenience and efficiency by using the host computer's native
303 representation of integers where possible, and a more general
304 representation where the required number does not fit in the native
305 form. Conversion between these two representations is automatic and
306 completely invisible to the Scheme level programmer.
307
308 The infinities @samp{+inf.0} and @samp{-inf.0} are considered to be
309 inexact integers. They are explained in detail in the next section,
310 together with reals and rationals.
311
312 C has a host of different integer types, and Guile offers a host of
313 functions to convert between them and the @code{SCM} representation.
314 For example, a C @code{int} can be handled with @code{scm_to_int} and
315 @code{scm_from_int}. Guile also defines a few C integer types of its
316 own, to help with differences between systems.
317
318 C integer types that are not covered can be handled with the generic
319 @code{scm_to_signed_integer} and @code{scm_from_signed_integer} for
320 signed types, or with @code{scm_to_unsigned_integer} and
321 @code{scm_from_unsigned_integer} for unsigned types.
322
323 Scheme integers can be exact and inexact. For example, a number
324 written as @code{3.0} with an explicit decimal-point is inexact, but
325 it is also an integer. The functions @code{integer?} and
326 @code{scm_is_integer} report true for such a number, but the functions
327 @code{scm_is_signed_integer} and @code{scm_is_unsigned_integer} only
328 allow exact integers and thus report false. Likewise, the conversion
329 functions like @code{scm_to_signed_integer} only accept exact
330 integers.
331
332 The motivation for this behavior is that the inexactness of a number
333 should not be lost silently. If you want to allow inexact integers,
334 you can explicitly insert a call to @code{inexact->exact} or to its C
335 equivalent @code{scm_inexact_to_exact}. (Only inexact integers will
336 be converted by this call into exact integers; inexact non-integers
337 will become exact fractions.)
338
339 @deffn {Scheme Procedure} integer? x
340 @deffnx {C Function} scm_integer_p (x)
341 Return @code{#t} if @var{x} is an exact or inexact integer number, else
342 @code{#f}.
343
344 @lisp
345 (integer? 487)
346 @result{} #t
347
348 (integer? 3.0)
349 @result{} #t
350
351 (integer? -3.4)
352 @result{} #f
353
354 (integer? +inf.0)
355 @result{} #t
356 @end lisp
357 @end deffn
358
359 @deftypefn {C Function} int scm_is_integer (SCM x)
360 This is equivalent to @code{scm_is_true (scm_integer_p (x))}.
361 @end deftypefn
362
363 @defvr {C Type} scm_t_int8
364 @defvrx {C Type} scm_t_uint8
365 @defvrx {C Type} scm_t_int16
366 @defvrx {C Type} scm_t_uint16
367 @defvrx {C Type} scm_t_int32
368 @defvrx {C Type} scm_t_uint32
369 @defvrx {C Type} scm_t_int64
370 @defvrx {C Type} scm_t_uint64
371 @defvrx {C Type} scm_t_intmax
372 @defvrx {C Type} scm_t_uintmax
373 The C types are equivalent to the corresponding ISO C types but are
374 defined on all platforms, with the exception of @code{scm_t_int64} and
375 @code{scm_t_uint64}, which are only defined when a 64-bit type is
376 available. For example, @code{scm_t_int8} is equivalent to
377 @code{int8_t}.
378
379 You can regard these definitions as a stop-gap measure until all
380 platforms provide these types. If you know that all the platforms
381 that you are interested in already provide these types, it is better
382 to use them directly instead of the types provided by Guile.
383 @end defvr
384
385 @deftypefn {C Function} int scm_is_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
386 @deftypefnx {C Function} int scm_is_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
387 Return @code{1} when @var{x} represents an exact integer that is
388 between @var{min} and @var{max}, inclusive.
389
390 These functions can be used to check whether a @code{SCM} value will
391 fit into a given range, such as the range of a given C integer type.
392 If you just want to convert a @code{SCM} value to a given C integer
393 type, use one of the conversion functions directly.
394 @end deftypefn
395
396 @deftypefn {C Function} scm_t_intmax scm_to_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
397 @deftypefnx {C Function} scm_t_uintmax scm_to_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
398 When @var{x} represents an exact integer that is between @var{min} and
399 @var{max} inclusive, return that integer. Else signal an error,
400 either a `wrong-type' error when @var{x} is not an exact integer, or
401 an `out-of-range' error when it doesn't fit the given range.
402 @end deftypefn
403
404 @deftypefn {C Function} SCM scm_from_signed_integer (scm_t_intmax x)
405 @deftypefnx {C Function} SCM scm_from_unsigned_integer (scm_t_uintmax x)
406 Return the @code{SCM} value that represents the integer @var{x}. This
407 function will always succeed and will always return an exact number.
408 @end deftypefn
409
410 @deftypefn {C Function} char scm_to_char (SCM x)
411 @deftypefnx {C Function} {signed char} scm_to_schar (SCM x)
412 @deftypefnx {C Function} {unsigned char} scm_to_uchar (SCM x)
413 @deftypefnx {C Function} short scm_to_short (SCM x)
414 @deftypefnx {C Function} {unsigned short} scm_to_ushort (SCM x)
415 @deftypefnx {C Function} int scm_to_int (SCM x)
416 @deftypefnx {C Function} {unsigned int} scm_to_uint (SCM x)
417 @deftypefnx {C Function} long scm_to_long (SCM x)
418 @deftypefnx {C Function} {unsigned long} scm_to_ulong (SCM x)
419 @deftypefnx {C Function} {long long} scm_to_long_long (SCM x)
420 @deftypefnx {C Function} {unsigned long long} scm_to_ulong_long (SCM x)
421 @deftypefnx {C Function} size_t scm_to_size_t (SCM x)
422 @deftypefnx {C Function} ssize_t scm_to_ssize_t (SCM x)
423 @deftypefnx {C Function} scm_t_int8 scm_to_int8 (SCM x)
424 @deftypefnx {C Function} scm_t_uint8 scm_to_uint8 (SCM x)
425 @deftypefnx {C Function} scm_t_int16 scm_to_int16 (SCM x)
426 @deftypefnx {C Function} scm_t_uint16 scm_to_uint16 (SCM x)
427 @deftypefnx {C Function} scm_t_int32 scm_to_int32 (SCM x)
428 @deftypefnx {C Function} scm_t_uint32 scm_to_uint32 (SCM x)
429 @deftypefnx {C Function} scm_t_int64 scm_to_int64 (SCM x)
430 @deftypefnx {C Function} scm_t_uint64 scm_to_uint64 (SCM x)
431 @deftypefnx {C Function} scm_t_intmax scm_to_intmax (SCM x)
432 @deftypefnx {C Function} scm_t_uintmax scm_to_uintmax (SCM x)
433 When @var{x} represents an exact integer that fits into the indicated
434 C type, return that integer. Else signal an error, either a
435 `wrong-type' error when @var{x} is not an exact integer, or an
436 `out-of-range' error when it doesn't fit the given range.
437
438 The functions @code{scm_to_long_long}, @code{scm_to_ulong_long},
439 @code{scm_to_int64}, and @code{scm_to_uint64} are only available when
440 the corresponding types are.
441 @end deftypefn
442
443 @deftypefn {C Function} SCM scm_from_char (char x)
444 @deftypefnx {C Function} SCM scm_from_schar (signed char x)
445 @deftypefnx {C Function} SCM scm_from_uchar (unsigned char x)
446 @deftypefnx {C Function} SCM scm_from_short (short x)
447 @deftypefnx {C Function} SCM scm_from_ushort (unsigned short x)
448 @deftypefnx {C Function} SCM scm_from_int (int x)
449 @deftypefnx {C Function} SCM scm_from_uint (unsigned int x)
450 @deftypefnx {C Function} SCM scm_from_long (long x)
451 @deftypefnx {C Function} SCM scm_from_ulong (unsigned long x)
452 @deftypefnx {C Function} SCM scm_from_long_long (long long x)
453 @deftypefnx {C Function} SCM scm_from_ulong_long (unsigned long long x)
454 @deftypefnx {C Function} SCM scm_from_size_t (size_t x)
455 @deftypefnx {C Function} SCM scm_from_ssize_t (ssize_t x)
456 @deftypefnx {C Function} SCM scm_from_int8 (scm_t_int8 x)
457 @deftypefnx {C Function} SCM scm_from_uint8 (scm_t_uint8 x)
458 @deftypefnx {C Function} SCM scm_from_int16 (scm_t_int16 x)
459 @deftypefnx {C Function} SCM scm_from_uint16 (scm_t_uint16 x)
460 @deftypefnx {C Function} SCM scm_from_int32 (scm_t_int32 x)
461 @deftypefnx {C Function} SCM scm_from_uint32 (scm_t_uint32 x)
462 @deftypefnx {C Function} SCM scm_from_int64 (scm_t_int64 x)
463 @deftypefnx {C Function} SCM scm_from_uint64 (scm_t_uint64 x)
464 @deftypefnx {C Function} SCM scm_from_intmax (scm_t_intmax x)
465 @deftypefnx {C Function} SCM scm_from_uintmax (scm_t_uintmax x)
466 Return the @code{SCM} value that represents the integer @var{x}.
467 These functions will always succeed and will always return an exact
468 number.
469 @end deftypefn
470
471 @deftypefn {C Function} void scm_to_mpz (SCM val, mpz_t rop)
472 Assign @var{val} to the multiple precision integer @var{rop}.
473 @var{val} must be an exact integer, otherwise an error will be
474 signalled. @var{rop} must have been initialized with @code{mpz_init}
475 before this function is called. When @var{rop} is no longer needed
476 the occupied space must be freed with @code{mpz_clear}.
477 @xref{Initializing Integers,,, gmp, GNU MP Manual}, for details.
478 @end deftypefn
479
480 @deftypefn {C Function} SCM scm_from_mpz (mpz_t val)
481 Return the @code{SCM} value that represents @var{val}.
482 @end deftypefn
483
484 @node Reals and Rationals
485 @subsubsection Real and Rational Numbers
486 @tpindex Real numbers
487 @tpindex Rational numbers
488
489 @rnindex real?
490 @rnindex rational?
491
492 Mathematically, the real numbers are the set of numbers that describe
493 all possible points along a continuous, infinite, one-dimensional line.
494 The rational numbers are the set of all numbers that can be written as
495 fractions @var{p}/@var{q}, where @var{p} and @var{q} are integers.
496 All rational numbers are also real, but there are real numbers that
497 are not rational, for example @m{\sqrt2, the square root of 2}, and
498 @m{\pi,pi}.
499
500 Guile can represent both exact and inexact rational numbers, but it
501 can not represent irrational numbers. Exact rationals are represented
502 by storing the numerator and denominator as two exact integers.
503 Inexact rationals are stored as floating point numbers using the C
504 type @code{double}.
505
506 Exact rationals are written as a fraction of integers. There must be
507 no whitespace around the slash:
508
509 @lisp
510 1/2
511 -22/7
512 @end lisp
513
514 Even though the actual encoding of inexact rationals is in binary, it
515 may be helpful to think of it as a decimal number with a limited
516 number of significant figures and a decimal point somewhere, since
517 this corresponds to the standard notation for non-whole numbers. For
518 example:
519
520 @lisp
521 0.34
522 -0.00000142857931198
523 -5648394822220000000000.0
524 4.0
525 @end lisp
526
527 The limited precision of Guile's encoding means that any ``real'' number
528 in Guile can be written in a rational form, by multiplying and then dividing
529 by sufficient powers of 10 (or in fact, 2). For example,
530 @samp{-0.00000142857931198} is the same as @minus{}142857931198 divided by
531 100000000000000000. In Guile's current incarnation, therefore, the
532 @code{rational?} and @code{real?} predicates are equivalent.
533
534
535 Dividing by an exact zero leads to a error message, as one might
536 expect. However, dividing by an inexact zero does not produce an
537 error. Instead, the result of the division is either plus or minus
538 infinity, depending on the sign of the divided number.
539
540 The infinities are written @samp{+inf.0} and @samp{-inf.0},
541 respectively. This syntax is also recognized by @code{read} as an
542 extension to the usual Scheme syntax.
543
544 Dividing zero by zero yields something that is not a number at all:
545 @samp{+nan.0}. This is the special `not a number' value.
546
547 On platforms that follow @acronym{IEEE} 754 for their floating point
548 arithmetic, the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values
549 are implemented using the corresponding @acronym{IEEE} 754 values.
550 They behave in arithmetic operations like @acronym{IEEE} 754 describes
551 it, i.e., @code{(= +nan.0 +nan.0)} @result{} @code{#f}.
552
553 The infinities are inexact integers and are considered to be both even
554 and odd. While @samp{+nan.0} is not @code{=} to itself, it is
555 @code{eqv?} to itself.
556
557 To test for the special values, use the functions @code{inf?} and
558 @code{nan?}.
559
560 @deffn {Scheme Procedure} real? obj
561 @deffnx {C Function} scm_real_p (obj)
562 Return @code{#t} if @var{obj} is a real number, else @code{#f}. Note
563 that the sets of integer and rational values form subsets of the set
564 of real numbers, so the predicate will also be fulfilled if @var{obj}
565 is an integer number or a rational number.
566 @end deffn
567
568 @deffn {Scheme Procedure} rational? x
569 @deffnx {C Function} scm_rational_p (x)
570 Return @code{#t} if @var{x} is a rational number, @code{#f} otherwise.
571 Note that the set of integer values forms a subset of the set of
572 rational numbers, i. e. the predicate will also be fulfilled if
573 @var{x} is an integer number.
574
575 Since Guile can not represent irrational numbers, every number
576 satisfying @code{real?} also satisfies @code{rational?} in Guile.
577 @end deffn
578
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}.
583
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.
587
588 @lisp
589 (rationalize (inexact->exact 1.2) 1/100)
590 @result{} 6/5
591 @end lisp
592
593 @end deffn
594
595 @deffn {Scheme Procedure} inf? x
596 @deffnx {C Function} scm_inf_p (x)
597 Return @code{#t} if @var{x} is either @samp{+inf.0} or @samp{-inf.0},
598 @code{#f} otherwise.
599 @end deffn
600
601 @deffn {Scheme Procedure} nan? x
602 @deffnx {C Function} scm_nan_p (x)
603 Return @code{#t} if @var{x} is @samp{+nan.0}, @code{#f} otherwise.
604 @end deffn
605
606 @deffn {Scheme Procedure} nan
607 @deffnx {C Function} scm_nan ()
608 Return NaN.
609 @end deffn
610
611 @deffn {Scheme Procedure} inf
612 @deffnx {C Function} scm_inf ()
613 Return Inf.
614 @end deffn
615
616 @deffn {Scheme Procedure} numerator x
617 @deffnx {C Function} scm_numerator (x)
618 Return the numerator of the rational number @var{x}.
619 @end deffn
620
621 @deffn {Scheme Procedure} denominator x
622 @deffnx {C Function} scm_denominator (x)
623 Return the denominator of the rational number @var{x}.
624 @end deffn
625
626 @deftypefn {C Function} int scm_is_real (SCM val)
627 @deftypefnx {C Function} int scm_is_rational (SCM val)
628 Equivalent to @code{scm_is_true (scm_real_p (val))} and
629 @code{scm_is_true (scm_rational_p (val))}, respectively.
630 @end deftypefn
631
632 @deftypefn {C Function} double scm_to_double (SCM val)
633 Returns the number closest to @var{val} that is representable as a
634 @code{double}. Returns infinity for a @var{val} that is too large in
635 magnitude. The argument @var{val} must be a real number.
636 @end deftypefn
637
638 @deftypefn {C Function} SCM scm_from_double (double val)
639 Return the @code{SCM} value that represents @var{val}. The returned
640 value is inexact according to the predicate @code{inexact?}, but it
641 will be exactly equal to @var{val}.
642 @end deftypefn
643
644 @node Complex Numbers
645 @subsubsection Complex Numbers
646 @tpindex Complex numbers
647
648 @rnindex complex?
649
650 Complex numbers are the set of numbers that describe all possible points
651 in a two-dimensional space. The two coordinates of a particular point
652 in this space are known as the @dfn{real} and @dfn{imaginary} parts of
653 the complex number that describes that point.
654
655 In Guile, complex numbers are written in rectangular form as the sum of
656 their real and imaginary parts, using the symbol @code{i} to indicate
657 the imaginary part.
658
659 @lisp
660 3+4i
661 @result{}
662 3.0+4.0i
663
664 (* 3-8i 2.3+0.3i)
665 @result{}
666 9.3-17.5i
667 @end lisp
668
669 @cindex polar form
670 @noindent
671 Polar form can also be used, with an @samp{@@} between magnitude and
672 angle,
673
674 @lisp
675 1@@3.141592 @result{} -1.0 (approx)
676 -1@@1.57079 @result{} 0.0-1.0i (approx)
677 @end lisp
678
679 Guile represents a complex number with a non-zero imaginary part as a
680 pair of inexact rationals, so the real and imaginary parts of a
681 complex number have the same properties of inexactness and limited
682 precision as single inexact rational numbers. Guile can not represent
683 exact complex numbers with non-zero imaginary parts.
684
685 @deffn {Scheme Procedure} complex? z
686 @deffnx {C Function} scm_complex_p (z)
687 Return @code{#t} if @var{x} is a complex number, @code{#f}
688 otherwise. Note that the sets of real, rational and integer
689 values form subsets of the set of complex numbers, i. e. the
690 predicate will also be fulfilled if @var{x} is a real,
691 rational or integer number.
692 @end deffn
693
694 @deftypefn {C Function} int scm_is_complex (SCM val)
695 Equivalent to @code{scm_is_true (scm_complex_p (val))}.
696 @end deftypefn
697
698 @node Exactness
699 @subsubsection Exact and Inexact Numbers
700 @tpindex Exact numbers
701 @tpindex Inexact numbers
702
703 @rnindex exact?
704 @rnindex inexact?
705 @rnindex exact->inexact
706 @rnindex inexact->exact
707
708 R5RS requires that a calculation involving inexact numbers always
709 produces an inexact result. To meet this requirement, Guile
710 distinguishes between an exact integer value such as @samp{5} and the
711 corresponding inexact real value which, to the limited precision
712 available, has no fractional part, and is printed as @samp{5.0}. Guile
713 will only convert the latter value to the former when forced to do so by
714 an invocation of the @code{inexact->exact} procedure.
715
716 @deffn {Scheme Procedure} exact? z
717 @deffnx {C Function} scm_exact_p (z)
718 Return @code{#t} if the number @var{z} is exact, @code{#f}
719 otherwise.
720
721 @lisp
722 (exact? 2)
723 @result{} #t
724
725 (exact? 0.5)
726 @result{} #f
727
728 (exact? (/ 2))
729 @result{} #t
730 @end lisp
731
732 @end deffn
733
734 @deffn {Scheme Procedure} inexact? z
735 @deffnx {C Function} scm_inexact_p (z)
736 Return @code{#t} if the number @var{z} is inexact, @code{#f}
737 else.
738 @end deffn
739
740 @deffn {Scheme Procedure} inexact->exact z
741 @deffnx {C Function} scm_inexact_to_exact (z)
742 Return an exact number that is numerically closest to @var{z}, when
743 there is one. For inexact rationals, Guile returns the exact rational
744 that is numerically equal to the inexact rational. Inexact complex
745 numbers with a non-zero imaginary part can not be made exact.
746
747 @lisp
748 (inexact->exact 0.5)
749 @result{} 1/2
750 @end lisp
751
752 The following happens because 12/10 is not exactly representable as a
753 @code{double} (on most platforms). However, when reading a decimal
754 number that has been marked exact with the ``#e'' prefix, Guile is
755 able to represent it correctly.
756
757 @lisp
758 (inexact->exact 1.2)
759 @result{} 5404319552844595/4503599627370496
760
761 #e1.2
762 @result{} 6/5
763 @end lisp
764
765 @end deffn
766
767 @c begin (texi-doc-string "guile" "exact->inexact")
768 @deffn {Scheme Procedure} exact->inexact z
769 @deffnx {C Function} scm_exact_to_inexact (z)
770 Convert the number @var{z} to its inexact representation.
771 @end deffn
772
773
774 @node Number Syntax
775 @subsubsection Read Syntax for Numerical Data
776
777 The read syntax for integers is a string of digits, optionally
778 preceded by a minus or plus character, a code indicating the
779 base in which the integer is encoded, and a code indicating whether
780 the number is exact or inexact. The supported base codes are:
781
782 @table @code
783 @item #b
784 @itemx #B
785 the integer is written in binary (base 2)
786
787 @item #o
788 @itemx #O
789 the integer is written in octal (base 8)
790
791 @item #d
792 @itemx #D
793 the integer is written in decimal (base 10)
794
795 @item #x
796 @itemx #X
797 the integer is written in hexadecimal (base 16)
798 @end table
799
800 If the base code is omitted, the integer is assumed to be decimal. The
801 following examples show how these base codes are used.
802
803 @lisp
804 -13
805 @result{} -13
806
807 #d-13
808 @result{} -13
809
810 #x-13
811 @result{} -19
812
813 #b+1101
814 @result{} 13
815
816 #o377
817 @result{} 255
818 @end lisp
819
820 The codes for indicating exactness (which can, incidentally, be applied
821 to all numerical values) are:
822
823 @table @code
824 @item #e
825 @itemx #E
826 the number is exact
827
828 @item #i
829 @itemx #I
830 the number is inexact.
831 @end table
832
833 If the exactness indicator is omitted, the number is exact unless it
834 contains a radix point. Since Guile can not represent exact complex
835 numbers, an error is signalled when asking for them.
836
837 @lisp
838 (exact? 1.2)
839 @result{} #f
840
841 (exact? #e1.2)
842 @result{} #t
843
844 (exact? #e+1i)
845 ERROR: Wrong type argument
846 @end lisp
847
848 Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for
849 plus and minus infinity, respectively. The value must be written
850 exactly as shown, that is, they always must have a sign and exactly
851 one zero digit after the decimal point. It also understands
852 @samp{+nan.0} and @samp{-nan.0} for the special `not-a-number' value.
853 The sign is ignored for `not-a-number' and the value is always printed
854 as @samp{+nan.0}.
855
856 @node Integer Operations
857 @subsubsection Operations on Integer Values
858 @rnindex odd?
859 @rnindex even?
860 @rnindex quotient
861 @rnindex remainder
862 @rnindex modulo
863 @rnindex gcd
864 @rnindex lcm
865
866 @deffn {Scheme Procedure} odd? n
867 @deffnx {C Function} scm_odd_p (n)
868 Return @code{#t} if @var{n} is an odd number, @code{#f}
869 otherwise.
870 @end deffn
871
872 @deffn {Scheme Procedure} even? n
873 @deffnx {C Function} scm_even_p (n)
874 Return @code{#t} if @var{n} is an even number, @code{#f}
875 otherwise.
876 @end deffn
877
878 @c begin (texi-doc-string "guile" "quotient")
879 @c begin (texi-doc-string "guile" "remainder")
880 @deffn {Scheme Procedure} quotient n d
881 @deffnx {Scheme Procedure} remainder n d
882 @deffnx {C Function} scm_quotient (n, d)
883 @deffnx {C Function} scm_remainder (n, d)
884 Return the quotient or remainder from @var{n} divided by @var{d}. The
885 quotient is rounded towards zero, and the remainder will have the same
886 sign as @var{n}. In all cases quotient and remainder satisfy
887 @math{@var{n} = @var{q}*@var{d} + @var{r}}.
888
889 @lisp
890 (remainder 13 4) @result{} 1
891 (remainder -13 4) @result{} -1
892 @end lisp
893 @end deffn
894
895 @c begin (texi-doc-string "guile" "modulo")
896 @deffn {Scheme Procedure} modulo n d
897 @deffnx {C Function} scm_modulo (n, d)
898 Return the remainder from @var{n} divided by @var{d}, with the same
899 sign as @var{d}.
900
901 @lisp
902 (modulo 13 4) @result{} 1
903 (modulo -13 4) @result{} 3
904 (modulo 13 -4) @result{} -3
905 (modulo -13 -4) @result{} -1
906 @end lisp
907 @end deffn
908
909 @c begin (texi-doc-string "guile" "gcd")
910 @deffn {Scheme Procedure} gcd x@dots{}
911 @deffnx {C Function} scm_gcd (x, y)
912 Return the greatest common divisor of all arguments.
913 If called without arguments, 0 is returned.
914
915 The C function @code{scm_gcd} always takes two arguments, while the
916 Scheme function can take an arbitrary number.
917 @end deffn
918
919 @c begin (texi-doc-string "guile" "lcm")
920 @deffn {Scheme Procedure} lcm x@dots{}
921 @deffnx {C Function} scm_lcm (x, y)
922 Return the least common multiple of the arguments.
923 If called without arguments, 1 is returned.
924
925 The C function @code{scm_lcm} always takes two arguments, while the
926 Scheme function can take an arbitrary number.
927 @end deffn
928
929 @deffn {Scheme Procedure} modulo-expt n k m
930 @deffnx {C Function} scm_modulo_expt (n, k, m)
931 Return @var{n} raised to the integer exponent
932 @var{k}, modulo @var{m}.
933
934 @lisp
935 (modulo-expt 2 3 5)
936 @result{} 3
937 @end lisp
938 @end deffn
939
940 @node Comparison
941 @subsubsection Comparison Predicates
942 @rnindex zero?
943 @rnindex positive?
944 @rnindex negative?
945
946 The C comparison functions below always takes two arguments, while the
947 Scheme functions can take an arbitrary number. Also keep in mind that
948 the C functions return one of the Scheme boolean values
949 @code{SCM_BOOL_T} or @code{SCM_BOOL_F} which are both true as far as C
950 is concerned. Thus, always write @code{scm_is_true (scm_num_eq_p (x,
951 y))} when testing the two Scheme numbers @code{x} and @code{y} for
952 equality, for example.
953
954 @c begin (texi-doc-string "guile" "=")
955 @deffn {Scheme Procedure} =
956 @deffnx {C Function} scm_num_eq_p (x, y)
957 Return @code{#t} if all parameters are numerically equal.
958 @end deffn
959
960 @c begin (texi-doc-string "guile" "<")
961 @deffn {Scheme Procedure} <
962 @deffnx {C Function} scm_less_p (x, y)
963 Return @code{#t} if the list of parameters is monotonically
964 increasing.
965 @end deffn
966
967 @c begin (texi-doc-string "guile" ">")
968 @deffn {Scheme Procedure} >
969 @deffnx {C Function} scm_gr_p (x, y)
970 Return @code{#t} if the list of parameters is monotonically
971 decreasing.
972 @end deffn
973
974 @c begin (texi-doc-string "guile" "<=")
975 @deffn {Scheme Procedure} <=
976 @deffnx {C Function} scm_leq_p (x, y)
977 Return @code{#t} if the list of parameters is monotonically
978 non-decreasing.
979 @end deffn
980
981 @c begin (texi-doc-string "guile" ">=")
982 @deffn {Scheme Procedure} >=
983 @deffnx {C Function} scm_geq_p (x, y)
984 Return @code{#t} if the list of parameters is monotonically
985 non-increasing.
986 @end deffn
987
988 @c begin (texi-doc-string "guile" "zero?")
989 @deffn {Scheme Procedure} zero? z
990 @deffnx {C Function} scm_zero_p (z)
991 Return @code{#t} if @var{z} is an exact or inexact number equal to
992 zero.
993 @end deffn
994
995 @c begin (texi-doc-string "guile" "positive?")
996 @deffn {Scheme Procedure} positive? x
997 @deffnx {C Function} scm_positive_p (x)
998 Return @code{#t} if @var{x} is an exact or inexact number greater than
999 zero.
1000 @end deffn
1001
1002 @c begin (texi-doc-string "guile" "negative?")
1003 @deffn {Scheme Procedure} negative? x
1004 @deffnx {C Function} scm_negative_p (x)
1005 Return @code{#t} if @var{x} is an exact or inexact number less than
1006 zero.
1007 @end deffn
1008
1009
1010 @node Conversion
1011 @subsubsection Converting Numbers To and From Strings
1012 @rnindex number->string
1013 @rnindex string->number
1014
1015 The following procedures read and write numbers according to their
1016 external representation as defined by R5RS (@pxref{Lexical structure,
1017 R5RS Lexical Structure,, r5rs, The Revised^5 Report on the Algorithmic
1018 Language Scheme}). @xref{Number Input and Output, the @code{(ice-9
1019 i18n)} module}, for locale-dependent number parsing.
1020
1021 @deffn {Scheme Procedure} number->string n [radix]
1022 @deffnx {C Function} scm_number_to_string (n, radix)
1023 Return a string holding the external representation of the
1024 number @var{n} in the given @var{radix}. If @var{n} is
1025 inexact, a radix of 10 will be used.
1026 @end deffn
1027
1028 @deffn {Scheme Procedure} string->number string [radix]
1029 @deffnx {C Function} scm_string_to_number (string, radix)
1030 Return a number of the maximally precise representation
1031 expressed by the given @var{string}. @var{radix} must be an
1032 exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}
1033 is a default radix that may be overridden by an explicit radix
1034 prefix in @var{string} (e.g. "#o177"). If @var{radix} is not
1035 supplied, then the default radix is 10. If string is not a
1036 syntactically valid notation for a number, then
1037 @code{string->number} returns @code{#f}.
1038 @end deffn
1039
1040 @deftypefn {C Function} SCM scm_c_locale_stringn_to_number (const char *string, size_t len, unsigned radix)
1041 As per @code{string->number} above, but taking a C string, as pointer
1042 and length. The string characters should be in the current locale
1043 encoding (@code{locale} in the name refers only to that, there's no
1044 locale-dependent parsing).
1045 @end deftypefn
1046
1047
1048 @node Complex
1049 @subsubsection Complex Number Operations
1050 @rnindex make-rectangular
1051 @rnindex make-polar
1052 @rnindex real-part
1053 @rnindex imag-part
1054 @rnindex magnitude
1055 @rnindex angle
1056
1057 @deffn {Scheme Procedure} make-rectangular real_part imaginary_part
1058 @deffnx {C Function} scm_make_rectangular (real_part, imaginary_part)
1059 Return a complex number constructed of the given @var{real-part} and @var{imaginary-part} parts.
1060 @end deffn
1061
1062 @deffn {Scheme Procedure} make-polar x y
1063 @deffnx {C Function} scm_make_polar (x, y)
1064 @cindex polar form
1065 Return the complex number @var{x} * e^(i * @var{y}).
1066 @end deffn
1067
1068 @c begin (texi-doc-string "guile" "real-part")
1069 @deffn {Scheme Procedure} real-part z
1070 @deffnx {C Function} scm_real_part (z)
1071 Return the real part of the number @var{z}.
1072 @end deffn
1073
1074 @c begin (texi-doc-string "guile" "imag-part")
1075 @deffn {Scheme Procedure} imag-part z
1076 @deffnx {C Function} scm_imag_part (z)
1077 Return the imaginary part of the number @var{z}.
1078 @end deffn
1079
1080 @c begin (texi-doc-string "guile" "magnitude")
1081 @deffn {Scheme Procedure} magnitude z
1082 @deffnx {C Function} scm_magnitude (z)
1083 Return the magnitude of the number @var{z}. This is the same as
1084 @code{abs} for real arguments, but also allows complex numbers.
1085 @end deffn
1086
1087 @c begin (texi-doc-string "guile" "angle")
1088 @deffn {Scheme Procedure} angle z
1089 @deffnx {C Function} scm_angle (z)
1090 Return the angle of the complex number @var{z}.
1091 @end deffn
1092
1093 @deftypefn {C Function} SCM scm_c_make_rectangular (double re, double im)
1094 @deftypefnx {C Function} SCM scm_c_make_polar (double x, double y)
1095 Like @code{scm_make_rectangular} or @code{scm_make_polar},
1096 respectively, but these functions take @code{double}s as their
1097 arguments.
1098 @end deftypefn
1099
1100 @deftypefn {C Function} double scm_c_real_part (z)
1101 @deftypefnx {C Function} double scm_c_imag_part (z)
1102 Returns the real or imaginary part of @var{z} as a @code{double}.
1103 @end deftypefn
1104
1105 @deftypefn {C Function} double scm_c_magnitude (z)
1106 @deftypefnx {C Function} double scm_c_angle (z)
1107 Returns the magnitude or angle of @var{z} as a @code{double}.
1108 @end deftypefn
1109
1110
1111 @node Arithmetic
1112 @subsubsection Arithmetic Functions
1113 @rnindex max
1114 @rnindex min
1115 @rnindex +
1116 @rnindex *
1117 @rnindex -
1118 @rnindex /
1119 @findex 1+
1120 @findex 1-
1121 @rnindex abs
1122 @rnindex floor
1123 @rnindex ceiling
1124 @rnindex truncate
1125 @rnindex round
1126
1127 The C arithmetic functions below always takes two arguments, while the
1128 Scheme functions can take an arbitrary number. When you need to
1129 invoke them with just one argument, for example to compute the
1130 equivalent od @code{(- x)}, pass @code{SCM_UNDEFINED} as the second
1131 one: @code{scm_difference (x, SCM_UNDEFINED)}.
1132
1133 @c begin (texi-doc-string "guile" "+")
1134 @deffn {Scheme Procedure} + z1 @dots{}
1135 @deffnx {C Function} scm_sum (z1, z2)
1136 Return the sum of all parameter values. Return 0 if called without any
1137 parameters.
1138 @end deffn
1139
1140 @c begin (texi-doc-string "guile" "-")
1141 @deffn {Scheme Procedure} - z1 z2 @dots{}
1142 @deffnx {C Function} scm_difference (z1, z2)
1143 If called with one argument @var{z1}, -@var{z1} is returned. Otherwise
1144 the sum of all but the first argument are subtracted from the first
1145 argument.
1146 @end deffn
1147
1148 @c begin (texi-doc-string "guile" "*")
1149 @deffn {Scheme Procedure} * z1 @dots{}
1150 @deffnx {C Function} scm_product (z1, z2)
1151 Return the product of all arguments. If called without arguments, 1 is
1152 returned.
1153 @end deffn
1154
1155 @c begin (texi-doc-string "guile" "/")
1156 @deffn {Scheme Procedure} / z1 z2 @dots{}
1157 @deffnx {C Function} scm_divide (z1, z2)
1158 Divide the first argument by the product of the remaining arguments. If
1159 called with one argument @var{z1}, 1/@var{z1} is returned.
1160 @end deffn
1161
1162 @deffn {Scheme Procedure} 1+ z
1163 @deffnx {C Function} scm_oneplus (z)
1164 Return @math{@var{z} + 1}.
1165 @end deffn
1166
1167 @deffn {Scheme Procedure} 1- z
1168 @deffnx {C function} scm_oneminus (z)
1169 Return @math{@var{z} - 1}.
1170 @end deffn
1171
1172 @c begin (texi-doc-string "guile" "abs")
1173 @deffn {Scheme Procedure} abs x
1174 @deffnx {C Function} scm_abs (x)
1175 Return the absolute value of @var{x}.
1176
1177 @var{x} must be a number with zero imaginary part. To calculate the
1178 magnitude of a complex number, use @code{magnitude} instead.
1179 @end deffn
1180
1181 @c begin (texi-doc-string "guile" "max")
1182 @deffn {Scheme Procedure} max x1 x2 @dots{}
1183 @deffnx {C Function} scm_max (x1, x2)
1184 Return the maximum of all parameter values.
1185 @end deffn
1186
1187 @c begin (texi-doc-string "guile" "min")
1188 @deffn {Scheme Procedure} min x1 x2 @dots{}
1189 @deffnx {C Function} scm_min (x1, x2)
1190 Return the minimum of all parameter values.
1191 @end deffn
1192
1193 @c begin (texi-doc-string "guile" "truncate")
1194 @deffn {Scheme Procedure} truncate x
1195 @deffnx {C Function} scm_truncate_number (x)
1196 Round the inexact number @var{x} towards zero.
1197 @end deffn
1198
1199 @c begin (texi-doc-string "guile" "round")
1200 @deffn {Scheme Procedure} round x
1201 @deffnx {C Function} scm_round_number (x)
1202 Round the inexact number @var{x} to the nearest integer. When exactly
1203 halfway between two integers, round to the even one.
1204 @end deffn
1205
1206 @c begin (texi-doc-string "guile" "floor")
1207 @deffn {Scheme Procedure} floor x
1208 @deffnx {C Function} scm_floor (x)
1209 Round the number @var{x} towards minus infinity.
1210 @end deffn
1211
1212 @c begin (texi-doc-string "guile" "ceiling")
1213 @deffn {Scheme Procedure} ceiling x
1214 @deffnx {C Function} scm_ceiling (x)
1215 Round the number @var{x} towards infinity.
1216 @end deffn
1217
1218 @deftypefn {C Function} double scm_c_truncate (double x)
1219 @deftypefnx {C Function} double scm_c_round (double x)
1220 Like @code{scm_truncate_number} or @code{scm_round_number},
1221 respectively, but these functions take and return @code{double}
1222 values.
1223 @end deftypefn
1224
1225 @node Scientific
1226 @subsubsection Scientific Functions
1227
1228 The following procedures accept any kind of number as arguments,
1229 including complex numbers.
1230
1231 @rnindex sqrt
1232 @c begin (texi-doc-string "guile" "sqrt")
1233 @deffn {Scheme Procedure} sqrt z
1234 Return the square root of @var{z}. Of the two possible roots
1235 (positive and negative), the one with the a positive real part is
1236 returned, or if that's zero then a positive imaginary part. Thus,
1237
1238 @example
1239 (sqrt 9.0) @result{} 3.0
1240 (sqrt -9.0) @result{} 0.0+3.0i
1241 (sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i
1242 (sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i
1243 @end example
1244 @end deffn
1245
1246 @rnindex expt
1247 @c begin (texi-doc-string "guile" "expt")
1248 @deffn {Scheme Procedure} expt z1 z2
1249 Return @var{z1} raised to the power of @var{z2}.
1250 @end deffn
1251
1252 @rnindex sin
1253 @c begin (texi-doc-string "guile" "sin")
1254 @deffn {Scheme Procedure} sin z
1255 Return the sine of @var{z}.
1256 @end deffn
1257
1258 @rnindex cos
1259 @c begin (texi-doc-string "guile" "cos")
1260 @deffn {Scheme Procedure} cos z
1261 Return the cosine of @var{z}.
1262 @end deffn
1263
1264 @rnindex tan
1265 @c begin (texi-doc-string "guile" "tan")
1266 @deffn {Scheme Procedure} tan z
1267 Return the tangent of @var{z}.
1268 @end deffn
1269
1270 @rnindex asin
1271 @c begin (texi-doc-string "guile" "asin")
1272 @deffn {Scheme Procedure} asin z
1273 Return the arcsine of @var{z}.
1274 @end deffn
1275
1276 @rnindex acos
1277 @c begin (texi-doc-string "guile" "acos")
1278 @deffn {Scheme Procedure} acos z
1279 Return the arccosine of @var{z}.
1280 @end deffn
1281
1282 @rnindex atan
1283 @c begin (texi-doc-string "guile" "atan")
1284 @deffn {Scheme Procedure} atan z
1285 @deffnx {Scheme Procedure} atan y x
1286 Return the arctangent of @var{z}, or of @math{@var{y}/@var{x}}.
1287 @end deffn
1288
1289 @rnindex exp
1290 @c begin (texi-doc-string "guile" "exp")
1291 @deffn {Scheme Procedure} exp z
1292 Return e to the power of @var{z}, where e is the base of natural
1293 logarithms (2.71828@dots{}).
1294 @end deffn
1295
1296 @rnindex log
1297 @c begin (texi-doc-string "guile" "log")
1298 @deffn {Scheme Procedure} log z
1299 Return the natural logarithm of @var{z}.
1300 @end deffn
1301
1302 @c begin (texi-doc-string "guile" "log10")
1303 @deffn {Scheme Procedure} log10 z
1304 Return the base 10 logarithm of @var{z}.
1305 @end deffn
1306
1307 @c begin (texi-doc-string "guile" "sinh")
1308 @deffn {Scheme Procedure} sinh z
1309 Return the hyperbolic sine of @var{z}.
1310 @end deffn
1311
1312 @c begin (texi-doc-string "guile" "cosh")
1313 @deffn {Scheme Procedure} cosh z
1314 Return the hyperbolic cosine of @var{z}.
1315 @end deffn
1316
1317 @c begin (texi-doc-string "guile" "tanh")
1318 @deffn {Scheme Procedure} tanh z
1319 Return the hyperbolic tangent of @var{z}.
1320 @end deffn
1321
1322 @c begin (texi-doc-string "guile" "asinh")
1323 @deffn {Scheme Procedure} asinh z
1324 Return the hyperbolic arcsine of @var{z}.
1325 @end deffn
1326
1327 @c begin (texi-doc-string "guile" "acosh")
1328 @deffn {Scheme Procedure} acosh z
1329 Return the hyperbolic arccosine of @var{z}.
1330 @end deffn
1331
1332 @c begin (texi-doc-string "guile" "atanh")
1333 @deffn {Scheme Procedure} atanh z
1334 Return the hyperbolic arctangent of @var{z}.
1335 @end deffn
1336
1337
1338 @node Bitwise Operations
1339 @subsubsection Bitwise Operations
1340
1341 For the following bitwise functions, negative numbers are treated as
1342 infinite precision twos-complements. For instance @math{-6} is bits
1343 @math{@dots{}111010}, with infinitely many ones on the left. It can
1344 be seen that adding 6 (binary 110) to such a bit pattern gives all
1345 zeros.
1346
1347 @deffn {Scheme Procedure} logand n1 n2 @dots{}
1348 @deffnx {C Function} scm_logand (n1, n2)
1349 Return the bitwise @sc{and} of the integer arguments.
1350
1351 @lisp
1352 (logand) @result{} -1
1353 (logand 7) @result{} 7
1354 (logand #b111 #b011 #b001) @result{} 1
1355 @end lisp
1356 @end deffn
1357
1358 @deffn {Scheme Procedure} logior n1 n2 @dots{}
1359 @deffnx {C Function} scm_logior (n1, n2)
1360 Return the bitwise @sc{or} of the integer arguments.
1361
1362 @lisp
1363 (logior) @result{} 0
1364 (logior 7) @result{} 7
1365 (logior #b000 #b001 #b011) @result{} 3
1366 @end lisp
1367 @end deffn
1368
1369 @deffn {Scheme Procedure} logxor n1 n2 @dots{}
1370 @deffnx {C Function} scm_loxor (n1, n2)
1371 Return the bitwise @sc{xor} of the integer arguments. A bit is
1372 set in the result if it is set in an odd number of arguments.
1373
1374 @lisp
1375 (logxor) @result{} 0
1376 (logxor 7) @result{} 7
1377 (logxor #b000 #b001 #b011) @result{} 2
1378 (logxor #b000 #b001 #b011 #b011) @result{} 1
1379 @end lisp
1380 @end deffn
1381
1382 @deffn {Scheme Procedure} lognot n
1383 @deffnx {C Function} scm_lognot (n)
1384 Return the integer which is the ones-complement of the integer
1385 argument, ie.@: each 0 bit is changed to 1 and each 1 bit to 0.
1386
1387 @lisp
1388 (number->string (lognot #b10000000) 2)
1389 @result{} "-10000001"
1390 (number->string (lognot #b0) 2)
1391 @result{} "-1"
1392 @end lisp
1393 @end deffn
1394
1395 @deffn {Scheme Procedure} logtest j k
1396 @deffnx {C Function} scm_logtest (j, k)
1397 Test whether @var{j} and @var{k} have any 1 bits in common. This is
1398 equivalent to @code{(not (zero? (logand j k)))}, but without actually
1399 calculating the @code{logand}, just testing for non-zero.
1400
1401 @lisp
1402 (logtest #b0100 #b1011) @result{} #f
1403 (logtest #b0100 #b0111) @result{} #t
1404 @end lisp
1405 @end deffn
1406
1407 @deffn {Scheme Procedure} logbit? index j
1408 @deffnx {C Function} scm_logbit_p (index, j)
1409 Test whether bit number @var{index} in @var{j} is set. @var{index}
1410 starts from 0 for the least significant bit.
1411
1412 @lisp
1413 (logbit? 0 #b1101) @result{} #t
1414 (logbit? 1 #b1101) @result{} #f
1415 (logbit? 2 #b1101) @result{} #t
1416 (logbit? 3 #b1101) @result{} #t
1417 (logbit? 4 #b1101) @result{} #f
1418 @end lisp
1419 @end deffn
1420
1421 @deffn {Scheme Procedure} ash n cnt
1422 @deffnx {C Function} scm_ash (n, cnt)
1423 Return @var{n} shifted left by @var{cnt} bits, or shifted right if
1424 @var{cnt} is negative. This is an ``arithmetic'' shift.
1425
1426 This is effectively a multiplication by @m{2^{cnt}, 2^@var{cnt}}, and
1427 when @var{cnt} is negative it's a division, rounded towards negative
1428 infinity. (Note that this is not the same rounding as @code{quotient}
1429 does.)
1430
1431 With @var{n} viewed as an infinite precision twos complement,
1432 @code{ash} means a left shift introducing zero bits, or a right shift
1433 dropping bits.
1434
1435 @lisp
1436 (number->string (ash #b1 3) 2) @result{} "1000"
1437 (number->string (ash #b1010 -1) 2) @result{} "101"
1438
1439 ;; -23 is bits ...11101001, -6 is bits ...111010
1440 (ash -23 -2) @result{} -6
1441 @end lisp
1442 @end deffn
1443
1444 @deffn {Scheme Procedure} logcount n
1445 @deffnx {C Function} scm_logcount (n)
1446 Return the number of bits in integer @var{n}. If @var{n} is
1447 positive, the 1-bits in its binary representation are counted.
1448 If negative, the 0-bits in its two's-complement binary
1449 representation are counted. If zero, 0 is returned.
1450
1451 @lisp
1452 (logcount #b10101010)
1453 @result{} 4
1454 (logcount 0)
1455 @result{} 0
1456 (logcount -2)
1457 @result{} 1
1458 @end lisp
1459 @end deffn
1460
1461 @deffn {Scheme Procedure} integer-length n
1462 @deffnx {C Function} scm_integer_length (n)
1463 Return the number of bits necessary to represent @var{n}.
1464
1465 For positive @var{n} this is how many bits to the most significant one
1466 bit. For negative @var{n} it's how many bits to the most significant
1467 zero bit in twos complement form.
1468
1469 @lisp
1470 (integer-length #b10101010) @result{} 8
1471 (integer-length #b1111) @result{} 4
1472 (integer-length 0) @result{} 0
1473 (integer-length -1) @result{} 0
1474 (integer-length -256) @result{} 8
1475 (integer-length -257) @result{} 9
1476 @end lisp
1477 @end deffn
1478
1479 @deffn {Scheme Procedure} integer-expt n k
1480 @deffnx {C Function} scm_integer_expt (n, k)
1481 Return @var{n} raised to the power @var{k}. @var{k} must be an exact
1482 integer, @var{n} can be any number.
1483
1484 Negative @var{k} is supported, and results in @m{1/n^|k|, 1/n^abs(k)}
1485 in the usual way. @math{@var{n}^0} is 1, as usual, and that includes
1486 @math{0^0} is 1.
1487
1488 @lisp
1489 (integer-expt 2 5) @result{} 32
1490 (integer-expt -3 3) @result{} -27
1491 (integer-expt 5 -3) @result{} 1/125
1492 (integer-expt 0 0) @result{} 1
1493 @end lisp
1494 @end deffn
1495
1496 @deffn {Scheme Procedure} bit-extract n start end
1497 @deffnx {C Function} scm_bit_extract (n, start, end)
1498 Return the integer composed of the @var{start} (inclusive)
1499 through @var{end} (exclusive) bits of @var{n}. The
1500 @var{start}th bit becomes the 0-th bit in the result.
1501
1502 @lisp
1503 (number->string (bit-extract #b1101101010 0 4) 2)
1504 @result{} "1010"
1505 (number->string (bit-extract #b1101101010 4 9) 2)
1506 @result{} "10110"
1507 @end lisp
1508 @end deffn
1509
1510
1511 @node Random
1512 @subsubsection Random Number Generation
1513
1514 Pseudo-random numbers are generated from a random state object, which
1515 can be created with @code{seed->random-state}. The @var{state}
1516 parameter to the various functions below is optional, it defaults to
1517 the state object in the @code{*random-state*} variable.
1518
1519 @deffn {Scheme Procedure} copy-random-state [state]
1520 @deffnx {C Function} scm_copy_random_state (state)
1521 Return a copy of the random state @var{state}.
1522 @end deffn
1523
1524 @deffn {Scheme Procedure} random n [state]
1525 @deffnx {C Function} scm_random (n, state)
1526 Return a number in [0, @var{n}).
1527
1528 Accepts a positive integer or real n and returns a
1529 number of the same type between zero (inclusive) and
1530 @var{n} (exclusive). The values returned have a uniform
1531 distribution.
1532 @end deffn
1533
1534 @deffn {Scheme Procedure} random:exp [state]
1535 @deffnx {C Function} scm_random_exp (state)
1536 Return an inexact real in an exponential distribution with mean
1537 1. For an exponential distribution with mean @var{u} use @code{(*
1538 @var{u} (random:exp))}.
1539 @end deffn
1540
1541 @deffn {Scheme Procedure} random:hollow-sphere! vect [state]
1542 @deffnx {C Function} scm_random_hollow_sphere_x (vect, state)
1543 Fills @var{vect} with inexact real random numbers the sum of whose
1544 squares is equal to 1.0. Thinking of @var{vect} as coordinates in
1545 space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1546 the coordinates are uniformly distributed over the surface of the unit
1547 n-sphere.
1548 @end deffn
1549
1550 @deffn {Scheme Procedure} random:normal [state]
1551 @deffnx {C Function} scm_random_normal (state)
1552 Return an inexact real in a normal distribution. The distribution
1553 used has mean 0 and standard deviation 1. For a normal distribution
1554 with mean @var{m} and standard deviation @var{d} use @code{(+ @var{m}
1555 (* @var{d} (random:normal)))}.
1556 @end deffn
1557
1558 @deffn {Scheme Procedure} random:normal-vector! vect [state]
1559 @deffnx {C Function} scm_random_normal_vector_x (vect, state)
1560 Fills @var{vect} with inexact real random numbers that are
1561 independent and standard normally distributed
1562 (i.e., with mean 0 and variance 1).
1563 @end deffn
1564
1565 @deffn {Scheme Procedure} random:solid-sphere! vect [state]
1566 @deffnx {C Function} scm_random_solid_sphere_x (vect, state)
1567 Fills @var{vect} with inexact real random numbers the sum of whose
1568 squares is less than 1.0. Thinking of @var{vect} as coordinates in
1569 space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1570 the coordinates are uniformly distributed within the unit
1571 @var{n}-sphere.
1572 @c FIXME: What does this mean, particularly the n-sphere part?
1573 @end deffn
1574
1575 @deffn {Scheme Procedure} random:uniform [state]
1576 @deffnx {C Function} scm_random_uniform (state)
1577 Return a uniformly distributed inexact real random number in
1578 [0,1).
1579 @end deffn
1580
1581 @deffn {Scheme Procedure} seed->random-state seed
1582 @deffnx {C Function} scm_seed_to_random_state (seed)
1583 Return a new random state using @var{seed}.
1584 @end deffn
1585
1586 @defvar *random-state*
1587 The global random state used by the above functions when the
1588 @var{state} parameter is not given.
1589 @end defvar
1590
1591 Note that the initial value of @code{*random-state*} is the same every
1592 time Guile starts up. Therefore, if you don't pass a @var{state}
1593 parameter to the above procedures, and you don't set
1594 @code{*random-state*} to @code{(seed->random-state your-seed)}, where
1595 @code{your-seed} is something that @emph{isn't} the same every time,
1596 you'll get the same sequence of ``random'' numbers on every run.
1597
1598 For example, unless the relevant source code has changed, @code{(map
1599 random (cdr (iota 30)))}, if the first use of random numbers since
1600 Guile started up, will always give:
1601
1602 @lisp
1603 (map random (cdr (iota 19)))
1604 @result{}
1605 (0 1 1 2 2 2 1 2 6 7 10 0 5 3 12 5 5 12)
1606 @end lisp
1607
1608 To use the time of day as the random seed, you can use code like this:
1609
1610 @lisp
1611 (let ((time (gettimeofday)))
1612 (set! *random-state*
1613 (seed->random-state (+ (car time)
1614 (cdr time)))))
1615 @end lisp
1616
1617 @noindent
1618 And then (depending on the time of day, of course):
1619
1620 @lisp
1621 (map random (cdr (iota 19)))
1622 @result{}
1623 (0 0 1 0 2 4 5 4 5 5 9 3 10 1 8 3 14 17)
1624 @end lisp
1625
1626 For security applications, such as password generation, you should use
1627 more bits of seed. Otherwise an open source password generator could
1628 be attacked by guessing the seed@dots{} but that's a subject for
1629 another manual.
1630
1631
1632 @node Characters
1633 @subsection Characters
1634 @tpindex Characters
1635
1636 In Scheme, there is a data type to describe a single character.
1637
1638 Defining what exactly a character @emph{is} can be more complicated
1639 than it seems. Guile follows the advice of R6RS and uses The Unicode
1640 Standard to help define what a character is. So, for Guile, a
1641 character is anything in the Unicode Character Database.
1642
1643 @cindex code point
1644 @cindex Unicode code point
1645
1646 The Unicode Character Database is basically a table of characters
1647 indexed using integers called 'code points'. Valid code points are in
1648 the ranges 0 to @code{#xD7FF} inclusive or @code{#xE000} to
1649 @code{#x10FFFF} inclusive, which is about 1.1 million code points.
1650
1651 @cindex designated code point
1652 @cindex code point, designated
1653
1654 Any code point that has been assigned to a character or that has
1655 otherwise been given a meaning by Unicode is called a 'designated code
1656 point'. Most of the designated code points, about 200,000 of them,
1657 indicate characters, accents or other combining marks that modify
1658 other characters, symbols, whitespace, and control characters. Some
1659 are not characters but indicators that suggest how to format or
1660 display neighboring characters.
1661
1662 @cindex reserved code point
1663 @cindex code point, reserved
1664
1665 If a code point is not a designated code point -- if it has not been
1666 assigned to a character by The Unicode Standard -- it is a 'reserved
1667 code point', meaning that they are reserved for future use. Most of
1668 the code points, about 800,000, are 'reserved code points'.
1669
1670 By convention, a Unicode code point is written as
1671 ``U+XXXX'' where ``XXXX'' is a hexadecimal number. Please note that
1672 this convenient notation is not valid code. Guile does not interpret
1673 ``U+XXXX'' as a character.
1674
1675 In Scheme, a character literal is written as @code{#\@var{name}} where
1676 @var{name} is the name of the character that you want. Printable
1677 characters have their usual single character name; for example,
1678 @code{#\a} is a lower case @code{a}.
1679
1680 Some of the code points are 'combining characters' that are not meant
1681 to be printed by themselves but are instead meant to modify the
1682 appearance of the previous character. For combining characters, an
1683 alternate form of the character literal is @code{#\} followed by
1684 U+25CC (a small, dotted circle), followed by the combining character.
1685 This allows the combining character to be drawn on the circle, not on
1686 the backslash of @code{#\}.
1687
1688 Many of the non-printing characters, such as whitespace characters and
1689 control characters, also have names.
1690
1691 The most commonly used non-printing characters have long character
1692 names, described in the table below.
1693
1694 @multitable {@code{#\backspace}} {Preferred}
1695 @item Character Name @tab Codepoint
1696 @item @code{#\nul} @tab U+0000
1697 @item @code{#\alarm} @tab u+0007
1698 @item @code{#\backspace} @tab U+0008
1699 @item @code{#\tab} @tab U+0009
1700 @item @code{#\linefeed} @tab U+000A
1701 @item @code{#\newline} @tab U+000A
1702 @item @code{#\vtab} @tab U+000B
1703 @item @code{#\page} @tab U+000C
1704 @item @code{#\return} @tab U+000D
1705 @item @code{#\esc} @tab U+001B
1706 @item @code{#\space} @tab U+0020
1707 @item @code{#\delete} @tab U+007F
1708 @end multitable
1709
1710 There are also short names for all of the ``C0 control characters''
1711 (those with code points below 32). The following table lists the short
1712 name for each character.
1713
1714 @multitable @columnfractions .25 .25 .25 .25
1715 @item 0 = @code{#\nul}
1716 @tab 1 = @code{#\soh}
1717 @tab 2 = @code{#\stx}
1718 @tab 3 = @code{#\etx}
1719 @item 4 = @code{#\eot}
1720 @tab 5 = @code{#\enq}
1721 @tab 6 = @code{#\ack}
1722 @tab 7 = @code{#\bel}
1723 @item 8 = @code{#\bs}
1724 @tab 9 = @code{#\ht}
1725 @tab 10 = @code{#\lf}
1726 @tab 11 = @code{#\vt}
1727 @item 12 = @code{#\ff}
1728 @tab 13 = @code{#\cr}
1729 @tab 14 = @code{#\so}
1730 @tab 15 = @code{#\si}
1731 @item 16 = @code{#\dle}
1732 @tab 17 = @code{#\dc1}
1733 @tab 18 = @code{#\dc2}
1734 @tab 19 = @code{#\dc3}
1735 @item 20 = @code{#\dc4}
1736 @tab 21 = @code{#\nak}
1737 @tab 22 = @code{#\syn}
1738 @tab 23 = @code{#\etb}
1739 @item 24 = @code{#\can}
1740 @tab 25 = @code{#\em}
1741 @tab 26 = @code{#\sub}
1742 @tab 27 = @code{#\esc}
1743 @item 28 = @code{#\fs}
1744 @tab 29 = @code{#\gs}
1745 @tab 30 = @code{#\rs}
1746 @tab 31 = @code{#\us}
1747 @item 32 = @code{#\sp}
1748 @end multitable
1749
1750 The short name for the ``delete'' character (code point U+007F) is
1751 @code{#\del}.
1752
1753 There are also a few alternative names left over for compatibility with
1754 previous versions of Guile.
1755
1756 @multitable {@code{#\backspace}} {Preferred}
1757 @item Alternate @tab Standard
1758 @item @code{#\nl} @tab @code{#\newline}
1759 @item @code{#\np} @tab @code{#\page}
1760 @item @code{#\null} @tab @code{#\nul}
1761 @end multitable
1762
1763 Characters may also be written using their code point values. They can
1764 be written with as an octal number, such as @code{#\10} for
1765 @code{#\bs} or @code{#\177} for @code{#\del}.
1766
1767 When the @code{r6rs-hex-escapes} reader option is enabled, there is an
1768 additional syntax for character escapes: @code{#\xHHHH} -- the letter 'x'
1769 followed by a hexadecimal number of one to eight digits.
1770
1771 @lisp
1772 (read-enable 'r6rs-hex-escapes)
1773 @end lisp
1774
1775 Enabling this option will also change the hex escape format for strings. More
1776 on string escapes can be found at (@pxref{String Syntax}). More on reader
1777 options in general can be found at (@pxref{Reader options}).
1778
1779 @rnindex char?
1780 @deffn {Scheme Procedure} char? x
1781 @deffnx {C Function} scm_char_p (x)
1782 Return @code{#t} iff @var{x} is a character, else @code{#f}.
1783 @end deffn
1784
1785 Fundamentally, the character comparison operations below are
1786 numeric comparisons of the character's code points.
1787
1788 @rnindex char=?
1789 @deffn {Scheme Procedure} char=? x y
1790 Return @code{#t} iff code point of @var{x} is equal to the code point
1791 of @var{y}, else @code{#f}.
1792 @end deffn
1793
1794 @rnindex char<?
1795 @deffn {Scheme Procedure} char<? x y
1796 Return @code{#t} iff the code point of @var{x} is less than the code
1797 point of @var{y}, else @code{#f}.
1798 @end deffn
1799
1800 @rnindex char<=?
1801 @deffn {Scheme Procedure} char<=? x y
1802 Return @code{#t} iff the code point of @var{x} is less than or equal
1803 to the code point of @var{y}, else @code{#f}.
1804 @end deffn
1805
1806 @rnindex char>?
1807 @deffn {Scheme Procedure} char>? x y
1808 Return @code{#t} iff the code point of @var{x} is greater than the
1809 code point of @var{y}, else @code{#f}.
1810 @end deffn
1811
1812 @rnindex char>=?
1813 @deffn {Scheme Procedure} char>=? x y
1814 Return @code{#t} iff the code point of @var{x} is greater than or
1815 equal to the code point of @var{y}, else @code{#f}.
1816 @end deffn
1817
1818 @cindex case folding
1819
1820 Case-insensitive character comparisons use @emph{Unicode case
1821 folding}. In case folding comparisons, if a character is lowercase
1822 and has an uppercase form that can be expressed as a single character,
1823 it is converted to uppercase before comparison. All other characters
1824 undergo no conversion before the comparison occurs. This includes the
1825 German sharp S (Eszett) which is not uppercased before conversion
1826 because its uppercase form has two characters. Unicode case folding
1827 is language independent: it uses rules that are generally true, but,
1828 it cannot cover all cases for all languages.
1829
1830 @rnindex char-ci=?
1831 @deffn {Scheme Procedure} char-ci=? x y
1832 Return @code{#t} iff the case-folded code point of @var{x} is the same
1833 as the case-folded code point of @var{y}, else @code{#f}.
1834 @end deffn
1835
1836 @rnindex char-ci<?
1837 @deffn {Scheme Procedure} char-ci<? x y
1838 Return @code{#t} iff the case-folded code point of @var{x} is less
1839 than the case-folded code point of @var{y}, else @code{#f}.
1840 @end deffn
1841
1842 @rnindex char-ci<=?
1843 @deffn {Scheme Procedure} char-ci<=? x y
1844 Return @code{#t} iff the case-folded code point of @var{x} is less
1845 than or equal to the case-folded code point of @var{y}, else
1846 @code{#f}.
1847 @end deffn
1848
1849 @rnindex char-ci>?
1850 @deffn {Scheme Procedure} char-ci>? x y
1851 Return @code{#t} iff the case-folded code point of @var{x} is greater
1852 than the case-folded code point of @var{y}, else @code{#f}.
1853 @end deffn
1854
1855 @rnindex char-ci>=?
1856 @deffn {Scheme Procedure} char-ci>=? x y
1857 Return @code{#t} iff the case-folded code point of @var{x} is greater
1858 than or equal to the case-folded code point of @var{y}, else
1859 @code{#f}.
1860 @end deffn
1861
1862 @rnindex char-alphabetic?
1863 @deffn {Scheme Procedure} char-alphabetic? chr
1864 @deffnx {C Function} scm_char_alphabetic_p (chr)
1865 Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.
1866 @end deffn
1867
1868 @rnindex char-numeric?
1869 @deffn {Scheme Procedure} char-numeric? chr
1870 @deffnx {C Function} scm_char_numeric_p (chr)
1871 Return @code{#t} iff @var{chr} is numeric, else @code{#f}.
1872 @end deffn
1873
1874 @rnindex char-whitespace?
1875 @deffn {Scheme Procedure} char-whitespace? chr
1876 @deffnx {C Function} scm_char_whitespace_p (chr)
1877 Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.
1878 @end deffn
1879
1880 @rnindex char-upper-case?
1881 @deffn {Scheme Procedure} char-upper-case? chr
1882 @deffnx {C Function} scm_char_upper_case_p (chr)
1883 Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.
1884 @end deffn
1885
1886 @rnindex char-lower-case?
1887 @deffn {Scheme Procedure} char-lower-case? chr
1888 @deffnx {C Function} scm_char_lower_case_p (chr)
1889 Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.
1890 @end deffn
1891
1892 @deffn {Scheme Procedure} char-is-both? chr
1893 @deffnx {C Function} scm_char_is_both_p (chr)
1894 Return @code{#t} iff @var{chr} is either uppercase or lowercase, else
1895 @code{#f}.
1896 @end deffn
1897
1898 @deffn {Scheme Procedure} char-general-category chr
1899 @deffnx {C Function} scm_char_general_category (chr)
1900 Return a symbol giving the two-letter name of the Unicode general
1901 category assigned to @var{chr} or @code{#f} if no named category is
1902 assigned. The following table provides a list of category names along
1903 with their meanings.
1904
1905 @multitable @columnfractions .1 .4 .1 .4
1906 @item Lu
1907 @tab Uppercase letter
1908 @tab Pf
1909 @tab Final quote punctuation
1910 @item Ll
1911 @tab Lowercase letter
1912 @tab Po
1913 @tab Other punctuation
1914 @item Lt
1915 @tab Titlecase letter
1916 @tab Sm
1917 @tab Math symbol
1918 @item Lm
1919 @tab Modifier letter
1920 @tab Sc
1921 @tab Currency symbol
1922 @item Lo
1923 @tab Other letter
1924 @tab Sk
1925 @tab Modifier symbol
1926 @item Mn
1927 @tab Non-spacing mark
1928 @tab So
1929 @tab Other symbol
1930 @item Mc
1931 @tab Combining spacing mark
1932 @tab Zs
1933 @tab Space separator
1934 @item Me
1935 @tab Enclosing mark
1936 @tab Zl
1937 @tab Line separator
1938 @item Nd
1939 @tab Decimal digit number
1940 @tab Zp
1941 @tab Paragraph separator
1942 @item Nl
1943 @tab Letter number
1944 @tab Cc
1945 @tab Control
1946 @item No
1947 @tab Other number
1948 @tab Cf
1949 @tab Format
1950 @item Pc
1951 @tab Connector punctuation
1952 @tab Cs
1953 @tab Surrogate
1954 @item Pd
1955 @tab Dash punctuation
1956 @tab Co
1957 @tab Private use
1958 @item Ps
1959 @tab Open punctuation
1960 @tab Cn
1961 @tab Unassigned
1962 @item Pe
1963 @tab Close punctuation
1964 @tab
1965 @tab
1966 @item Pi
1967 @tab Initial quote punctuation
1968 @tab
1969 @tab
1970 @end multitable
1971 @end deffn
1972
1973 @rnindex char->integer
1974 @deffn {Scheme Procedure} char->integer chr
1975 @deffnx {C Function} scm_char_to_integer (chr)
1976 Return the code point of @var{chr}.
1977 @end deffn
1978
1979 @rnindex integer->char
1980 @deffn {Scheme Procedure} integer->char n
1981 @deffnx {C Function} scm_integer_to_char (n)
1982 Return the character that has code point @var{n}. The integer @var{n}
1983 must be a valid code point. Valid code points are in the ranges 0 to
1984 @code{#xD7FF} inclusive or @code{#xE000} to @code{#x10FFFF} inclusive.
1985 @end deffn
1986
1987 @rnindex char-upcase
1988 @deffn {Scheme Procedure} char-upcase chr
1989 @deffnx {C Function} scm_char_upcase (chr)
1990 Return the uppercase character version of @var{chr}.
1991 @end deffn
1992
1993 @rnindex char-downcase
1994 @deffn {Scheme Procedure} char-downcase chr
1995 @deffnx {C Function} scm_char_downcase (chr)
1996 Return the lowercase character version of @var{chr}.
1997 @end deffn
1998
1999 @rnindex char-titlecase
2000 @deffn {Scheme Procedure} char-titlecase chr
2001 @deffnx {C Function} scm_char_titlecase (chr)
2002 Return the titlecase character version of @var{chr} if one exists;
2003 otherwise return the uppercase version.
2004
2005 For most characters these will be the same, but the Unicode Standard
2006 includes certain digraph compatibility characters, such as @code{U+01F3}
2007 ``dz'', for which the uppercase and titlecase characters are different
2008 (@code{U+01F1} ``DZ'' and @code{U+01F2} ``Dz'' in this case,
2009 respectively).
2010 @end deffn
2011
2012 @tindex scm_t_wchar
2013 @deftypefn {C Function} scm_t_wchar scm_c_upcase (scm_t_wchar @var{c})
2014 @deftypefnx {C Function} scm_t_wchar scm_c_downcase (scm_t_wchar @var{c})
2015 @deftypefnx {C Function} scm_t_wchar scm_c_titlecase (scm_t_wchar @var{c})
2016
2017 These C functions take an integer representation of a Unicode
2018 codepoint and return the codepoint corresponding to its uppercase,
2019 lowercase, and titlecase forms respectively. The type
2020 @code{scm_t_wchar} is a signed, 32-bit integer.
2021 @end deftypefn
2022
2023 @node Character Sets
2024 @subsection Character Sets
2025
2026 The features described in this section correspond directly to SRFI-14.
2027
2028 The data type @dfn{charset} implements sets of characters
2029 (@pxref{Characters}). Because the internal representation of
2030 character sets is not visible to the user, a lot of procedures for
2031 handling them are provided.
2032
2033 Character sets can be created, extended, tested for the membership of a
2034 characters and be compared to other character sets.
2035
2036 @menu
2037 * Character Set Predicates/Comparison::
2038 * Iterating Over Character Sets:: Enumerate charset elements.
2039 * Creating Character Sets:: Making new charsets.
2040 * Querying Character Sets:: Test charsets for membership etc.
2041 * Character-Set Algebra:: Calculating new charsets.
2042 * Standard Character Sets:: Variables containing predefined charsets.
2043 @end menu
2044
2045 @node Character Set Predicates/Comparison
2046 @subsubsection Character Set Predicates/Comparison
2047
2048 Use these procedures for testing whether an object is a character set,
2049 or whether several character sets are equal or subsets of each other.
2050 @code{char-set-hash} can be used for calculating a hash value, maybe for
2051 usage in fast lookup procedures.
2052
2053 @deffn {Scheme Procedure} char-set? obj
2054 @deffnx {C Function} scm_char_set_p (obj)
2055 Return @code{#t} if @var{obj} is a character set, @code{#f}
2056 otherwise.
2057 @end deffn
2058
2059 @deffn {Scheme Procedure} char-set= . char_sets
2060 @deffnx {C Function} scm_char_set_eq (char_sets)
2061 Return @code{#t} if all given character sets are equal.
2062 @end deffn
2063
2064 @deffn {Scheme Procedure} char-set<= . char_sets
2065 @deffnx {C Function} scm_char_set_leq (char_sets)
2066 Return @code{#t} if every character set @var{cs}i is a subset
2067 of character set @var{cs}i+1.
2068 @end deffn
2069
2070 @deffn {Scheme Procedure} char-set-hash cs [bound]
2071 @deffnx {C Function} scm_char_set_hash (cs, bound)
2072 Compute a hash value for the character set @var{cs}. If
2073 @var{bound} is given and non-zero, it restricts the
2074 returned value to the range 0 @dots{} @var{bound - 1}.
2075 @end deffn
2076
2077 @c ===================================================================
2078
2079 @node Iterating Over Character Sets
2080 @subsubsection Iterating Over Character Sets
2081
2082 Character set cursors are a means for iterating over the members of a
2083 character sets. After creating a character set cursor with
2084 @code{char-set-cursor}, a cursor can be dereferenced with
2085 @code{char-set-ref}, advanced to the next member with
2086 @code{char-set-cursor-next}. Whether a cursor has passed past the last
2087 element of the set can be checked with @code{end-of-char-set?}.
2088
2089 Additionally, mapping and (un-)folding procedures for character sets are
2090 provided.
2091
2092 @deffn {Scheme Procedure} char-set-cursor cs
2093 @deffnx {C Function} scm_char_set_cursor (cs)
2094 Return a cursor into the character set @var{cs}.
2095 @end deffn
2096
2097 @deffn {Scheme Procedure} char-set-ref cs cursor
2098 @deffnx {C Function} scm_char_set_ref (cs, cursor)
2099 Return the character at the current cursor position
2100 @var{cursor} in the character set @var{cs}. It is an error to
2101 pass a cursor for which @code{end-of-char-set?} returns true.
2102 @end deffn
2103
2104 @deffn {Scheme Procedure} char-set-cursor-next cs cursor
2105 @deffnx {C Function} scm_char_set_cursor_next (cs, cursor)
2106 Advance the character set cursor @var{cursor} to the next
2107 character in the character set @var{cs}. It is an error if the
2108 cursor given satisfies @code{end-of-char-set?}.
2109 @end deffn
2110
2111 @deffn {Scheme Procedure} end-of-char-set? cursor
2112 @deffnx {C Function} scm_end_of_char_set_p (cursor)
2113 Return @code{#t} if @var{cursor} has reached the end of a
2114 character set, @code{#f} otherwise.
2115 @end deffn
2116
2117 @deffn {Scheme Procedure} char-set-fold kons knil cs
2118 @deffnx {C Function} scm_char_set_fold (kons, knil, cs)
2119 Fold the procedure @var{kons} over the character set @var{cs},
2120 initializing it with @var{knil}.
2121 @end deffn
2122
2123 @deffn {Scheme Procedure} char-set-unfold p f g seed [base_cs]
2124 @deffnx {C Function} scm_char_set_unfold (p, f, g, seed, base_cs)
2125 This is a fundamental constructor for character sets.
2126 @itemize @bullet
2127 @item @var{g} is used to generate a series of ``seed'' values
2128 from the initial seed: @var{seed}, (@var{g} @var{seed}),
2129 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
2130 @item @var{p} tells us when to stop -- when it returns true
2131 when applied to one of the seed values.
2132 @item @var{f} maps each seed value to a character. These
2133 characters are added to the base character set @var{base_cs} to
2134 form the result; @var{base_cs} defaults to the empty set.
2135 @end itemize
2136 @end deffn
2137
2138 @deffn {Scheme Procedure} char-set-unfold! p f g seed base_cs
2139 @deffnx {C Function} scm_char_set_unfold_x (p, f, g, seed, base_cs)
2140 This is a fundamental constructor for character sets.
2141 @itemize @bullet
2142 @item @var{g} is used to generate a series of ``seed'' values
2143 from the initial seed: @var{seed}, (@var{g} @var{seed}),
2144 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
2145 @item @var{p} tells us when to stop -- when it returns true
2146 when applied to one of the seed values.
2147 @item @var{f} maps each seed value to a character. These
2148 characters are added to the base character set @var{base_cs} to
2149 form the result; @var{base_cs} defaults to the empty set.
2150 @end itemize
2151 @end deffn
2152
2153 @deffn {Scheme Procedure} char-set-for-each proc cs
2154 @deffnx {C Function} scm_char_set_for_each (proc, cs)
2155 Apply @var{proc} to every character in the character set
2156 @var{cs}. The return value is not specified.
2157 @end deffn
2158
2159 @deffn {Scheme Procedure} char-set-map proc cs
2160 @deffnx {C Function} scm_char_set_map (proc, cs)
2161 Map the procedure @var{proc} over every character in @var{cs}.
2162 @var{proc} must be a character -> character procedure.
2163 @end deffn
2164
2165 @c ===================================================================
2166
2167 @node Creating Character Sets
2168 @subsubsection Creating Character Sets
2169
2170 New character sets are produced with these procedures.
2171
2172 @deffn {Scheme Procedure} char-set-copy cs
2173 @deffnx {C Function} scm_char_set_copy (cs)
2174 Return a newly allocated character set containing all
2175 characters in @var{cs}.
2176 @end deffn
2177
2178 @deffn {Scheme Procedure} char-set . rest
2179 @deffnx {C Function} scm_char_set (rest)
2180 Return a character set containing all given characters.
2181 @end deffn
2182
2183 @deffn {Scheme Procedure} list->char-set list [base_cs]
2184 @deffnx {C Function} scm_list_to_char_set (list, base_cs)
2185 Convert the character list @var{list} to a character set. If
2186 the character set @var{base_cs} is given, the character in this
2187 set are also included in the result.
2188 @end deffn
2189
2190 @deffn {Scheme Procedure} list->char-set! list base_cs
2191 @deffnx {C Function} scm_list_to_char_set_x (list, base_cs)
2192 Convert the character list @var{list} to a character set. The
2193 characters are added to @var{base_cs} and @var{base_cs} is
2194 returned.
2195 @end deffn
2196
2197 @deffn {Scheme Procedure} string->char-set str [base_cs]
2198 @deffnx {C Function} scm_string_to_char_set (str, base_cs)
2199 Convert the string @var{str} to a character set. If the
2200 character set @var{base_cs} is given, the characters in this
2201 set are also included in the result.
2202 @end deffn
2203
2204 @deffn {Scheme Procedure} string->char-set! str base_cs
2205 @deffnx {C Function} scm_string_to_char_set_x (str, base_cs)
2206 Convert the string @var{str} to a character set. The
2207 characters from the string are added to @var{base_cs}, and
2208 @var{base_cs} is returned.
2209 @end deffn
2210
2211 @deffn {Scheme Procedure} char-set-filter pred cs [base_cs]
2212 @deffnx {C Function} scm_char_set_filter (pred, cs, base_cs)
2213 Return a character set containing every character from @var{cs}
2214 so that it satisfies @var{pred}. If provided, the characters
2215 from @var{base_cs} are added to the result.
2216 @end deffn
2217
2218 @deffn {Scheme Procedure} char-set-filter! pred cs base_cs
2219 @deffnx {C Function} scm_char_set_filter_x (pred, cs, base_cs)
2220 Return a character set containing every character from @var{cs}
2221 so that it satisfies @var{pred}. The characters are added to
2222 @var{base_cs} and @var{base_cs} is returned.
2223 @end deffn
2224
2225 @deffn {Scheme Procedure} ucs-range->char-set lower upper [error [base_cs]]
2226 @deffnx {C Function} scm_ucs_range_to_char_set (lower, upper, error, base_cs)
2227 Return a character set containing all characters whose
2228 character codes lie in the half-open range
2229 [@var{lower},@var{upper}).
2230
2231 If @var{error} is a true value, an error is signalled if the
2232 specified range contains characters which are not contained in
2233 the implemented character range. If @var{error} is @code{#f},
2234 these characters are silently left out of the resulting
2235 character set.
2236
2237 The characters in @var{base_cs} are added to the result, if
2238 given.
2239 @end deffn
2240
2241 @deffn {Scheme Procedure} ucs-range->char-set! lower upper error base_cs
2242 @deffnx {C Function} scm_ucs_range_to_char_set_x (lower, upper, error, base_cs)
2243 Return a character set containing all characters whose
2244 character codes lie in the half-open range
2245 [@var{lower},@var{upper}).
2246
2247 If @var{error} is a true value, an error is signalled if the
2248 specified range contains characters which are not contained in
2249 the implemented character range. If @var{error} is @code{#f},
2250 these characters are silently left out of the resulting
2251 character set.
2252
2253 The characters are added to @var{base_cs} and @var{base_cs} is
2254 returned.
2255 @end deffn
2256
2257 @deffn {Scheme Procedure} ->char-set x
2258 @deffnx {C Function} scm_to_char_set (x)
2259 Coerces x into a char-set. @var{x} may be a string, character or
2260 char-set. A string is converted to the set of its constituent
2261 characters; a character is converted to a singleton set; a char-set is
2262 returned as-is.
2263 @end deffn
2264
2265 @c ===================================================================
2266
2267 @node Querying Character Sets
2268 @subsubsection Querying Character Sets
2269
2270 Access the elements and other information of a character set with these
2271 procedures.
2272
2273 @deffn {Scheme Procedure} %char-set-dump cs
2274 Returns an association list containing debugging information
2275 for @var{cs}. The association list has the following entries.
2276 @table @code
2277 @item char-set
2278 The char-set itself
2279 @item len
2280 The number of groups of contiguous code points the char-set
2281 contains
2282 @item ranges
2283 A list of lists where each sublist is a range of code points
2284 and their associated characters
2285 @end table
2286 The return value of this function cannot be relied upon to be
2287 consistent between versions of Guile and should not be used in code.
2288 @end deffn
2289
2290 @deffn {Scheme Procedure} char-set-size cs
2291 @deffnx {C Function} scm_char_set_size (cs)
2292 Return the number of elements in character set @var{cs}.
2293 @end deffn
2294
2295 @deffn {Scheme Procedure} char-set-count pred cs
2296 @deffnx {C Function} scm_char_set_count (pred, cs)
2297 Return the number of the elements int the character set
2298 @var{cs} which satisfy the predicate @var{pred}.
2299 @end deffn
2300
2301 @deffn {Scheme Procedure} char-set->list cs
2302 @deffnx {C Function} scm_char_set_to_list (cs)
2303 Return a list containing the elements of the character set
2304 @var{cs}.
2305 @end deffn
2306
2307 @deffn {Scheme Procedure} char-set->string cs
2308 @deffnx {C Function} scm_char_set_to_string (cs)
2309 Return a string containing the elements of the character set
2310 @var{cs}. The order in which the characters are placed in the
2311 string is not defined.
2312 @end deffn
2313
2314 @deffn {Scheme Procedure} char-set-contains? cs ch
2315 @deffnx {C Function} scm_char_set_contains_p (cs, ch)
2316 Return @code{#t} iff the character @var{ch} is contained in the
2317 character set @var{cs}.
2318 @end deffn
2319
2320 @deffn {Scheme Procedure} char-set-every pred cs
2321 @deffnx {C Function} scm_char_set_every (pred, cs)
2322 Return a true value if every character in the character set
2323 @var{cs} satisfies the predicate @var{pred}.
2324 @end deffn
2325
2326 @deffn {Scheme Procedure} char-set-any pred cs
2327 @deffnx {C Function} scm_char_set_any (pred, cs)
2328 Return a true value if any character in the character set
2329 @var{cs} satisfies the predicate @var{pred}.
2330 @end deffn
2331
2332 @c ===================================================================
2333
2334 @node Character-Set Algebra
2335 @subsubsection Character-Set Algebra
2336
2337 Character sets can be manipulated with the common set algebra operation,
2338 such as union, complement, intersection etc. All of these procedures
2339 provide side-effecting variants, which modify their character set
2340 argument(s).
2341
2342 @deffn {Scheme Procedure} char-set-adjoin cs . rest
2343 @deffnx {C Function} scm_char_set_adjoin (cs, rest)
2344 Add all character arguments to the first argument, which must
2345 be a character set.
2346 @end deffn
2347
2348 @deffn {Scheme Procedure} char-set-delete cs . rest
2349 @deffnx {C Function} scm_char_set_delete (cs, rest)
2350 Delete all character arguments from the first argument, which
2351 must be a character set.
2352 @end deffn
2353
2354 @deffn {Scheme Procedure} char-set-adjoin! cs . rest
2355 @deffnx {C Function} scm_char_set_adjoin_x (cs, rest)
2356 Add all character arguments to the first argument, which must
2357 be a character set.
2358 @end deffn
2359
2360 @deffn {Scheme Procedure} char-set-delete! cs . rest
2361 @deffnx {C Function} scm_char_set_delete_x (cs, rest)
2362 Delete all character arguments from the first argument, which
2363 must be a character set.
2364 @end deffn
2365
2366 @deffn {Scheme Procedure} char-set-complement cs
2367 @deffnx {C Function} scm_char_set_complement (cs)
2368 Return the complement of the character set @var{cs}.
2369 @end deffn
2370
2371 Note that the complement of a character set is likely to contain many
2372 reserved code points (code points that are not associated with
2373 characters). It may be helpful to modify the output of
2374 @code{char-set-complement} by computing its intersection with the set
2375 of designated code points, @code{char-set:designated}.
2376
2377 @deffn {Scheme Procedure} char-set-union . rest
2378 @deffnx {C Function} scm_char_set_union (rest)
2379 Return the union of all argument character sets.
2380 @end deffn
2381
2382 @deffn {Scheme Procedure} char-set-intersection . rest
2383 @deffnx {C Function} scm_char_set_intersection (rest)
2384 Return the intersection of all argument character sets.
2385 @end deffn
2386
2387 @deffn {Scheme Procedure} char-set-difference cs1 . rest
2388 @deffnx {C Function} scm_char_set_difference (cs1, rest)
2389 Return the difference of all argument character sets.
2390 @end deffn
2391
2392 @deffn {Scheme Procedure} char-set-xor . rest
2393 @deffnx {C Function} scm_char_set_xor (rest)
2394 Return the exclusive-or of all argument character sets.
2395 @end deffn
2396
2397 @deffn {Scheme Procedure} char-set-diff+intersection cs1 . rest
2398 @deffnx {C Function} scm_char_set_diff_plus_intersection (cs1, rest)
2399 Return the difference and the intersection of all argument
2400 character sets.
2401 @end deffn
2402
2403 @deffn {Scheme Procedure} char-set-complement! cs
2404 @deffnx {C Function} scm_char_set_complement_x (cs)
2405 Return the complement of the character set @var{cs}.
2406 @end deffn
2407
2408 @deffn {Scheme Procedure} char-set-union! cs1 . rest
2409 @deffnx {C Function} scm_char_set_union_x (cs1, rest)
2410 Return the union of all argument character sets.
2411 @end deffn
2412
2413 @deffn {Scheme Procedure} char-set-intersection! cs1 . rest
2414 @deffnx {C Function} scm_char_set_intersection_x (cs1, rest)
2415 Return the intersection of all argument character sets.
2416 @end deffn
2417
2418 @deffn {Scheme Procedure} char-set-difference! cs1 . rest
2419 @deffnx {C Function} scm_char_set_difference_x (cs1, rest)
2420 Return the difference of all argument character sets.
2421 @end deffn
2422
2423 @deffn {Scheme Procedure} char-set-xor! cs1 . rest
2424 @deffnx {C Function} scm_char_set_xor_x (cs1, rest)
2425 Return the exclusive-or of all argument character sets.
2426 @end deffn
2427
2428 @deffn {Scheme Procedure} char-set-diff+intersection! cs1 cs2 . rest
2429 @deffnx {C Function} scm_char_set_diff_plus_intersection_x (cs1, cs2, rest)
2430 Return the difference and the intersection of all argument
2431 character sets.
2432 @end deffn
2433
2434 @c ===================================================================
2435
2436 @node Standard Character Sets
2437 @subsubsection Standard Character Sets
2438
2439 In order to make the use of the character set data type and procedures
2440 useful, several predefined character set variables exist.
2441
2442 @cindex codeset
2443 @cindex charset
2444 @cindex locale
2445
2446 These character sets are locale independent and are not recomputed
2447 upon a @code{setlocale} call. They contain characters from the whole
2448 range of Unicode code points. For instance, @code{char-set:letter}
2449 contains about 94,000 characters.
2450
2451 @defvr {Scheme Variable} char-set:lower-case
2452 @defvrx {C Variable} scm_char_set_lower_case
2453 All lower-case characters.
2454 @end defvr
2455
2456 @defvr {Scheme Variable} char-set:upper-case
2457 @defvrx {C Variable} scm_char_set_upper_case
2458 All upper-case characters.
2459 @end defvr
2460
2461 @defvr {Scheme Variable} char-set:title-case
2462 @defvrx {C Variable} scm_char_set_title_case
2463 All single characters that function as if they were an upper-case
2464 letter followed by a lower-case letter.
2465 @end defvr
2466
2467 @defvr {Scheme Variable} char-set:letter
2468 @defvrx {C Variable} scm_char_set_letter
2469 All letters. This includes @code{char-set:lower-case},
2470 @code{char-set:upper-case}, @code{char-set:title-case}, and many
2471 letters that have no case at all. For example, Chinese and Japanese
2472 characters typically have no concept of case.
2473 @end defvr
2474
2475 @defvr {Scheme Variable} char-set:digit
2476 @defvrx {C Variable} scm_char_set_digit
2477 All digits.
2478 @end defvr
2479
2480 @defvr {Scheme Variable} char-set:letter+digit
2481 @defvrx {C Variable} scm_char_set_letter_and_digit
2482 The union of @code{char-set:letter} and @code{char-set:digit}.
2483 @end defvr
2484
2485 @defvr {Scheme Variable} char-set:graphic
2486 @defvrx {C Variable} scm_char_set_graphic
2487 All characters which would put ink on the paper.
2488 @end defvr
2489
2490 @defvr {Scheme Variable} char-set:printing
2491 @defvrx {C Variable} scm_char_set_printing
2492 The union of @code{char-set:graphic} and @code{char-set:whitespace}.
2493 @end defvr
2494
2495 @defvr {Scheme Variable} char-set:whitespace
2496 @defvrx {C Variable} scm_char_set_whitespace
2497 All whitespace characters.
2498 @end defvr
2499
2500 @defvr {Scheme Variable} char-set:blank
2501 @defvrx {C Variable} scm_char_set_blank
2502 All horizontal whitespace characters, which notably includes
2503 @code{#\space} and @code{#\tab}.
2504 @end defvr
2505
2506 @defvr {Scheme Variable} char-set:iso-control
2507 @defvrx {C Variable} scm_char_set_iso_control
2508 The ISO control characters are the C0 control characters (U+0000 to
2509 U+001F), delete (U+007F), and the C1 control characters (U+0080 to
2510 U+009F).
2511 @end defvr
2512
2513 @defvr {Scheme Variable} char-set:punctuation
2514 @defvrx {C Variable} scm_char_set_punctuation
2515 All punctuation characters, such as the characters
2516 @code{!"#%&'()*,-./:;?@@[\\]_@{@}}
2517 @end defvr
2518
2519 @defvr {Scheme Variable} char-set:symbol
2520 @defvrx {C Variable} scm_char_set_symbol
2521 All symbol characters, such as the characters @code{$+<=>^`|~}.
2522 @end defvr
2523
2524 @defvr {Scheme Variable} char-set:hex-digit
2525 @defvrx {C Variable} scm_char_set_hex_digit
2526 The hexadecimal digits @code{0123456789abcdefABCDEF}.
2527 @end defvr
2528
2529 @defvr {Scheme Variable} char-set:ascii
2530 @defvrx {C Variable} scm_char_set_ascii
2531 All ASCII characters.
2532 @end defvr
2533
2534 @defvr {Scheme Variable} char-set:empty
2535 @defvrx {C Variable} scm_char_set_empty
2536 The empty character set.
2537 @end defvr
2538
2539 @defvr {Scheme Variable} char-set:designated
2540 @defvrx {C Variable} scm_char_set_designated
2541 This character set contains all designated code points. This includes
2542 all the code points to which Unicode has assigned a character or other
2543 meaning.
2544 @end defvr
2545
2546 @defvr {Scheme Variable} char-set:full
2547 @defvrx {C Variable} scm_char_set_full
2548 This character set contains all possible code points. This includes
2549 both designated and reserved code points.
2550 @end defvr
2551
2552 @node Strings
2553 @subsection Strings
2554 @tpindex Strings
2555
2556 Strings are fixed-length sequences of characters. They can be created
2557 by calling constructor procedures, but they can also literally get
2558 entered at the @acronym{REPL} or in Scheme source files.
2559
2560 @c Guile provides a rich set of string processing procedures, because text
2561 @c handling is very important when Guile is used as a scripting language.
2562
2563 Strings always carry the information about how many characters they are
2564 composed of with them, so there is no special end-of-string character,
2565 like in C. That means that Scheme strings can contain any character,
2566 even the @samp{#\nul} character @samp{\0}.
2567
2568 To use strings efficiently, you need to know a bit about how Guile
2569 implements them. In Guile, a string consists of two parts, a head and
2570 the actual memory where the characters are stored. When a string (or
2571 a substring of it) is copied, only a new head gets created, the memory
2572 is usually not copied. The two heads start out pointing to the same
2573 memory.
2574
2575 When one of these two strings is modified, as with @code{string-set!},
2576 their common memory does get copied so that each string has its own
2577 memory and modifying one does not accidentally modify the other as well.
2578 Thus, Guile's strings are `copy on write'; the actual copying of their
2579 memory is delayed until one string is written to.
2580
2581 This implementation makes functions like @code{substring} very
2582 efficient in the common case that no modifications are done to the
2583 involved strings.
2584
2585 If you do know that your strings are getting modified right away, you
2586 can use @code{substring/copy} instead of @code{substring}. This
2587 function performs the copy immediately at the time of creation. This
2588 is more efficient, especially in a multi-threaded program. Also,
2589 @code{substring/copy} can avoid the problem that a short substring
2590 holds on to the memory of a very large original string that could
2591 otherwise be recycled.
2592
2593 If you want to avoid the copy altogether, so that modifications of one
2594 string show up in the other, you can use @code{substring/shared}. The
2595 strings created by this procedure are called @dfn{mutation sharing
2596 substrings} since the substring and the original string share
2597 modifications to each other.
2598
2599 If you want to prevent modifications, use @code{substring/read-only}.
2600
2601 Guile provides all procedures of SRFI-13 and a few more.
2602
2603 @menu
2604 * String Syntax:: Read syntax for strings.
2605 * String Predicates:: Testing strings for certain properties.
2606 * String Constructors:: Creating new string objects.
2607 * List/String Conversion:: Converting from/to lists of characters.
2608 * String Selection:: Select portions from strings.
2609 * String Modification:: Modify parts or whole strings.
2610 * String Comparison:: Lexicographic ordering predicates.
2611 * String Searching:: Searching in strings.
2612 * Alphabetic Case Mapping:: Convert the alphabetic case of strings.
2613 * Reversing and Appending Strings:: Appending strings to form a new string.
2614 * Mapping Folding and Unfolding:: Iterating over strings.
2615 * Miscellaneous String Operations:: Replicating, insertion, parsing, ...
2616 * Conversion to/from C::
2617 * String Internals:: The storage strategy for strings.
2618 @end menu
2619
2620 @node String Syntax
2621 @subsubsection String Read Syntax
2622
2623 @c In the following @code is used to get a good font in TeX etc, but
2624 @c is omitted for Info format, so as not to risk any confusion over
2625 @c whether surrounding ` ' quotes are part of the escape or are
2626 @c special in a string (they're not).
2627
2628 The read syntax for strings is an arbitrarily long sequence of
2629 characters enclosed in double quotes (@nicode{"}).
2630
2631 Backslash is an escape character and can be used to insert the following
2632 special characters. @nicode{\"} and @nicode{\\} are R5RS standard, the
2633 next seven are R6RS standard --- notice they follow C syntax --- and the
2634 remaining four are Guile extensions.
2635
2636 @table @asis
2637 @item @nicode{\\}
2638 Backslash character.
2639
2640 @item @nicode{\"}
2641 Double quote character (an unescaped @nicode{"} is otherwise the end
2642 of the string).
2643
2644 @item @nicode{\a}
2645 Bell character (ASCII 7).
2646
2647 @item @nicode{\f}
2648 Formfeed character (ASCII 12).
2649
2650 @item @nicode{\n}
2651 Newline character (ASCII 10).
2652
2653 @item @nicode{\r}
2654 Carriage return character (ASCII 13).
2655
2656 @item @nicode{\t}
2657 Tab character (ASCII 9).
2658
2659 @item @nicode{\v}
2660 Vertical tab character (ASCII 11).
2661
2662 @item @nicode{\b}
2663 Backspace character (ASCII 8).
2664
2665 @item @nicode{\0}
2666 NUL character (ASCII 0).
2667
2668 @item @nicode{\xHH}
2669 Character code given by two hexadecimal digits. For example
2670 @nicode{\x7f} for an ASCII DEL (127).
2671
2672 @item @nicode{\uHHHH}
2673 Character code given by four hexadecimal digits. For example
2674 @nicode{\u0100} for a capital A with macron (U+0100).
2675
2676 @item @nicode{\UHHHHHH}
2677 Character code given by six hexadecimal digits. For example
2678 @nicode{\U010402}.
2679 @end table
2680
2681 @noindent
2682 The following are examples of string literals:
2683
2684 @lisp
2685 "foo"
2686 "bar plonk"
2687 "Hello World"
2688 "\"Hi\", he said."
2689 @end lisp
2690
2691 The three escape sequences @code{\xHH}, @code{\uHHHH} and @code{\UHHHHHH} were
2692 chosen to not break compatibility with code written for previous versions of
2693 Guile. The R6RS specification suggests a different, incompatible syntax for hex
2694 escapes: @code{\xHHHH;} -- a character code followed by one to eight hexadecimal
2695 digits terminated with a semicolon. If this escape format is desired instead,
2696 it can be enabled with the reader option @code{r6rs-hex-escapes}.
2697
2698 @lisp
2699 (read-enable 'r6rs-hex-escapes)
2700 @end lisp
2701
2702 Enabling this option will also change the hex escape format for characters.
2703 More on character escapes can be found at (@pxref{Characters}). More on
2704 reader options in general can be found at (@pxref{Reader options}).
2705
2706 @node String Predicates
2707 @subsubsection String Predicates
2708
2709 The following procedures can be used to check whether a given string
2710 fulfills some specified property.
2711
2712 @rnindex string?
2713 @deffn {Scheme Procedure} string? obj
2714 @deffnx {C Function} scm_string_p (obj)
2715 Return @code{#t} if @var{obj} is a string, else @code{#f}.
2716 @end deffn
2717
2718 @deftypefn {C Function} int scm_is_string (SCM obj)
2719 Returns @code{1} if @var{obj} is a string, @code{0} otherwise.
2720 @end deftypefn
2721
2722 @deffn {Scheme Procedure} string-null? str
2723 @deffnx {C Function} scm_string_null_p (str)
2724 Return @code{#t} if @var{str}'s length is zero, and
2725 @code{#f} otherwise.
2726 @lisp
2727 (string-null? "") @result{} #t
2728 y @result{} "foo"
2729 (string-null? y) @result{} #f
2730 @end lisp
2731 @end deffn
2732
2733 @deffn {Scheme Procedure} string-any char_pred s [start [end]]
2734 @deffnx {C Function} scm_string_any (char_pred, s, start, end)
2735 Check if @var{char_pred} is true for any character in string @var{s}.
2736
2737 @var{char_pred} can be a character to check for any equal to that, or
2738 a character set (@pxref{Character Sets}) to check for any in that set,
2739 or a predicate procedure to call.
2740
2741 For a procedure, calls @code{(@var{char_pred} c)} are made
2742 successively on the characters from @var{start} to @var{end}. If
2743 @var{char_pred} returns true (ie.@: non-@code{#f}), @code{string-any}
2744 stops and that return value is the return from @code{string-any}. The
2745 call on the last character (ie.@: at @math{@var{end}-1}), if that
2746 point is reached, is a tail call.
2747
2748 If there are no characters in @var{s} (ie.@: @var{start} equals
2749 @var{end}) then the return is @code{#f}.
2750 @end deffn
2751
2752 @deffn {Scheme Procedure} string-every char_pred s [start [end]]
2753 @deffnx {C Function} scm_string_every (char_pred, s, start, end)
2754 Check if @var{char_pred} is true for every character in string
2755 @var{s}.
2756
2757 @var{char_pred} can be a character to check for every character equal
2758 to that, or a character set (@pxref{Character Sets}) to check for
2759 every character being in that set, or a predicate procedure to call.
2760
2761 For a procedure, calls @code{(@var{char_pred} c)} are made
2762 successively on the characters from @var{start} to @var{end}. If
2763 @var{char_pred} returns @code{#f}, @code{string-every} stops and
2764 returns @code{#f}. The call on the last character (ie.@: at
2765 @math{@var{end}-1}), if that point is reached, is a tail call and the
2766 return from that call is the return from @code{string-every}.
2767
2768 If there are no characters in @var{s} (ie.@: @var{start} equals
2769 @var{end}) then the return is @code{#t}.
2770 @end deffn
2771
2772 @node String Constructors
2773 @subsubsection String Constructors
2774
2775 The string constructor procedures create new string objects, possibly
2776 initializing them with some specified character data. See also
2777 @xref{String Selection}, for ways to create strings from existing
2778 strings.
2779
2780 @c FIXME::martin: list->string belongs into `List/String Conversion'
2781
2782 @deffn {Scheme Procedure} string char@dots{}
2783 @rnindex string
2784 Return a newly allocated string made from the given character
2785 arguments.
2786
2787 @example
2788 (string #\x #\y #\z) @result{} "xyz"
2789 (string) @result{} ""
2790 @end example
2791 @end deffn
2792
2793 @deffn {Scheme Procedure} list->string lst
2794 @deffnx {C Function} scm_string (lst)
2795 @rnindex list->string
2796 Return a newly allocated string made from a list of characters.
2797
2798 @example
2799 (list->string '(#\a #\b #\c)) @result{} "abc"
2800 @end example
2801 @end deffn
2802
2803 @deffn {Scheme Procedure} reverse-list->string lst
2804 @deffnx {C Function} scm_reverse_list_to_string (lst)
2805 Return a newly allocated string made from a list of characters, in
2806 reverse order.
2807
2808 @example
2809 (reverse-list->string '(#\a #\B #\c)) @result{} "cBa"
2810 @end example
2811 @end deffn
2812
2813 @rnindex make-string
2814 @deffn {Scheme Procedure} make-string k [chr]
2815 @deffnx {C Function} scm_make_string (k, chr)
2816 Return a newly allocated string of
2817 length @var{k}. If @var{chr} is given, then all elements of
2818 the string are initialized to @var{chr}, otherwise the contents
2819 of the @var{string} are unspecified.
2820 @end deffn
2821
2822 @deftypefn {C Function} SCM scm_c_make_string (size_t len, SCM chr)
2823 Like @code{scm_make_string}, but expects the length as a
2824 @code{size_t}.
2825 @end deftypefn
2826
2827 @deffn {Scheme Procedure} string-tabulate proc len
2828 @deffnx {C Function} scm_string_tabulate (proc, len)
2829 @var{proc} is an integer->char procedure. Construct a string
2830 of size @var{len} by applying @var{proc} to each index to
2831 produce the corresponding string element. The order in which
2832 @var{proc} is applied to the indices is not specified.
2833 @end deffn
2834
2835 @deffn {Scheme Procedure} string-join ls [delimiter [grammar]]
2836 @deffnx {C Function} scm_string_join (ls, delimiter, grammar)
2837 Append the string in the string list @var{ls}, using the string
2838 @var{delim} as a delimiter between the elements of @var{ls}.
2839 @var{grammar} is a symbol which specifies how the delimiter is
2840 placed between the strings, and defaults to the symbol
2841 @code{infix}.
2842
2843 @table @code
2844 @item infix
2845 Insert the separator between list elements. An empty string
2846 will produce an empty list.
2847 @item string-infix
2848 Like @code{infix}, but will raise an error if given the empty
2849 list.
2850 @item suffix
2851 Insert the separator after every list element.
2852 @item prefix
2853 Insert the separator before each list element.
2854 @end table
2855 @end deffn
2856
2857 @node List/String Conversion
2858 @subsubsection List/String conversion
2859
2860 When processing strings, it is often convenient to first convert them
2861 into a list representation by using the procedure @code{string->list},
2862 work with the resulting list, and then convert it back into a string.
2863 These procedures are useful for similar tasks.
2864
2865 @rnindex string->list
2866 @deffn {Scheme Procedure} string->list str [start [end]]
2867 @deffnx {C Function} scm_substring_to_list (str, start, end)
2868 @deffnx {C Function} scm_string_to_list (str)
2869 Convert the string @var{str} into a list of characters.
2870 @end deffn
2871
2872 @deffn {Scheme Procedure} string-split str chr
2873 @deffnx {C Function} scm_string_split (str, chr)
2874 Split the string @var{str} into the a list of the substrings delimited
2875 by appearances of the character @var{chr}. Note that an empty substring
2876 between separator characters will result in an empty string in the
2877 result list.
2878
2879 @lisp
2880 (string-split "root:x:0:0:root:/root:/bin/bash" #\:)
2881 @result{}
2882 ("root" "x" "0" "0" "root" "/root" "/bin/bash")
2883
2884 (string-split "::" #\:)
2885 @result{}
2886 ("" "" "")
2887
2888 (string-split "" #\:)
2889 @result{}
2890 ("")
2891 @end lisp
2892 @end deffn
2893
2894
2895 @node String Selection
2896 @subsubsection String Selection
2897
2898 Portions of strings can be extracted by these procedures.
2899 @code{string-ref} delivers individual characters whereas
2900 @code{substring} can be used to extract substrings from longer strings.
2901
2902 @rnindex string-length
2903 @deffn {Scheme Procedure} string-length string
2904 @deffnx {C Function} scm_string_length (string)
2905 Return the number of characters in @var{string}.
2906 @end deffn
2907
2908 @deftypefn {C Function} size_t scm_c_string_length (SCM str)
2909 Return the number of characters in @var{str} as a @code{size_t}.
2910 @end deftypefn
2911
2912 @rnindex string-ref
2913 @deffn {Scheme Procedure} string-ref str k
2914 @deffnx {C Function} scm_string_ref (str, k)
2915 Return character @var{k} of @var{str} using zero-origin
2916 indexing. @var{k} must be a valid index of @var{str}.
2917 @end deffn
2918
2919 @deftypefn {C Function} SCM scm_c_string_ref (SCM str, size_t k)
2920 Return character @var{k} of @var{str} using zero-origin
2921 indexing. @var{k} must be a valid index of @var{str}.
2922 @end deftypefn
2923
2924 @rnindex string-copy
2925 @deffn {Scheme Procedure} string-copy str [start [end]]
2926 @deffnx {C Function} scm_substring_copy (str, start, end)
2927 @deffnx {C Function} scm_string_copy (str)
2928 Return a copy of the given string @var{str}.
2929
2930 The returned string shares storage with @var{str} initially, but it is
2931 copied as soon as one of the two strings is modified.
2932 @end deffn
2933
2934 @rnindex substring
2935 @deffn {Scheme Procedure} substring str start [end]
2936 @deffnx {C Function} scm_substring (str, start, end)
2937 Return a new string formed from the characters
2938 of @var{str} beginning with index @var{start} (inclusive) and
2939 ending with index @var{end} (exclusive).
2940 @var{str} must be a string, @var{start} and @var{end} must be
2941 exact integers satisfying:
2942
2943 0 <= @var{start} <= @var{end} <= @code{(string-length @var{str})}.
2944
2945 The returned string shares storage with @var{str} initially, but it is
2946 copied as soon as one of the two strings is modified.
2947 @end deffn
2948
2949 @deffn {Scheme Procedure} substring/shared str start [end]
2950 @deffnx {C Function} scm_substring_shared (str, start, end)
2951 Like @code{substring}, but the strings continue to share their storage
2952 even if they are modified. Thus, modifications to @var{str} show up
2953 in the new string, and vice versa.
2954 @end deffn
2955
2956 @deffn {Scheme Procedure} substring/copy str start [end]
2957 @deffnx {C Function} scm_substring_copy (str, start, end)
2958 Like @code{substring}, but the storage for the new string is copied
2959 immediately.
2960 @end deffn
2961
2962 @deffn {Scheme Procedure} substring/read-only str start [end]
2963 @deffnx {C Function} scm_substring_read_only (str, start, end)
2964 Like @code{substring}, but the resulting string can not be modified.
2965 @end deffn
2966
2967 @deftypefn {C Function} SCM scm_c_substring (SCM str, size_t start, size_t end)
2968 @deftypefnx {C Function} SCM scm_c_substring_shared (SCM str, size_t start, size_t end)
2969 @deftypefnx {C Function} SCM scm_c_substring_copy (SCM str, size_t start, size_t end)
2970 @deftypefnx {C Function} SCM scm_c_substring_read_only (SCM str, size_t start, size_t end)
2971 Like @code{scm_substring}, etc. but the bounds are given as a @code{size_t}.
2972 @end deftypefn
2973
2974 @deffn {Scheme Procedure} string-take s n
2975 @deffnx {C Function} scm_string_take (s, n)
2976 Return the @var{n} first characters of @var{s}.
2977 @end deffn
2978
2979 @deffn {Scheme Procedure} string-drop s n
2980 @deffnx {C Function} scm_string_drop (s, n)
2981 Return all but the first @var{n} characters of @var{s}.
2982 @end deffn
2983
2984 @deffn {Scheme Procedure} string-take-right s n
2985 @deffnx {C Function} scm_string_take_right (s, n)
2986 Return the @var{n} last characters of @var{s}.
2987 @end deffn
2988
2989 @deffn {Scheme Procedure} string-drop-right s n
2990 @deffnx {C Function} scm_string_drop_right (s, n)
2991 Return all but the last @var{n} characters of @var{s}.
2992 @end deffn
2993
2994 @deffn {Scheme Procedure} string-pad s len [chr [start [end]]]
2995 @deffnx {Scheme Procedure} string-pad-right s len [chr [start [end]]]
2996 @deffnx {C Function} scm_string_pad (s, len, chr, start, end)
2997 @deffnx {C Function} scm_string_pad_right (s, len, chr, start, end)
2998 Take characters @var{start} to @var{end} from the string @var{s} and
2999 either pad with @var{char} or truncate them to give @var{len}
3000 characters.
3001
3002 @code{string-pad} pads or truncates on the left, so for example
3003
3004 @example
3005 (string-pad "x" 3) @result{} " x"
3006 (string-pad "abcde" 3) @result{} "cde"
3007 @end example
3008
3009 @code{string-pad-right} pads or truncates on the right, so for example
3010
3011 @example
3012 (string-pad-right "x" 3) @result{} "x "
3013 (string-pad-right "abcde" 3) @result{} "abc"
3014 @end example
3015 @end deffn
3016
3017 @deffn {Scheme Procedure} string-trim s [char_pred [start [end]]]
3018 @deffnx {Scheme Procedure} string-trim-right s [char_pred [start [end]]]
3019 @deffnx {Scheme Procedure} string-trim-both s [char_pred [start [end]]]
3020 @deffnx {C Function} scm_string_trim (s, char_pred, start, end)
3021 @deffnx {C Function} scm_string_trim_right (s, char_pred, start, end)
3022 @deffnx {C Function} scm_string_trim_both (s, char_pred, start, end)
3023 Trim occurrences of @var{char_pred} from the ends of @var{s}.
3024
3025 @code{string-trim} trims @var{char_pred} characters from the left
3026 (start) of the string, @code{string-trim-right} trims them from the
3027 right (end) of the string, @code{string-trim-both} trims from both
3028 ends.
3029
3030 @var{char_pred} can be a character, a character set, or a predicate
3031 procedure to call on each character. If @var{char_pred} is not given
3032 the default is whitespace as per @code{char-set:whitespace}
3033 (@pxref{Standard Character Sets}).
3034
3035 @example
3036 (string-trim " x ") @result{} "x "
3037 (string-trim-right "banana" #\a) @result{} "banan"
3038 (string-trim-both ".,xy:;" char-set:punctuation)
3039 @result{} "xy"
3040 (string-trim-both "xyzzy" (lambda (c)
3041 (or (eqv? c #\x)
3042 (eqv? c #\y))))
3043 @result{} "zz"
3044 @end example
3045 @end deffn
3046
3047 @node String Modification
3048 @subsubsection String Modification
3049
3050 These procedures are for modifying strings in-place. This means that the
3051 result of the operation is not a new string; instead, the original string's
3052 memory representation is modified.
3053
3054 @rnindex string-set!
3055 @deffn {Scheme Procedure} string-set! str k chr
3056 @deffnx {C Function} scm_string_set_x (str, k, chr)
3057 Store @var{chr} in element @var{k} of @var{str} and return
3058 an unspecified value. @var{k} must be a valid index of
3059 @var{str}.
3060 @end deffn
3061
3062 @deftypefn {C Function} void scm_c_string_set_x (SCM str, size_t k, SCM chr)
3063 Like @code{scm_string_set_x}, but the index is given as a @code{size_t}.
3064 @end deftypefn
3065
3066 @rnindex string-fill!
3067 @deffn {Scheme Procedure} string-fill! str chr [start [end]]
3068 @deffnx {C Function} scm_substring_fill_x (str, chr, start, end)
3069 @deffnx {C Function} scm_string_fill_x (str, chr)
3070 Stores @var{chr} in every element of the given @var{str} and
3071 returns an unspecified value.
3072 @end deffn
3073
3074 @deffn {Scheme Procedure} substring-fill! str start end fill
3075 @deffnx {C Function} scm_substring_fill_x (str, start, end, fill)
3076 Change every character in @var{str} between @var{start} and
3077 @var{end} to @var{fill}.
3078
3079 @lisp
3080 (define y "abcdefg")
3081 (substring-fill! y 1 3 #\r)
3082 y
3083 @result{} "arrdefg"
3084 @end lisp
3085 @end deffn
3086
3087 @deffn {Scheme Procedure} substring-move! str1 start1 end1 str2 start2
3088 @deffnx {C Function} scm_substring_move_x (str1, start1, end1, str2, start2)
3089 Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}
3090 into @var{str2} beginning at position @var{start2}.
3091 @var{str1} and @var{str2} can be the same string.
3092 @end deffn
3093
3094 @deffn {Scheme Procedure} string-copy! target tstart s [start [end]]
3095 @deffnx {C Function} scm_string_copy_x (target, tstart, s, start, end)
3096 Copy the sequence of characters from index range [@var{start},
3097 @var{end}) in string @var{s} to string @var{target}, beginning
3098 at index @var{tstart}. The characters are copied left-to-right
3099 or right-to-left as needed -- the copy is guaranteed to work,
3100 even if @var{target} and @var{s} are the same string. It is an
3101 error if the copy operation runs off the end of the target
3102 string.
3103 @end deffn
3104
3105
3106 @node String Comparison
3107 @subsubsection String Comparison
3108
3109 The procedures in this section are similar to the character ordering
3110 predicates (@pxref{Characters}), but are defined on character sequences.
3111
3112 The first set is specified in R5RS and has names that end in @code{?}.
3113 The second set is specified in SRFI-13 and the names have not ending
3114 @code{?}.
3115
3116 The predicates ending in @code{-ci} ignore the character case
3117 when comparing strings. For now, case-insensitive comparison is done
3118 using the R5RS rules, where every lower-case character that has a
3119 single character upper-case form is converted to uppercase before
3120 comparison. See @xref{Text Collation, the @code{(ice-9
3121 i18n)} module}, for locale-dependent string comparison.
3122
3123 @rnindex string=?
3124 @deffn {Scheme Procedure} string=? [s1 [s2 . rest]]
3125 @deffnx {C Function} scm_i_string_equal_p (s1, s2, rest)
3126 Lexicographic equality predicate; return @code{#t} if the two
3127 strings are the same length and contain the same characters in
3128 the same positions, otherwise return @code{#f}.
3129
3130 The procedure @code{string-ci=?} treats upper and lower case
3131 letters as though they were the same character, but
3132 @code{string=?} treats upper and lower case as distinct
3133 characters.
3134 @end deffn
3135
3136 @rnindex string<?
3137 @deffn {Scheme Procedure} string<? [s1 [s2 . rest]]
3138 @deffnx {C Function} scm_i_string_less_p (s1, s2, rest)
3139 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3140 is lexicographically less than @var{s2}.
3141 @end deffn
3142
3143 @rnindex string<=?
3144 @deffn {Scheme Procedure} string<=? [s1 [s2 . rest]]
3145 @deffnx {C Function} scm_i_string_leq_p (s1, s2, rest)
3146 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3147 is lexicographically less than or equal to @var{s2}.
3148 @end deffn
3149
3150 @rnindex string>?
3151 @deffn {Scheme Procedure} string>? [s1 [s2 . rest]]
3152 @deffnx {C Function} scm_i_string_gr_p (s1, s2, rest)
3153 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3154 is lexicographically greater than @var{s2}.
3155 @end deffn
3156
3157 @rnindex string>=?
3158 @deffn {Scheme Procedure} string>=? [s1 [s2 . rest]]
3159 @deffnx {C Function} scm_i_string_geq_p (s1, s2, rest)
3160 Lexicographic ordering predicate; return @code{#t} if @var{s1}
3161 is lexicographically greater than or equal to @var{s2}.
3162 @end deffn
3163
3164 @rnindex string-ci=?
3165 @deffn {Scheme Procedure} string-ci=? [s1 [s2 . rest]]
3166 @deffnx {C Function} scm_i_string_ci_equal_p (s1, s2, rest)
3167 Case-insensitive string equality predicate; return @code{#t} if
3168 the two strings are the same length and their component
3169 characters match (ignoring case) at each position; otherwise
3170 return @code{#f}.
3171 @end deffn
3172
3173 @rnindex string-ci<?
3174 @deffn {Scheme Procedure} string-ci<? [s1 [s2 . rest]]
3175 @deffnx {C Function} scm_i_string_ci_less_p (s1, s2, rest)
3176 Case insensitive lexicographic ordering predicate; return
3177 @code{#t} if @var{s1} is lexicographically less than @var{s2}
3178 regardless of case.
3179 @end deffn
3180
3181 @rnindex string<=?
3182 @deffn {Scheme Procedure} string-ci<=? [s1 [s2 . rest]]
3183 @deffnx {C Function} scm_i_string_ci_leq_p (s1, s2, rest)
3184 Case insensitive lexicographic ordering predicate; return
3185 @code{#t} if @var{s1} is lexicographically less than or equal
3186 to @var{s2} regardless of case.
3187 @end deffn
3188
3189 @rnindex string-ci>?
3190 @deffn {Scheme Procedure} string-ci>? [s1 [s2 . rest]]
3191 @deffnx {C Function} scm_i_string_ci_gr_p (s1, s2, rest)
3192 Case insensitive lexicographic ordering predicate; return
3193 @code{#t} if @var{s1} is lexicographically greater than
3194 @var{s2} regardless of case.
3195 @end deffn
3196
3197 @rnindex string-ci>=?
3198 @deffn {Scheme Procedure} string-ci>=? [s1 [s2 . rest]]
3199 @deffnx {C Function} scm_i_string_ci_geq_p (s1, s2, rest)
3200 Case insensitive lexicographic ordering predicate; return
3201 @code{#t} if @var{s1} is lexicographically greater than or
3202 equal to @var{s2} regardless of case.
3203 @end deffn
3204
3205 @deffn {Scheme Procedure} string-compare s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
3206 @deffnx {C Function} scm_string_compare (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
3207 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
3208 mismatch index, depending upon whether @var{s1} is less than,
3209 equal to, or greater than @var{s2}. The mismatch index is the
3210 largest index @var{i} such that for every 0 <= @var{j} <
3211 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
3212 @var{i} is the first position that does not match.
3213 @end deffn
3214
3215 @deffn {Scheme Procedure} string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
3216 @deffnx {C Function} scm_string_compare_ci (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
3217 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
3218 mismatch index, depending upon whether @var{s1} is less than,
3219 equal to, or greater than @var{s2}. The mismatch index is the
3220 largest index @var{i} such that for every 0 <= @var{j} <
3221 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
3222 @var{i} is the first position where the lowercased letters
3223 do not match.
3224
3225 @end deffn
3226
3227 @deffn {Scheme Procedure} string= s1 s2 [start1 [end1 [start2 [end2]]]]
3228 @deffnx {C Function} scm_string_eq (s1, s2, start1, end1, start2, end2)
3229 Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
3230 value otherwise.
3231 @end deffn
3232
3233 @deffn {Scheme Procedure} string<> s1 s2 [start1 [end1 [start2 [end2]]]]
3234 @deffnx {C Function} scm_string_neq (s1, s2, start1, end1, start2, end2)
3235 Return @code{#f} if @var{s1} and @var{s2} are equal, a true
3236 value otherwise.
3237 @end deffn
3238
3239 @deffn {Scheme Procedure} string< s1 s2 [start1 [end1 [start2 [end2]]]]
3240 @deffnx {C Function} scm_string_lt (s1, s2, start1, end1, start2, end2)
3241 Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
3242 true value otherwise.
3243 @end deffn
3244
3245 @deffn {Scheme Procedure} string> s1 s2 [start1 [end1 [start2 [end2]]]]
3246 @deffnx {C Function} scm_string_gt (s1, s2, start1, end1, start2, end2)
3247 Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
3248 true value otherwise.
3249 @end deffn
3250
3251 @deffn {Scheme Procedure} string<= s1 s2 [start1 [end1 [start2 [end2]]]]
3252 @deffnx {C Function} scm_string_le (s1, s2, start1, end1, start2, end2)
3253 Return @code{#f} if @var{s1} is greater to @var{s2}, a true
3254 value otherwise.
3255 @end deffn
3256
3257 @deffn {Scheme Procedure} string>= s1 s2 [start1 [end1 [start2 [end2]]]]
3258 @deffnx {C Function} scm_string_ge (s1, s2, start1, end1, start2, end2)
3259 Return @code{#f} if @var{s1} is less to @var{s2}, a true value
3260 otherwise.
3261 @end deffn
3262
3263 @deffn {Scheme Procedure} string-ci= s1 s2 [start1 [end1 [start2 [end2]]]]
3264 @deffnx {C Function} scm_string_ci_eq (s1, s2, start1, end1, start2, end2)
3265 Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
3266 value otherwise. The character comparison is done
3267 case-insensitively.
3268 @end deffn
3269
3270 @deffn {Scheme Procedure} string-ci<> s1 s2 [start1 [end1 [start2 [end2]]]]
3271 @deffnx {C Function} scm_string_ci_neq (s1, s2, start1, end1, start2, end2)
3272 Return @code{#f} if @var{s1} and @var{s2} are equal, a true
3273 value otherwise. The character comparison is done
3274 case-insensitively.
3275 @end deffn
3276
3277 @deffn {Scheme Procedure} string-ci< s1 s2 [start1 [end1 [start2 [end2]]]]
3278 @deffnx {C Function} scm_string_ci_lt (s1, s2, start1, end1, start2, end2)
3279 Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
3280 true value otherwise. The character comparison is done
3281 case-insensitively.
3282 @end deffn
3283
3284 @deffn {Scheme Procedure} string-ci> s1 s2 [start1 [end1 [start2 [end2]]]]
3285 @deffnx {C Function} scm_string_ci_gt (s1, s2, start1, end1, start2, end2)
3286 Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
3287 true value otherwise. The character comparison is done
3288 case-insensitively.
3289 @end deffn
3290
3291 @deffn {Scheme Procedure} string-ci<= s1 s2 [start1 [end1 [start2 [end2]]]]
3292 @deffnx {C Function} scm_string_ci_le (s1, s2, start1, end1, start2, end2)
3293 Return @code{#f} if @var{s1} is greater to @var{s2}, a true
3294 value otherwise. The character comparison is done
3295 case-insensitively.
3296 @end deffn
3297
3298 @deffn {Scheme Procedure} string-ci>= s1 s2 [start1 [end1 [start2 [end2]]]]
3299 @deffnx {C Function} scm_string_ci_ge (s1, s2, start1, end1, start2, end2)
3300 Return @code{#f} if @var{s1} is less to @var{s2}, a true value
3301 otherwise. The character comparison is done
3302 case-insensitively.
3303 @end deffn
3304
3305 @deffn {Scheme Procedure} string-hash s [bound [start [end]]]
3306 @deffnx {C Function} scm_substring_hash (s, bound, start, end)
3307 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).
3308 @end deffn
3309
3310 @deffn {Scheme Procedure} string-hash-ci s [bound [start [end]]]
3311 @deffnx {C Function} scm_substring_hash_ci (s, bound, start, end)
3312 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).
3313 @end deffn
3314
3315 Because the same visual appearance of an abstract Unicode character can
3316 be obtained via multiple sequences of Unicode characters, even the
3317 case-insensitive string comparison functions described above may return
3318 @code{#f} when presented with strings containing different
3319 representations of the same character. For example, the Unicode
3320 character ``LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE'' can be
3321 represented with a single character (U+1E69) or by the character ``LATIN
3322 SMALL LETTER S'' (U+0073) followed by the combining marks ``COMBINING
3323 DOT BELOW'' (U+0323) and ``COMBINING DOT ABOVE'' (U+0307).
3324
3325 For this reason, it is often desirable to ensure that the strings
3326 to be compared are using a mutually consistent representation for every
3327 character. The Unicode standard defines two methods of normalizing the
3328 contents of strings: Decomposition, which breaks composite characters
3329 into a set of constituent characters with an ordering defined by the
3330 Unicode Standard; and composition, which performs the converse.
3331
3332 There are two decomposition operations. ``Canonical decomposition''
3333 produces character sequences that share the same visual appearance as
3334 the original characters, while ``compatiblity decomposition'' produces
3335 ones whose visual appearances may differ from the originals but which
3336 represent the same abstract character.
3337
3338 These operations are encapsulated in the following set of normalization
3339 forms:
3340
3341 @table @dfn
3342 @item NFD
3343 Characters are decomposed to their canonical forms.
3344
3345 @item NFKD
3346 Characters are decomposed to their compatibility forms.
3347
3348 @item NFC
3349 Characters are decomposed to their canonical forms, then composed.
3350
3351 @item NFKC
3352 Characters are decomposed to their compatibility forms, then composed.
3353
3354 @end table
3355
3356 The functions below put their arguments into one of the forms described
3357 above.
3358
3359 @deffn {Scheme Procedure} string-normalize-nfd s
3360 @deffnx {C Function} scm_string_normalize_nfd (s)
3361 Return the @code{NFD} normalized form of @var{s}.
3362 @end deffn
3363
3364 @deffn {Scheme Procedure} string-normalize-nfkd s
3365 @deffnx {C Function} scm_string_normalize_nfkd (s)
3366 Return the @code{NFKD} normalized form of @var{s}.
3367 @end deffn
3368
3369 @deffn {Scheme Procedure} string-normalize-nfc s
3370 @deffnx {C Function} scm_string_normalize_nfc (s)
3371 Return the @code{NFC} normalized form of @var{s}.
3372 @end deffn
3373
3374 @deffn {Scheme Procedure} string-normalize-nfkc s
3375 @deffnx {C Function} scm_string_normalize_nfkc (s)
3376 Return the @code{NFKC} normalized form of @var{s}.
3377 @end deffn
3378
3379 @node String Searching
3380 @subsubsection String Searching
3381
3382 @deffn {Scheme Procedure} string-index s char_pred [start [end]]
3383 @deffnx {C Function} scm_string_index (s, char_pred, start, end)
3384 Search through the string @var{s} from left to right, returning
3385 the index of the first occurrence of a character which
3386
3387 @itemize @bullet
3388 @item
3389 equals @var{char_pred}, if it is character,
3390
3391 @item
3392 satisfies the predicate @var{char_pred}, if it is a procedure,
3393
3394 @item
3395 is in the set @var{char_pred}, if it is a character set.
3396 @end itemize
3397 @end deffn
3398
3399 @deffn {Scheme Procedure} string-rindex s char_pred [start [end]]
3400 @deffnx {C Function} scm_string_rindex (s, char_pred, start, end)
3401 Search through the string @var{s} from right to left, returning
3402 the index of the last occurrence of a character which
3403
3404 @itemize @bullet
3405 @item
3406 equals @var{char_pred}, if it is character,
3407
3408 @item
3409 satisfies the predicate @var{char_pred}, if it is a procedure,
3410
3411 @item
3412 is in the set if @var{char_pred} is a character set.
3413 @end itemize
3414 @end deffn
3415
3416 @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 [end1 [start2 [end2]]]]
3417 @deffnx {C Function} scm_string_prefix_length (s1, s2, start1, end1, start2, end2)
3418 Return the length of the longest common prefix of the two
3419 strings.
3420 @end deffn
3421
3422 @deffn {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3423 @deffnx {C Function} scm_string_prefix_length_ci (s1, s2, start1, end1, start2, end2)
3424 Return the length of the longest common prefix of the two
3425 strings, ignoring character case.
3426 @end deffn
3427
3428 @deffn {Scheme Procedure} string-suffix-length s1 s2 [start1 [end1 [start2 [end2]]]]
3429 @deffnx {C Function} scm_string_suffix_length (s1, s2, start1, end1, start2, end2)
3430 Return the length of the longest common suffix of the two
3431 strings.
3432 @end deffn
3433
3434 @deffn {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3435 @deffnx {C Function} scm_string_suffix_length_ci (s1, s2, start1, end1, start2, end2)
3436 Return the length of the longest common suffix of the two
3437 strings, ignoring character case.
3438 @end deffn
3439
3440 @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 [end1 [start2 [end2]]]]
3441 @deffnx {C Function} scm_string_prefix_p (s1, s2, start1, end1, start2, end2)
3442 Is @var{s1} a prefix of @var{s2}?
3443 @end deffn
3444
3445 @deffn {Scheme Procedure} string-prefix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
3446 @deffnx {C Function} scm_string_prefix_ci_p (s1, s2, start1, end1, start2, end2)
3447 Is @var{s1} a prefix of @var{s2}, ignoring character case?
3448 @end deffn
3449
3450 @deffn {Scheme Procedure} string-suffix? s1 s2 [start1 [end1 [start2 [end2]]]]
3451 @deffnx {C Function} scm_string_suffix_p (s1, s2, start1, end1, start2, end2)
3452 Is @var{s1} a suffix of @var{s2}?
3453 @end deffn
3454
3455 @deffn {Scheme Procedure} string-suffix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
3456 @deffnx {C Function} scm_string_suffix_ci_p (s1, s2, start1, end1, start2, end2)
3457 Is @var{s1} a suffix of @var{s2}, ignoring character case?
3458 @end deffn
3459
3460 @deffn {Scheme Procedure} string-index-right s char_pred [start [end]]
3461 @deffnx {C Function} scm_string_index_right (s, char_pred, start, end)
3462 Search through the string @var{s} from right to left, returning
3463 the index of the last occurrence of a character which
3464
3465 @itemize @bullet
3466 @item
3467 equals @var{char_pred}, if it is character,
3468
3469 @item
3470 satisfies the predicate @var{char_pred}, if it is a procedure,
3471
3472 @item
3473 is in the set if @var{char_pred} is a character set.
3474 @end itemize
3475 @end deffn
3476
3477 @deffn {Scheme Procedure} string-skip s char_pred [start [end]]
3478 @deffnx {C Function} scm_string_skip (s, char_pred, start, end)
3479 Search through the string @var{s} from left to right, returning
3480 the index of the first occurrence of a character which
3481
3482 @itemize @bullet
3483 @item
3484 does not equal @var{char_pred}, if it is character,
3485
3486 @item
3487 does not satisfy the predicate @var{char_pred}, if it is a
3488 procedure,
3489
3490 @item
3491 is not in the set if @var{char_pred} is a character set.
3492 @end itemize
3493 @end deffn
3494
3495 @deffn {Scheme Procedure} string-skip-right s char_pred [start [end]]
3496 @deffnx {C Function} scm_string_skip_right (s, char_pred, start, end)
3497 Search through the string @var{s} from right to left, returning
3498 the index of the last occurrence of a character which
3499
3500 @itemize @bullet
3501 @item
3502 does not equal @var{char_pred}, if it is character,
3503
3504 @item
3505 does not satisfy the predicate @var{char_pred}, if it is a
3506 procedure,
3507
3508 @item
3509 is not in the set if @var{char_pred} is a character set.
3510 @end itemize
3511 @end deffn
3512
3513 @deffn {Scheme Procedure} string-count s char_pred [start [end]]
3514 @deffnx {C Function} scm_string_count (s, char_pred, start, end)
3515 Return the count of the number of characters in the string
3516 @var{s} which
3517
3518 @itemize @bullet
3519 @item
3520 equals @var{char_pred}, if it is character,
3521
3522 @item
3523 satisfies the predicate @var{char_pred}, if it is a procedure.
3524
3525 @item
3526 is in the set @var{char_pred}, if it is a character set.
3527 @end itemize
3528 @end deffn
3529
3530 @deffn {Scheme Procedure} string-contains s1 s2 [start1 [end1 [start2 [end2]]]]
3531 @deffnx {C Function} scm_string_contains (s1, s2, start1, end1, start2, end2)
3532 Does string @var{s1} contain string @var{s2}? Return the index
3533 in @var{s1} where @var{s2} occurs as a substring, or false.
3534 The optional start/end indices restrict the operation to the
3535 indicated substrings.
3536 @end deffn
3537
3538 @deffn {Scheme Procedure} string-contains-ci s1 s2 [start1 [end1 [start2 [end2]]]]
3539 @deffnx {C Function} scm_string_contains_ci (s1, s2, start1, end1, start2, end2)
3540 Does string @var{s1} contain string @var{s2}? Return the index
3541 in @var{s1} where @var{s2} occurs as a substring, or false.
3542 The optional start/end indices restrict the operation to the
3543 indicated substrings. Character comparison is done
3544 case-insensitively.
3545 @end deffn
3546
3547 @node Alphabetic Case Mapping
3548 @subsubsection Alphabetic Case Mapping
3549
3550 These are procedures for mapping strings to their upper- or lower-case
3551 equivalents, respectively, or for capitalizing strings.
3552
3553 They use the basic case mapping rules for Unicode characters. No
3554 special language or context rules are considered. The resulting strings
3555 are guaranteed to be the same length as the input strings.
3556
3557 @xref{Character Case Mapping, the @code{(ice-9
3558 i18n)} module}, for locale-dependent case conversions.
3559
3560 @deffn {Scheme Procedure} string-upcase str [start [end]]
3561 @deffnx {C Function} scm_substring_upcase (str, start, end)
3562 @deffnx {C Function} scm_string_upcase (str)
3563 Upcase every character in @code{str}.
3564 @end deffn
3565
3566 @deffn {Scheme Procedure} string-upcase! str [start [end]]
3567 @deffnx {C Function} scm_substring_upcase_x (str, start, end)
3568 @deffnx {C Function} scm_string_upcase_x (str)
3569 Destructively upcase every character in @code{str}.
3570
3571 @lisp
3572 (string-upcase! y)
3573 @result{} "ARRDEFG"
3574 y
3575 @result{} "ARRDEFG"
3576 @end lisp
3577 @end deffn
3578
3579 @deffn {Scheme Procedure} string-downcase str [start [end]]
3580 @deffnx {C Function} scm_substring_downcase (str, start, end)
3581 @deffnx {C Function} scm_string_downcase (str)
3582 Downcase every character in @var{str}.
3583 @end deffn
3584
3585 @deffn {Scheme Procedure} string-downcase! str [start [end]]
3586 @deffnx {C Function} scm_substring_downcase_x (str, start, end)
3587 @deffnx {C Function} scm_string_downcase_x (str)
3588 Destructively downcase every character in @var{str}.
3589
3590 @lisp
3591 y
3592 @result{} "ARRDEFG"
3593 (string-downcase! y)
3594 @result{} "arrdefg"
3595 y
3596 @result{} "arrdefg"
3597 @end lisp
3598 @end deffn
3599
3600 @deffn {Scheme Procedure} string-capitalize str
3601 @deffnx {C Function} scm_string_capitalize (str)
3602 Return a freshly allocated string with the characters in
3603 @var{str}, where the first character of every word is
3604 capitalized.
3605 @end deffn
3606
3607 @deffn {Scheme Procedure} string-capitalize! str
3608 @deffnx {C Function} scm_string_capitalize_x (str)
3609 Upcase the first character of every word in @var{str}
3610 destructively and return @var{str}.
3611
3612 @lisp
3613 y @result{} "hello world"
3614 (string-capitalize! y) @result{} "Hello World"
3615 y @result{} "Hello World"
3616 @end lisp
3617 @end deffn
3618
3619 @deffn {Scheme Procedure} string-titlecase str [start [end]]
3620 @deffnx {C Function} scm_string_titlecase (str, start, end)
3621 Titlecase every first character in a word in @var{str}.
3622 @end deffn
3623
3624 @deffn {Scheme Procedure} string-titlecase! str [start [end]]
3625 @deffnx {C Function} scm_string_titlecase_x (str, start, end)
3626 Destructively titlecase every first character in a word in
3627 @var{str}.
3628 @end deffn
3629
3630 @node Reversing and Appending Strings
3631 @subsubsection Reversing and Appending Strings
3632
3633 @deffn {Scheme Procedure} string-reverse str [start [end]]
3634 @deffnx {C Function} scm_string_reverse (str, start, end)
3635 Reverse the string @var{str}. The optional arguments
3636 @var{start} and @var{end} delimit the region of @var{str} to
3637 operate on.
3638 @end deffn
3639
3640 @deffn {Scheme Procedure} string-reverse! str [start [end]]
3641 @deffnx {C Function} scm_string_reverse_x (str, start, end)
3642 Reverse the string @var{str} in-place. The optional arguments
3643 @var{start} and @var{end} delimit the region of @var{str} to
3644 operate on. The return value is unspecified.
3645 @end deffn
3646
3647 @rnindex string-append
3648 @deffn {Scheme Procedure} string-append . args
3649 @deffnx {C Function} scm_string_append (args)
3650 Return a newly allocated string whose characters form the
3651 concatenation of the given strings, @var{args}.
3652
3653 @example
3654 (let ((h "hello "))
3655 (string-append h "world"))
3656 @result{} "hello world"
3657 @end example
3658 @end deffn
3659
3660 @deffn {Scheme Procedure} string-append/shared . rest
3661 @deffnx {C Function} scm_string_append_shared (rest)
3662 Like @code{string-append}, but the result may share memory
3663 with the argument strings.
3664 @end deffn
3665
3666 @deffn {Scheme Procedure} string-concatenate ls
3667 @deffnx {C Function} scm_string_concatenate (ls)
3668 Append the elements of @var{ls} (which must be strings)
3669 together into a single string. Guaranteed to return a freshly
3670 allocated string.
3671 @end deffn
3672
3673 @deffn {Scheme Procedure} string-concatenate-reverse ls [final_string [end]]
3674 @deffnx {C Function} scm_string_concatenate_reverse (ls, final_string, end)
3675 Without optional arguments, this procedure is equivalent to
3676
3677 @lisp
3678 (string-concatenate (reverse ls))
3679 @end lisp
3680
3681 If the optional argument @var{final_string} is specified, it is
3682 consed onto the beginning to @var{ls} before performing the
3683 list-reverse and string-concatenate operations. If @var{end}
3684 is given, only the characters of @var{final_string} up to index
3685 @var{end} are used.
3686
3687 Guaranteed to return a freshly allocated string.
3688 @end deffn
3689
3690 @deffn {Scheme Procedure} string-concatenate/shared ls
3691 @deffnx {C Function} scm_string_concatenate_shared (ls)
3692 Like @code{string-concatenate}, but the result may share memory
3693 with the strings in the list @var{ls}.
3694 @end deffn
3695
3696 @deffn {Scheme Procedure} string-concatenate-reverse/shared ls [final_string [end]]
3697 @deffnx {C Function} scm_string_concatenate_reverse_shared (ls, final_string, end)
3698 Like @code{string-concatenate-reverse}, but the result may
3699 share memory with the strings in the @var{ls} arguments.
3700 @end deffn
3701
3702 @node Mapping Folding and Unfolding
3703 @subsubsection Mapping, Folding, and Unfolding
3704
3705 @deffn {Scheme Procedure} string-map proc s [start [end]]
3706 @deffnx {C Function} scm_string_map (proc, s, start, end)
3707 @var{proc} is a char->char procedure, it is mapped over
3708 @var{s}. The order in which the procedure is applied to the
3709 string elements is not specified.
3710 @end deffn
3711
3712 @deffn {Scheme Procedure} string-map! proc s [start [end]]
3713 @deffnx {C Function} scm_string_map_x (proc, s, start, end)
3714 @var{proc} is a char->char procedure, it is mapped over
3715 @var{s}. The order in which the procedure is applied to the
3716 string elements is not specified. The string @var{s} is
3717 modified in-place, the return value is not specified.
3718 @end deffn
3719
3720 @deffn {Scheme Procedure} string-for-each proc s [start [end]]
3721 @deffnx {C Function} scm_string_for_each (proc, s, start, end)
3722 @var{proc} is mapped over @var{s} in left-to-right order. The
3723 return value is not specified.
3724 @end deffn
3725
3726 @deffn {Scheme Procedure} string-for-each-index proc s [start [end]]
3727 @deffnx {C Function} scm_string_for_each_index (proc, s, start, end)
3728 Call @code{(@var{proc} i)} for each index i in @var{s}, from left to
3729 right.
3730
3731 For example, to change characters to alternately upper and lower case,
3732
3733 @example
3734 (define str (string-copy "studly"))
3735 (string-for-each-index
3736 (lambda (i)
3737 (string-set! str i
3738 ((if (even? i) char-upcase char-downcase)
3739 (string-ref str i))))
3740 str)
3741 str @result{} "StUdLy"
3742 @end example
3743 @end deffn
3744
3745 @deffn {Scheme Procedure} string-fold kons knil s [start [end]]
3746 @deffnx {C Function} scm_string_fold (kons, knil, s, start, end)
3747 Fold @var{kons} over the characters of @var{s}, with @var{knil}
3748 as the terminating element, from left to right. @var{kons}
3749 must expect two arguments: The actual character and the last
3750 result of @var{kons}' application.
3751 @end deffn
3752
3753 @deffn {Scheme Procedure} string-fold-right kons knil s [start [end]]
3754 @deffnx {C Function} scm_string_fold_right (kons, knil, s, start, end)
3755 Fold @var{kons} over the characters of @var{s}, with @var{knil}
3756 as the terminating element, from right to left. @var{kons}
3757 must expect two arguments: The actual character and the last
3758 result of @var{kons}' application.
3759 @end deffn
3760
3761 @deffn {Scheme Procedure} string-unfold p f g seed [base [make_final]]
3762 @deffnx {C Function} scm_string_unfold (p, f, g, seed, base, make_final)
3763 @itemize @bullet
3764 @item @var{g} is used to generate a series of @emph{seed}
3765 values from the initial @var{seed}: @var{seed}, (@var{g}
3766 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
3767 @dots{}
3768 @item @var{p} tells us when to stop -- when it returns true
3769 when applied to one of these seed values.
3770 @item @var{f} maps each seed value to the corresponding
3771 character in the result string. These chars are assembled
3772 into the string in a left-to-right order.
3773 @item @var{base} is the optional initial/leftmost portion
3774 of the constructed string; it default to the empty
3775 string.
3776 @item @var{make_final} is applied to the terminal seed
3777 value (on which @var{p} returns true) to produce
3778 the final/rightmost portion of the constructed string.
3779 The default is nothing extra.
3780 @end itemize
3781 @end deffn
3782
3783 @deffn {Scheme Procedure} string-unfold-right p f g seed [base [make_final]]
3784 @deffnx {C Function} scm_string_unfold_right (p, f, g, seed, base, make_final)
3785 @itemize @bullet
3786 @item @var{g} is used to generate a series of @emph{seed}
3787 values from the initial @var{seed}: @var{seed}, (@var{g}
3788 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
3789 @dots{}
3790 @item @var{p} tells us when to stop -- when it returns true
3791 when applied to one of these seed values.
3792 @item @var{f} maps each seed value to the corresponding
3793 character in the result string. These chars are assembled
3794 into the string in a right-to-left order.
3795 @item @var{base} is the optional initial/rightmost portion
3796 of the constructed string; it default to the empty
3797 string.
3798 @item @var{make_final} is applied to the terminal seed
3799 value (on which @var{p} returns true) to produce
3800 the final/leftmost portion of the constructed string.
3801 It defaults to @code{(lambda (x) )}.
3802 @end itemize
3803 @end deffn
3804
3805 @node Miscellaneous String Operations
3806 @subsubsection Miscellaneous String Operations
3807
3808 @deffn {Scheme Procedure} xsubstring s from [to [start [end]]]
3809 @deffnx {C Function} scm_xsubstring (s, from, to, start, end)
3810 This is the @emph{extended substring} procedure that implements
3811 replicated copying of a substring of some string.
3812
3813 @var{s} is a string, @var{start} and @var{end} are optional
3814 arguments that demarcate a substring of @var{s}, defaulting to
3815 0 and the length of @var{s}. Replicate this substring up and
3816 down index space, in both the positive and negative directions.
3817 @code{xsubstring} returns the substring of this string
3818 beginning at index @var{from}, and ending at @var{to}, which
3819 defaults to @var{from} + (@var{end} - @var{start}).
3820 @end deffn
3821
3822 @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto [start [end]]]
3823 @deffnx {C Function} scm_string_xcopy_x (target, tstart, s, sfrom, sto, start, end)
3824 Exactly the same as @code{xsubstring}, but the extracted text
3825 is written into the string @var{target} starting at index
3826 @var{tstart}. The operation is not defined if @code{(eq?
3827 @var{target} @var{s})} or these arguments share storage -- you
3828 cannot copy a string on top of itself.
3829 @end deffn
3830
3831 @deffn {Scheme Procedure} string-replace s1 s2 [start1 [end1 [start2 [end2]]]]
3832 @deffnx {C Function} scm_string_replace (s1, s2, start1, end1, start2, end2)
3833 Return the string @var{s1}, but with the characters
3834 @var{start1} @dots{} @var{end1} replaced by the characters
3835 @var{start2} @dots{} @var{end2} from @var{s2}.
3836 @end deffn
3837
3838 @deffn {Scheme Procedure} string-tokenize s [token_set [start [end]]]
3839 @deffnx {C Function} scm_string_tokenize (s, token_set, start, end)
3840 Split the string @var{s} into a list of substrings, where each
3841 substring is a maximal non-empty contiguous sequence of
3842 characters from the character set @var{token_set}, which
3843 defaults to @code{char-set:graphic}.
3844 If @var{start} or @var{end} indices are provided, they restrict
3845 @code{string-tokenize} to operating on the indicated substring
3846 of @var{s}.
3847 @end deffn
3848
3849 @deffn {Scheme Procedure} string-filter s char_pred [start [end]]
3850 @deffnx {C Function} scm_string_filter (s, char_pred, start, end)
3851 Filter the string @var{s}, retaining only those characters which
3852 satisfy @var{char_pred}.
3853
3854 If @var{char_pred} is a procedure, it is applied to each character as
3855 a predicate, if it is a character, it is tested for equality and if it
3856 is a character set, it is tested for membership.
3857 @end deffn
3858
3859 @deffn {Scheme Procedure} string-delete s char_pred [start [end]]
3860 @deffnx {C Function} scm_string_delete (s, char_pred, start, end)
3861 Delete characters satisfying @var{char_pred} from @var{s}.
3862
3863 If @var{char_pred} is a procedure, it is applied to each character as
3864 a predicate, if it is a character, it is tested for equality and if it
3865 is a character set, it is tested for membership.
3866 @end deffn
3867
3868 @node Conversion to/from C
3869 @subsubsection Conversion to/from C
3870
3871 When creating a Scheme string from a C string or when converting a
3872 Scheme string to a C string, the concept of character encoding becomes
3873 important.
3874
3875 In C, a string is just a sequence of bytes, and the character encoding
3876 describes the relation between these bytes and the actual characters
3877 that make up the string. For Scheme strings, character encoding is
3878 not an issue (most of the time), since in Scheme you never get to see
3879 the bytes, only the characters.
3880
3881 Converting to C and converting from C each have their own challenges.
3882
3883 When converting from C to Scheme, it is important that the sequence of
3884 bytes in the C string be valid with respect to its encoding. ASCII
3885 strings, for example, can't have any bytes greater than 127. An ASCII
3886 byte greater than 127 is considered @emph{ill-formed} and cannot be
3887 converted into a Scheme character.
3888
3889 Problems can occur in the reverse operation as well. Not all character
3890 encodings can hold all possible Scheme characters. Some encodings, like
3891 ASCII for example, can only describe a small subset of all possible
3892 characters. So, when converting to C, one must first decide what to do
3893 with Scheme characters that can't be represented in the C string.
3894
3895 Converting a Scheme string to a C string will often allocate fresh
3896 memory to hold the result. You must take care that this memory is
3897 properly freed eventually. In many cases, this can be achieved by
3898 using @code{scm_dynwind_free} inside an appropriate dynwind context,
3899 @xref{Dynamic Wind}.
3900
3901 @deftypefn {C Function} SCM scm_from_locale_string (const char *str)
3902 @deftypefnx {C Function} SCM scm_from_locale_stringn (const char *str, size_t len)
3903 Creates a new Scheme string that has the same contents as @var{str} when
3904 interpreted in the locale character encoding of the
3905 @code{current-input-port}.
3906
3907 For @code{scm_from_locale_string}, @var{str} must be null-terminated.
3908
3909 For @code{scm_from_locale_stringn}, @var{len} specifies the length of
3910 @var{str} in bytes, and @var{str} does not need to be null-terminated.
3911 If @var{len} is @code{(size_t)-1}, then @var{str} does need to be
3912 null-terminated and the real length will be found with @code{strlen}.
3913
3914 If the C string is ill-formed, an error will be raised.
3915 @end deftypefn
3916
3917 @deftypefn {C Function} SCM scm_take_locale_string (char *str)
3918 @deftypefnx {C Function} SCM scm_take_locale_stringn (char *str, size_t len)
3919 Like @code{scm_from_locale_string} and @code{scm_from_locale_stringn},
3920 respectively, but also frees @var{str} with @code{free} eventually.
3921 Thus, you can use this function when you would free @var{str} anyway
3922 immediately after creating the Scheme string. In certain cases, Guile
3923 can then use @var{str} directly as its internal representation.
3924 @end deftypefn
3925
3926 @deftypefn {C Function} {char *} scm_to_locale_string (SCM str)
3927 @deftypefnx {C Function} {char *} scm_to_locale_stringn (SCM str, size_t *lenp)
3928 Returns a C string with the same contents as @var{str} in the locale
3929 encoding of the @code{current-output-port}. The C string must be freed
3930 with @code{free} eventually, maybe by using @code{scm_dynwind_free},
3931 @xref{Dynamic Wind}.
3932
3933 For @code{scm_to_locale_string}, the returned string is
3934 null-terminated and an error is signalled when @var{str} contains
3935 @code{#\nul} characters.
3936
3937 For @code{scm_to_locale_stringn} and @var{lenp} not @code{NULL},
3938 @var{str} might contain @code{#\nul} characters and the length of the
3939 returned string in bytes is stored in @code{*@var{lenp}}. The
3940 returned string will not be null-terminated in this case. If
3941 @var{lenp} is @code{NULL}, @code{scm_to_locale_stringn} behaves like
3942 @code{scm_to_locale_string}.
3943
3944 If a character in @var{str} cannot be represented in the locale encoding
3945 of the current output port, the port conversion strategy of the current
3946 output port will determine the result, @xref{Ports}. If output port's
3947 conversion strategy is @code{error}, an error will be raised. If it is
3948 @code{subsitute}, a replacement character, such as a question mark, will
3949 be inserted in its place. If it is @code{escape}, a hex escape will be
3950 inserted in its place.
3951 @end deftypefn
3952
3953 @deftypefn {C Function} size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
3954 Puts @var{str} as a C string in the current locale encoding into the
3955 memory pointed to by @var{buf}. The buffer at @var{buf} has room for
3956 @var{max_len} bytes and @code{scm_to_local_stringbuf} will never store
3957 more than that. No terminating @code{'\0'} will be stored.
3958
3959 The return value of @code{scm_to_locale_stringbuf} is the number of
3960 bytes that are needed for all of @var{str}, regardless of whether
3961 @var{buf} was large enough to hold them. Thus, when the return value
3962 is larger than @var{max_len}, only @var{max_len} bytes have been
3963 stored and you probably need to try again with a larger buffer.
3964 @end deftypefn
3965
3966 @node String Internals
3967 @subsubsection String Internals
3968
3969 Guile stores each string in memory as a contiguous array of Unicode code
3970 points along with an associated set of attributes. If all of the code
3971 points of a string have an integer range between 0 and 255 inclusive,
3972 the code point array is stored as one byte per code point: it is stored
3973 as an ISO-8859-1 (aka Latin-1) string. If any of the code points of the
3974 string has an integer value greater that 255, the code point array is
3975 stored as four bytes per code point: it is stored as a UTF-32 string.
3976
3977 Conversion between the one-byte-per-code-point and
3978 four-bytes-per-code-point representations happens automatically as
3979 necessary.
3980
3981 No API is provided to set the internal representation of strings;
3982 however, there are pair of procedures available to query it. These are
3983 debugging procedures. Using them in production code is discouraged,
3984 since the details of Guile's internal representation of strings may
3985 change from release to release.
3986
3987 @deffn {Scheme Procedure} string-bytes-per-char str
3988 @deffnx {C Function} scm_string_bytes_per_char (str)
3989 Return the number of bytes used to encode a Unicode code point in string
3990 @var{str}. The result is one or four.
3991 @end deffn
3992
3993 @deffn {Scheme Procedure} %string-dump str
3994 @deffnx {C Function} scm_sys_string_dump (str)
3995 Returns an association list containing debugging information for
3996 @var{str}. The association list has the following entries.
3997 @table @code
3998
3999 @item string
4000 The string itself.
4001
4002 @item start
4003 The start index of the string into its stringbuf
4004
4005 @item length
4006 The length of the string
4007
4008 @item shared
4009 If this string is a substring, it returns its
4010 parent string. Otherwise, it returns @code{#f}
4011
4012 @item read-only
4013 @code{#t} if the string is read-only
4014
4015 @item stringbuf-chars
4016 A new string containing this string's stringbuf's characters
4017
4018 @item stringbuf-length
4019 The number of characters in this stringbuf
4020
4021 @item stringbuf-shared
4022 @code{#t} if this stringbuf is shared
4023
4024 @item stringbuf-wide
4025 @code{#t} if this stringbuf's characters are stored in a 32-bit buffer,
4026 or @code{#f} if they are stored in an 8-bit buffer
4027 @end table
4028 @end deffn
4029
4030
4031 @node Bytevectors
4032 @subsection Bytevectors
4033
4034 @cindex bytevector
4035 @cindex R6RS
4036
4037 A @dfn{bytevector} is a raw bit string. The @code{(rnrs bytevector)}
4038 module provides the programming interface specified by the
4039 @uref{http://www.r6rs.org/, Revised^6 Report on the Algorithmic Language
4040 Scheme (R6RS)}. It contains procedures to manipulate bytevectors and
4041 interpret their contents in a number of ways: bytevector contents can be
4042 accessed as signed or unsigned integer of various sizes and endianness,
4043 as IEEE-754 floating point numbers, or as strings. It is a useful tool
4044 to encode and decode binary data.
4045
4046 The R6RS (Section 4.3.4) specifies an external representation for
4047 bytevectors, whereby the octets (integers in the range 0--255) contained
4048 in the bytevector are represented as a list prefixed by @code{#vu8}:
4049
4050 @lisp
4051 #vu8(1 53 204)
4052 @end lisp
4053
4054 denotes a 3-byte bytevector containing the octets 1, 53, and 204. Like
4055 string literals, booleans, etc., bytevectors are ``self-quoting'', i.e.,
4056 they do not need to be quoted:
4057
4058 @lisp
4059 #vu8(1 53 204)
4060 @result{} #vu8(1 53 204)
4061 @end lisp
4062
4063 Bytevectors can be used with the binary input/output primitives of the
4064 R6RS (@pxref{R6RS I/O Ports}).
4065
4066 @menu
4067 * Bytevector Endianness:: Dealing with byte order.
4068 * Bytevector Manipulation:: Creating, copying, manipulating bytevectors.
4069 * Bytevectors as Integers:: Interpreting bytes as integers.
4070 * Bytevectors and Integer Lists:: Converting to/from an integer list.
4071 * Bytevectors as Floats:: Interpreting bytes as real numbers.
4072 * Bytevectors as Strings:: Interpreting bytes as Unicode strings.
4073 * Bytevectors as Generalized Vectors:: Guile extension to the bytevector API.
4074 * Bytevectors as Uniform Vectors:: Bytevectors and SRFI-4.
4075 @end menu
4076
4077 @node Bytevector Endianness
4078 @subsubsection Endianness
4079
4080 @cindex endianness
4081 @cindex byte order
4082 @cindex word order
4083
4084 Some of the following procedures take an @var{endianness} parameter.
4085 The @dfn{endianness} is defined as the order of bytes in multi-byte
4086 numbers: numbers encoded in @dfn{big endian} have their most
4087 significant bytes written first, whereas numbers encoded in
4088 @dfn{little endian} have their least significant bytes
4089 first@footnote{Big-endian and little-endian are the most common
4090 ``endiannesses'', but others do exist. For instance, the GNU MP
4091 library allows @dfn{word order} to be specified independently of
4092 @dfn{byte order} (@pxref{Integer Import and Export,,, gmp, The GNU
4093 Multiple Precision Arithmetic Library Manual}).}.
4094
4095 Little-endian is the native endianness of the IA32 architecture and
4096 its derivatives, while big-endian is native to SPARC and PowerPC,
4097 among others. The @code{native-endianness} procedure returns the
4098 native endianness of the machine it runs on.
4099
4100 @deffn {Scheme Procedure} native-endianness
4101 @deffnx {C Function} scm_native_endianness ()
4102 Return a value denoting the native endianness of the host machine.
4103 @end deffn
4104
4105 @deffn {Scheme Macro} endianness symbol
4106 Return an object denoting the endianness specified by @var{symbol}. If
4107 @var{symbol} is neither @code{big} nor @code{little} then an error is
4108 raised at expand-time.
4109 @end deffn
4110
4111 @defvr {C Variable} scm_endianness_big
4112 @defvrx {C Variable} scm_endianness_little
4113 The objects denoting big- and little-endianness, respectively.
4114 @end defvr
4115
4116
4117 @node Bytevector Manipulation
4118 @subsubsection Manipulating Bytevectors
4119
4120 Bytevectors can be created, copied, and analyzed with the following
4121 procedures and C functions.
4122
4123 @deffn {Scheme Procedure} make-bytevector len [fill]
4124 @deffnx {C Function} scm_make_bytevector (len, fill)
4125 @deffnx {C Function} scm_c_make_bytevector (size_t len)
4126 Return a new bytevector of @var{len} bytes. Optionally, if @var{fill}
4127 is given, fill it with @var{fill}; @var{fill} must be in the range
4128 [-128,255].
4129 @end deffn
4130
4131 @deffn {Scheme Procedure} bytevector? obj
4132 @deffnx {C Function} scm_bytevector_p (obj)
4133 Return true if @var{obj} is a bytevector.
4134 @end deffn
4135
4136 @deftypefn {C Function} int scm_is_bytevector (SCM obj)
4137 Equivalent to @code{scm_is_true (scm_bytevector_p (obj))}.
4138 @end deftypefn
4139
4140 @deffn {Scheme Procedure} bytevector-length bv
4141 @deffnx {C Function} scm_bytevector_length (bv)
4142 Return the length in bytes of bytevector @var{bv}.
4143 @end deffn
4144
4145 @deftypefn {C Function} size_t scm_c_bytevector_length (SCM bv)
4146 Likewise, return the length in bytes of bytevector @var{bv}.
4147 @end deftypefn
4148
4149 @deffn {Scheme Procedure} bytevector=? bv1 bv2
4150 @deffnx {C Function} scm_bytevector_eq_p (bv1, bv2)
4151 Return is @var{bv1} equals to @var{bv2}---i.e., if they have the same
4152 length and contents.
4153 @end deffn
4154
4155 @deffn {Scheme Procedure} bytevector-fill! bv fill
4156 @deffnx {C Function} scm_bytevector_fill_x (bv, fill)
4157 Fill bytevector @var{bv} with @var{fill}, a byte.
4158 @end deffn
4159
4160 @deffn {Scheme Procedure} bytevector-copy! source source-start target target-start len
4161 @deffnx {C Function} scm_bytevector_copy_x (source, source_start, target, target_start, len)
4162 Copy @var{len} bytes from @var{source} into @var{target}, starting
4163 reading from @var{source-start} (a positive index within @var{source})
4164 and start writing at @var{target-start}.
4165 @end deffn
4166
4167 @deffn {Scheme Procedure} bytevector-copy bv
4168 @deffnx {C Function} scm_bytevector_copy (bv)
4169 Return a newly allocated copy of @var{bv}.
4170 @end deffn
4171
4172 @deftypefn {C Function} scm_t_uint8 scm_c_bytevector_ref (SCM bv, size_t index)
4173 Return the byte at @var{index} in bytevector @var{bv}.
4174 @end deftypefn
4175
4176 @deftypefn {C Function} void scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
4177 Set the byte at @var{index} in @var{bv} to @var{value}.
4178 @end deftypefn
4179
4180 Low-level C macros are available. They do not perform any
4181 type-checking; as such they should be used with care.
4182
4183 @deftypefn {C Macro} size_t SCM_BYTEVECTOR_LENGTH (bv)
4184 Return the length in bytes of bytevector @var{bv}.
4185 @end deftypefn
4186
4187 @deftypefn {C Macro} {signed char *} SCM_BYTEVECTOR_CONTENTS (bv)
4188 Return a pointer to the contents of bytevector @var{bv}.
4189 @end deftypefn
4190
4191
4192 @node Bytevectors as Integers
4193 @subsubsection Interpreting Bytevector Contents as Integers
4194
4195 The contents of a bytevector can be interpreted as a sequence of
4196 integers of any given size, sign, and endianness.
4197
4198 @lisp
4199 (let ((bv (make-bytevector 4)))
4200 (bytevector-u8-set! bv 0 #x12)
4201 (bytevector-u8-set! bv 1 #x34)
4202 (bytevector-u8-set! bv 2 #x56)
4203 (bytevector-u8-set! bv 3 #x78)
4204
4205 (map (lambda (number)
4206 (number->string number 16))
4207 (list (bytevector-u8-ref bv 0)
4208 (bytevector-u16-ref bv 0 (endianness big))
4209 (bytevector-u32-ref bv 0 (endianness little)))))
4210
4211 @result{} ("12" "1234" "78563412")
4212 @end lisp
4213
4214 The most generic procedures to interpret bytevector contents as integers
4215 are described below.
4216
4217 @deffn {Scheme Procedure} bytevector-uint-ref bv index endianness size
4218 @deffnx {Scheme Procedure} bytevector-sint-ref bv index endianness size
4219 @deffnx {C Function} scm_bytevector_uint_ref (bv, index, endianness, size)
4220 @deffnx {C Function} scm_bytevector_sint_ref (bv, index, endianness, size)
4221 Return the @var{size}-byte long unsigned (resp. signed) integer at
4222 index @var{index} in @var{bv}, decoded according to @var{endianness}.
4223 @end deffn
4224
4225 @deffn {Scheme Procedure} bytevector-uint-set! bv index value endianness size
4226 @deffnx {Scheme Procedure} bytevector-sint-set! bv index value endianness size
4227 @deffnx {C Function} scm_bytevector_uint_set_x (bv, index, value, endianness, size)
4228 @deffnx {C Function} scm_bytevector_sint_set_x (bv, index, value, endianness, size)
4229 Set the @var{size}-byte long unsigned (resp. signed) integer at
4230 @var{index} to @var{value}, encoded according to @var{endianness}.
4231 @end deffn
4232
4233 The following procedures are similar to the ones above, but specialized
4234 to a given integer size:
4235
4236 @deffn {Scheme Procedure} bytevector-u8-ref bv index
4237 @deffnx {Scheme Procedure} bytevector-s8-ref bv index
4238 @deffnx {Scheme Procedure} bytevector-u16-ref bv index endianness
4239 @deffnx {Scheme Procedure} bytevector-s16-ref bv index endianness
4240 @deffnx {Scheme Procedure} bytevector-u32-ref bv index endianness
4241 @deffnx {Scheme Procedure} bytevector-s32-ref bv index endianness
4242 @deffnx {Scheme Procedure} bytevector-u64-ref bv index endianness
4243 @deffnx {Scheme Procedure} bytevector-s64-ref bv index endianness
4244 @deffnx {C Function} scm_bytevector_u8_ref (bv, index)
4245 @deffnx {C Function} scm_bytevector_s8_ref (bv, index)
4246 @deffnx {C Function} scm_bytevector_u16_ref (bv, index, endianness)
4247 @deffnx {C Function} scm_bytevector_s16_ref (bv, index, endianness)
4248 @deffnx {C Function} scm_bytevector_u32_ref (bv, index, endianness)
4249 @deffnx {C Function} scm_bytevector_s32_ref (bv, index, endianness)
4250 @deffnx {C Function} scm_bytevector_u64_ref (bv, index, endianness)
4251 @deffnx {C Function} scm_bytevector_s64_ref (bv, index, endianness)
4252 Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
4253 16, 32 or 64) from @var{bv} at @var{index}, decoded according to
4254 @var{endianness}.
4255 @end deffn
4256
4257 @deffn {Scheme Procedure} bytevector-u8-set! bv index value
4258 @deffnx {Scheme Procedure} bytevector-s8-set! bv index value
4259 @deffnx {Scheme Procedure} bytevector-u16-set! bv index value endianness
4260 @deffnx {Scheme Procedure} bytevector-s16-set! bv index value endianness
4261 @deffnx {Scheme Procedure} bytevector-u32-set! bv index value endianness
4262 @deffnx {Scheme Procedure} bytevector-s32-set! bv index value endianness
4263 @deffnx {Scheme Procedure} bytevector-u64-set! bv index value endianness
4264 @deffnx {Scheme Procedure} bytevector-s64-set! bv index value endianness
4265 @deffnx {C Function} scm_bytevector_u8_set_x (bv, index, value)
4266 @deffnx {C Function} scm_bytevector_s8_set_x (bv, index, value)
4267 @deffnx {C Function} scm_bytevector_u16_set_x (bv, index, value, endianness)
4268 @deffnx {C Function} scm_bytevector_s16_set_x (bv, index, value, endianness)
4269 @deffnx {C Function} scm_bytevector_u32_set_x (bv, index, value, endianness)
4270 @deffnx {C Function} scm_bytevector_s32_set_x (bv, index, value, endianness)
4271 @deffnx {C Function} scm_bytevector_u64_set_x (bv, index, value, endianness)
4272 @deffnx {C Function} scm_bytevector_s64_set_x (bv, index, value, endianness)
4273 Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
4274 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to
4275 @var{endianness}.
4276 @end deffn
4277
4278 Finally, a variant specialized for the host's endianness is available
4279 for each of these functions (with the exception of the @code{u8}
4280 accessors, for obvious reasons):
4281
4282 @deffn {Scheme Procedure} bytevector-u16-native-ref bv index
4283 @deffnx {Scheme Procedure} bytevector-s16-native-ref bv index
4284 @deffnx {Scheme Procedure} bytevector-u32-native-ref bv index
4285 @deffnx {Scheme Procedure} bytevector-s32-native-ref bv index
4286 @deffnx {Scheme Procedure} bytevector-u64-native-ref bv index
4287 @deffnx {Scheme Procedure} bytevector-s64-native-ref bv index
4288 @deffnx {C Function} scm_bytevector_u16_native_ref (bv, index)
4289 @deffnx {C Function} scm_bytevector_s16_native_ref (bv, index)
4290 @deffnx {C Function} scm_bytevector_u32_native_ref (bv, index)
4291 @deffnx {C Function} scm_bytevector_s32_native_ref (bv, index)
4292 @deffnx {C Function} scm_bytevector_u64_native_ref (bv, index)
4293 @deffnx {C Function} scm_bytevector_s64_native_ref (bv, index)
4294 Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
4295 16, 32 or 64) from @var{bv} at @var{index}, decoded according to the
4296 host's native endianness.
4297 @end deffn
4298
4299 @deffn {Scheme Procedure} bytevector-u16-native-set! bv index value
4300 @deffnx {Scheme Procedure} bytevector-s16-native-set! bv index value
4301 @deffnx {Scheme Procedure} bytevector-u32-native-set! bv index value
4302 @deffnx {Scheme Procedure} bytevector-s32-native-set! bv index value
4303 @deffnx {Scheme Procedure} bytevector-u64-native-set! bv index value
4304 @deffnx {Scheme Procedure} bytevector-s64-native-set! bv index value
4305 @deffnx {C Function} scm_bytevector_u16_native_set_x (bv, index, value)
4306 @deffnx {C Function} scm_bytevector_s16_native_set_x (bv, index, value)
4307 @deffnx {C Function} scm_bytevector_u32_native_set_x (bv, index, value)
4308 @deffnx {C Function} scm_bytevector_s32_native_set_x (bv, index, value)
4309 @deffnx {C Function} scm_bytevector_u64_native_set_x (bv, index, value)
4310 @deffnx {C Function} scm_bytevector_s64_native_set_x (bv, index, value)
4311 Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
4312 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to the
4313 host's native endianness.
4314 @end deffn
4315
4316
4317 @node Bytevectors and Integer Lists
4318 @subsubsection Converting Bytevectors to/from Integer Lists
4319
4320 Bytevector contents can readily be converted to/from lists of signed or
4321 unsigned integers:
4322
4323 @lisp
4324 (bytevector->sint-list (u8-list->bytevector (make-list 4 255))
4325 (endianness little) 2)
4326 @result{} (-1 -1)
4327 @end lisp
4328
4329 @deffn {Scheme Procedure} bytevector->u8-list bv
4330 @deffnx {C Function} scm_bytevector_to_u8_list (bv)
4331 Return a newly allocated list of unsigned 8-bit integers from the
4332 contents of @var{bv}.
4333 @end deffn
4334
4335 @deffn {Scheme Procedure} u8-list->bytevector lst
4336 @deffnx {C Function} scm_u8_list_to_bytevector (lst)
4337 Return a newly allocated bytevector consisting of the unsigned 8-bit
4338 integers listed in @var{lst}.
4339 @end deffn
4340
4341 @deffn {Scheme Procedure} bytevector->uint-list bv endianness size
4342 @deffnx {Scheme Procedure} bytevector->sint-list bv endianness size
4343 @deffnx {C Function} scm_bytevector_to_uint_list (bv, endianness, size)
4344 @deffnx {C Function} scm_bytevector_to_sint_list (bv, endianness, size)
4345 Return a list of unsigned (resp. signed) integers of @var{size} bytes
4346 representing the contents of @var{bv}, decoded according to
4347 @var{endianness}.
4348 @end deffn
4349
4350 @deffn {Scheme Procedure} uint-list->bytevector lst endianness size
4351 @deffnx {Scheme Procedure} sint-list->bytevector lst endianness size
4352 @deffnx {C Function} scm_uint_list_to_bytevector (lst, endianness, size)
4353 @deffnx {C Function} scm_sint_list_to_bytevector (lst, endianness, size)
4354 Return a new bytevector containing the unsigned (resp. signed) integers
4355 listed in @var{lst} and encoded on @var{size} bytes according to
4356 @var{endianness}.
4357 @end deffn
4358
4359 @node Bytevectors as Floats
4360 @subsubsection Interpreting Bytevector Contents as Floating Point Numbers
4361
4362 @cindex IEEE-754 floating point numbers
4363
4364 Bytevector contents can also be accessed as IEEE-754 single- or
4365 double-precision floating point numbers (respectively 32 and 64-bit
4366 long) using the procedures described here.
4367
4368 @deffn {Scheme Procedure} bytevector-ieee-single-ref bv index endianness
4369 @deffnx {Scheme Procedure} bytevector-ieee-double-ref bv index endianness
4370 @deffnx {C Function} scm_bytevector_ieee_single_ref (bv, index, endianness)
4371 @deffnx {C Function} scm_bytevector_ieee_double_ref (bv, index, endianness)
4372 Return the IEEE-754 single-precision floating point number from @var{bv}
4373 at @var{index} according to @var{endianness}.
4374 @end deffn
4375
4376 @deffn {Scheme Procedure} bytevector-ieee-single-set! bv index value endianness
4377 @deffnx {Scheme Procedure} bytevector-ieee-double-set! bv index value endianness
4378 @deffnx {C Function} scm_bytevector_ieee_single_set_x (bv, index, value, endianness)
4379 @deffnx {C Function} scm_bytevector_ieee_double_set_x (bv, index, value, endianness)
4380 Store real number @var{value} in @var{bv} at @var{index} according to
4381 @var{endianness}.
4382 @end deffn
4383
4384 Specialized procedures are also available:
4385
4386 @deffn {Scheme Procedure} bytevector-ieee-single-native-ref bv index
4387 @deffnx {Scheme Procedure} bytevector-ieee-double-native-ref bv index
4388 @deffnx {C Function} scm_bytevector_ieee_single_native_ref (bv, index)
4389 @deffnx {C Function} scm_bytevector_ieee_double_native_ref (bv, index)
4390 Return the IEEE-754 single-precision floating point number from @var{bv}
4391 at @var{index} according to the host's native endianness.
4392 @end deffn
4393
4394 @deffn {Scheme Procedure} bytevector-ieee-single-native-set! bv index value
4395 @deffnx {Scheme Procedure} bytevector-ieee-double-native-set! bv index value
4396 @deffnx {C Function} scm_bytevector_ieee_single_native_set_x (bv, index, value)
4397 @deffnx {C Function} scm_bytevector_ieee_double_native_set_x (bv, index, value)
4398 Store real number @var{value} in @var{bv} at @var{index} according to
4399 the host's native endianness.
4400 @end deffn
4401
4402
4403 @node Bytevectors as Strings
4404 @subsubsection Interpreting Bytevector Contents as Unicode Strings
4405
4406 @cindex Unicode string encoding
4407
4408 Bytevector contents can also be interpreted as Unicode strings encoded
4409 in one of the most commonly available encoding formats@footnote{Guile
4410 1.8 does @emph{not} support Unicode strings. Therefore, the procedures
4411 described here assume that Guile strings are internally encoded
4412 according to the current locale. For instance, if @code{$LC_CTYPE} is
4413 @code{fr_FR.ISO-8859-1}, then @code{string->utf-8} @i{et al.} will
4414 assume that Guile strings are Latin-1-encoded.}.
4415
4416 @lisp
4417 (utf8->string (u8-list->bytevector '(99 97 102 101)))
4418 @result{} "cafe"
4419
4420 (string->utf8 "caf@'e") ;; SMALL LATIN LETTER E WITH ACUTE ACCENT
4421 @result{} #vu8(99 97 102 195 169)
4422 @end lisp
4423
4424 @deffn {Scheme Procedure} string->utf8 str
4425 @deffnx {Scheme Procedure} string->utf16 str [endianness]
4426 @deffnx {Scheme Procedure} string->utf32 str [endianness]
4427 @deffnx {C Function} scm_string_to_utf8 (str)
4428 @deffnx {C Function} scm_string_to_utf16 (str, endianness)
4429 @deffnx {C Function} scm_string_to_utf32 (str, endianness)
4430 Return a newly allocated bytevector that contains the UTF-8, UTF-16, or
4431 UTF-32 (aka. UCS-4) encoding of @var{str}. For UTF-16 and UTF-32,
4432 @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
4433 it defaults to big endian.
4434 @end deffn
4435
4436 @deffn {Scheme Procedure} utf8->string utf
4437 @deffnx {Scheme Procedure} utf16->string utf [endianness]
4438 @deffnx {Scheme Procedure} utf32->string utf [endianness]
4439 @deffnx {C Function} scm_utf8_to_string (utf)
4440 @deffnx {C Function} scm_utf16_to_string (utf, endianness)
4441 @deffnx {C Function} scm_utf32_to_string (utf, endianness)
4442 Return a newly allocated string that contains from the UTF-8-, UTF-16-,
4443 or UTF-32-decoded contents of bytevector @var{utf}. For UTF-16 and UTF-32,
4444 @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
4445 it defaults to big endian.
4446 @end deffn
4447
4448 @node Bytevectors as Generalized Vectors
4449 @subsubsection Accessing Bytevectors with the Generalized Vector API
4450
4451 As an extension to the R6RS, Guile allows bytevectors to be manipulated
4452 with the @dfn{generalized vector} procedures (@pxref{Generalized
4453 Vectors}). This also allows bytevectors to be accessed using the
4454 generic @dfn{array} procedures (@pxref{Array Procedures}). When using
4455 these APIs, bytes are accessed one at a time as 8-bit unsigned integers:
4456
4457 @example
4458 (define bv #vu8(0 1 2 3))
4459
4460 (generalized-vector? bv)
4461 @result{} #t
4462
4463 (generalized-vector-ref bv 2)
4464 @result{} 2
4465
4466 (generalized-vector-set! bv 2 77)
4467 (array-ref bv 2)
4468 @result{} 77
4469
4470 (array-type bv)
4471 @result{} vu8
4472 @end example
4473
4474
4475 @node Bytevectors as Uniform Vectors
4476 @subsubsection Accessing Bytevectors with the SRFI-4 API
4477
4478 Bytevectors may also be accessed with the SRFI-4 API. @xref{SRFI-4 and
4479 Bytevectors}, for more information.
4480
4481
4482 @node Regular Expressions
4483 @subsection Regular Expressions
4484 @tpindex Regular expressions
4485
4486 @cindex regular expressions
4487 @cindex regex
4488 @cindex emacs regexp
4489
4490 A @dfn{regular expression} (or @dfn{regexp}) is a pattern that
4491 describes a whole class of strings. A full description of regular
4492 expressions and their syntax is beyond the scope of this manual;
4493 an introduction can be found in the Emacs manual (@pxref{Regexps,
4494 , Syntax of Regular Expressions, emacs, The GNU Emacs Manual}), or
4495 in many general Unix reference books.
4496
4497 If your system does not include a POSIX regular expression library,
4498 and you have not linked Guile with a third-party regexp library such
4499 as Rx, these functions will not be available. You can tell whether
4500 your Guile installation includes regular expression support by
4501 checking whether @code{(provided? 'regex)} returns true.
4502
4503 The following regexp and string matching features are provided by the
4504 @code{(ice-9 regex)} module. Before using the described functions,
4505 you should load this module by executing @code{(use-modules (ice-9
4506 regex))}.
4507
4508 @menu
4509 * Regexp Functions:: Functions that create and match regexps.
4510 * Match Structures:: Finding what was matched by a regexp.
4511 * Backslash Escapes:: Removing the special meaning of regexp
4512 meta-characters.
4513 @end menu
4514
4515
4516 @node Regexp Functions
4517 @subsubsection Regexp Functions
4518
4519 By default, Guile supports POSIX extended regular expressions.
4520 That means that the characters @samp{(}, @samp{)}, @samp{+} and
4521 @samp{?} are special, and must be escaped if you wish to match the
4522 literal characters.
4523
4524 This regular expression interface was modeled after that
4525 implemented by SCSH, the Scheme Shell. It is intended to be
4526 upwardly compatible with SCSH regular expressions.
4527
4528 Zero bytes (@code{#\nul}) cannot be used in regex patterns or input
4529 strings, since the underlying C functions treat that as the end of
4530 string. If there's a zero byte an error is thrown.
4531
4532 Patterns and input strings are treated as being in the locale
4533 character set if @code{setlocale} has been called (@pxref{Locales}),
4534 and in a multibyte locale this includes treating multi-byte sequences
4535 as a single character. (Guile strings are currently merely bytes,
4536 though this may change in the future, @xref{Conversion to/from C}.)
4537
4538 @deffn {Scheme Procedure} string-match pattern str [start]
4539 Compile the string @var{pattern} into a regular expression and compare
4540 it with @var{str}. The optional numeric argument @var{start} specifies
4541 the position of @var{str} at which to begin matching.
4542
4543 @code{string-match} returns a @dfn{match structure} which
4544 describes what, if anything, was matched by the regular
4545 expression. @xref{Match Structures}. If @var{str} does not match
4546 @var{pattern} at all, @code{string-match} returns @code{#f}.
4547 @end deffn
4548
4549 Two examples of a match follow. In the first example, the pattern
4550 matches the four digits in the match string. In the second, the pattern
4551 matches nothing.
4552
4553 @example
4554 (string-match "[0-9][0-9][0-9][0-9]" "blah2002")
4555 @result{} #("blah2002" (4 . 8))
4556
4557 (string-match "[A-Za-z]" "123456")
4558 @result{} #f
4559 @end example
4560
4561 Each time @code{string-match} is called, it must compile its
4562 @var{pattern} argument into a regular expression structure. This
4563 operation is expensive, which makes @code{string-match} inefficient if
4564 the same regular expression is used several times (for example, in a
4565 loop). For better performance, you can compile a regular expression in
4566 advance and then match strings against the compiled regexp.
4567
4568 @deffn {Scheme Procedure} make-regexp pat flag@dots{}
4569 @deffnx {C Function} scm_make_regexp (pat, flaglst)
4570 Compile the regular expression described by @var{pat}, and
4571 return the compiled regexp structure. If @var{pat} does not
4572 describe a legal regular expression, @code{make-regexp} throws
4573 a @code{regular-expression-syntax} error.
4574
4575 The @var{flag} arguments change the behavior of the compiled
4576 regular expression. The following values may be supplied:
4577
4578 @defvar regexp/icase
4579 Consider uppercase and lowercase letters to be the same when
4580 matching.
4581 @end defvar
4582
4583 @defvar regexp/newline
4584 If a newline appears in the target string, then permit the
4585 @samp{^} and @samp{$} operators to match immediately after or
4586 immediately before the newline, respectively. Also, the
4587 @samp{.} and @samp{[^...]} operators will never match a newline
4588 character. The intent of this flag is to treat the target
4589 string as a buffer containing many lines of text, and the
4590 regular expression as a pattern that may match a single one of
4591 those lines.
4592 @end defvar
4593
4594 @defvar regexp/basic
4595 Compile a basic (``obsolete'') regexp instead of the extended
4596 (``modern'') regexps that are the default. Basic regexps do
4597 not consider @samp{|}, @samp{+} or @samp{?} to be special
4598 characters, and require the @samp{@{...@}} and @samp{(...)}
4599 metacharacters to be backslash-escaped (@pxref{Backslash
4600 Escapes}). There are several other differences between basic
4601 and extended regular expressions, but these are the most
4602 significant.
4603 @end defvar
4604
4605 @defvar regexp/extended
4606 Compile an extended regular expression rather than a basic
4607 regexp. This is the default behavior; this flag will not
4608 usually be needed. If a call to @code{make-regexp} includes
4609 both @code{regexp/basic} and @code{regexp/extended} flags, the
4610 one which comes last will override the earlier one.
4611 @end defvar
4612 @end deffn
4613
4614 @deffn {Scheme Procedure} regexp-exec rx str [start [flags]]
4615 @deffnx {C Function} scm_regexp_exec (rx, str, start, flags)
4616 Match the compiled regular expression @var{rx} against
4617 @code{str}. If the optional integer @var{start} argument is
4618 provided, begin matching from that position in the string.
4619 Return a match structure describing the results of the match,
4620 or @code{#f} if no match could be found.
4621
4622 The @var{flags} argument changes the matching behavior. The following
4623 flag values may be supplied, use @code{logior} (@pxref{Bitwise
4624 Operations}) to combine them,
4625
4626 @defvar regexp/notbol
4627 Consider that the @var{start} offset into @var{str} is not the
4628 beginning of a line and should not match operator @samp{^}.
4629
4630 If @var{rx} was created with the @code{regexp/newline} option above,
4631 @samp{^} will still match after a newline in @var{str}.
4632 @end defvar
4633
4634 @defvar regexp/noteol
4635 Consider that the end of @var{str} is not the end of a line and should
4636 not match operator @samp{$}.
4637
4638 If @var{rx} was created with the @code{regexp/newline} option above,
4639 @samp{$} will still match before a newline in @var{str}.
4640 @end defvar
4641 @end deffn
4642
4643 @lisp
4644 ;; Regexp to match uppercase letters
4645 (define r (make-regexp "[A-Z]*"))
4646
4647 ;; Regexp to match letters, ignoring case
4648 (define ri (make-regexp "[A-Z]*" regexp/icase))
4649
4650 ;; Search for bob using regexp r
4651 (match:substring (regexp-exec r "bob"))
4652 @result{} "" ; no match
4653
4654 ;; Search for bob using regexp ri
4655 (match:substring (regexp-exec ri "Bob"))
4656 @result{} "Bob" ; matched case insensitive
4657 @end lisp
4658
4659 @deffn {Scheme Procedure} regexp? obj
4660 @deffnx {C Function} scm_regexp_p (obj)
4661 Return @code{#t} if @var{obj} is a compiled regular expression,
4662 or @code{#f} otherwise.
4663 @end deffn
4664
4665 @sp 1
4666 @deffn {Scheme Procedure} list-matches regexp str [flags]
4667 Return a list of match structures which are the non-overlapping
4668 matches of @var{regexp} in @var{str}. @var{regexp} can be either a
4669 pattern string or a compiled regexp. The @var{flags} argument is as
4670 per @code{regexp-exec} above.
4671
4672 @example
4673 (map match:substring (list-matches "[a-z]+" "abc 42 def 78"))
4674 @result{} ("abc" "def")
4675 @end example
4676 @end deffn
4677
4678 @deffn {Scheme Procedure} fold-matches regexp str init proc [flags]
4679 Apply @var{proc} to the non-overlapping matches of @var{regexp} in
4680 @var{str}, to build a result. @var{regexp} can be either a pattern
4681 string or a compiled regexp. The @var{flags} argument is as per
4682 @code{regexp-exec} above.
4683
4684 @var{proc} is called as @code{(@var{proc} match prev)} where
4685 @var{match} is a match structure and @var{prev} is the previous return
4686 from @var{proc}. For the first call @var{prev} is the given
4687 @var{init} parameter. @code{fold-matches} returns the final value
4688 from @var{proc}.
4689
4690 For example to count matches,
4691
4692 @example
4693 (fold-matches "[a-z][0-9]" "abc x1 def y2" 0
4694 (lambda (match count)
4695 (1+ count)))
4696 @result{} 2
4697 @end example
4698 @end deffn
4699
4700 @sp 1
4701 Regular expressions are commonly used to find patterns in one string
4702 and replace them with the contents of another string. The following
4703 functions are convenient ways to do this.
4704
4705 @c begin (scm-doc-string "regex.scm" "regexp-substitute")
4706 @deffn {Scheme Procedure} regexp-substitute port match [item@dots{}]
4707 Write to @var{port} selected parts of the match structure @var{match}.
4708 Or if @var{port} is @code{#f} then form a string from those parts and
4709 return that.
4710
4711 Each @var{item} specifies a part to be written, and may be one of the
4712 following,
4713
4714 @itemize @bullet
4715 @item
4716 A string. String arguments are written out verbatim.
4717
4718 @item
4719 An integer. The submatch with that number is written
4720 (@code{match:substring}). Zero is the entire match.
4721
4722 @item
4723 The symbol @samp{pre}. The portion of the matched string preceding
4724 the regexp match is written (@code{match:prefix}).
4725
4726 @item
4727 The symbol @samp{post}. The portion of the matched string following
4728 the regexp match is written (@code{match:suffix}).
4729 @end itemize
4730
4731 For example, changing a match and retaining the text before and after,
4732
4733 @example
4734 (regexp-substitute #f (string-match "[0-9]+" "number 25 is good")
4735 'pre "37" 'post)
4736 @result{} "number 37 is good"
4737 @end example
4738
4739 Or matching a @sc{yyyymmdd} format date such as @samp{20020828} and
4740 re-ordering and hyphenating the fields.
4741
4742 @lisp
4743 (define date-regex
4744 "([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])")
4745 (define s "Date 20020429 12am.")
4746 (regexp-substitute #f (string-match date-regex s)
4747 'pre 2 "-" 3 "-" 1 'post " (" 0 ")")
4748 @result{} "Date 04-29-2002 12am. (20020429)"
4749 @end lisp
4750 @end deffn
4751
4752
4753 @c begin (scm-doc-string "regex.scm" "regexp-substitute")
4754 @deffn {Scheme Procedure} regexp-substitute/global port regexp target [item@dots{}]
4755 @cindex search and replace
4756 Write to @var{port} selected parts of matches of @var{regexp} in
4757 @var{target}. If @var{port} is @code{#f} then form a string from
4758 those parts and return that. @var{regexp} can be a string or a
4759 compiled regex.
4760
4761 This is similar to @code{regexp-substitute}, but allows global
4762 substitutions on @var{target}. Each @var{item} behaves as per
4763 @code{regexp-substitute}, with the following differences,
4764
4765 @itemize @bullet
4766 @item
4767 A function. Called as @code{(@var{item} match)} with the match
4768 structure for the @var{regexp} match, it should return a string to be
4769 written to @var{port}.
4770
4771 @item
4772 The symbol @samp{post}. This doesn't output anything, but instead
4773 causes @code{regexp-substitute/global} to recurse on the unmatched
4774 portion of @var{target}.
4775
4776 This @emph{must} be supplied to perform a global search and replace on
4777 @var{target}; without it @code{regexp-substitute/global} returns after
4778 a single match and output.
4779 @end itemize
4780
4781 For example, to collapse runs of tabs and spaces to a single hyphen
4782 each,
4783
4784 @example
4785 (regexp-substitute/global #f "[ \t]+" "this is the text"
4786 'pre "-" 'post)
4787 @result{} "this-is-the-text"
4788 @end example
4789
4790 Or using a function to reverse the letters in each word,
4791
4792 @example
4793 (regexp-substitute/global #f "[a-z]+" "to do and not-do"
4794 'pre (lambda (m) (string-reverse (match:substring m))) 'post)
4795 @result{} "ot od dna ton-od"
4796 @end example
4797
4798 Without the @code{post} symbol, just one regexp match is made. For
4799 example the following is the date example from
4800 @code{regexp-substitute} above, without the need for the separate
4801 @code{string-match} call.
4802
4803 @lisp
4804 (define date-regex
4805 "([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])")
4806 (define s "Date 20020429 12am.")
4807 (regexp-substitute/global #f date-regex s
4808 'pre 2 "-" 3 "-" 1 'post " (" 0 ")")
4809
4810 @result{} "Date 04-29-2002 12am. (20020429)"
4811 @end lisp
4812 @end deffn
4813
4814
4815 @node Match Structures
4816 @subsubsection Match Structures
4817
4818 @cindex match structures
4819
4820 A @dfn{match structure} is the object returned by @code{string-match} and
4821 @code{regexp-exec}. It describes which portion of a string, if any,
4822 matched the given regular expression. Match structures include: a
4823 reference to the string that was checked for matches; the starting and
4824 ending positions of the regexp match; and, if the regexp included any
4825 parenthesized subexpressions, the starting and ending positions of each
4826 submatch.
4827
4828 In each of the regexp match functions described below, the @code{match}
4829 argument must be a match structure returned by a previous call to
4830 @code{string-match} or @code{regexp-exec}. Most of these functions
4831 return some information about the original target string that was
4832 matched against a regular expression; we will call that string
4833 @var{target} for easy reference.
4834
4835 @c begin (scm-doc-string "regex.scm" "regexp-match?")
4836 @deffn {Scheme Procedure} regexp-match? obj
4837 Return @code{#t} if @var{obj} is a match structure returned by a
4838 previous call to @code{regexp-exec}, or @code{#f} otherwise.
4839 @end deffn
4840
4841 @c begin (scm-doc-string "regex.scm" "match:substring")
4842 @deffn {Scheme Procedure} match:substring match [n]
4843 Return the portion of @var{target} matched by subexpression number
4844 @var{n}. Submatch 0 (the default) represents the entire regexp match.
4845 If the regular expression as a whole matched, but the subexpression
4846 number @var{n} did not match, return @code{#f}.
4847 @end deffn
4848
4849 @lisp
4850 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4851 (match:substring s)
4852 @result{} "2002"
4853
4854 ;; match starting at offset 6 in the string
4855 (match:substring
4856 (string-match "[0-9][0-9][0-9][0-9]" "blah987654" 6))
4857 @result{} "7654"
4858 @end lisp
4859
4860 @c begin (scm-doc-string "regex.scm" "match:start")
4861 @deffn {Scheme Procedure} match:start match [n]
4862 Return the starting position of submatch number @var{n}.
4863 @end deffn
4864
4865 In the following example, the result is 4, since the match starts at
4866 character index 4:
4867
4868 @lisp
4869 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4870 (match:start s)
4871 @result{} 4
4872 @end lisp
4873
4874 @c begin (scm-doc-string "regex.scm" "match:end")
4875 @deffn {Scheme Procedure} match:end match [n]
4876 Return the ending position of submatch number @var{n}.
4877 @end deffn
4878
4879 In the following example, the result is 8, since the match runs between
4880 characters 4 and 8 (i.e. the ``2002'').
4881
4882 @lisp
4883 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4884 (match:end s)
4885 @result{} 8
4886 @end lisp
4887
4888 @c begin (scm-doc-string "regex.scm" "match:prefix")
4889 @deffn {Scheme Procedure} match:prefix match
4890 Return the unmatched portion of @var{target} preceding the regexp match.
4891
4892 @lisp
4893 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4894 (match:prefix s)
4895 @result{} "blah"
4896 @end lisp
4897 @end deffn
4898
4899 @c begin (scm-doc-string "regex.scm" "match:suffix")
4900 @deffn {Scheme Procedure} match:suffix match
4901 Return the unmatched portion of @var{target} following the regexp match.
4902 @end deffn
4903
4904 @lisp
4905 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4906 (match:suffix s)
4907 @result{} "foo"
4908 @end lisp
4909
4910 @c begin (scm-doc-string "regex.scm" "match:count")
4911 @deffn {Scheme Procedure} match:count match
4912 Return the number of parenthesized subexpressions from @var{match}.
4913 Note that the entire regular expression match itself counts as a
4914 subexpression, and failed submatches are included in the count.
4915 @end deffn
4916
4917 @c begin (scm-doc-string "regex.scm" "match:string")
4918 @deffn {Scheme Procedure} match:string match
4919 Return the original @var{target} string.
4920 @end deffn
4921
4922 @lisp
4923 (define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
4924 (match:string s)
4925 @result{} "blah2002foo"
4926 @end lisp
4927
4928
4929 @node Backslash Escapes
4930 @subsubsection Backslash Escapes
4931
4932 Sometimes you will want a regexp to match characters like @samp{*} or
4933 @samp{$} exactly. For example, to check whether a particular string
4934 represents a menu entry from an Info node, it would be useful to match
4935 it against a regexp like @samp{^* [^:]*::}. However, this won't work;
4936 because the asterisk is a metacharacter, it won't match the @samp{*} at
4937 the beginning of the string. In this case, we want to make the first
4938 asterisk un-magic.
4939
4940 You can do this by preceding the metacharacter with a backslash
4941 character @samp{\}. (This is also called @dfn{quoting} the
4942 metacharacter, and is known as a @dfn{backslash escape}.) When Guile
4943 sees a backslash in a regular expression, it considers the following
4944 glyph to be an ordinary character, no matter what special meaning it
4945 would ordinarily have. Therefore, we can make the above example work by
4946 changing the regexp to @samp{^\* [^:]*::}. The @samp{\*} sequence tells
4947 the regular expression engine to match only a single asterisk in the
4948 target string.
4949
4950 Since the backslash is itself a metacharacter, you may force a regexp to
4951 match a backslash in the target string by preceding the backslash with
4952 itself. For example, to find variable references in a @TeX{} program,
4953 you might want to find occurrences of the string @samp{\let\} followed
4954 by any number of alphabetic characters. The regular expression
4955 @samp{\\let\\[A-Za-z]*} would do this: the double backslashes in the
4956 regexp each match a single backslash in the target string.
4957
4958 @c begin (scm-doc-string "regex.scm" "regexp-quote")
4959 @deffn {Scheme Procedure} regexp-quote str
4960 Quote each special character found in @var{str} with a backslash, and
4961 return the resulting string.
4962 @end deffn
4963
4964 @strong{Very important:} Using backslash escapes in Guile source code
4965 (as in Emacs Lisp or C) can be tricky, because the backslash character
4966 has special meaning for the Guile reader. For example, if Guile
4967 encounters the character sequence @samp{\n} in the middle of a string
4968 while processing Scheme code, it replaces those characters with a
4969 newline character. Similarly, the character sequence @samp{\t} is
4970 replaced by a horizontal tab. Several of these @dfn{escape sequences}
4971 are processed by the Guile reader before your code is executed.
4972 Unrecognized escape sequences are ignored: if the characters @samp{\*}
4973 appear in a string, they will be translated to the single character
4974 @samp{*}.
4975
4976 This translation is obviously undesirable for regular expressions, since
4977 we want to be able to include backslashes in a string in order to
4978 escape regexp metacharacters. Therefore, to make sure that a backslash
4979 is preserved in a string in your Guile program, you must use @emph{two}
4980 consecutive backslashes:
4981
4982 @lisp
4983 (define Info-menu-entry-pattern (make-regexp "^\\* [^:]*"))
4984 @end lisp
4985
4986 The string in this example is preprocessed by the Guile reader before
4987 any code is executed. The resulting argument to @code{make-regexp} is
4988 the string @samp{^\* [^:]*}, which is what we really want.
4989
4990 This also means that in order to write a regular expression that matches
4991 a single backslash character, the regular expression string in the
4992 source code must include @emph{four} backslashes. Each consecutive pair
4993 of backslashes gets translated by the Guile reader to a single
4994 backslash, and the resulting double-backslash is interpreted by the
4995 regexp engine as matching a single backslash character. Hence:
4996
4997 @lisp
4998 (define tex-variable-pattern (make-regexp "\\\\let\\\\=[A-Za-z]*"))
4999 @end lisp
5000
5001 The reason for the unwieldiness of this syntax is historical. Both
5002 regular expression pattern matchers and Unix string processing systems
5003 have traditionally used backslashes with the special meanings
5004 described above. The POSIX regular expression specification and ANSI C
5005 standard both require these semantics. Attempting to abandon either
5006 convention would cause other kinds of compatibility problems, possibly
5007 more severe ones. Therefore, without extending the Scheme reader to
5008 support strings with different quoting conventions (an ungainly and
5009 confusing extension when implemented in other languages), we must adhere
5010 to this cumbersome escape syntax.
5011
5012
5013 @node Symbols
5014 @subsection Symbols
5015 @tpindex Symbols
5016
5017 Symbols in Scheme are widely used in three ways: as items of discrete
5018 data, as lookup keys for alists and hash tables, and to denote variable
5019 references.
5020
5021 A @dfn{symbol} is similar to a string in that it is defined by a
5022 sequence of characters. The sequence of characters is known as the
5023 symbol's @dfn{name}. In the usual case --- that is, where the symbol's
5024 name doesn't include any characters that could be confused with other
5025 elements of Scheme syntax --- a symbol is written in a Scheme program by
5026 writing the sequence of characters that make up the name, @emph{without}
5027 any quotation marks or other special syntax. For example, the symbol
5028 whose name is ``multiply-by-2'' is written, simply:
5029
5030 @lisp
5031 multiply-by-2
5032 @end lisp
5033
5034 Notice how this differs from a @emph{string} with contents
5035 ``multiply-by-2'', which is written with double quotation marks, like
5036 this:
5037
5038 @lisp
5039 "multiply-by-2"
5040 @end lisp
5041
5042 Looking beyond how they are written, symbols are different from strings
5043 in two important respects.
5044
5045 The first important difference is uniqueness. If the same-looking
5046 string is read twice from two different places in a program, the result
5047 is two @emph{different} string objects whose contents just happen to be
5048 the same. If, on the other hand, the same-looking symbol is read twice
5049 from two different places in a program, the result is the @emph{same}
5050 symbol object both times.
5051
5052 Given two read symbols, you can use @code{eq?} to test whether they are
5053 the same (that is, have the same name). @code{eq?} is the most
5054 efficient comparison operator in Scheme, and comparing two symbols like
5055 this is as fast as comparing, for example, two numbers. Given two
5056 strings, on the other hand, you must use @code{equal?} or
5057 @code{string=?}, which are much slower comparison operators, to
5058 determine whether the strings have the same contents.
5059
5060 @lisp
5061 (define sym1 (quote hello))
5062 (define sym2 (quote hello))
5063 (eq? sym1 sym2) @result{} #t
5064
5065 (define str1 "hello")
5066 (define str2 "hello")
5067 (eq? str1 str2) @result{} #f
5068 (equal? str1 str2) @result{} #t
5069 @end lisp
5070
5071 The second important difference is that symbols, unlike strings, are not
5072 self-evaluating. This is why we need the @code{(quote @dots{})}s in the
5073 example above: @code{(quote hello)} evaluates to the symbol named
5074 "hello" itself, whereas an unquoted @code{hello} is @emph{read} as the
5075 symbol named "hello" and evaluated as a variable reference @dots{} about
5076 which more below (@pxref{Symbol Variables}).
5077
5078 @menu
5079 * Symbol Data:: Symbols as discrete data.
5080 * Symbol Keys:: Symbols as lookup keys.
5081 * Symbol Variables:: Symbols as denoting variables.
5082 * Symbol Primitives:: Operations related to symbols.
5083 * Symbol Props:: Function slots and property lists.
5084 * Symbol Read Syntax:: Extended read syntax for symbols.
5085 * Symbol Uninterned:: Uninterned symbols.
5086 @end menu
5087
5088
5089 @node Symbol Data
5090 @subsubsection Symbols as Discrete Data
5091
5092 Numbers and symbols are similar to the extent that they both lend
5093 themselves to @code{eq?} comparison. But symbols are more descriptive
5094 than numbers, because a symbol's name can be used directly to describe
5095 the concept for which that symbol stands.
5096
5097 For example, imagine that you need to represent some colours in a
5098 computer program. Using numbers, you would have to choose arbitrarily
5099 some mapping between numbers and colours, and then take care to use that
5100 mapping consistently:
5101
5102 @lisp
5103 ;; 1=red, 2=green, 3=purple
5104
5105 (if (eq? (colour-of car) 1)
5106 ...)
5107 @end lisp
5108
5109 @noindent
5110 You can make the mapping more explicit and the code more readable by
5111 defining constants:
5112
5113 @lisp
5114 (define red 1)
5115 (define green 2)
5116 (define purple 3)
5117
5118 (if (eq? (colour-of car) red)
5119 ...)
5120 @end lisp
5121
5122 @noindent
5123 But the simplest and clearest approach is not to use numbers at all, but
5124 symbols whose names specify the colours that they refer to:
5125
5126 @lisp
5127 (if (eq? (colour-of car) 'red)
5128 ...)
5129 @end lisp
5130
5131 The descriptive advantages of symbols over numbers increase as the set
5132 of concepts that you want to describe grows. Suppose that a car object
5133 can have other properties as well, such as whether it has or uses:
5134
5135 @itemize @bullet
5136 @item
5137 automatic or manual transmission
5138 @item
5139 leaded or unleaded fuel
5140 @item
5141 power steering (or not).
5142 @end itemize
5143
5144 @noindent
5145 Then a car's combined property set could be naturally represented and
5146 manipulated as a list of symbols:
5147
5148 @lisp
5149 (properties-of car1)
5150 @result{}
5151 (red manual unleaded power-steering)
5152
5153 (if (memq 'power-steering (properties-of car1))
5154 (display "Unfit people can drive this car.\n")
5155 (display "You'll need strong arms to drive this car!\n"))
5156 @print{}
5157 Unfit people can drive this car.
5158 @end lisp
5159
5160 Remember, the fundamental property of symbols that we are relying on
5161 here is that an occurrence of @code{'red} in one part of a program is an
5162 @emph{indistinguishable} symbol from an occurrence of @code{'red} in
5163 another part of a program; this means that symbols can usefully be
5164 compared using @code{eq?}. At the same time, symbols have naturally
5165 descriptive names. This combination of efficiency and descriptive power
5166 makes them ideal for use as discrete data.
5167
5168
5169 @node Symbol Keys
5170 @subsubsection Symbols as Lookup Keys
5171
5172 Given their efficiency and descriptive power, it is natural to use
5173 symbols as the keys in an association list or hash table.
5174
5175 To illustrate this, consider a more structured representation of the car
5176 properties example from the preceding subsection. Rather than
5177 mixing all the properties up together in a flat list, we could use an
5178 association list like this:
5179
5180 @lisp
5181 (define car1-properties '((colour . red)
5182 (transmission . manual)
5183 (fuel . unleaded)
5184 (steering . power-assisted)))
5185 @end lisp
5186
5187 Notice how this structure is more explicit and extensible than the flat
5188 list. For example it makes clear that @code{manual} refers to the
5189 transmission rather than, say, the windows or the locking of the car.
5190 It also allows further properties to use the same symbols among their
5191 possible values without becoming ambiguous:
5192
5193 @lisp
5194 (define car1-properties '((colour . red)
5195 (transmission . manual)
5196 (fuel . unleaded)
5197 (steering . power-assisted)
5198 (seat-colour . red)
5199 (locking . manual)))
5200 @end lisp
5201
5202 With a representation like this, it is easy to use the efficient
5203 @code{assq-XXX} family of procedures (@pxref{Association Lists}) to
5204 extract or change individual pieces of information:
5205
5206 @lisp
5207 (assq-ref car1-properties 'fuel) @result{} unleaded
5208 (assq-ref car1-properties 'transmission) @result{} manual
5209
5210 (assq-set! car1-properties 'seat-colour 'black)
5211 @result{}
5212 ((colour . red)
5213 (transmission . manual)
5214 (fuel . unleaded)
5215 (steering . power-assisted)
5216 (seat-colour . black)
5217 (locking . manual)))
5218 @end lisp
5219
5220 Hash tables also have keys, and exactly the same arguments apply to the
5221 use of symbols in hash tables as in association lists. The hash value
5222 that Guile uses to decide where to add a symbol-keyed entry to a hash
5223 table can be obtained by calling the @code{symbol-hash} procedure:
5224
5225 @deffn {Scheme Procedure} symbol-hash symbol
5226 @deffnx {C Function} scm_symbol_hash (symbol)
5227 Return a hash value for @var{symbol}.
5228 @end deffn
5229
5230 See @ref{Hash Tables} for information about hash tables in general, and
5231 for why you might choose to use a hash table rather than an association
5232 list.
5233
5234
5235 @node Symbol Variables
5236 @subsubsection Symbols as Denoting Variables
5237
5238 When an unquoted symbol in a Scheme program is evaluated, it is
5239 interpreted as a variable reference, and the result of the evaluation is
5240 the appropriate variable's value.
5241
5242 For example, when the expression @code{(string-length "abcd")} is read
5243 and evaluated, the sequence of characters @code{string-length} is read
5244 as the symbol whose name is "string-length". This symbol is associated
5245 with a variable whose value is the procedure that implements string
5246 length calculation. Therefore evaluation of the @code{string-length}
5247 symbol results in that procedure.
5248
5249 The details of the connection between an unquoted symbol and the
5250 variable to which it refers are explained elsewhere. See @ref{Binding
5251 Constructs}, for how associations between symbols and variables are
5252 created, and @ref{Modules}, for how those associations are affected by
5253 Guile's module system.
5254
5255
5256 @node Symbol Primitives
5257 @subsubsection Operations Related to Symbols
5258
5259 Given any Scheme value, you can determine whether it is a symbol using
5260 the @code{symbol?} primitive:
5261
5262 @rnindex symbol?
5263 @deffn {Scheme Procedure} symbol? obj
5264 @deffnx {C Function} scm_symbol_p (obj)
5265 Return @code{#t} if @var{obj} is a symbol, otherwise return
5266 @code{#f}.
5267 @end deffn
5268
5269 @deftypefn {C Function} int scm_is_symbol (SCM val)
5270 Equivalent to @code{scm_is_true (scm_symbol_p (val))}.
5271 @end deftypefn
5272
5273 Once you know that you have a symbol, you can obtain its name as a
5274 string by calling @code{symbol->string}. Note that Guile differs by
5275 default from R5RS on the details of @code{symbol->string} as regards
5276 case-sensitivity:
5277
5278 @rnindex symbol->string
5279 @deffn {Scheme Procedure} symbol->string s
5280 @deffnx {C Function} scm_symbol_to_string (s)
5281 Return the name of symbol @var{s} as a string. By default, Guile reads
5282 symbols case-sensitively, so the string returned will have the same case
5283 variation as the sequence of characters that caused @var{s} to be
5284 created.
5285
5286 If Guile is set to read symbols case-insensitively (as specified by
5287 R5RS), and @var{s} comes into being as part of a literal expression
5288 (@pxref{Literal expressions,,,r5rs, The Revised^5 Report on Scheme}) or
5289 by a call to the @code{read} or @code{string-ci->symbol} procedures,
5290 Guile converts any alphabetic characters in the symbol's name to
5291 lower case before creating the symbol object, so the string returned
5292 here will be in lower case.
5293
5294 If @var{s} was created by @code{string->symbol}, the case of characters
5295 in the string returned will be the same as that in the string that was
5296 passed to @code{string->symbol}, regardless of Guile's case-sensitivity
5297 setting at the time @var{s} was created.
5298
5299 It is an error to apply mutation procedures like @code{string-set!} to
5300 strings returned by this procedure.
5301 @end deffn
5302
5303 Most symbols are created by writing them literally in code. However it
5304 is also possible to create symbols programmatically using the following
5305 @code{string->symbol} and @code{string-ci->symbol} procedures:
5306
5307 @rnindex string->symbol
5308 @deffn {Scheme Procedure} string->symbol string
5309 @deffnx {C Function} scm_string_to_symbol (string)
5310 Return the symbol whose name is @var{string}. This procedure can create
5311 symbols with names containing special characters or letters in the
5312 non-standard case, but it is usually a bad idea to create such symbols
5313 because in some implementations of Scheme they cannot be read as
5314 themselves.
5315 @end deffn
5316
5317 @deffn {Scheme Procedure} string-ci->symbol str
5318 @deffnx {C Function} scm_string_ci_to_symbol (str)
5319 Return the symbol whose name is @var{str}. If Guile is currently
5320 reading symbols case-insensitively, @var{str} is converted to lowercase
5321 before the returned symbol is looked up or created.
5322 @end deffn
5323
5324 The following examples illustrate Guile's detailed behaviour as regards
5325 the case-sensitivity of symbols:
5326
5327 @lisp
5328 (read-enable 'case-insensitive) ; R5RS compliant behaviour
5329
5330 (symbol->string 'flying-fish) @result{} "flying-fish"
5331 (symbol->string 'Martin) @result{} "martin"
5332 (symbol->string
5333 (string->symbol "Malvina")) @result{} "Malvina"
5334
5335 (eq? 'mISSISSIppi 'mississippi) @result{} #t
5336 (string->symbol "mISSISSIppi") @result{} mISSISSIppi
5337 (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
5338 (eq? 'LolliPop
5339 (string->symbol (symbol->string 'LolliPop))) @result{} #t
5340 (string=? "K. Harper, M.D."
5341 (symbol->string
5342 (string->symbol "K. Harper, M.D."))) @result{} #t
5343
5344 (read-disable 'case-insensitive) ; Guile default behaviour
5345
5346 (symbol->string 'flying-fish) @result{} "flying-fish"
5347 (symbol->string 'Martin) @result{} "Martin"
5348 (symbol->string
5349 (string->symbol "Malvina")) @result{} "Malvina"
5350
5351 (eq? 'mISSISSIppi 'mississippi) @result{} #f
5352 (string->symbol "mISSISSIppi") @result{} mISSISSIppi
5353 (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #t
5354 (eq? 'LolliPop
5355 (string->symbol (symbol->string 'LolliPop))) @result{} #t
5356 (string=? "K. Harper, M.D."
5357 (symbol->string
5358 (string->symbol "K. Harper, M.D."))) @result{} #t
5359 @end lisp
5360
5361 From C, there are lower level functions that construct a Scheme symbol
5362 from a C string in the current locale encoding.
5363
5364 When you want to do more from C, you should convert between symbols
5365 and strings using @code{scm_symbol_to_string} and
5366 @code{scm_string_to_symbol} and work with the strings.
5367
5368 @deffn {C Function} scm_from_locale_symbol (const char *name)
5369 @deffnx {C Function} scm_from_locale_symboln (const char *name, size_t len)
5370 Construct and return a Scheme symbol whose name is specified by
5371 @var{name}. For @code{scm_from_locale_symbol}, @var{name} must be null
5372 terminated; for @code{scm_from_locale_symboln} the length of @var{name} is
5373 specified explicitly by @var{len}.
5374 @end deffn
5375
5376 @deftypefn {C Function} SCM scm_take_locale_symbol (char *str)
5377 @deftypefnx {C Function} SCM scm_take_locale_symboln (char *str, size_t len)
5378 Like @code{scm_from_locale_symbol} and @code{scm_from_locale_symboln},
5379 respectively, but also frees @var{str} with @code{free} eventually.
5380 Thus, you can use this function when you would free @var{str} anyway
5381 immediately after creating the Scheme string. In certain cases, Guile
5382 can then use @var{str} directly as its internal representation.
5383 @end deftypefn
5384
5385 The size of a symbol can also be obtained from C:
5386
5387 @deftypefn {C Function} size_t scm_c_symbol_length (SCM sym)
5388 Return the number of characters in @var{sym}.
5389 @end deftypefn
5390
5391 Finally, some applications, especially those that generate new Scheme
5392 code dynamically, need to generate symbols for use in the generated
5393 code. The @code{gensym} primitive meets this need:
5394
5395 @deffn {Scheme Procedure} gensym [prefix]
5396 @deffnx {C Function} scm_gensym (prefix)
5397 Create a new symbol with a name constructed from a prefix and a counter
5398 value. The string @var{prefix} can be specified as an optional
5399 argument. Default prefix is @samp{@w{ g}}. The counter is increased by 1
5400 at each call. There is no provision for resetting the counter.
5401 @end deffn
5402
5403 The symbols generated by @code{gensym} are @emph{likely} to be unique,
5404 since their names begin with a space and it is only otherwise possible
5405 to generate such symbols if a programmer goes out of their way to do
5406 so. Uniqueness can be guaranteed by instead using uninterned symbols
5407 (@pxref{Symbol Uninterned}), though they can't be usefully written out
5408 and read back in.
5409
5410
5411 @node Symbol Props
5412 @subsubsection Function Slots and Property Lists
5413
5414 In traditional Lisp dialects, symbols are often understood as having
5415 three kinds of value at once:
5416
5417 @itemize @bullet
5418 @item
5419 a @dfn{variable} value, which is used when the symbol appears in
5420 code in a variable reference context
5421
5422 @item
5423 a @dfn{function} value, which is used when the symbol appears in
5424 code in a function name position (i.e. as the first element in an
5425 unquoted list)
5426
5427 @item
5428 a @dfn{property list} value, which is used when the symbol is given as
5429 the first argument to Lisp's @code{put} or @code{get} functions.
5430 @end itemize
5431
5432 Although Scheme (as one of its simplifications with respect to Lisp)
5433 does away with the distinction between variable and function namespaces,
5434 Guile currently retains some elements of the traditional structure in
5435 case they turn out to be useful when implementing translators for other
5436 languages, in particular Emacs Lisp.
5437
5438 Specifically, Guile symbols have two extra slots. for a symbol's
5439 property list, and for its ``function value.'' The following procedures
5440 are provided to access these slots.
5441
5442 @deffn {Scheme Procedure} symbol-fref symbol
5443 @deffnx {C Function} scm_symbol_fref (symbol)
5444 Return the contents of @var{symbol}'s @dfn{function slot}.
5445 @end deffn
5446
5447 @deffn {Scheme Procedure} symbol-fset! symbol value
5448 @deffnx {C Function} scm_symbol_fset_x (symbol, value)
5449 Set the contents of @var{symbol}'s function slot to @var{value}.
5450 @end deffn
5451
5452 @deffn {Scheme Procedure} symbol-pref symbol
5453 @deffnx {C Function} scm_symbol_pref (symbol)
5454 Return the @dfn{property list} currently associated with @var{symbol}.
5455 @end deffn
5456
5457 @deffn {Scheme Procedure} symbol-pset! symbol value
5458 @deffnx {C Function} scm_symbol_pset_x (symbol, value)
5459 Set @var{symbol}'s property list to @var{value}.
5460 @end deffn
5461
5462 @deffn {Scheme Procedure} symbol-property sym prop
5463 From @var{sym}'s property list, return the value for property
5464 @var{prop}. The assumption is that @var{sym}'s property list is an
5465 association list whose keys are distinguished from each other using
5466 @code{equal?}; @var{prop} should be one of the keys in that list. If
5467 the property list has no entry for @var{prop}, @code{symbol-property}
5468 returns @code{#f}.
5469 @end deffn
5470
5471 @deffn {Scheme Procedure} set-symbol-property! sym prop val
5472 In @var{sym}'s property list, set the value for property @var{prop} to
5473 @var{val}, or add a new entry for @var{prop}, with value @var{val}, if
5474 none already exists. For the structure of the property list, see
5475 @code{symbol-property}.
5476 @end deffn
5477
5478 @deffn {Scheme Procedure} symbol-property-remove! sym prop
5479 From @var{sym}'s property list, remove the entry for property
5480 @var{prop}, if there is one. For the structure of the property list,
5481 see @code{symbol-property}.
5482 @end deffn
5483
5484 Support for these extra slots may be removed in a future release, and it
5485 is probably better to avoid using them. For a more modern and Schemely
5486 approach to properties, see @ref{Object Properties}.
5487
5488
5489 @node Symbol Read Syntax
5490 @subsubsection Extended Read Syntax for Symbols
5491
5492 The read syntax for a symbol is a sequence of letters, digits, and
5493 @dfn{extended alphabetic characters}, beginning with a character that
5494 cannot begin a number. In addition, the special cases of @code{+},
5495 @code{-}, and @code{...} are read as symbols even though numbers can
5496 begin with @code{+}, @code{-} or @code{.}.
5497
5498 Extended alphabetic characters may be used within identifiers as if
5499 they were letters. The set of extended alphabetic characters is:
5500
5501 @example
5502 ! $ % & * + - . / : < = > ? @@ ^ _ ~
5503 @end example
5504
5505 In addition to the standard read syntax defined above (which is taken
5506 from R5RS (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on
5507 Scheme})), Guile provides an extended symbol read syntax that allows the
5508 inclusion of unusual characters such as space characters, newlines and
5509 parentheses. If (for whatever reason) you need to write a symbol
5510 containing characters not mentioned above, you can do so as follows.
5511
5512 @itemize @bullet
5513 @item
5514 Begin the symbol with the characters @code{#@{},
5515
5516 @item
5517 write the characters of the symbol and
5518
5519 @item
5520 finish the symbol with the characters @code{@}#}.
5521 @end itemize
5522
5523 Here are a few examples of this form of read syntax. The first symbol
5524 needs to use extended syntax because it contains a space character, the
5525 second because it contains a line break, and the last because it looks
5526 like a number.
5527
5528 @lisp
5529 #@{foo bar@}#
5530
5531 #@{what
5532 ever@}#
5533
5534 #@{4242@}#
5535 @end lisp
5536
5537 Although Guile provides this extended read syntax for symbols,
5538 widespread usage of it is discouraged because it is not portable and not
5539 very readable.
5540
5541
5542 @node Symbol Uninterned
5543 @subsubsection Uninterned Symbols
5544
5545 What makes symbols useful is that they are automatically kept unique.
5546 There are no two symbols that are distinct objects but have the same
5547 name. But of course, there is no rule without exception. In addition
5548 to the normal symbols that have been discussed up to now, you can also
5549 create special @dfn{uninterned} symbols that behave slightly
5550 differently.
5551
5552 To understand what is different about them and why they might be useful,
5553 we look at how normal symbols are actually kept unique.
5554
5555 Whenever Guile wants to find the symbol with a specific name, for
5556 example during @code{read} or when executing @code{string->symbol}, it
5557 first looks into a table of all existing symbols to find out whether a
5558 symbol with the given name already exists. When this is the case, Guile
5559 just returns that symbol. When not, a new symbol with the name is
5560 created and entered into the table so that it can be found later.
5561
5562 Sometimes you might want to create a symbol that is guaranteed `fresh',
5563 i.e. a symbol that did not exist previously. You might also want to
5564 somehow guarantee that no one else will ever unintentionally stumble
5565 across your symbol in the future. These properties of a symbol are
5566 often needed when generating code during macro expansion. When
5567 introducing new temporary variables, you want to guarantee that they
5568 don't conflict with variables in other people's code.
5569
5570 The simplest way to arrange for this is to create a new symbol but
5571 not enter it into the global table of all symbols. That way, no one
5572 will ever get access to your symbol by chance. Symbols that are not in
5573 the table are called @dfn{uninterned}. Of course, symbols that
5574 @emph{are} in the table are called @dfn{interned}.
5575
5576 You create new uninterned symbols with the function @code{make-symbol}.
5577 You can test whether a symbol is interned or not with
5578 @code{symbol-interned?}.
5579
5580 Uninterned symbols break the rule that the name of a symbol uniquely
5581 identifies the symbol object. Because of this, they can not be written
5582 out and read back in like interned symbols. Currently, Guile has no
5583 support for reading uninterned symbols. Note that the function
5584 @code{gensym} does not return uninterned symbols for this reason.
5585
5586 @deffn {Scheme Procedure} make-symbol name
5587 @deffnx {C Function} scm_make_symbol (name)
5588 Return a new uninterned symbol with the name @var{name}. The returned
5589 symbol is guaranteed to be unique and future calls to
5590 @code{string->symbol} will not return it.
5591 @end deffn
5592
5593 @deffn {Scheme Procedure} symbol-interned? symbol
5594 @deffnx {C Function} scm_symbol_interned_p (symbol)
5595 Return @code{#t} if @var{symbol} is interned, otherwise return
5596 @code{#f}.
5597 @end deffn
5598
5599 For example:
5600
5601 @lisp
5602 (define foo-1 (string->symbol "foo"))
5603 (define foo-2 (string->symbol "foo"))
5604 (define foo-3 (make-symbol "foo"))
5605 (define foo-4 (make-symbol "foo"))
5606
5607 (eq? foo-1 foo-2)
5608 @result{} #t
5609 ; Two interned symbols with the same name are the same object,
5610
5611 (eq? foo-1 foo-3)
5612 @result{} #f
5613 ; but a call to make-symbol with the same name returns a
5614 ; distinct object.
5615
5616 (eq? foo-3 foo-4)
5617 @result{} #f
5618 ; A call to make-symbol always returns a new object, even for
5619 ; the same name.
5620
5621 foo-3
5622 @result{} #<uninterned-symbol foo 8085290>
5623 ; Uninterned symbols print differently from interned symbols,
5624
5625 (symbol? foo-3)
5626 @result{} #t
5627 ; but they are still symbols,
5628
5629 (symbol-interned? foo-3)
5630 @result{} #f
5631 ; just not interned.
5632 @end lisp
5633
5634
5635 @node Keywords
5636 @subsection Keywords
5637 @tpindex Keywords
5638
5639 Keywords are self-evaluating objects with a convenient read syntax that
5640 makes them easy to type.
5641
5642 Guile's keyword support conforms to R5RS, and adds a (switchable) read
5643 syntax extension to permit keywords to begin with @code{:} as well as
5644 @code{#:}, or to end with @code{:}.
5645
5646 @menu
5647 * Why Use Keywords?:: Motivation for keyword usage.
5648 * Coding With Keywords:: How to use keywords.
5649 * Keyword Read Syntax:: Read syntax for keywords.
5650 * Keyword Procedures:: Procedures for dealing with keywords.
5651 @end menu
5652
5653 @node Why Use Keywords?
5654 @subsubsection Why Use Keywords?
5655
5656 Keywords are useful in contexts where a program or procedure wants to be
5657 able to accept a large number of optional arguments without making its
5658 interface unmanageable.
5659
5660 To illustrate this, consider a hypothetical @code{make-window}
5661 procedure, which creates a new window on the screen for drawing into
5662 using some graphical toolkit. There are many parameters that the caller
5663 might like to specify, but which could also be sensibly defaulted, for
5664 example:
5665
5666 @itemize @bullet
5667 @item
5668 color depth -- Default: the color depth for the screen
5669
5670 @item
5671 background color -- Default: white
5672
5673 @item
5674 width -- Default: 600
5675
5676 @item
5677 height -- Default: 400
5678 @end itemize
5679
5680 If @code{make-window} did not use keywords, the caller would have to
5681 pass in a value for each possible argument, remembering the correct
5682 argument order and using a special value to indicate the default value
5683 for that argument:
5684
5685 @lisp
5686 (make-window 'default ;; Color depth
5687 'default ;; Background color
5688 800 ;; Width
5689 100 ;; Height
5690 @dots{}) ;; More make-window arguments
5691 @end lisp
5692
5693 With keywords, on the other hand, defaulted arguments are omitted, and
5694 non-default arguments are clearly tagged by the appropriate keyword. As
5695 a result, the invocation becomes much clearer:
5696
5697 @lisp
5698 (make-window #:width 800 #:height 100)
5699 @end lisp
5700
5701 On the other hand, for a simpler procedure with few arguments, the use
5702 of keywords would be a hindrance rather than a help. The primitive
5703 procedure @code{cons}, for example, would not be improved if it had to
5704 be invoked as
5705
5706 @lisp
5707 (cons #:car x #:cdr y)
5708 @end lisp
5709
5710 So the decision whether to use keywords or not is purely pragmatic: use
5711 them if they will clarify the procedure invocation at point of call.
5712
5713 @node Coding With Keywords
5714 @subsubsection Coding With Keywords
5715
5716 If a procedure wants to support keywords, it should take a rest argument
5717 and then use whatever means is convenient to extract keywords and their
5718 corresponding arguments from the contents of that rest argument.
5719
5720 The following example illustrates the principle: the code for
5721 @code{make-window} uses a helper procedure called
5722 @code{get-keyword-value} to extract individual keyword arguments from
5723 the rest argument.
5724
5725 @lisp
5726 (define (get-keyword-value args keyword default)
5727 (let ((kv (memq keyword args)))
5728 (if (and kv (>= (length kv) 2))
5729 (cadr kv)
5730 default)))
5731
5732 (define (make-window . args)
5733 (let ((depth (get-keyword-value args #:depth screen-depth))
5734 (bg (get-keyword-value args #:bg "white"))
5735 (width (get-keyword-value args #:width 800))
5736 (height (get-keyword-value args #:height 100))
5737 @dots{})
5738 @dots{}))
5739 @end lisp
5740
5741 But you don't need to write @code{get-keyword-value}. The @code{(ice-9
5742 optargs)} module provides a set of powerful macros that you can use to
5743 implement keyword-supporting procedures like this:
5744
5745 @lisp
5746 (use-modules (ice-9 optargs))
5747
5748 (define (make-window . args)
5749 (let-keywords args #f ((depth screen-depth)
5750 (bg "white")
5751 (width 800)
5752 (height 100))
5753 ...))
5754 @end lisp
5755
5756 @noindent
5757 Or, even more economically, like this:
5758
5759 @lisp
5760 (use-modules (ice-9 optargs))
5761
5762 (define* (make-window #:key (depth screen-depth)
5763 (bg "white")
5764 (width 800)
5765 (height 100))
5766 ...)
5767 @end lisp
5768
5769 For further details on @code{let-keywords}, @code{define*} and other
5770 facilities provided by the @code{(ice-9 optargs)} module, see
5771 @ref{Optional Arguments}.
5772
5773
5774 @node Keyword Read Syntax
5775 @subsubsection Keyword Read Syntax
5776
5777 Guile, by default, only recognizes a keyword syntax that is compatible
5778 with R5RS. A token of the form @code{#:NAME}, where @code{NAME} has the
5779 same syntax as a Scheme symbol (@pxref{Symbol Read Syntax}), is the
5780 external representation of the keyword named @code{NAME}. Keyword
5781 objects print using this syntax as well, so values containing keyword
5782 objects can be read back into Guile. When used in an expression,
5783 keywords are self-quoting objects.
5784
5785 If the @code{keyword} read option is set to @code{'prefix}, Guile also
5786 recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
5787 of the form @code{:NAME} are read as symbols, as required by R5RS.
5788
5789 @cindex SRFI-88 keyword syntax
5790
5791 If the @code{keyword} read option is set to @code{'postfix}, Guile
5792 recognizes the SRFI-88 read syntax @code{NAME:} (@pxref{SRFI-88}).
5793 Otherwise, tokens of this form are read as symbols.
5794
5795 To enable and disable the alternative non-R5RS keyword syntax, you use
5796 the @code{read-set!} procedure documented in @ref{User level options
5797 interfaces} and @ref{Reader options}. Note that the @code{prefix} and
5798 @code{postfix} syntax are mutually exclusive.
5799
5800 @lisp
5801 (read-set! keywords 'prefix)
5802
5803 #:type
5804 @result{}
5805 #:type
5806
5807 :type
5808 @result{}
5809 #:type
5810
5811 (read-set! keywords 'postfix)
5812
5813 type:
5814 @result{}
5815 #:type
5816
5817 :type
5818 @result{}
5819 :type
5820
5821 (read-set! keywords #f)
5822
5823 #:type
5824 @result{}
5825 #:type
5826
5827 :type
5828 @print{}
5829 ERROR: In expression :type:
5830 ERROR: Unbound variable: :type
5831 ABORT: (unbound-variable)
5832 @end lisp
5833
5834 @node Keyword Procedures
5835 @subsubsection Keyword Procedures
5836
5837 @deffn {Scheme Procedure} keyword? obj
5838 @deffnx {C Function} scm_keyword_p (obj)
5839 Return @code{#t} if the argument @var{obj} is a keyword, else
5840 @code{#f}.
5841 @end deffn
5842
5843 @deffn {Scheme Procedure} keyword->symbol keyword
5844 @deffnx {C Function} scm_keyword_to_symbol (keyword)
5845 Return the symbol with the same name as @var{keyword}.
5846 @end deffn
5847
5848 @deffn {Scheme Procedure} symbol->keyword symbol
5849 @deffnx {C Function} scm_symbol_to_keyword (symbol)
5850 Return the keyword with the same name as @var{symbol}.
5851 @end deffn
5852
5853 @deftypefn {C Function} int scm_is_keyword (SCM obj)
5854 Equivalent to @code{scm_is_true (scm_keyword_p (@var{obj}))}.
5855 @end deftypefn
5856
5857 @deftypefn {C Function} SCM scm_from_locale_keyword (const char *str)
5858 @deftypefnx {C Function} SCM scm_from_locale_keywordn (const char *str, size_t len)
5859 Equivalent to @code{scm_symbol_to_keyword (scm_from_locale_symbol
5860 (@var{str}))} and @code{scm_symbol_to_keyword (scm_from_locale_symboln
5861 (@var{str}, @var{len}))}, respectively.
5862 @end deftypefn
5863
5864 @node Other Types
5865 @subsection ``Functionality-Centric'' Data Types
5866
5867 Procedures and macros are documented in their own chapter: see
5868 @ref{Procedures and Macros}.
5869
5870 Variable objects are documented as part of the description of Guile's
5871 module system: see @ref{Variables}.
5872
5873 Asyncs, dynamic roots and fluids are described in the chapter on
5874 scheduling: see @ref{Scheduling}.
5875
5876 Hooks are documented in the chapter on general utility functions: see
5877 @ref{Hooks}.
5878
5879 Ports are described in the chapter on I/O: see @ref{Input and Output}.
5880
5881
5882 @c Local Variables:
5883 @c TeX-master: "guile.texi"
5884 @c End: