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