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