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