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