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