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