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