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