(scm_make_complex): Discouraged by moving to discouraged.h 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.
45* Characters:: New character names.
46* Strings:: Special things about strings.
47* Regular Expressions:: Pattern matching and substitution.
48* Symbols:: Symbols.
49* Keywords:: Self-quoting, customizable display keywords.
50* Other Types:: "Functionality-centric" data types.
51@end menu
52
53
54@node Booleans
55@subsection Booleans
56@tpindex Booleans
57
58The two boolean values are @code{#t} for true and @code{#f} for false.
59
60Boolean values are returned by predicate procedures, such as the general
61equality predicates @code{eq?}, @code{eqv?} and @code{equal?}
62(@pxref{Equality}) and numerical and string comparison operators like
63@code{string=?} (@pxref{String Comparison}) and @code{<=}
64(@pxref{Comparison}).
65
66@lisp
67(<= 3 8)
68@result{} #t
69
70(<= 3 -3)
71@result{} #f
72
73(equal? "house" "houses")
74@result{} #f
75
76(eq? #f #f)
77@result{}
78#t
79@end lisp
80
81In test condition contexts like @code{if} and @code{cond} (@pxref{if
82cond case}), where a group of subexpressions will be evaluated only if a
83@var{condition} expression evaluates to ``true'', ``true'' means any
84value at all except @code{#f}.
85
86@lisp
87(if #t "yes" "no")
88@result{} "yes"
89
90(if 0 "yes" "no")
91@result{} "yes"
92
93(if #f "yes" "no")
94@result{} "no"
95@end lisp
96
97A result of this asymmetry is that typical Scheme source code more often
98uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to
99represent an @code{if} or @code{cond} false value, whereas @code{#t} is
100not necessary to represent an @code{if} or @code{cond} true value.
101
102It is important to note that @code{#f} is @strong{not} equivalent to any
103other Scheme value. In particular, @code{#f} is not the same as the
104number 0 (like in C and C++), and not the same as the ``empty list''
105(like in some Lisp dialects).
106
107In C, the two Scheme boolean values are available as the two constants
108@code{SCM_BOOL_T} for @code{#t} and @code{SCM_BOOL_F} for @code{#f}.
109Care must be taken with the false value @code{SCM_BOOL_F}: it is not
110false when used in C conditionals. In order to test for it, use
111@code{scm_is_false} or @code{scm_is_true}.
112
113@rnindex not
114@deffn {Scheme Procedure} not x
115@deffnx {C Function} scm_not (x)
116Return @code{#t} if @var{x} is @code{#f}, else return @code{#f}.
117@end deffn
118
119@rnindex boolean?
120@deffn {Scheme Procedure} boolean? obj
121@deffnx {C Function} scm_boolean_p (obj)
122Return @code{#t} if @var{obj} is either @code{#t} or @code{#f}, else
123return @code{#f}.
124@end deffn
125
126@deftypevr {C Macro} SCM SCM_BOOL_T
127The @code{SCM} representation of the Scheme object @code{#t}.
128@end deftypevr
129
130@deftypevr {C Macro} SCM SCM_BOOL_F
131The @code{SCM} representation of the Scheme object @code{#f}.
132@end deftypevr
133
134@deftypefn {C Function} int scm_is_true (SCM obj)
135Return @code{0} if @var{obj} is @code{#f}, else return @code{1}.
136@end deftypefn
137
138@deftypefn {C Function} int scm_is_false (SCM obj)
139Return @code{1} if @var{obj} is @code{#f}, else return @code{0}.
140@end deftypefn
141
142@deftypefn {C Function} int scm_is_bool (SCM obj)
143Return @code{1} if @var{obj} is either @code{#t} or @code{#f}, else
144return @code{0}.
145@end deftypefn
146
147@deftypefn {C Function} SCM scm_from_bool (int val)
148Return @code{#f} if @var{val} is @code{0}, else return @code{#t}.
149@end deftypefn
150
151@deftypefn {C Function} int scm_to_bool (SCM val)
152Return @code{1} if @var{val} is @code{SCM_BOOL_T}, return @code{0}
153when @var{val} is @code{SCM_BOOL_F}, else signal a `wrong type' error.
154
155You should probably use @code{scm_is_true} instead of this function
156when you just want to test a @code{SCM} value for trueness.
157@end deftypefn
158
159@node Numbers
160@subsection Numerical data types
161@tpindex Numbers
162
163Guile supports a rich ``tower'' of numerical types --- integer,
164rational, real and complex --- and provides an extensive set of
165mathematical and scientific functions for operating on numerical
166data. This section of the manual documents those types and functions.
167
168You may also find it illuminating to read R5RS's presentation of numbers
169in Scheme, which is particularly clear and accessible: see
170@ref{Numbers,,,r5rs,R5RS}.
171
172@menu
173* Numerical Tower:: Scheme's numerical "tower".
174* Integers:: Whole numbers.
175* Reals and Rationals:: Real and rational numbers.
176* Complex Numbers:: Complex numbers.
177* Exactness:: Exactness and inexactness.
178* Number Syntax:: Read syntax for numerical data.
179* Integer Operations:: Operations on integer values.
180* Comparison:: Comparison predicates.
181* Conversion:: Converting numbers to and from strings.
182* Complex:: Complex number operations.
183* Arithmetic:: Arithmetic functions.
184* Scientific:: Scientific functions.
185* Primitive Numerics:: Primitive numeric functions.
186* Bitwise Operations:: Logical AND, OR, NOT, and so on.
187* Random:: Random number generation.
188@end menu
189
190
191@node Numerical Tower
192@subsubsection Scheme's Numerical ``Tower''
193@rnindex number?
194
195Scheme's numerical ``tower'' consists of the following categories of
196numbers:
197
198@table @dfn
199@item integers
200Whole numbers, positive or negative; e.g.@: --5, 0, 18.
201
202@item rationals
203The set of numbers that can be expressed as @math{@var{p}/@var{q}}
204where @var{p} and @var{q} are integers; e.g.@: @math{9/16} works, but
205pi (an irrational number) doesn't. These include integers
206(@math{@var{n}/1}).
207
208@item real numbers
209The set of numbers that describes all possible positions along a
210one-dimensional line. This includes rationals as well as irrational
211numbers.
212
213@item complex numbers
214The set of numbers that describes all possible positions in a two
215dimensional space. This includes real as well as imaginary numbers
216(@math{@var{a}+@var{b}i}, where @var{a} is the @dfn{real part},
217@var{b} is the @dfn{imaginary part}, and @math{i} is the square root of
218@minus{}1.)
219@end table
220
221It is called a tower because each category ``sits on'' the one that
222follows it, in the sense that every integer is also a rational, every
223rational is also real, and every real number is also a complex number
224(but with zero imaginary part).
225
226In addition to the classification into integers, rationals, reals and
227complex numbers, Scheme also distinguishes between whether a number is
228represented exactly or not. For example, the result of
229@m{2\sin(\pi/4),sin(pi/4)} is exactly @m{\sqrt{2},2^(1/2)} but Guile
230can neither represent @m{\pi/4,pi/4} nor @m{\sqrt{2},2^(1/2)} exactly.
231Instead, it stores an inexact approximation, using the C type
232@code{double}.
233
234Guile can represent exact rationals of any magnitude, inexact
235rationals that fit into a C @code{double}, and inexact complex numbers
236with @code{double} real and imaginary parts.
237
238The @code{number?} predicate may be applied to any Scheme value to
239discover whether the value is any of the supported numerical types.
240
241@deffn {Scheme Procedure} number? obj
242@deffnx {C Function} scm_number_p (obj)
243Return @code{#t} if @var{obj} is any kind of number, else @code{#f}.
244@end deffn
245
246For example:
247
248@lisp
249(number? 3)
250@result{} #t
251
252(number? "hello there!")
253@result{} #f
254
255(define pi 3.141592654)
256(number? pi)
257@result{} #t
258@end lisp
259
260The next few subsections document each of Guile's numerical data types
261in detail.
262
263@node Integers
264@subsubsection Integers
265
266@tpindex Integer numbers
267
268@rnindex integer?
269
270Integers are whole numbers, that is numbers with no fractional part,
271such as 2, 83, and @minus{}3789.
272
273Integers in Guile can be arbitrarily big, as shown by the following
274example.
275
276@lisp
277(define (factorial n)
278 (let loop ((n n) (product 1))
279 (if (= n 0)
280 product
281 (loop (- n 1) (* product n)))))
282
283(factorial 3)
284@result{} 6
285
286(factorial 20)
287@result{} 2432902008176640000
288
289(- (factorial 45))
290@result{} -119622220865480194561963161495657715064383733760000000000
291@end lisp
292
293Readers whose background is in programming languages where integers are
294limited by the need to fit into just 4 or 8 bytes of memory may find
295this surprising, or suspect that Guile's representation of integers is
296inefficient. In fact, Guile achieves a near optimal balance of
297convenience and efficiency by using the host computer's native
298representation of integers where possible, and a more general
299representation where the required number does not fit in the native
300form. Conversion between these two representations is automatic and
301completely invisible to the Scheme level programmer.
302
303The infinities @samp{+inf.0} and @samp{-inf.0} are considered to be
304inexact integers. They are explained in detail in the next section,
305together with reals and rationals.
306
307C has a host of different integer types, and Guile offers a host of
308functions to convert between them and the @code{SCM} representation.
309For example, a C @code{int} can be handled with @code{scm_to_int} and
310@code{scm_from_int}. Guile also defines a few C integer types of its
311own, to help with differences between systems.
312
313C integer types that are not covered can be handled with the generic
314@code{scm_to_signed_integer} and @code{scm_from_signed_integer} for
315signed types, or with @code{scm_to_unsigned_integer} and
316@code{scm_from_unsigned_integer} for unsigned types.
317
318Scheme integers can be exact and inexact. For example, a number
319written as @code{3.0} with an explicit decimal-point is inexact, but
320it is also an integer. The functions @code{integer?} and
321@code{scm_is_integer} report true for such a number, but the functions
322@code{scm_is_signed_integer} and @code{scm_is_unsigned_integer} only
323allow exact integers and thus report false. Likewise, the conversion
324functions like @code{scm_to_signed_integer} only accept exact
325integers.
326
327The motivation for this behavior is that the inexactness of a number
328should not be lost silently. If you want to allow inexact integers,
329you can explicitely insert a call to @code{inexact->exact} or to its C
330equivalent @code{scm_inexact_to_exact}. (Only inexact integers will
331be converted by this call into exact integers; inexact non-integers
332will become exact fractions.)
333
334@deffn {Scheme Procedure} integer? x
335@deffnx {C Function} scm_integer_p (x)
336Return @code{#t} if @var{x} is an exactor inexact integer number, else
337@code{#f}.
338
339@lisp
340(integer? 487)
341@result{} #t
342
343(integer? 3.0)
344@result{} #t
345
346(integer? -3.4)
347@result{} #f
348
349(integer? +inf.0)
350@result{} #t
351@end lisp
352@end deffn
353
354@deftypefn {C Function} int scm_is_integer (SCM x)
355This is equivalent to @code{scm_is_true (scm_integer_p (x))}.
356@end deftypefn
357
358@defvr {C Type} scm_t_int8
359@defvrx {C Type} scm_t_uint8
360@defvrx {C Type} scm_t_int16
361@defvrx {C Type} scm_t_uint16
362@defvrx {C Type} scm_t_int32
363@defvrx {C Type} scm_t_uint32
364@defvrx {C Type} scm_t_int64
365@defvrx {C Type} scm_t_uint64
366@defvrx {C Type} scm_t_intmax
367@defvrx {C Type} scm_t_uintmax
368The C types are equivalent to the corresponding ISO C types but are
369defined on all platforms, with the exception of @code{scm_t_int64} and
370@code{scm_t_uint64}, which are only defined when a 64-bit type is
371available. For example, @code{scm_t_int8} is equivalent to
372@code{int8_t}.
373
374You can regard these definitions as a stop-gap measure until all
375platforms provide these types. If you know that all the platforms
376that you are interested in already provide these types, it is better
377to use them directly instead of the types provided by Guile.
378@end defvr
379
380@deftypefn {C Function} int scm_is_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
381@deftypefnx {C Function} int scm_is_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
382Return @code{1} when @var{x} represents an exact integer that is
383between @var{min} and @var{max}, inclusive.
384
385These functions can be used to check whether a @code{SCM} value will
386fit into a given range, such as the range of a given C integer type.
387If you just want to convert a @code{SCM} value to a given C integer
388type, use one of the conversion functions directly.
389@end deftypefn
390
391@deftypefn {C Function} scm_t_intmax scm_to_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
392@deftypefnx {C Function} scm_t_uintmax scm_to_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
393When @var{x} represents an exact integer that is between @var{min} and
394@var{max} inclusive, return that integer. Else signal an error,
395either a `wrong-type' error when @var{x} is not an exact integer, or
396an `out-of-range' error when it doesn't fit the given range.
397@end deftypefn
398
399@deftypefn {C Function} SCM scm_from_signed_integer (scm_t_intmax x)
400@deftypefnx {C Function} SCM scm_from_unsigned_integer (scm_t_uintmax x)
401Return the @code{SCM} value that represents the integer @var{x}. This
402function will always succeed and will always return an exact number.
403@end deftypefn
404
405@deftypefn {C Function} char scm_to_char (SCM x)
406@deftypefnx {C Function} {signed char} scm_to_schar (SCM x)
407@deftypefnx {C Function} {unsigned char} scm_to_uchar (SCM x)
408@deftypefnx {C Function} short scm_to_short (SCM x)
409@deftypefnx {C Function} {unsigned short} scm_to_ushort (SCM x)
410@deftypefnx {C Function} int scm_to_int (SCM x)
411@deftypefnx {C Function} {unsigned int} scm_to_uint (SCM x)
412@deftypefnx {C Function} long scm_to_long (SCM x)
413@deftypefnx {C Function} {unsigned long} scm_to_ulong (SCM x)
414@deftypefnx {C Function} {long long} scm_to_long_long (SCM x)
415@deftypefnx {C Function} {unsigned long long} scm_to_ulong_long (SCM x)
416@deftypefnx {C Function} size_t scm_to_size_t (SCM x)
417@deftypefnx {C Function} ssize_t scm_to_ssize_t (SCM x)
418@deftypefnx {C Function} scm_t_int8 scm_to_int8 (SCM x)
419@deftypefnx {C Function} scm_t_uint8 scm_to_uint8 (SCM x)
420@deftypefnx {C Function} scm_t_int16 scm_to_int16 (SCM x)
421@deftypefnx {C Function} scm_t_uint16 scm_to_uint16 (SCM x)
422@deftypefnx {C Function} scm_t_int32 scm_to_int32 (SCM x)
423@deftypefnx {C Function} scm_t_uint32 scm_to_uint32 (SCM x)
424@deftypefnx {C Function} scm_t_int64 scm_to_int64 (SCM x)
425@deftypefnx {C Function} scm_t_uint64 scm_to_uint64 (SCM x)
426@deftypefnx {C Function} scm_t_intmax scm_to_intmax (SCM x)
427@deftypefnx {C Function} scm_t_uintmax scm_to_uintmax (SCM x)
428When @var{x} represents an exact integer that fits into the indicated
429C type, return that integer. Else signal an error, either a
430`wrong-type' error when @var{x} is not an exact integer, or an
431`out-of-range' error when it doesn't fit the given range.
432
433The functions @code{scm_to_long_long}, @code{scm_to_ulong_long},
434@code{scm_to_int64}, and @code{scm_to_uint64} are only available when
435the corresponding types are.
436@end deftypefn
437
438@deftypefn {C Function} SCM scm_from_char (char x)
439@deftypefnx {C Function} SCM scm_from_schar (signed char x)
440@deftypefnx {C Function} SCM scm_from_uchar (unsigned char x)
441@deftypefnx {C Function} SCM scm_from_short (short x)
442@deftypefnx {C Function} SCM scm_from_ushort (unsigned short x)
443@deftypefnx {C Function} SCM scm_from_int (int x)
444@deftypefnx {C Function} SCM scm_from_uint (unsigned int x)
445@deftypefnx {C Function} SCM scm_from_long (long x)
446@deftypefnx {C Function} SCM scm_from_ulong (unsigned long x)
447@deftypefnx {C Function} SCM scm_from_long_long (long long x)
448@deftypefnx {C Function} SCM scm_from_ulong_long (unsigned long long x)
449@deftypefnx {C Function} SCM scm_from_size_t (size_t x)
450@deftypefnx {C Function} SCM scm_from_ssize_t (ssize_t x)
451@deftypefnx {C Function} SCM scm_from_int8 (scm_t_int8 x)
452@deftypefnx {C Function} SCM scm_from_uint8 (scm_t_uint8 x)
453@deftypefnx {C Function} SCM scm_from_int16 (scm_t_int16 x)
454@deftypefnx {C Function} SCM scm_from_uint16 (scm_t_uint16 x)
455@deftypefnx {C Function} SCM scm_from_int32 (scm_t_int32 x)
456@deftypefnx {C Function} SCM scm_from_uint32 (scm_t_uint32 x)
457@deftypefnx {C Function} SCM scm_from_int64 (scm_t_int64 x)
458@deftypefnx {C Function} SCM scm_from_uint64 (scm_t_uint64 x)
459@deftypefnx {C Function} SCM scm_from_intmax (scm_t_intmax x)
460@deftypefnx {C Function} SCM scm_from_uintmax (scm_t_uintmax x)
461Return the @code{SCM} value that represents the integer @var{x}.
462These functions will always succeed and will always return an exact
463number.
464@end deftypefn
465
466@node Reals and Rationals
467@subsubsection Real and Rational Numbers
468@tpindex Real numbers
469@tpindex Rational numbers
470
471@rnindex real?
472@rnindex rational?
473
474Mathematically, the real numbers are the set of numbers that describe
475all possible points along a continuous, infinite, one-dimensional line.
476The rational numbers are the set of all numbers that can be written as
477fractions @var{p}/@var{q}, where @var{p} and @var{q} are integers.
478All rational numbers are also real, but there are real numbers that
479are not rational, for example the square root of 2, and pi.
480
481Guile can represent both exact and inexact rational numbers, but it
482can not represent irrational numbers. Exact rationals are represented
483by storing the numerator and denominator as two exact integers.
484Inexact rationals are stored as floating point numbers using the C
485type @code{double}.
486
487Exact rationals are written as a fraction of integers. There must be
488no whitespace around the slash:
489
490@lisp
4911/2
492-22/7
493@end lisp
494
495Even though the actual encoding of inexact rationals is in binary, it
496may be helpful to think of it as a decimal number with a limited
497number of significant figures and a decimal point somewhere, since
498this corresponds to the standard notation for non-whole numbers. For
499example:
500
501@lisp
5020.34
503-0.00000142857931198
504-5648394822220000000000.0
5054.0
506@end lisp
507
508The limited precision of Guile's encoding means that any ``real'' number
509in Guile can be written in a rational form, by multiplying and then dividing
510by sufficient powers of 10 (or in fact, 2). For example,
511@samp{-0.00000142857931198} is the same as @minus{}142857931198 divided by
512100000000000000000. In Guile's current incarnation, therefore, the
513@code{rational?} and @code{real?} predicates are equivalent.
514
515
516Dividing by an exact zero leads to a error message, as one might
517expect. However, dividing by an inexact zero does not produce an
518error. Instead, the result of the division is either plus or minus
519infinity, depending on the sign of the divided number.
520
521The infinities are written @samp{+inf.0} and @samp{-inf.0},
522respectivly. This syntax is also recognized by @code{read} as an
523extension to the usual Scheme syntax.
524
525Dividing zero by zero yields something that is not a number at all:
526@samp{+nan.0}. This is the special `not a number' value.
527
528On platforms that follow @acronym{IEEE} 754 for their floating point
529arithmetic, the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values
530are implemented using the corresponding @acronym{IEEE} 754 values.
531They behave in arithmetic operations like @acronym{IEEE} 754 describes
532it, i.e., @code{(= +nan.0 +nan.0)} @result{} @code{#f}.
533
534The infinities are inexact integers and are considered to be both even
535and odd. While @samp{+nan.0} is not @code{=} to itself, it is
536@code{eqv?} to itself.
537
538To test for the special values, use the functions @code{inf?} and
539@code{nan?}.
540
541@deffn {Scheme Procedure} real? obj
542@deffnx {C Function} scm_real_p (obj)
543Return @code{#t} if @var{obj} is a real number, else @code{#f}. Note
544that the sets of integer and rational values form subsets of the set
545of real numbers, so the predicate will also be fulfilled if @var{obj}
546is an integer number or a rational number.
547@end deffn
548
549@deffn {Scheme Procedure} rational? x
550@deffnx {C Function} scm_rational_p (x)
551Return @code{#t} if @var{x} is a rational number, @code{#f} otherwise.
552Note that the set of integer values forms a subset of the set of
553rational numbers, i. e. the predicate will also be fulfilled if
554@var{x} is an integer number.
555
556Since Guile can not represent irrational numbers, every number
557satisfying @code{real?} also satisfies @code{rational?} in Guile.
558@end deffn
559
560@deffn {Scheme Procedure} rationalize x eps
561@deffnx {C Function} scm_rationalize (x, eps)
562Returns the @emph{simplest} rational number differing
563from @var{x} by no more than @var{eps}.
564
565As required by @acronym{R5RS}, @code{rationalize} only returns an
566exact result when both its arguments are exact. Thus, you might need
567to use @code{inexact->exact} on the arguments.
568
569@lisp
570(rationalize (inexact->exact 1.2) 1/100)
571@result{} 6/5
572@end lisp
573
574@end deffn
575
d3df9759
MV
576@deffn {Scheme Procedure} inf? x
577@deffnx {C Function} scm_inf_p (x)
07d83abe
MV
578Return @code{#t} if @var{x} is either @samp{+inf.0} or @samp{-inf.0},
579@code{#f} otherwise.
580@end deffn
581
582@deffn {Scheme Procedure} nan? x
d3df9759 583@deffnx {C Function} scm_nan_p (x)
07d83abe
MV
584Return @code{#t} if @var{x} is @samp{+nan.0}, @code{#f} otherwise.
585@end deffn
586
d3df9759
MV
587@deffn {Scheme Procedure} numerator x
588@deffnx {C Function} scm_numerator (x)
589Return the numerator of the rational number @var{x}.
590@end deffn
591
592@deffn {Scheme Procedure} denominator x
593@deffnx {C Function} scm_denominator (x)
594Return the denominator of the rational number @var{x}.
595@end deffn
596
597@deftypefn {C Function} int scm_is_real (SCM val)
598@deftypefnx {C Function} int scm_is_rational (SCM val)
599Equivalent to @code{scm_is_true (scm_real_p (val))} and
600@code{scm_is_true (scm_rational_p (val))}, respectively.
601@end deftypefn
602
603@deftypefn {C Function} double scm_to_double (SCM val)
604Returns the number closest to @var{val} that is representable as a
605@code{double}. Returns infinity for a @var{val} that is too large in
606magnitude. The argument @var{val} must be a real number.
607@end deftypefn
608
609@deftypefn {C Function} SCM scm_from_double (double val)
610Return the @code{SCM} value that representats @var{val}. The returned
611value is inexact according to the predicate @code{inexact?}, but it
612will be exactly equal to @var{val}.
613@end deftypefn
614
07d83abe
MV
615@node Complex Numbers
616@subsubsection Complex Numbers
617@tpindex Complex numbers
618
619@rnindex complex?
620
621Complex numbers are the set of numbers that describe all possible points
622in a two-dimensional space. The two coordinates of a particular point
623in this space are known as the @dfn{real} and @dfn{imaginary} parts of
624the complex number that describes that point.
625
626In Guile, complex numbers are written in rectangular form as the sum of
627their real and imaginary parts, using the symbol @code{i} to indicate
628the imaginary part.
629
630@lisp
6313+4i
632@result{}
6333.0+4.0i
634
635(* 3-8i 2.3+0.3i)
636@result{}
6379.3-17.5i
638@end lisp
639
640Guile represents a complex number with a non-zero imaginary part as a
641pair of inexact rationals, so the real and imaginary parts of a
642complex number have the same properties of inexactness and limited
643precision as single inexact rational numbers. Guile can not represent
644exact complex numbers with non-zero imaginary parts.
645
646@deffn {Scheme Procedure} complex? x
647@deffnx {C Function} scm_number_p (x)
648Return @code{#t} if @var{x} is a complex number, @code{#f}
649otherwise. Note that the sets of real, rational and integer
650values form subsets of the set of complex numbers, i. e. the
651predicate will also be fulfilled if @var{x} is a real,
652rational or integer number.
653@end deffn
654
655
656@node Exactness
657@subsubsection Exact and Inexact Numbers
658@tpindex Exact numbers
659@tpindex Inexact numbers
660
661@rnindex exact?
662@rnindex inexact?
663@rnindex exact->inexact
664@rnindex inexact->exact
665
666R5RS requires that a calculation involving inexact numbers always
667produces an inexact result. To meet this requirement, Guile
668distinguishes between an exact integer value such as @samp{5} and the
669corresponding inexact real value which, to the limited precision
670available, has no fractional part, and is printed as @samp{5.0}. Guile
671will only convert the latter value to the former when forced to do so by
672an invocation of the @code{inexact->exact} procedure.
673
674@deffn {Scheme Procedure} exact? z
675@deffnx {C Function} scm_exact_p (z)
676Return @code{#t} if the number @var{z} is exact, @code{#f}
677otherwise.
678
679@lisp
680(exact? 2)
681@result{} #t
682
683(exact? 0.5)
684@result{} #f
685
686(exact? (/ 2))
687@result{} #t
688@end lisp
689
690@end deffn
691
692@deffn {Scheme Procedure} inexact? z
693@deffnx {C Function} scm_inexact_p (z)
694Return @code{#t} if the number @var{z} is inexact, @code{#f}
695else.
696@end deffn
697
698@deffn {Scheme Procedure} inexact->exact z
699@deffnx {C Function} scm_inexact_to_exact (z)
700Return an exact number that is numerically closest to @var{z}, when
701there is one. For inexact rationals, Guile returns the exact rational
702that is numerically equal to the inexact rational. Inexact complex
703numbers with a non-zero imaginary part can not be made exact.
704
705@lisp
706(inexact->exact 0.5)
707@result{} 1/2
708@end lisp
709
710The following happens because 12/10 is not exactly representable as a
711@code{double} (on most platforms). However, when reading a decimal
712number that has been marked exact with the ``#e'' prefix, Guile is
713able to represent it correctly.
714
715@lisp
716(inexact->exact 1.2)
717@result{} 5404319552844595/4503599627370496
718
719#e1.2
720@result{} 6/5
721@end lisp
722
723@end deffn
724
725@c begin (texi-doc-string "guile" "exact->inexact")
726@deffn {Scheme Procedure} exact->inexact z
727@deffnx {C Function} scm_exact_to_inexact (z)
728Convert the number @var{z} to its inexact representation.
729@end deffn
730
731
732@node Number Syntax
733@subsubsection Read Syntax for Numerical Data
734
735The read syntax for integers is a string of digits, optionally
736preceded by a minus or plus character, a code indicating the
737base in which the integer is encoded, and a code indicating whether
738the number is exact or inexact. The supported base codes are:
739
740@table @code
741@item #b
742@itemx #B
743the integer is written in binary (base 2)
744
745@item #o
746@itemx #O
747the integer is written in octal (base 8)
748
749@item #d
750@itemx #D
751the integer is written in decimal (base 10)
752
753@item #x
754@itemx #X
755the integer is written in hexadecimal (base 16)
756@end table
757
758If the base code is omitted, the integer is assumed to be decimal. The
759following examples show how these base codes are used.
760
761@lisp
762-13
763@result{} -13
764
765#d-13
766@result{} -13
767
768#x-13
769@result{} -19
770
771#b+1101
772@result{} 13
773
774#o377
775@result{} 255
776@end lisp
777
778The codes for indicating exactness (which can, incidentally, be applied
779to all numerical values) are:
780
781@table @code
782@item #e
783@itemx #E
784the number is exact
785
786@item #i
787@itemx #I
788the number is inexact.
789@end table
790
791If the exactness indicator is omitted, the number is exact unless it
792contains a radix point. Since Guile can not represent exact complex
793numbers, an error is signalled when asking for them.
794
795@lisp
796(exact? 1.2)
797@result{} #f
798
799(exact? #e1.2)
800@result{} #t
801
802(exact? #e+1i)
803ERROR: Wrong type argument
804@end lisp
805
806Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for
807plus and minus infinity, respectively. The value must be written
808exactly as shown, that is, they always must have a sign and exactly
809one zero digit after the decimal point. It also understands
810@samp{+nan.0} and @samp{-nan.0} for the special `not-a-number' value.
811The sign is ignored for `not-a-number' and the value is always printed
812as @samp{+nan.0}.
813
814@node Integer Operations
815@subsubsection Operations on Integer Values
816@rnindex odd?
817@rnindex even?
818@rnindex quotient
819@rnindex remainder
820@rnindex modulo
821@rnindex gcd
822@rnindex lcm
823
824@deffn {Scheme Procedure} odd? n
825@deffnx {C Function} scm_odd_p (n)
826Return @code{#t} if @var{n} is an odd number, @code{#f}
827otherwise.
828@end deffn
829
830@deffn {Scheme Procedure} even? n
831@deffnx {C Function} scm_even_p (n)
832Return @code{#t} if @var{n} is an even number, @code{#f}
833otherwise.
834@end deffn
835
836@c begin (texi-doc-string "guile" "quotient")
837@c begin (texi-doc-string "guile" "remainder")
838@deffn {Scheme Procedure} quotient n d
839@deffnx {Scheme Procedure} remainder n d
840@deffnx {C Function} scm_quotient (n, d)
841@deffnx {C Function} scm_remainder (n, d)
842Return the quotient or remainder from @var{n} divided by @var{d}. The
843quotient is rounded towards zero, and the remainder will have the same
844sign as @var{n}. In all cases quotient and remainder satisfy
845@math{@var{n} = @var{q}*@var{d} + @var{r}}.
846
847@lisp
848(remainder 13 4) @result{} 1
849(remainder -13 4) @result{} -1
850@end lisp
851@end deffn
852
853@c begin (texi-doc-string "guile" "modulo")
854@deffn {Scheme Procedure} modulo n d
855@deffnx {C Function} scm_modulo (n, d)
856Return the remainder from @var{n} divided by @var{d}, with the same
857sign as @var{d}.
858
859@lisp
860(modulo 13 4) @result{} 1
861(modulo -13 4) @result{} 3
862(modulo 13 -4) @result{} -3
863(modulo -13 -4) @result{} -1
864@end lisp
865@end deffn
866
867@c begin (texi-doc-string "guile" "gcd")
868@deffn {Scheme Procedure} gcd
869@deffnx {C Function} scm_gcd (x, y)
870Return the greatest common divisor of all arguments.
871If called without arguments, 0 is returned.
872
873The C function @code{scm_gcd} always takes two arguments, while the
874Scheme function can take an arbitrary number.
875@end deffn
876
877@c begin (texi-doc-string "guile" "lcm")
878@deffn {Scheme Procedure} lcm
879@deffnx {C Function} scm_lcm (x, y)
880Return the least common multiple of the arguments.
881If called without arguments, 1 is returned.
882
883The C function @code{scm_lcm} always takes two arguments, while the
884Scheme function can take an arbitrary number.
885@end deffn
886
887
888@node Comparison
889@subsubsection Comparison Predicates
890@rnindex zero?
891@rnindex positive?
892@rnindex negative?
893
894The C comparison functions below always takes two arguments, while the
895Scheme functions can take an arbitrary number. Also keep in mind that
896the C functions return one of the Scheme boolean values
897@code{SCM_BOOL_T} or @code{SCM_BOOL_F} which are both true as far as C
898is concerned. Thus, always write @code{scm_is_true (scm_num_eq_p (x,
899y))} when testing the two Scheme numbers @code{x} and @code{y} for
900equality, for example.
901
902@c begin (texi-doc-string "guile" "=")
903@deffn {Scheme Procedure} =
904@deffnx {C Function} scm_num_eq_p (x, y)
905Return @code{#t} if all parameters are numerically equal.
906@end deffn
907
908@c begin (texi-doc-string "guile" "<")
909@deffn {Scheme Procedure} <
910@deffnx {C Function} scm_less_p (x, y)
911Return @code{#t} if the list of parameters is monotonically
912increasing.
913@end deffn
914
915@c begin (texi-doc-string "guile" ">")
916@deffn {Scheme Procedure} >
917@deffnx {C Function} scm_gr_p (x, y)
918Return @code{#t} if the list of parameters is monotonically
919decreasing.
920@end deffn
921
922@c begin (texi-doc-string "guile" "<=")
923@deffn {Scheme Procedure} <=
924@deffnx {C Function} scm_leq_p (x, y)
925Return @code{#t} if the list of parameters is monotonically
926non-decreasing.
927@end deffn
928
929@c begin (texi-doc-string "guile" ">=")
930@deffn {Scheme Procedure} >=
931@deffnx {C Function} scm_geq_p (x, y)
932Return @code{#t} if the list of parameters is monotonically
933non-increasing.
934@end deffn
935
936@c begin (texi-doc-string "guile" "zero?")
937@deffn {Scheme Procedure} zero? z
938@deffnx {C Function} scm_zero_p (z)
939Return @code{#t} if @var{z} is an exact or inexact number equal to
940zero.
941@end deffn
942
943@c begin (texi-doc-string "guile" "positive?")
944@deffn {Scheme Procedure} positive? x
945@deffnx {C Function} scm_positive_p (x)
946Return @code{#t} if @var{x} is an exact or inexact number greater than
947zero.
948@end deffn
949
950@c begin (texi-doc-string "guile" "negative?")
951@deffn {Scheme Procedure} negative? x
952@deffnx {C Function} scm_negative_p (x)
953Return @code{#t} if @var{x} is an exact or inexact number less than
954zero.
955@end deffn
956
957
958@node Conversion
959@subsubsection Converting Numbers To and From Strings
960@rnindex number->string
961@rnindex string->number
962
963@deffn {Scheme Procedure} number->string n [radix]
964@deffnx {C Function} scm_number_to_string (n, radix)
965Return a string holding the external representation of the
966number @var{n} in the given @var{radix}. If @var{n} is
967inexact, a radix of 10 will be used.
968@end deffn
969
970@deffn {Scheme Procedure} string->number string [radix]
971@deffnx {C Function} scm_string_to_number (string, radix)
972Return a number of the maximally precise representation
973expressed by the given @var{string}. @var{radix} must be an
974exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}
975is a default radix that may be overridden by an explicit radix
976prefix in @var{string} (e.g. "#o177"). If @var{radix} is not
977supplied, then the default radix is 10. If string is not a
978syntactically valid notation for a number, then
979@code{string->number} returns @code{#f}.
980@end deffn
981
982
983@node Complex
984@subsubsection Complex Number Operations
985@rnindex make-rectangular
986@rnindex make-polar
987@rnindex real-part
988@rnindex imag-part
989@rnindex magnitude
990@rnindex angle
991
992@deffn {Scheme Procedure} make-rectangular real imaginary
993@deffnx {C Function} scm_make_rectangular (real, imaginary)
994Return a complex number constructed of the given @var{real} and
995@var{imaginary} parts.
996@end deffn
997
998@deffn {Scheme Procedure} make-polar x y
999@deffnx {C Function} scm_make_polar (x, y)
1000Return the complex number @var{x} * e^(i * @var{y}).
1001@end deffn
1002
1003@c begin (texi-doc-string "guile" "real-part")
1004@deffn {Scheme Procedure} real-part z
1005@deffnx {C Function} scm_real_part (z)
1006Return the real part of the number @var{z}.
1007@end deffn
1008
1009@c begin (texi-doc-string "guile" "imag-part")
1010@deffn {Scheme Procedure} imag-part z
1011@deffnx {C Function} scm_imag_part (z)
1012Return the imaginary part of the number @var{z}.
1013@end deffn
1014
1015@c begin (texi-doc-string "guile" "magnitude")
1016@deffn {Scheme Procedure} magnitude z
1017@deffnx {C Function} scm_magnitude (z)
1018Return the magnitude of the number @var{z}. This is the same as
1019@code{abs} for real arguments, but also allows complex numbers.
1020@end deffn
1021
1022@c begin (texi-doc-string "guile" "angle")
1023@deffn {Scheme Procedure} angle z
1024@deffnx {C Function} scm_angle (z)
1025Return the angle of the complex number @var{z}.
1026@end deffn
1027
1028
1029@node Arithmetic
1030@subsubsection Arithmetic Functions
1031@rnindex max
1032@rnindex min
1033@rnindex +
1034@rnindex *
1035@rnindex -
1036@rnindex /
1037@rnindex abs
1038@rnindex floor
1039@rnindex ceiling
1040@rnindex truncate
1041@rnindex round
1042
1043The C arithmetic functions below always takes two arguments, while the
1044Scheme functions can take an arbitrary number. When you need to
1045invoke them with just one argument, for example to compute the
1046equivalent od @code{(- x)}, pass @code{SCM_UNDEFINED} as the second
1047one: @code{scm_difference (x, SCM_UNDEFINED)}.
1048
1049@c begin (texi-doc-string "guile" "+")
1050@deffn {Scheme Procedure} + z1 @dots{}
1051@deffnx {C Function} scm_sum (z1, z2)
1052Return the sum of all parameter values. Return 0 if called without any
1053parameters.
1054@end deffn
1055
1056@c begin (texi-doc-string "guile" "-")
1057@deffn {Scheme Procedure} - z1 z2 @dots{}
1058@deffnx {C Function} scm_difference (z1, z2)
1059If called with one argument @var{z1}, -@var{z1} is returned. Otherwise
1060the sum of all but the first argument are subtracted from the first
1061argument.
1062@end deffn
1063
1064@c begin (texi-doc-string "guile" "*")
1065@deffn {Scheme Procedure} * z1 @dots{}
1066@deffnx {C Function} scm_product (z1, z2)
1067Return the product of all arguments. If called without arguments, 1 is
1068returned.
1069@end deffn
1070
1071@c begin (texi-doc-string "guile" "/")
1072@deffn {Scheme Procedure} / z1 z2 @dots{}
1073@deffnx {C Function} scm_divide (z1, z2)
1074Divide the first argument by the product of the remaining arguments. If
1075called with one argument @var{z1}, 1/@var{z1} is returned.
1076@end deffn
1077
1078@c begin (texi-doc-string "guile" "abs")
1079@deffn {Scheme Procedure} abs x
1080@deffnx {C Function} scm_abs (x)
1081Return the absolute value of @var{x}.
1082
1083@var{x} must be a number with zero imaginary part. To calculate the
1084magnitude of a complex number, use @code{magnitude} instead.
1085@end deffn
1086
1087@c begin (texi-doc-string "guile" "max")
1088@deffn {Scheme Procedure} max x1 x2 @dots{}
1089@deffnx {C Function} scm_max (x1, x2)
1090Return the maximum of all parameter values.
1091@end deffn
1092
1093@c begin (texi-doc-string "guile" "min")
1094@deffn {Scheme Procedure} min x1 x2 @dots{}
1095@deffnx {C Function} scm_min (x1, x2)
1096Return the minimum of all parameter values.
1097@end deffn
1098
1099@c begin (texi-doc-string "guile" "truncate")
1100@deffn {Scheme Procedure} truncate
1101@deffnx {C Function} scm_truncate_number (x)
1102Round the inexact number @var{x} towards zero.
1103@end deffn
1104
1105@c begin (texi-doc-string "guile" "round")
1106@deffn {Scheme Procedure} round x
1107@deffnx {C Function} scm_round_number (x)
1108Round the inexact number @var{x} to the nearest integer. When exactly
1109halfway between two integers, round to the even one.
1110@end deffn
1111
1112@c begin (texi-doc-string "guile" "floor")
1113@deffn {Scheme Procedure} floor x
1114@deffnx {C Function} scm_floor (x)
1115Round the number @var{x} towards minus infinity.
1116@end deffn
1117
1118@c begin (texi-doc-string "guile" "ceiling")
1119@deffn {Scheme Procedure} ceiling x
1120@deffnx {C Function} scm_ceiling (x)
1121Round the number @var{x} towards infinity.
1122@end deffn
1123
1124
1125@node Scientific
1126@subsubsection Scientific Functions
1127
1128The following procedures accept any kind of number as arguments,
1129including complex numbers.
1130
1131@rnindex sqrt
1132@c begin (texi-doc-string "guile" "sqrt")
1133@deffn {Scheme Procedure} sqrt z
1134Return the square root of @var{z}.
1135@end deffn
1136
1137@rnindex expt
1138@c begin (texi-doc-string "guile" "expt")
1139@deffn {Scheme Procedure} expt z1 z2
1140Return @var{z1} raised to the power of @var{z2}.
1141@end deffn
1142
1143@rnindex sin
1144@c begin (texi-doc-string "guile" "sin")
1145@deffn {Scheme Procedure} sin z
1146Return the sine of @var{z}.
1147@end deffn
1148
1149@rnindex cos
1150@c begin (texi-doc-string "guile" "cos")
1151@deffn {Scheme Procedure} cos z
1152Return the cosine of @var{z}.
1153@end deffn
1154
1155@rnindex tan
1156@c begin (texi-doc-string "guile" "tan")
1157@deffn {Scheme Procedure} tan z
1158Return the tangent of @var{z}.
1159@end deffn
1160
1161@rnindex asin
1162@c begin (texi-doc-string "guile" "asin")
1163@deffn {Scheme Procedure} asin z
1164Return the arcsine of @var{z}.
1165@end deffn
1166
1167@rnindex acos
1168@c begin (texi-doc-string "guile" "acos")
1169@deffn {Scheme Procedure} acos z
1170Return the arccosine of @var{z}.
1171@end deffn
1172
1173@rnindex atan
1174@c begin (texi-doc-string "guile" "atan")
1175@deffn {Scheme Procedure} atan z
1176@deffnx {Scheme Procedure} atan y x
1177Return the arctangent of @var{z}, or of @math{@var{y}/@var{x}}.
1178@end deffn
1179
1180@rnindex exp
1181@c begin (texi-doc-string "guile" "exp")
1182@deffn {Scheme Procedure} exp z
1183Return e to the power of @var{z}, where e is the base of natural
1184logarithms (2.71828@dots{}).
1185@end deffn
1186
1187@rnindex log
1188@c begin (texi-doc-string "guile" "log")
1189@deffn {Scheme Procedure} log z
1190Return the natural logarithm of @var{z}.
1191@end deffn
1192
1193@c begin (texi-doc-string "guile" "log10")
1194@deffn {Scheme Procedure} log10 z
1195Return the base 10 logarithm of @var{z}.
1196@end deffn
1197
1198@c begin (texi-doc-string "guile" "sinh")
1199@deffn {Scheme Procedure} sinh z
1200Return the hyperbolic sine of @var{z}.
1201@end deffn
1202
1203@c begin (texi-doc-string "guile" "cosh")
1204@deffn {Scheme Procedure} cosh z
1205Return the hyperbolic cosine of @var{z}.
1206@end deffn
1207
1208@c begin (texi-doc-string "guile" "tanh")
1209@deffn {Scheme Procedure} tanh z
1210Return the hyperbolic tangent of @var{z}.
1211@end deffn
1212
1213@c begin (texi-doc-string "guile" "asinh")
1214@deffn {Scheme Procedure} asinh z
1215Return the hyperbolic arcsine of @var{z}.
1216@end deffn
1217
1218@c begin (texi-doc-string "guile" "acosh")
1219@deffn {Scheme Procedure} acosh z
1220Return the hyperbolic arccosine of @var{z}.
1221@end deffn
1222
1223@c begin (texi-doc-string "guile" "atanh")
1224@deffn {Scheme Procedure} atanh z
1225Return the hyperbolic arctangent of @var{z}.
1226@end deffn
1227
1228
1229@node Primitive Numerics
1230@subsubsection Primitive Numeric Functions
1231
1232Many of Guile's numeric procedures which accept any kind of numbers as
1233arguments, including complex numbers, are implemented as Scheme
1234procedures that use the following real number-based primitives. These
1235primitives signal an error if they are called with complex arguments.
1236
1237@c begin (texi-doc-string "guile" "$abs")
1238@deffn {Scheme Procedure} $abs x
1239Return the absolute value of @var{x}.
1240@end deffn
1241
1242@c begin (texi-doc-string "guile" "$sqrt")
1243@deffn {Scheme Procedure} $sqrt x
1244Return the square root of @var{x}.
1245@end deffn
1246
1247@deffn {Scheme Procedure} $expt x y
1248@deffnx {C Function} scm_sys_expt (x, y)
1249Return @var{x} raised to the power of @var{y}. This
1250procedure does not accept complex arguments.
1251@end deffn
1252
1253@c begin (texi-doc-string "guile" "$sin")
1254@deffn {Scheme Procedure} $sin x
1255Return the sine of @var{x}.
1256@end deffn
1257
1258@c begin (texi-doc-string "guile" "$cos")
1259@deffn {Scheme Procedure} $cos x
1260Return the cosine of @var{x}.
1261@end deffn
1262
1263@c begin (texi-doc-string "guile" "$tan")
1264@deffn {Scheme Procedure} $tan x
1265Return the tangent of @var{x}.
1266@end deffn
1267
1268@c begin (texi-doc-string "guile" "$asin")
1269@deffn {Scheme Procedure} $asin x
1270Return the arcsine of @var{x}.
1271@end deffn
1272
1273@c begin (texi-doc-string "guile" "$acos")
1274@deffn {Scheme Procedure} $acos x
1275Return the arccosine of @var{x}.
1276@end deffn
1277
1278@c begin (texi-doc-string "guile" "$atan")
1279@deffn {Scheme Procedure} $atan x
1280Return the arctangent of @var{x} in the range @minus{}@math{PI/2} to
1281@math{PI/2}.
1282@end deffn
1283
1284@deffn {Scheme Procedure} $atan2 x y
1285@deffnx {C Function} scm_sys_atan2 (x, y)
1286Return the arc tangent of the two arguments @var{x} and
1287@var{y}. This is similar to calculating the arc tangent of
1288@var{x} / @var{y}, except that the signs of both arguments
1289are used to determine the quadrant of the result. This
1290procedure does not accept complex arguments.
1291@end deffn
1292
1293@c begin (texi-doc-string "guile" "$exp")
1294@deffn {Scheme Procedure} $exp x
1295Return e to the power of @var{x}, where e is the base of natural
1296logarithms (2.71828@dots{}).
1297@end deffn
1298
1299@c begin (texi-doc-string "guile" "$log")
1300@deffn {Scheme Procedure} $log x
1301Return the natural logarithm of @var{x}.
1302@end deffn
1303
1304@c begin (texi-doc-string "guile" "$sinh")
1305@deffn {Scheme Procedure} $sinh x
1306Return the hyperbolic sine of @var{x}.
1307@end deffn
1308
1309@c begin (texi-doc-string "guile" "$cosh")
1310@deffn {Scheme Procedure} $cosh x
1311Return the hyperbolic cosine of @var{x}.
1312@end deffn
1313
1314@c begin (texi-doc-string "guile" "$tanh")
1315@deffn {Scheme Procedure} $tanh x
1316Return the hyperbolic tangent of @var{x}.
1317@end deffn
1318
1319@c begin (texi-doc-string "guile" "$asinh")
1320@deffn {Scheme Procedure} $asinh x
1321Return the hyperbolic arcsine of @var{x}.
1322@end deffn
1323
1324@c begin (texi-doc-string "guile" "$acosh")
1325@deffn {Scheme Procedure} $acosh x
1326Return the hyperbolic arccosine of @var{x}.
1327@end deffn
1328
1329@c begin (texi-doc-string "guile" "$atanh")
1330@deffn {Scheme Procedure} $atanh x
1331Return the hyperbolic arctangent of @var{x}.
1332@end deffn
1333
1334C functions for the above are provided by the standard mathematics
1335library. Naturally these expect and return @code{double} arguments
1336(@pxref{Mathematics,,, libc, GNU C Library Reference Manual}).
1337
1338@multitable {xx} {Scheme Procedure} {C Function}
1339@item @tab Scheme Procedure @tab C Function
1340
1341@item @tab @code{$abs} @tab @code{fabs}
1342@item @tab @code{$sqrt} @tab @code{sqrt}
1343@item @tab @code{$sin} @tab @code{sin}
1344@item @tab @code{$cos} @tab @code{cos}
1345@item @tab @code{$tan} @tab @code{tan}
1346@item @tab @code{$asin} @tab @code{asin}
1347@item @tab @code{$acos} @tab @code{acos}
1348@item @tab @code{$atan} @tab @code{atan}
1349@item @tab @code{$atan2} @tab @code{atan2}
1350@item @tab @code{$exp} @tab @code{exp}
1351@item @tab @code{$expt} @tab @code{pow}
1352@item @tab @code{$log} @tab @code{log}
1353@item @tab @code{$sinh} @tab @code{sinh}
1354@item @tab @code{$cosh} @tab @code{cosh}
1355@item @tab @code{$tanh} @tab @code{tanh}
1356@item @tab @code{$asinh} @tab @code{asinh}
1357@item @tab @code{$acosh} @tab @code{acosh}
1358@item @tab @code{$atanh} @tab @code{atanh}
1359@end multitable
1360
1361@code{asinh}, @code{acosh} and @code{atanh} are C99 standard but might
1362not be available on older systems. Guile provides the following
1363equivalents (on all systems).
1364
1365@deftypefn {C Function} double scm_asinh (double x)
1366@deftypefnx {C Function} double scm_acosh (double x)
1367@deftypefnx {C Function} double scm_atanh (double x)
1368Return the hyperbolic arcsine, arccosine or arctangent of @var{x}
1369respectively.
1370@end deftypefn
1371
1372
1373@node Bitwise Operations
1374@subsubsection Bitwise Operations
1375
1376For the following bitwise functions, negative numbers are treated as
1377infinite precision twos-complements. For instance @math{-6} is bits
1378@math{@dots{}111010}, with infinitely many ones on the left. It can
1379be seen that adding 6 (binary 110) to such a bit pattern gives all
1380zeros.
1381
1382@deffn {Scheme Procedure} logand n1 n2 @dots{}
1383@deffnx {C Function} scm_logand (n1, n2)
1384Return the bitwise @sc{and} of the integer arguments.
1385
1386@lisp
1387(logand) @result{} -1
1388(logand 7) @result{} 7
1389(logand #b111 #b011 #b001) @result{} 1
1390@end lisp
1391@end deffn
1392
1393@deffn {Scheme Procedure} logior n1 n2 @dots{}
1394@deffnx {C Function} scm_logior (n1, n2)
1395Return the bitwise @sc{or} of the integer arguments.
1396
1397@lisp
1398(logior) @result{} 0
1399(logior 7) @result{} 7
1400(logior #b000 #b001 #b011) @result{} 3
1401@end lisp
1402@end deffn
1403
1404@deffn {Scheme Procedure} logxor n1 n2 @dots{}
1405@deffnx {C Function} scm_loxor (n1, n2)
1406Return the bitwise @sc{xor} of the integer arguments. A bit is
1407set in the result if it is set in an odd number of arguments.
1408
1409@lisp
1410(logxor) @result{} 0
1411(logxor 7) @result{} 7
1412(logxor #b000 #b001 #b011) @result{} 2
1413(logxor #b000 #b001 #b011 #b011) @result{} 1
1414@end lisp
1415@end deffn
1416
1417@deffn {Scheme Procedure} lognot n
1418@deffnx {C Function} scm_lognot (n)
1419Return the integer which is the ones-complement of the integer
1420argument, ie.@: each 0 bit is changed to 1 and each 1 bit to 0.
1421
1422@lisp
1423(number->string (lognot #b10000000) 2)
1424 @result{} "-10000001"
1425(number->string (lognot #b0) 2)
1426 @result{} "-1"
1427@end lisp
1428@end deffn
1429
1430@deffn {Scheme Procedure} logtest j k
1431@deffnx {C Function} scm_logtest (j, k)
1432@lisp
1433(logtest j k) @equiv{} (not (zero? (logand j k)))
1434
1435(logtest #b0100 #b1011) @result{} #f
1436(logtest #b0100 #b0111) @result{} #t
1437@end lisp
1438@end deffn
1439
1440@deffn {Scheme Procedure} logbit? index j
1441@deffnx {C Function} scm_logbit_p (index, j)
1442@lisp
1443(logbit? index j) @equiv{} (logtest (integer-expt 2 index) j)
1444
1445(logbit? 0 #b1101) @result{} #t
1446(logbit? 1 #b1101) @result{} #f
1447(logbit? 2 #b1101) @result{} #t
1448(logbit? 3 #b1101) @result{} #t
1449(logbit? 4 #b1101) @result{} #f
1450@end lisp
1451@end deffn
1452
1453@deffn {Scheme Procedure} ash n cnt
1454@deffnx {C Function} scm_ash (n, cnt)
1455Return @var{n} shifted left by @var{cnt} bits, or shifted right if
1456@var{cnt} is negative. This is an ``arithmetic'' shift.
1457
1458This is effectively a multiplication by @m{2^{cnt}, 2^@var{cnt}}, and
1459when @var{cnt} is negative it's a division, rounded towards negative
1460infinity. (Note that this is not the same rounding as @code{quotient}
1461does.)
1462
1463With @var{n} viewed as an infinite precision twos complement,
1464@code{ash} means a left shift introducing zero bits, or a right shift
1465dropping bits.
1466
1467@lisp
1468(number->string (ash #b1 3) 2) @result{} "1000"
1469(number->string (ash #b1010 -1) 2) @result{} "101"
1470
1471;; -23 is bits ...11101001, -6 is bits ...111010
1472(ash -23 -2) @result{} -6
1473@end lisp
1474@end deffn
1475
1476@deffn {Scheme Procedure} logcount n
1477@deffnx {C Function} scm_logcount (n)
1478Return the number of bits in integer @var{n}. If integer is
1479positive, the 1-bits in its binary representation are counted.
1480If negative, the 0-bits in its two's-complement binary
1481representation are counted. If 0, 0 is returned.
1482
1483@lisp
1484(logcount #b10101010)
1485 @result{} 4
1486(logcount 0)
1487 @result{} 0
1488(logcount -2)
1489 @result{} 1
1490@end lisp
1491@end deffn
1492
1493@deffn {Scheme Procedure} integer-length n
1494@deffnx {C Function} scm_integer_length (n)
1495Return the number of bits necessary to represent @var{n}.
1496
1497For positive @var{n} this is how many bits to the most significant one
1498bit. For negative @var{n} it's how many bits to the most significant
1499zero bit in twos complement form.
1500
1501@lisp
1502(integer-length #b10101010) @result{} 8
1503(integer-length #b1111) @result{} 4
1504(integer-length 0) @result{} 0
1505(integer-length -1) @result{} 0
1506(integer-length -256) @result{} 8
1507(integer-length -257) @result{} 9
1508@end lisp
1509@end deffn
1510
1511@deffn {Scheme Procedure} integer-expt n k
1512@deffnx {C Function} scm_integer_expt (n, k)
1513Return @var{n} raised to the non-negative integer exponent
1514@var{k}.
1515
1516@lisp
1517(integer-expt 2 5)
1518 @result{} 32
1519(integer-expt -3 3)
1520 @result{} -27
1521@end lisp
1522@end deffn
1523
1524@deffn {Scheme Procedure} bit-extract n start end
1525@deffnx {C Function} scm_bit_extract (n, start, end)
1526Return the integer composed of the @var{start} (inclusive)
1527through @var{end} (exclusive) bits of @var{n}. The
1528@var{start}th bit becomes the 0-th bit in the result.
1529
1530@lisp
1531(number->string (bit-extract #b1101101010 0 4) 2)
1532 @result{} "1010"
1533(number->string (bit-extract #b1101101010 4 9) 2)
1534 @result{} "10110"
1535@end lisp
1536@end deffn
1537
1538
1539@node Random
1540@subsubsection Random Number Generation
1541
1542Pseudo-random numbers are generated from a random state object, which
1543can be created with @code{seed->random-state}. The @var{state}
1544parameter to the various functions below is optional, it defaults to
1545the state object in the @code{*random-state*} variable.
1546
1547@deffn {Scheme Procedure} copy-random-state [state]
1548@deffnx {C Function} scm_copy_random_state (state)
1549Return a copy of the random state @var{state}.
1550@end deffn
1551
1552@deffn {Scheme Procedure} random n [state]
1553@deffnx {C Function} scm_random (n, state)
1554Return a number in [0, @var{n}).
1555
1556Accepts a positive integer or real n and returns a
1557number of the same type between zero (inclusive) and
1558@var{n} (exclusive). The values returned have a uniform
1559distribution.
1560@end deffn
1561
1562@deffn {Scheme Procedure} random:exp [state]
1563@deffnx {C Function} scm_random_exp (state)
1564Return an inexact real in an exponential distribution with mean
15651. For an exponential distribution with mean @var{u} use @code{(*
1566@var{u} (random:exp))}.
1567@end deffn
1568
1569@deffn {Scheme Procedure} random:hollow-sphere! vect [state]
1570@deffnx {C Function} scm_random_hollow_sphere_x (vect, state)
1571Fills @var{vect} with inexact real random numbers the sum of whose
1572squares is equal to 1.0. Thinking of @var{vect} as coordinates in
1573space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1574the coordinates are uniformly distributed over the surface of the unit
1575n-sphere.
1576@end deffn
1577
1578@deffn {Scheme Procedure} random:normal [state]
1579@deffnx {C Function} scm_random_normal (state)
1580Return an inexact real in a normal distribution. The distribution
1581used has mean 0 and standard deviation 1. For a normal distribution
1582with mean @var{m} and standard deviation @var{d} use @code{(+ @var{m}
1583(* @var{d} (random:normal)))}.
1584@end deffn
1585
1586@deffn {Scheme Procedure} random:normal-vector! vect [state]
1587@deffnx {C Function} scm_random_normal_vector_x (vect, state)
1588Fills @var{vect} with inexact real random numbers that are
1589independent and standard normally distributed
1590(i.e., with mean 0 and variance 1).
1591@end deffn
1592
1593@deffn {Scheme Procedure} random:solid-sphere! vect [state]
1594@deffnx {C Function} scm_random_solid_sphere_x (vect, state)
1595Fills @var{vect} with inexact real random numbers the sum of whose
1596squares is less than 1.0. Thinking of @var{vect} as coordinates in
1597space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
1598the coordinates are uniformly distributed within the unit
1599@var{n}-sphere. The sum of the squares of the numbers is returned.
1600@c FIXME: What does this mean, particularly the n-sphere part?
1601@end deffn
1602
1603@deffn {Scheme Procedure} random:uniform [state]
1604@deffnx {C Function} scm_random_uniform (state)
1605Return a uniformly distributed inexact real random number in
1606[0,1).
1607@end deffn
1608
1609@deffn {Scheme Procedure} seed->random-state seed
1610@deffnx {C Function} scm_seed_to_random_state (seed)
1611Return a new random state using @var{seed}.
1612@end deffn
1613
1614@defvar *random-state*
1615The global random state used by the above functions when the
1616@var{state} parameter is not given.
1617@end defvar
1618
1619
1620@node Characters
1621@subsection Characters
1622@tpindex Characters
1623
1624@noindent
1625[@strong{FIXME}: how do you specify regular (non-control) characters?]
1626
1627Most of the ``control characters'' (those below codepoint 32) in the
1628@acronym{ASCII} character set, as well as the space, may be referred
1629to by name: for example, @code{#\tab}, @code{#\esc}, @code{#\stx}, and
1630so on. The following table describes the @acronym{ASCII} names for
1631each character.
1632
1633@multitable @columnfractions .25 .25 .25 .25
1634@item 0 = @code{#\nul}
1635 @tab 1 = @code{#\soh}
1636 @tab 2 = @code{#\stx}
1637 @tab 3 = @code{#\etx}
1638@item 4 = @code{#\eot}
1639 @tab 5 = @code{#\enq}
1640 @tab 6 = @code{#\ack}
1641 @tab 7 = @code{#\bel}
1642@item 8 = @code{#\bs}
1643 @tab 9 = @code{#\ht}
1644 @tab 10 = @code{#\nl}
1645 @tab 11 = @code{#\vt}
1646@item 12 = @code{#\np}
1647 @tab 13 = @code{#\cr}
1648 @tab 14 = @code{#\so}
1649 @tab 15 = @code{#\si}
1650@item 16 = @code{#\dle}
1651 @tab 17 = @code{#\dc1}
1652 @tab 18 = @code{#\dc2}
1653 @tab 19 = @code{#\dc3}
1654@item 20 = @code{#\dc4}
1655 @tab 21 = @code{#\nak}
1656 @tab 22 = @code{#\syn}
1657 @tab 23 = @code{#\etb}
1658@item 24 = @code{#\can}
1659 @tab 25 = @code{#\em}
1660 @tab 26 = @code{#\sub}
1661 @tab 27 = @code{#\esc}
1662@item 28 = @code{#\fs}
1663 @tab 29 = @code{#\gs}
1664 @tab 30 = @code{#\rs}
1665 @tab 31 = @code{#\us}
1666@item 32 = @code{#\sp}
1667@end multitable
1668
1669The ``delete'' character (octal 177) may be referred to with the name
1670@code{#\del}.
1671
1672Several characters have more than one name:
1673
1674@multitable {@code{#\backspace}} {Original}
1675@item Alias @tab Original
1676@item @code{#\space} @tab @code{#\sp}
1677@item @code{#\newline} @tab @code{#\nl}
1678@item @code{#\tab} @tab @code{#\ht}
1679@item @code{#\backspace} @tab @code{#\bs}
1680@item @code{#\return} @tab @code{#\cr}
1681@item @code{#\page} @tab @code{#\np}
1682@item @code{#\null} @tab @code{#\nul}
1683@end multitable
1684
1685@rnindex char?
1686@deffn {Scheme Procedure} char? x
1687@deffnx {C Function} scm_char_p (x)
1688Return @code{#t} iff @var{x} is a character, else @code{#f}.
1689@end deffn
1690
1691@rnindex char=?
1692@deffn {Scheme Procedure} char=? x y
1693Return @code{#t} iff @var{x} is the same character as @var{y}, else @code{#f}.
1694@end deffn
1695
1696@rnindex char<?
1697@deffn {Scheme Procedure} char<? x y
1698Return @code{#t} iff @var{x} is less than @var{y} in the @acronym{ASCII} sequence,
1699else @code{#f}.
1700@end deffn
1701
1702@rnindex char<=?
1703@deffn {Scheme Procedure} char<=? x y
1704Return @code{#t} iff @var{x} is less than or equal to @var{y} in the
1705@acronym{ASCII} sequence, else @code{#f}.
1706@end deffn
1707
1708@rnindex char>?
1709@deffn {Scheme Procedure} char>? x y
1710Return @code{#t} iff @var{x} is greater than @var{y} in the @acronym{ASCII}
1711sequence, else @code{#f}.
1712@end deffn
1713
1714@rnindex char>=?
1715@deffn {Scheme Procedure} char>=? x y
1716Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the
1717@acronym{ASCII} sequence, else @code{#f}.
1718@end deffn
1719
1720@rnindex char-ci=?
1721@deffn {Scheme Procedure} char-ci=? x y
1722Return @code{#t} iff @var{x} is the same character as @var{y} ignoring
1723case, else @code{#f}.
1724@end deffn
1725
1726@rnindex char-ci<?
1727@deffn {Scheme Procedure} char-ci<? x y
1728Return @code{#t} iff @var{x} is less than @var{y} in the @acronym{ASCII} sequence
1729ignoring case, else @code{#f}.
1730@end deffn
1731
1732@rnindex char-ci<=?
1733@deffn {Scheme Procedure} char-ci<=? x y
1734Return @code{#t} iff @var{x} is less than or equal to @var{y} in the
1735@acronym{ASCII} sequence ignoring case, else @code{#f}.
1736@end deffn
1737
1738@rnindex char-ci>?
1739@deffn {Scheme Procedure} char-ci>? x y
1740Return @code{#t} iff @var{x} is greater than @var{y} in the @acronym{ASCII}
1741sequence ignoring case, else @code{#f}.
1742@end deffn
1743
1744@rnindex char-ci>=?
1745@deffn {Scheme Procedure} char-ci>=? x y
1746Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the
1747@acronym{ASCII} sequence ignoring case, else @code{#f}.
1748@end deffn
1749
1750@rnindex char-alphabetic?
1751@deffn {Scheme Procedure} char-alphabetic? chr
1752@deffnx {C Function} scm_char_alphabetic_p (chr)
1753Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.
1754Alphabetic means the same thing as the @code{isalpha} C library function.
1755@end deffn
1756
1757@rnindex char-numeric?
1758@deffn {Scheme Procedure} char-numeric? chr
1759@deffnx {C Function} scm_char_numeric_p (chr)
1760Return @code{#t} iff @var{chr} is numeric, else @code{#f}.
1761Numeric means the same thing as the @code{isdigit} C library function.
1762@end deffn
1763
1764@rnindex char-whitespace?
1765@deffn {Scheme Procedure} char-whitespace? chr
1766@deffnx {C Function} scm_char_whitespace_p (chr)
1767Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.
1768Whitespace means the same thing as the @code{isspace} C library function.
1769@end deffn
1770
1771@rnindex char-upper-case?
1772@deffn {Scheme Procedure} char-upper-case? chr
1773@deffnx {C Function} scm_char_upper_case_p (chr)
1774Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.
1775Uppercase means the same thing as the @code{isupper} C library function.
1776@end deffn
1777
1778@rnindex char-lower-case?
1779@deffn {Scheme Procedure} char-lower-case? chr
1780@deffnx {C Function} scm_char_lower_case_p (chr)
1781Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.
1782Lowercase means the same thing as the @code{islower} C library function.
1783@end deffn
1784
1785@deffn {Scheme Procedure} char-is-both? chr
1786@deffnx {C Function} scm_char_is_both_p (chr)
1787Return @code{#t} iff @var{chr} is either uppercase or lowercase, else
1788@code{#f}. Uppercase and lowercase are as defined by the
1789@code{isupper} and @code{islower} C library functions.
1790@end deffn
1791
1792@rnindex char->integer
1793@deffn {Scheme Procedure} char->integer chr
1794@deffnx {C Function} scm_char_to_integer (chr)
1795Return the number corresponding to ordinal position of @var{chr} in the
1796@acronym{ASCII} sequence.
1797@end deffn
1798
1799@rnindex integer->char
1800@deffn {Scheme Procedure} integer->char n
1801@deffnx {C Function} scm_integer_to_char (n)
1802Return the character at position @var{n} in the @acronym{ASCII} sequence.
1803@end deffn
1804
1805@rnindex char-upcase
1806@deffn {Scheme Procedure} char-upcase chr
1807@deffnx {C Function} scm_char_upcase (chr)
1808Return the uppercase character version of @var{chr}.
1809@end deffn
1810
1811@rnindex char-downcase
1812@deffn {Scheme Procedure} char-downcase chr
1813@deffnx {C Function} scm_char_downcase (chr)
1814Return the lowercase character version of @var{chr}.
1815@end deffn
1816
1817@xref{Classification of Characters,,,libc,GNU C Library Reference
1818Manual}, for information about the @code{is*} Standard C functions
1819mentioned above.
1820
1821
1822@node Strings
1823@subsection Strings
1824@tpindex Strings
1825
1826Strings are fixed-length sequences of characters. They can be created
1827by calling constructor procedures, but they can also literally get
1828entered at the @acronym{REPL} or in Scheme source files.
1829
1830@c Guile provides a rich set of string processing procedures, because text
1831@c handling is very important when Guile is used as a scripting language.
1832
1833Strings always carry the information about how many characters they are
1834composed of with them, so there is no special end-of-string character,
1835like in C. That means that Scheme strings can contain any character,
1836even the @samp{NUL} character @samp{\0}. But note: Since most operating
1837system calls dealing with strings (such as for file operations) expect
1838strings to be zero-terminated, they might do unexpected things when
1839called with string containing unusual characters.
1840
1841@menu
1842* String Syntax:: Read syntax for strings.
1843* String Predicates:: Testing strings for certain properties.
1844* String Constructors:: Creating new string objects.
1845* List/String Conversion:: Converting from/to lists of characters.
1846* String Selection:: Select portions from strings.
1847* String Modification:: Modify parts or whole strings.
1848* String Comparison:: Lexicographic ordering predicates.
1849* String Searching:: Searching in strings.
1850* Alphabetic Case Mapping:: Convert the alphabetic case of strings.
1851* Appending Strings:: Appending strings to form a new string.
1852@end menu
1853
1854@node String Syntax
1855@subsubsection String Read Syntax
1856
1857@c In the following @code is used to get a good font in TeX etc, but
1858@c is omitted for Info format, so as not to risk any confusion over
1859@c whether surrounding ` ' quotes are part of the escape or are
1860@c special in a string (they're not).
1861
1862The read syntax for strings is an arbitrarily long sequence of
1863characters enclosed in double quotes (@nicode{"}). @footnote{Actually,
1864the current implementation restricts strings to a length of
1865@math{2^24}, or 16,777,216, characters. Sorry.}
1866
1867Backslash is an escape character and can be used to insert the
1868following special characters. @nicode{\"} and @nicode{\\} are R5RS
1869standard, the rest are Guile extensions, notice they follow C string
1870syntax.
1871
1872@table @asis
1873@item @nicode{\\}
1874Backslash character.
1875
1876@item @nicode{\"}
1877Double quote character (an unescaped @nicode{"} is otherwise the end
1878of the string).
1879
1880@item @nicode{\0}
1881NUL character (ASCII 0).
1882
1883@item @nicode{\a}
1884Bell character (ASCII 7).
1885
1886@item @nicode{\f}
1887Formfeed character (ASCII 12).
1888
1889@item @nicode{\n}
1890Newline character (ASCII 10).
1891
1892@item @nicode{\r}
1893Carriage return character (ASCII 13).
1894
1895@item @nicode{\t}
1896Tab character (ASCII 9).
1897
1898@item @nicode{\v}
1899Vertical tab character (ASCII 11).
1900
1901@item @nicode{\xHH}
1902Character code given by two hexadecimal digits. For example
1903@nicode{\x7f} for an ASCII DEL (127).
1904@end table
1905
1906@noindent
1907The following are examples of string literals:
1908
1909@lisp
1910"foo"
1911"bar plonk"
1912"Hello World"
1913"\"Hi\", he said."
1914@end lisp
1915
1916
1917@node String Predicates
1918@subsubsection String Predicates
1919
1920The following procedures can be used to check whether a given string
1921fulfills some specified property.
1922
1923@rnindex string?
1924@deffn {Scheme Procedure} string? obj
1925@deffnx {C Function} scm_string_p (obj)
1926Return @code{#t} if @var{obj} is a string, else @code{#f}.
1927@end deffn
1928
1929@deffn {Scheme Procedure} string-null? str
1930@deffnx {C Function} scm_string_null_p (str)
1931Return @code{#t} if @var{str}'s length is zero, and
1932@code{#f} otherwise.
1933@lisp
1934(string-null? "") @result{} #t
1935y @result{} "foo"
1936(string-null? y) @result{} #f
1937@end lisp
1938@end deffn
1939
1940@node String Constructors
1941@subsubsection String Constructors
1942
1943The string constructor procedures create new string objects, possibly
1944initializing them with some specified character data.
1945
1946@c FIXME::martin: list->string belongs into `List/String Conversion'
1947
1948@rnindex string
1949@rnindex list->string
1950@deffn {Scheme Procedure} string . chrs
1951@deffnx {Scheme Procedure} list->string chrs
1952@deffnx {C Function} scm_string (chrs)
1953Return a newly allocated string composed of the arguments,
1954@var{chrs}.
1955@end deffn
1956
1957@rnindex make-string
1958@deffn {Scheme Procedure} make-string k [chr]
1959@deffnx {C Function} scm_make_string (k, chr)
1960Return a newly allocated string of
1961length @var{k}. If @var{chr} is given, then all elements of
1962the string are initialized to @var{chr}, otherwise the contents
1963of the @var{string} are unspecified.
1964@end deffn
1965
1966@node List/String Conversion
1967@subsubsection List/String conversion
1968
1969When processing strings, it is often convenient to first convert them
1970into a list representation by using the procedure @code{string->list},
1971work with the resulting list, and then convert it back into a string.
1972These procedures are useful for similar tasks.
1973
1974@rnindex string->list
1975@deffn {Scheme Procedure} string->list str
1976@deffnx {C Function} scm_string_to_list (str)
1977Return a newly allocated list of the characters that make up
1978the given string @var{str}. @code{string->list} and
1979@code{list->string} are inverses as far as @samp{equal?} is
1980concerned.
1981@end deffn
1982
1983@deffn {Scheme Procedure} string-split str chr
1984@deffnx {C Function} scm_string_split (str, chr)
1985Split the string @var{str} into the a list of the substrings delimited
1986by appearances of the character @var{chr}. Note that an empty substring
1987between separator characters will result in an empty string in the
1988result list.
1989
1990@lisp
1991(string-split "root:x:0:0:root:/root:/bin/bash" #\:)
1992@result{}
1993("root" "x" "0" "0" "root" "/root" "/bin/bash")
1994
1995(string-split "::" #\:)
1996@result{}
1997("" "" "")
1998
1999(string-split "" #\:)
2000@result{}
2001("")
2002@end lisp
2003@end deffn
2004
2005
2006@node String Selection
2007@subsubsection String Selection
2008
2009Portions of strings can be extracted by these procedures.
2010@code{string-ref} delivers individual characters whereas
2011@code{substring} can be used to extract substrings from longer strings.
2012
2013@rnindex string-length
2014@deffn {Scheme Procedure} string-length string
2015@deffnx {C Function} scm_string_length (string)
2016Return the number of characters in @var{string}.
2017@end deffn
2018
2019@rnindex string-ref
2020@deffn {Scheme Procedure} string-ref str k
2021@deffnx {C Function} scm_string_ref (str, k)
2022Return character @var{k} of @var{str} using zero-origin
2023indexing. @var{k} must be a valid index of @var{str}.
2024@end deffn
2025
2026@rnindex string-copy
2027@deffn {Scheme Procedure} string-copy str
2028@deffnx {C Function} scm_string_copy (str)
2029Return a newly allocated copy of the given @var{string}.
2030@end deffn
2031
2032@rnindex substring
2033@deffn {Scheme Procedure} substring str start [end]
2034@deffnx {C Function} scm_substring (str, start, end)
2035Return a newly allocated string formed from the characters
2036of @var{str} beginning with index @var{start} (inclusive) and
2037ending with index @var{end} (exclusive).
2038@var{str} must be a string, @var{start} and @var{end} must be
2039exact integers satisfying:
2040
20410 <= @var{start} <= @var{end} <= @code{(string-length @var{str})}.
2042@end deffn
2043
2044@node String Modification
2045@subsubsection String Modification
2046
2047These procedures are for modifying strings in-place. This means that the
2048result of the operation is not a new string; instead, the original string's
2049memory representation is modified.
2050
2051@rnindex string-set!
2052@deffn {Scheme Procedure} string-set! str k chr
2053@deffnx {C Function} scm_string_set_x (str, k, chr)
2054Store @var{chr} in element @var{k} of @var{str} and return
2055an unspecified value. @var{k} must be a valid index of
2056@var{str}.
2057@end deffn
2058
2059@rnindex string-fill!
2060@deffn {Scheme Procedure} string-fill! str chr
2061@deffnx {C Function} scm_string_fill_x (str, chr)
2062Store @var{char} in every element of the given @var{string} and
2063return an unspecified value.
2064@end deffn
2065
2066@deffn {Scheme Procedure} substring-fill! str start end fill
2067@deffnx {C Function} scm_substring_fill_x (str, start, end, fill)
2068Change every character in @var{str} between @var{start} and
2069@var{end} to @var{fill}.
2070
2071@lisp
2072(define y "abcdefg")
2073(substring-fill! y 1 3 #\r)
2074y
2075@result{} "arrdefg"
2076@end lisp
2077@end deffn
2078
2079@deffn {Scheme Procedure} substring-move! str1 start1 end1 str2 start2
2080@deffnx {C Function} scm_substring_move_x (str1, start1, end1, str2, start2)
2081Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}
2082into @var{str2} beginning at position @var{start2}.
2083@var{str1} and @var{str2} can be the same string.
2084@end deffn
2085
2086
2087@node String Comparison
2088@subsubsection String Comparison
2089
2090The procedures in this section are similar to the character ordering
2091predicates (@pxref{Characters}), but are defined on character sequences.
2092They all return @code{#t} on success and @code{#f} on failure. The
2093predicates ending in @code{-ci} ignore the character case when comparing
2094strings.
2095
2096
2097@rnindex string=?
2098@deffn {Scheme Procedure} string=? s1 s2
2099Lexicographic equality predicate; return @code{#t} if the two
2100strings are the same length and contain the same characters in
2101the same positions, otherwise return @code{#f}.
2102
2103The procedure @code{string-ci=?} treats upper and lower case
2104letters as though they were the same character, but
2105@code{string=?} treats upper and lower case as distinct
2106characters.
2107@end deffn
2108
2109@rnindex string<?
2110@deffn {Scheme Procedure} string<? s1 s2
2111Lexicographic ordering predicate; return @code{#t} if @var{s1}
2112is lexicographically less than @var{s2}.
2113@end deffn
2114
2115@rnindex string<=?
2116@deffn {Scheme Procedure} string<=? s1 s2
2117Lexicographic ordering predicate; return @code{#t} if @var{s1}
2118is lexicographically less than or equal to @var{s2}.
2119@end deffn
2120
2121@rnindex string>?
2122@deffn {Scheme Procedure} string>? s1 s2
2123Lexicographic ordering predicate; return @code{#t} if @var{s1}
2124is lexicographically greater than @var{s2}.
2125@end deffn
2126
2127@rnindex string>=?
2128@deffn {Scheme Procedure} string>=? s1 s2
2129Lexicographic ordering predicate; return @code{#t} if @var{s1}
2130is lexicographically greater than or equal to @var{s2}.
2131@end deffn
2132
2133@rnindex string-ci=?
2134@deffn {Scheme Procedure} string-ci=? s1 s2
2135Case-insensitive string equality predicate; return @code{#t} if
2136the two strings are the same length and their component
2137characters match (ignoring case) at each position; otherwise
2138return @code{#f}.
2139@end deffn
2140
2141@rnindex string-ci<
2142@deffn {Scheme Procedure} string-ci<? s1 s2
2143Case insensitive lexicographic ordering predicate; return
2144@code{#t} if @var{s1} is lexicographically less than @var{s2}
2145regardless of case.
2146@end deffn
2147
2148@rnindex string<=?
2149@deffn {Scheme Procedure} string-ci<=? s1 s2
2150Case insensitive lexicographic ordering predicate; return
2151@code{#t} if @var{s1} is lexicographically less than or equal
2152to @var{s2} regardless of case.
2153@end deffn
2154
2155@rnindex string-ci>?
2156@deffn {Scheme Procedure} string-ci>? s1 s2
2157Case insensitive lexicographic ordering predicate; return
2158@code{#t} if @var{s1} is lexicographically greater than
2159@var{s2} regardless of case.
2160@end deffn
2161
2162@rnindex string-ci>=?
2163@deffn {Scheme Procedure} string-ci>=? s1 s2
2164Case insensitive lexicographic ordering predicate; return
2165@code{#t} if @var{s1} is lexicographically greater than or
2166equal to @var{s2} regardless of case.
2167@end deffn
2168
2169
2170@node String Searching
2171@subsubsection String Searching
2172
2173When searching for the index of a character in a string, these
2174procedures can be used.
2175
2176@deffn {Scheme Procedure} string-index str chr [frm [to]]
2177@deffnx {C Function} scm_string_index (str, chr, frm, to)
2178Return the index of the first occurrence of @var{chr} in
2179@var{str}. The optional integer arguments @var{frm} and
2180@var{to} limit the search to a portion of the string. This
2181procedure essentially implements the @code{index} or
2182@code{strchr} functions from the C library.
2183
2184@lisp
2185(string-index "weiner" #\e)
2186@result{} 1
2187
2188(string-index "weiner" #\e 2)
2189@result{} 4
2190
2191(string-index "weiner" #\e 2 4)
2192@result{} #f
2193@end lisp
2194@end deffn
2195
2196@deffn {Scheme Procedure} string-rindex str chr [frm [to]]
2197@deffnx {C Function} scm_string_rindex (str, chr, frm, to)
2198Like @code{string-index}, but search from the right of the
2199string rather than from the left. This procedure essentially
2200implements the @code{rindex} or @code{strrchr} functions from
2201the C library.
2202
2203@lisp
2204(string-rindex "weiner" #\e)
2205@result{} 4
2206
2207(string-rindex "weiner" #\e 2 4)
2208@result{} #f
2209
2210(string-rindex "weiner" #\e 2 5)
2211@result{} 4
2212@end lisp
2213@end deffn
2214
2215@node Alphabetic Case Mapping
2216@subsubsection Alphabetic Case Mapping
2217
2218These are procedures for mapping strings to their upper- or lower-case
2219equivalents, respectively, or for capitalizing strings.
2220
2221@deffn {Scheme Procedure} string-upcase str
2222@deffnx {C Function} scm_string_upcase (str)
2223Return a freshly allocated string containing the characters of
2224@var{str} in upper case.
2225@end deffn
2226
2227@deffn {Scheme Procedure} string-upcase! str
2228@deffnx {C Function} scm_string_upcase_x (str)
2229Destructively upcase every character in @var{str} and return
2230@var{str}.
2231@lisp
2232y @result{} "arrdefg"
2233(string-upcase! y) @result{} "ARRDEFG"
2234y @result{} "ARRDEFG"
2235@end lisp
2236@end deffn
2237
2238@deffn {Scheme Procedure} string-downcase str
2239@deffnx {C Function} scm_string_downcase (str)
2240Return a freshly allocation string containing the characters in
2241@var{str} in lower case.
2242@end deffn
2243
2244@deffn {Scheme Procedure} string-downcase! str
2245@deffnx {C Function} scm_string_downcase_x (str)
2246Destructively downcase every character in @var{str} and return
2247@var{str}.
2248@lisp
2249y @result{} "ARRDEFG"
2250(string-downcase! y) @result{} "arrdefg"
2251y @result{} "arrdefg"
2252@end lisp
2253@end deffn
2254
2255@deffn {Scheme Procedure} string-capitalize str
2256@deffnx {C Function} scm_string_capitalize (str)
2257Return a freshly allocated string with the characters in
2258@var{str}, where the first character of every word is
2259capitalized.
2260@end deffn
2261
2262@deffn {Scheme Procedure} string-capitalize! str
2263@deffnx {C Function} scm_string_capitalize_x (str)
2264Upcase the first character of every word in @var{str}
2265destructively and return @var{str}.
2266
2267@lisp
2268y @result{} "hello world"
2269(string-capitalize! y) @result{} "Hello World"
2270y @result{} "Hello World"
2271@end lisp
2272@end deffn
2273
2274
2275@node Appending Strings
2276@subsubsection Appending Strings
2277
2278The procedure @code{string-append} appends several strings together to
2279form a longer result string.
2280
2281@rnindex string-append
2282@deffn {Scheme Procedure} string-append . args
2283@deffnx {C Function} scm_string_append (args)
2284Return a newly allocated string whose characters form the
2285concatenation of the given strings, @var{args}.
2286
2287@example
2288(let ((h "hello "))
2289 (string-append h "world"))
2290@result{} "hello world"
2291@end example
2292@end deffn
2293
2294
2295@node Regular Expressions
2296@subsection Regular Expressions
2297@tpindex Regular expressions
2298
2299@cindex regular expressions
2300@cindex regex
2301@cindex emacs regexp
2302
2303A @dfn{regular expression} (or @dfn{regexp}) is a pattern that
2304describes a whole class of strings. A full description of regular
2305expressions and their syntax is beyond the scope of this manual;
2306an introduction can be found in the Emacs manual (@pxref{Regexps,
2307, Syntax of Regular Expressions, emacs, The GNU Emacs Manual}), or
2308in many general Unix reference books.
2309
2310If your system does not include a POSIX regular expression library,
2311and you have not linked Guile with a third-party regexp library such
2312as Rx, these functions will not be available. You can tell whether
2313your Guile installation includes regular expression support by
2314checking whether @code{(provided? 'regex)} returns true.
2315
2316The following regexp and string matching features are provided by the
2317@code{(ice-9 regex)} module. Before using the described functions,
2318you should load this module by executing @code{(use-modules (ice-9
2319regex))}.
2320
2321@menu
2322* Regexp Functions:: Functions that create and match regexps.
2323* Match Structures:: Finding what was matched by a regexp.
2324* Backslash Escapes:: Removing the special meaning of regexp
2325 meta-characters.
2326@end menu
2327
2328
2329@node Regexp Functions
2330@subsubsection Regexp Functions
2331
2332By default, Guile supports POSIX extended regular expressions.
2333That means that the characters @samp{(}, @samp{)}, @samp{+} and
2334@samp{?} are special, and must be escaped if you wish to match the
2335literal characters.
2336
2337This regular expression interface was modeled after that
2338implemented by SCSH, the Scheme Shell. It is intended to be
2339upwardly compatible with SCSH regular expressions.
2340
2341@deffn {Scheme Procedure} string-match pattern str [start]
2342Compile the string @var{pattern} into a regular expression and compare
2343it with @var{str}. The optional numeric argument @var{start} specifies
2344the position of @var{str} at which to begin matching.
2345
2346@code{string-match} returns a @dfn{match structure} which
2347describes what, if anything, was matched by the regular
2348expression. @xref{Match Structures}. If @var{str} does not match
2349@var{pattern} at all, @code{string-match} returns @code{#f}.
2350@end deffn
2351
2352Two examples of a match follow. In the first example, the pattern
2353matches the four digits in the match string. In the second, the pattern
2354matches nothing.
2355
2356@example
2357(string-match "[0-9][0-9][0-9][0-9]" "blah2002")
2358@result{} #("blah2002" (4 . 8))
2359
2360(string-match "[A-Za-z]" "123456")
2361@result{} #f
2362@end example
2363
2364Each time @code{string-match} is called, it must compile its
2365@var{pattern} argument into a regular expression structure. This
2366operation is expensive, which makes @code{string-match} inefficient if
2367the same regular expression is used several times (for example, in a
2368loop). For better performance, you can compile a regular expression in
2369advance and then match strings against the compiled regexp.
2370
2371@deffn {Scheme Procedure} make-regexp pat flag@dots{}
2372@deffnx {C Function} scm_make_regexp (pat, flaglst)
2373Compile the regular expression described by @var{pat}, and
2374return the compiled regexp structure. If @var{pat} does not
2375describe a legal regular expression, @code{make-regexp} throws
2376a @code{regular-expression-syntax} error.
2377
2378The @var{flag} arguments change the behavior of the compiled
2379regular expression. The following values may be supplied:
2380
2381@defvar regexp/icase
2382Consider uppercase and lowercase letters to be the same when
2383matching.
2384@end defvar
2385
2386@defvar regexp/newline
2387If a newline appears in the target string, then permit the
2388@samp{^} and @samp{$} operators to match immediately after or
2389immediately before the newline, respectively. Also, the
2390@samp{.} and @samp{[^...]} operators will never match a newline
2391character. The intent of this flag is to treat the target
2392string as a buffer containing many lines of text, and the
2393regular expression as a pattern that may match a single one of
2394those lines.
2395@end defvar
2396
2397@defvar regexp/basic
2398Compile a basic (``obsolete'') regexp instead of the extended
2399(``modern'') regexps that are the default. Basic regexps do
2400not consider @samp{|}, @samp{+} or @samp{?} to be special
2401characters, and require the @samp{@{...@}} and @samp{(...)}
2402metacharacters to be backslash-escaped (@pxref{Backslash
2403Escapes}). There are several other differences between basic
2404and extended regular expressions, but these are the most
2405significant.
2406@end defvar
2407
2408@defvar regexp/extended
2409Compile an extended regular expression rather than a basic
2410regexp. This is the default behavior; this flag will not
2411usually be needed. If a call to @code{make-regexp} includes
2412both @code{regexp/basic} and @code{regexp/extended} flags, the
2413one which comes last will override the earlier one.
2414@end defvar
2415@end deffn
2416
2417@deffn {Scheme Procedure} regexp-exec rx str [start [flags]]
2418@deffnx {C Function} scm_regexp_exec (rx, str, start, flags)
2419Match the compiled regular expression @var{rx} against
2420@code{str}. If the optional integer @var{start} argument is
2421provided, begin matching from that position in the string.
2422Return a match structure describing the results of the match,
2423or @code{#f} if no match could be found.
2424
2425The @var{flags} arguments change the matching behavior.
2426The following flags may be supplied:
2427
2428@defvar regexp/notbol
2429Operator @samp{^} always fails (unless @code{regexp/newline}
2430is used). Use this when the beginning of the string should
2431not be considered the beginning of a line.
2432@end defvar
2433
2434@defvar regexp/noteol
2435Operator @samp{$} always fails (unless @code{regexp/newline}
2436is used). Use this when the end of the string should not be
2437considered the end of a line.
2438@end defvar
2439@end deffn
2440
2441@lisp
2442;; Regexp to match uppercase letters
2443(define r (make-regexp "[A-Z]*"))
2444
2445;; Regexp to match letters, ignoring case
2446(define ri (make-regexp "[A-Z]*" regexp/icase))
2447
2448;; Search for bob using regexp r
2449(match:substring (regexp-exec r "bob"))
2450@result{} "" ; no match
2451
2452;; Search for bob using regexp ri
2453(match:substring (regexp-exec ri "Bob"))
2454@result{} "Bob" ; matched case insensitive
2455@end lisp
2456
2457@deffn {Scheme Procedure} regexp? obj
2458@deffnx {C Function} scm_regexp_p (obj)
2459Return @code{#t} if @var{obj} is a compiled regular expression,
2460or @code{#f} otherwise.
2461@end deffn
2462
2463Regular expressions are commonly used to find patterns in one string and
2464replace them with the contents of another string.
2465
2466@c begin (scm-doc-string "regex.scm" "regexp-substitute")
2467@deffn {Scheme Procedure} regexp-substitute port match [item@dots{}]
2468Write to the output port @var{port} selected contents of the match
2469structure @var{match}. Each @var{item} specifies what should be
2470written, and may be one of the following arguments:
2471
2472@itemize @bullet
2473@item
2474A string. String arguments are written out verbatim.
2475
2476@item
2477An integer. The submatch with that number is written.
2478
2479@item
2480The symbol @samp{pre}. The portion of the matched string preceding
2481the regexp match is written.
2482
2483@item
2484The symbol @samp{post}. The portion of the matched string following
2485the regexp match is written.
2486@end itemize
2487
2488The @var{port} argument may be @code{#f}, in which case nothing is
2489written; instead, @code{regexp-substitute} constructs a string from the
2490specified @var{item}s and returns that.
2491@end deffn
2492
2493The following example takes a regular expression that matches a standard
2494@sc{yyyymmdd}-format date such as @code{"20020828"}. The
2495@code{regexp-substitute} call returns a string computed from the
2496information in the match structure, consisting of the fields and text
2497from the original string reordered and reformatted.
2498
2499@lisp
2500(define date-regex "([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])")
2501(define s "Date 20020429 12am.")
2502(define sm (string-match date-regex s))
2503(regexp-substitute #f sm 'pre 2 "-" 3 "-" 1 'post " (" 0 ")")
2504@result{} "Date 04-29-2002 12am. (20020429)"
2505@end lisp
2506
2507@c begin (scm-doc-string "regex.scm" "regexp-substitute")
2508@deffn {Scheme Procedure} regexp-substitute/global port regexp target [item@dots{}]
2509Similar to @code{regexp-substitute}, but can be used to perform global
2510substitutions on @var{str}. Instead of taking a match structure as an
2511argument, @code{regexp-substitute/global} takes two string arguments: a
2512@var{regexp} string describing a regular expression, and a @var{target}
2513string which should be matched against this regular expression.
2514
2515Each @var{item} behaves as in @code{regexp-substitute}, with the
2516following exceptions:
2517
2518@itemize @bullet
2519@item
2520A function may be supplied. When this function is called, it will be
2521passed one argument: a match structure for a given regular expression
2522match. It should return a string to be written out to @var{port}.
2523
2524@item
2525The @samp{post} symbol causes @code{regexp-substitute/global} to recurse
2526on the unmatched portion of @var{str}. This @emph{must} be supplied in
2527order to perform global search-and-replace on @var{str}; if it is not
2528present among the @var{item}s, then @code{regexp-substitute/global} will
2529return after processing a single match.
2530@end itemize
2531@end deffn
2532
2533The example above for @code{regexp-substitute} could be rewritten as
2534follows to remove the @code{string-match} stage:
2535
2536@lisp
2537(define date-regex "([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])")
2538(define s "Date 20020429 12am.")
2539(regexp-substitute/global #f date-regex s
2540 'pre 2 "-" 3 "-" 1 'post " (" 0 ")")
2541@result{} "Date 04-29-2002 12am. (20020429)"
2542@end lisp
2543
2544
2545@node Match Structures
2546@subsubsection Match Structures
2547
2548@cindex match structures
2549
2550A @dfn{match structure} is the object returned by @code{string-match} and
2551@code{regexp-exec}. It describes which portion of a string, if any,
2552matched the given regular expression. Match structures include: a
2553reference to the string that was checked for matches; the starting and
2554ending positions of the regexp match; and, if the regexp included any
2555parenthesized subexpressions, the starting and ending positions of each
2556submatch.
2557
2558In each of the regexp match functions described below, the @code{match}
2559argument must be a match structure returned by a previous call to
2560@code{string-match} or @code{regexp-exec}. Most of these functions
2561return some information about the original target string that was
2562matched against a regular expression; we will call that string
2563@var{target} for easy reference.
2564
2565@c begin (scm-doc-string "regex.scm" "regexp-match?")
2566@deffn {Scheme Procedure} regexp-match? obj
2567Return @code{#t} if @var{obj} is a match structure returned by a
2568previous call to @code{regexp-exec}, or @code{#f} otherwise.
2569@end deffn
2570
2571@c begin (scm-doc-string "regex.scm" "match:substring")
2572@deffn {Scheme Procedure} match:substring match [n]
2573Return the portion of @var{target} matched by subexpression number
2574@var{n}. Submatch 0 (the default) represents the entire regexp match.
2575If the regular expression as a whole matched, but the subexpression
2576number @var{n} did not match, return @code{#f}.
2577@end deffn
2578
2579@lisp
2580(define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
2581(match:substring s)
2582@result{} "2002"
2583
2584;; match starting at offset 6 in the string
2585(match:substring
2586 (string-match "[0-9][0-9][0-9][0-9]" "blah987654" 6))
2587@result{} "7654"
2588@end lisp
2589
2590@c begin (scm-doc-string "regex.scm" "match:start")
2591@deffn {Scheme Procedure} match:start match [n]
2592Return the starting position of submatch number @var{n}.
2593@end deffn
2594
2595In the following example, the result is 4, since the match starts at
2596character index 4:
2597
2598@lisp
2599(define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
2600(match:start s)
2601@result{} 4
2602@end lisp
2603
2604@c begin (scm-doc-string "regex.scm" "match:end")
2605@deffn {Scheme Procedure} match:end match [n]
2606Return the ending position of submatch number @var{n}.
2607@end deffn
2608
2609In the following example, the result is 8, since the match runs between
2610characters 4 and 8 (i.e. the ``2002'').
2611
2612@lisp
2613(define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
2614(match:end s)
2615@result{} 8
2616@end lisp
2617
2618@c begin (scm-doc-string "regex.scm" "match:prefix")
2619@deffn {Scheme Procedure} match:prefix match
2620Return the unmatched portion of @var{target} preceding the regexp match.
2621
2622@lisp
2623(define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
2624(match:prefix s)
2625@result{} "blah"
2626@end lisp
2627@end deffn
2628
2629@c begin (scm-doc-string "regex.scm" "match:suffix")
2630@deffn {Scheme Procedure} match:suffix match
2631Return the unmatched portion of @var{target} following the regexp match.
2632@end deffn
2633
2634@lisp
2635(define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
2636(match:suffix s)
2637@result{} "foo"
2638@end lisp
2639
2640@c begin (scm-doc-string "regex.scm" "match:count")
2641@deffn {Scheme Procedure} match:count match
2642Return the number of parenthesized subexpressions from @var{match}.
2643Note that the entire regular expression match itself counts as a
2644subexpression, and failed submatches are included in the count.
2645@end deffn
2646
2647@c begin (scm-doc-string "regex.scm" "match:string")
2648@deffn {Scheme Procedure} match:string match
2649Return the original @var{target} string.
2650@end deffn
2651
2652@lisp
2653(define s (string-match "[0-9][0-9][0-9][0-9]" "blah2002foo"))
2654(match:string s)
2655@result{} "blah2002foo"
2656@end lisp
2657
2658
2659@node Backslash Escapes
2660@subsubsection Backslash Escapes
2661
2662Sometimes you will want a regexp to match characters like @samp{*} or
2663@samp{$} exactly. For example, to check whether a particular string
2664represents a menu entry from an Info node, it would be useful to match
2665it against a regexp like @samp{^* [^:]*::}. However, this won't work;
2666because the asterisk is a metacharacter, it won't match the @samp{*} at
2667the beginning of the string. In this case, we want to make the first
2668asterisk un-magic.
2669
2670You can do this by preceding the metacharacter with a backslash
2671character @samp{\}. (This is also called @dfn{quoting} the
2672metacharacter, and is known as a @dfn{backslash escape}.) When Guile
2673sees a backslash in a regular expression, it considers the following
2674glyph to be an ordinary character, no matter what special meaning it
2675would ordinarily have. Therefore, we can make the above example work by
2676changing the regexp to @samp{^\* [^:]*::}. The @samp{\*} sequence tells
2677the regular expression engine to match only a single asterisk in the
2678target string.
2679
2680Since the backslash is itself a metacharacter, you may force a regexp to
2681match a backslash in the target string by preceding the backslash with
2682itself. For example, to find variable references in a @TeX{} program,
2683you might want to find occurrences of the string @samp{\let\} followed
2684by any number of alphabetic characters. The regular expression
2685@samp{\\let\\[A-Za-z]*} would do this: the double backslashes in the
2686regexp each match a single backslash in the target string.
2687
2688@c begin (scm-doc-string "regex.scm" "regexp-quote")
2689@deffn {Scheme Procedure} regexp-quote str
2690Quote each special character found in @var{str} with a backslash, and
2691return the resulting string.
2692@end deffn
2693
2694@strong{Very important:} Using backslash escapes in Guile source code
2695(as in Emacs Lisp or C) can be tricky, because the backslash character
2696has special meaning for the Guile reader. For example, if Guile
2697encounters the character sequence @samp{\n} in the middle of a string
2698while processing Scheme code, it replaces those characters with a
2699newline character. Similarly, the character sequence @samp{\t} is
2700replaced by a horizontal tab. Several of these @dfn{escape sequences}
2701are processed by the Guile reader before your code is executed.
2702Unrecognized escape sequences are ignored: if the characters @samp{\*}
2703appear in a string, they will be translated to the single character
2704@samp{*}.
2705
2706This translation is obviously undesirable for regular expressions, since
2707we want to be able to include backslashes in a string in order to
2708escape regexp metacharacters. Therefore, to make sure that a backslash
2709is preserved in a string in your Guile program, you must use @emph{two}
2710consecutive backslashes:
2711
2712@lisp
2713(define Info-menu-entry-pattern (make-regexp "^\\* [^:]*"))
2714@end lisp
2715
2716The string in this example is preprocessed by the Guile reader before
2717any code is executed. The resulting argument to @code{make-regexp} is
2718the string @samp{^\* [^:]*}, which is what we really want.
2719
2720This also means that in order to write a regular expression that matches
2721a single backslash character, the regular expression string in the
2722source code must include @emph{four} backslashes. Each consecutive pair
2723of backslashes gets translated by the Guile reader to a single
2724backslash, and the resulting double-backslash is interpreted by the
2725regexp engine as matching a single backslash character. Hence:
2726
2727@lisp
2728(define tex-variable-pattern (make-regexp "\\\\let\\\\=[A-Za-z]*"))
2729@end lisp
2730
2731The reason for the unwieldiness of this syntax is historical. Both
2732regular expression pattern matchers and Unix string processing systems
2733have traditionally used backslashes with the special meanings
2734described above. The POSIX regular expression specification and ANSI C
2735standard both require these semantics. Attempting to abandon either
2736convention would cause other kinds of compatibility problems, possibly
2737more severe ones. Therefore, without extending the Scheme reader to
2738support strings with different quoting conventions (an ungainly and
2739confusing extension when implemented in other languages), we must adhere
2740to this cumbersome escape syntax.
2741
2742
2743@node Symbols
2744@subsection Symbols
2745@tpindex Symbols
2746
2747Symbols in Scheme are widely used in three ways: as items of discrete
2748data, as lookup keys for alists and hash tables, and to denote variable
2749references.
2750
2751A @dfn{symbol} is similar to a string in that it is defined by a
2752sequence of characters. The sequence of characters is known as the
2753symbol's @dfn{name}. In the usual case --- that is, where the symbol's
2754name doesn't include any characters that could be confused with other
2755elements of Scheme syntax --- a symbol is written in a Scheme program by
2756writing the sequence of characters that make up the name, @emph{without}
2757any quotation marks or other special syntax. For example, the symbol
2758whose name is ``multiply-by-2'' is written, simply:
2759
2760@lisp
2761multiply-by-2
2762@end lisp
2763
2764Notice how this differs from a @emph{string} with contents
2765``multiply-by-2'', which is written with double quotation marks, like
2766this:
2767
2768@lisp
2769"multiply-by-2"
2770@end lisp
2771
2772Looking beyond how they are written, symbols are different from strings
2773in two important respects.
2774
2775The first important difference is uniqueness. If the same-looking
2776string is read twice from two different places in a program, the result
2777is two @emph{different} string objects whose contents just happen to be
2778the same. If, on the other hand, the same-looking symbol is read twice
2779from two different places in a program, the result is the @emph{same}
2780symbol object both times.
2781
2782Given two read symbols, you can use @code{eq?} to test whether they are
2783the same (that is, have the same name). @code{eq?} is the most
2784efficient comparison operator in Scheme, and comparing two symbols like
2785this is as fast as comparing, for example, two numbers. Given two
2786strings, on the other hand, you must use @code{equal?} or
2787@code{string=?}, which are much slower comparison operators, to
2788determine whether the strings have the same contents.
2789
2790@lisp
2791(define sym1 (quote hello))
2792(define sym2 (quote hello))
2793(eq? sym1 sym2) @result{} #t
2794
2795(define str1 "hello")
2796(define str2 "hello")
2797(eq? str1 str2) @result{} #f
2798(equal? str1 str2) @result{} #t
2799@end lisp
2800
2801The second important difference is that symbols, unlike strings, are not
2802self-evaluating. This is why we need the @code{(quote @dots{})}s in the
2803example above: @code{(quote hello)} evaluates to the symbol named
2804"hello" itself, whereas an unquoted @code{hello} is @emph{read} as the
2805symbol named "hello" and evaluated as a variable reference @dots{} about
2806which more below (@pxref{Symbol Variables}).
2807
2808@menu
2809* Symbol Data:: Symbols as discrete data.
2810* Symbol Keys:: Symbols as lookup keys.
2811* Symbol Variables:: Symbols as denoting variables.
2812* Symbol Primitives:: Operations related to symbols.
2813* Symbol Props:: Function slots and property lists.
2814* Symbol Read Syntax:: Extended read syntax for symbols.
2815* Symbol Uninterned:: Uninterned symbols.
2816@end menu
2817
2818
2819@node Symbol Data
2820@subsubsection Symbols as Discrete Data
2821
2822Numbers and symbols are similar to the extent that they both lend
2823themselves to @code{eq?} comparison. But symbols are more descriptive
2824than numbers, because a symbol's name can be used directly to describe
2825the concept for which that symbol stands.
2826
2827For example, imagine that you need to represent some colours in a
2828computer program. Using numbers, you would have to choose arbitrarily
2829some mapping between numbers and colours, and then take care to use that
2830mapping consistently:
2831
2832@lisp
2833;; 1=red, 2=green, 3=purple
2834
2835(if (eq? (colour-of car) 1)
2836 ...)
2837@end lisp
2838
2839@noindent
2840You can make the mapping more explicit and the code more readable by
2841defining constants:
2842
2843@lisp
2844(define red 1)
2845(define green 2)
2846(define purple 3)
2847
2848(if (eq? (colour-of car) red)
2849 ...)
2850@end lisp
2851
2852@noindent
2853But the simplest and clearest approach is not to use numbers at all, but
2854symbols whose names specify the colours that they refer to:
2855
2856@lisp
2857(if (eq? (colour-of car) 'red)
2858 ...)
2859@end lisp
2860
2861The descriptive advantages of symbols over numbers increase as the set
2862of concepts that you want to describe grows. Suppose that a car object
2863can have other properties as well, such as whether it has or uses:
2864
2865@itemize @bullet
2866@item
2867automatic or manual transmission
2868@item
2869leaded or unleaded fuel
2870@item
2871power steering (or not).
2872@end itemize
2873
2874@noindent
2875Then a car's combined property set could be naturally represented and
2876manipulated as a list of symbols:
2877
2878@lisp
2879(properties-of car1)
2880@result{}
2881(red manual unleaded power-steering)
2882
2883(if (memq 'power-steering (properties-of car1))
2884 (display "Unfit people can drive this car.\n")
2885 (display "You'll need strong arms to drive this car!\n"))
2886@print{}
2887Unfit people can drive this car.
2888@end lisp
2889
2890Remember, the fundamental property of symbols that we are relying on
2891here is that an occurrence of @code{'red} in one part of a program is an
2892@emph{indistinguishable} symbol from an occurrence of @code{'red} in
2893another part of a program; this means that symbols can usefully be
2894compared using @code{eq?}. At the same time, symbols have naturally
2895descriptive names. This combination of efficiency and descriptive power
2896makes them ideal for use as discrete data.
2897
2898
2899@node Symbol Keys
2900@subsubsection Symbols as Lookup Keys
2901
2902Given their efficiency and descriptive power, it is natural to use
2903symbols as the keys in an association list or hash table.
2904
2905To illustrate this, consider a more structured representation of the car
2906properties example from the preceding subsection. Rather than
2907mixing all the properties up together in a flat list, we could use an
2908association list like this:
2909
2910@lisp
2911(define car1-properties '((colour . red)
2912 (transmission . manual)
2913 (fuel . unleaded)
2914 (steering . power-assisted)))
2915@end lisp
2916
2917Notice how this structure is more explicit and extensible than the flat
2918list. For example it makes clear that @code{manual} refers to the
2919transmission rather than, say, the windows or the locking of the car.
2920It also allows further properties to use the same symbols among their
2921possible values without becoming ambiguous:
2922
2923@lisp
2924(define car1-properties '((colour . red)
2925 (transmission . manual)
2926 (fuel . unleaded)
2927 (steering . power-assisted)
2928 (seat-colour . red)
2929 (locking . manual)))
2930@end lisp
2931
2932With a representation like this, it is easy to use the efficient
2933@code{assq-XXX} family of procedures (@pxref{Association Lists}) to
2934extract or change individual pieces of information:
2935
2936@lisp
2937(assq-ref car1-properties 'fuel) @result{} unleaded
2938(assq-ref car1-properties 'transmission) @result{} manual
2939
2940(assq-set! car1-properties 'seat-colour 'black)
2941@result{}
2942((colour . red)
2943 (transmission . manual)
2944 (fuel . unleaded)
2945 (steering . power-assisted)
2946 (seat-colour . black)
2947 (locking . manual)))
2948@end lisp
2949
2950Hash tables also have keys, and exactly the same arguments apply to the
2951use of symbols in hash tables as in association lists. The hash value
2952that Guile uses to decide where to add a symbol-keyed entry to a hash
2953table can be obtained by calling the @code{symbol-hash} procedure:
2954
2955@deffn {Scheme Procedure} symbol-hash symbol
2956@deffnx {C Function} scm_symbol_hash (symbol)
2957Return a hash value for @var{symbol}.
2958@end deffn
2959
2960See @ref{Hash Tables} for information about hash tables in general, and
2961for why you might choose to use a hash table rather than an association
2962list.
2963
2964
2965@node Symbol Variables
2966@subsubsection Symbols as Denoting Variables
2967
2968When an unquoted symbol in a Scheme program is evaluated, it is
2969interpreted as a variable reference, and the result of the evaluation is
2970the appropriate variable's value.
2971
2972For example, when the expression @code{(string-length "abcd")} is read
2973and evaluated, the sequence of characters @code{string-length} is read
2974as the symbol whose name is "string-length". This symbol is associated
2975with a variable whose value is the procedure that implements string
2976length calculation. Therefore evaluation of the @code{string-length}
2977symbol results in that procedure.
2978
2979The details of the connection between an unquoted symbol and the
2980variable to which it refers are explained elsewhere. See @ref{Binding
2981Constructs}, for how associations between symbols and variables are
2982created, and @ref{Modules}, for how those associations are affected by
2983Guile's module system.
2984
2985
2986@node Symbol Primitives
2987@subsubsection Operations Related to Symbols
2988
2989Given any Scheme value, you can determine whether it is a symbol using
2990the @code{symbol?} primitive:
2991
2992@rnindex symbol?
2993@deffn {Scheme Procedure} symbol? obj
2994@deffnx {C Function} scm_symbol_p (obj)
2995Return @code{#t} if @var{obj} is a symbol, otherwise return
2996@code{#f}.
2997@end deffn
2998
2999Once you know that you have a symbol, you can obtain its name as a
3000string by calling @code{symbol->string}. Note that Guile differs by
3001default from R5RS on the details of @code{symbol->string} as regards
3002case-sensitivity:
3003
3004@rnindex symbol->string
3005@deffn {Scheme Procedure} symbol->string s
3006@deffnx {C Function} scm_symbol_to_string (s)
3007Return the name of symbol @var{s} as a string. By default, Guile reads
3008symbols case-sensitively, so the string returned will have the same case
3009variation as the sequence of characters that caused @var{s} to be
3010created.
3011
3012If Guile is set to read symbols case-insensitively (as specified by
3013R5RS), and @var{s} comes into being as part of a literal expression
3014(@pxref{Literal expressions,,,r5rs, The Revised^5 Report on Scheme}) or
3015by a call to the @code{read} or @code{string-ci->symbol} procedures,
3016Guile converts any alphabetic characters in the symbol's name to
3017lower case before creating the symbol object, so the string returned
3018here will be in lower case.
3019
3020If @var{s} was created by @code{string->symbol}, the case of characters
3021in the string returned will be the same as that in the string that was
3022passed to @code{string->symbol}, regardless of Guile's case-sensitivity
3023setting at the time @var{s} was created.
3024
3025It is an error to apply mutation procedures like @code{string-set!} to
3026strings returned by this procedure.
3027@end deffn
3028
3029Most symbols are created by writing them literally in code. However it
3030is also possible to create symbols programmatically using the following
3031@code{string->symbol} and @code{string-ci->symbol} procedures:
3032
3033@rnindex string->symbol
3034@deffn {Scheme Procedure} string->symbol string
3035@deffnx {C Function} scm_string_to_symbol (string)
3036Return the symbol whose name is @var{string}. This procedure can create
3037symbols with names containing special characters or letters in the
3038non-standard case, but it is usually a bad idea to create such symbols
3039because in some implementations of Scheme they cannot be read as
3040themselves.
3041@end deffn
3042
3043@deffn {Scheme Procedure} string-ci->symbol str
3044@deffnx {C Function} scm_string_ci_to_symbol (str)
3045Return the symbol whose name is @var{str}. If Guile is currently
3046reading symbols case-insensitively, @var{str} is converted to lowercase
3047before the returned symbol is looked up or created.
3048@end deffn
3049
3050The following examples illustrate Guile's detailed behaviour as regards
3051the case-sensitivity of symbols:
3052
3053@lisp
3054(read-enable 'case-insensitive) ; R5RS compliant behaviour
3055
3056(symbol->string 'flying-fish) @result{} "flying-fish"
3057(symbol->string 'Martin) @result{} "martin"
3058(symbol->string
3059 (string->symbol "Malvina")) @result{} "Malvina"
3060
3061(eq? 'mISSISSIppi 'mississippi) @result{} #t
3062(string->symbol "mISSISSIppi") @result{} mISSISSIppi
3063(eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
3064(eq? 'LolliPop
3065 (string->symbol (symbol->string 'LolliPop))) @result{} #t
3066(string=? "K. Harper, M.D."
3067 (symbol->string
3068 (string->symbol "K. Harper, M.D."))) @result{} #t
3069
3070(read-disable 'case-insensitive) ; Guile default behaviour
3071
3072(symbol->string 'flying-fish) @result{} "flying-fish"
3073(symbol->string 'Martin) @result{} "Martin"
3074(symbol->string
3075 (string->symbol "Malvina")) @result{} "Malvina"
3076
3077(eq? 'mISSISSIppi 'mississippi) @result{} #f
3078(string->symbol "mISSISSIppi") @result{} mISSISSIppi
3079(eq? 'bitBlt (string->symbol "bitBlt")) @result{} #t
3080(eq? 'LolliPop
3081 (string->symbol (symbol->string 'LolliPop))) @result{} #t
3082(string=? "K. Harper, M.D."
3083 (symbol->string
3084 (string->symbol "K. Harper, M.D."))) @result{} #t
3085@end lisp
3086
3087From C, there are lower level functions that construct a Scheme symbol
3088from a null terminated C string or from a sequence of bytes whose length
3089is specified explicitly.
3090
3091@deffn {C Function} scm_str2symbol (const char * name)
3092@deffnx {C Function} scm_mem2symbol (const char * name, size_t len)
3093Construct and return a Scheme symbol whose name is specified by
3094@var{name}. For @code{scm_str2symbol} @var{name} must be null
3095terminated; For @code{scm_mem2symbol} the length of @var{name} is
3096specified explicitly by @var{len}.
3097@end deffn
3098
3099Finally, some applications, especially those that generate new Scheme
3100code dynamically, need to generate symbols for use in the generated
3101code. The @code{gensym} primitive meets this need:
3102
3103@deffn {Scheme Procedure} gensym [prefix]
3104@deffnx {C Function} scm_gensym (prefix)
3105Create a new symbol with a name constructed from a prefix and a counter
3106value. The string @var{prefix} can be specified as an optional
3107argument. Default prefix is @samp{@w{ g}}. The counter is increased by 1
3108at each call. There is no provision for resetting the counter.
3109@end deffn
3110
3111The symbols generated by @code{gensym} are @emph{likely} to be unique,
3112since their names begin with a space and it is only otherwise possible
3113to generate such symbols if a programmer goes out of their way to do
3114so. Uniqueness can be guaranteed by instead using uninterned symbols
3115(@pxref{Symbol Uninterned}), though they can't be usefully written out
3116and read back in.
3117
3118
3119@node Symbol Props
3120@subsubsection Function Slots and Property Lists
3121
3122In traditional Lisp dialects, symbols are often understood as having
3123three kinds of value at once:
3124
3125@itemize @bullet
3126@item
3127a @dfn{variable} value, which is used when the symbol appears in
3128code in a variable reference context
3129
3130@item
3131a @dfn{function} value, which is used when the symbol appears in
3132code in a function name position (i.e. as the first element in an
3133unquoted list)
3134
3135@item
3136a @dfn{property list} value, which is used when the symbol is given as
3137the first argument to Lisp's @code{put} or @code{get} functions.
3138@end itemize
3139
3140Although Scheme (as one of its simplifications with respect to Lisp)
3141does away with the distinction between variable and function namespaces,
3142Guile currently retains some elements of the traditional structure in
3143case they turn out to be useful when implementing translators for other
3144languages, in particular Emacs Lisp.
3145
3146Specifically, Guile symbols have two extra slots. for a symbol's
3147property list, and for its ``function value.'' The following procedures
3148are provided to access these slots.
3149
3150@deffn {Scheme Procedure} symbol-fref symbol
3151@deffnx {C Function} scm_symbol_fref (symbol)
3152Return the contents of @var{symbol}'s @dfn{function slot}.
3153@end deffn
3154
3155@deffn {Scheme Procedure} symbol-fset! symbol value
3156@deffnx {C Function} scm_symbol_fset_x (symbol, value)
3157Set the contents of @var{symbol}'s function slot to @var{value}.
3158@end deffn
3159
3160@deffn {Scheme Procedure} symbol-pref symbol
3161@deffnx {C Function} scm_symbol_pref (symbol)
3162Return the @dfn{property list} currently associated with @var{symbol}.
3163@end deffn
3164
3165@deffn {Scheme Procedure} symbol-pset! symbol value
3166@deffnx {C Function} scm_symbol_pset_x (symbol, value)
3167Set @var{symbol}'s property list to @var{value}.
3168@end deffn
3169
3170@deffn {Scheme Procedure} symbol-property sym prop
3171From @var{sym}'s property list, return the value for property
3172@var{prop}. The assumption is that @var{sym}'s property list is an
3173association list whose keys are distinguished from each other using
3174@code{equal?}; @var{prop} should be one of the keys in that list. If
3175the property list has no entry for @var{prop}, @code{symbol-property}
3176returns @code{#f}.
3177@end deffn
3178
3179@deffn {Scheme Procedure} set-symbol-property! sym prop val
3180In @var{sym}'s property list, set the value for property @var{prop} to
3181@var{val}, or add a new entry for @var{prop}, with value @var{val}, if
3182none already exists. For the structure of the property list, see
3183@code{symbol-property}.
3184@end deffn
3185
3186@deffn {Scheme Procedure} symbol-property-remove! sym prop
3187From @var{sym}'s property list, remove the entry for property
3188@var{prop}, if there is one. For the structure of the property list,
3189see @code{symbol-property}.
3190@end deffn
3191
3192Support for these extra slots may be removed in a future release, and it
3193is probably better to avoid using them. (In release 1.6, Guile itself
3194uses the property list slot sparingly, and the function slot not at
3195all.) For a more modern and Schemely approach to properties, see
3196@ref{Object Properties}.
3197
3198
3199@node Symbol Read Syntax
3200@subsubsection Extended Read Syntax for Symbols
3201
3202The read syntax for a symbol is a sequence of letters, digits, and
3203@dfn{extended alphabetic characters}, beginning with a character that
3204cannot begin a number. In addition, the special cases of @code{+},
3205@code{-}, and @code{...} are read as symbols even though numbers can
3206begin with @code{+}, @code{-} or @code{.}.
3207
3208Extended alphabetic characters may be used within identifiers as if
3209they were letters. The set of extended alphabetic characters is:
3210
3211@example
3212! $ % & * + - . / : < = > ? @@ ^ _ ~
3213@end example
3214
3215In addition to the standard read syntax defined above (which is taken
3216from R5RS (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on
3217Scheme})), Guile provides an extended symbol read syntax that allows the
3218inclusion of unusual characters such as space characters, newlines and
3219parentheses. If (for whatever reason) you need to write a symbol
3220containing characters not mentioned above, you can do so as follows.
3221
3222@itemize @bullet
3223@item
3224Begin the symbol with the characters @code{#@{},
3225
3226@item
3227write the characters of the symbol and
3228
3229@item
3230finish the symbol with the characters @code{@}#}.
3231@end itemize
3232
3233Here are a few examples of this form of read syntax. The first symbol
3234needs to use extended syntax because it contains a space character, the
3235second because it contains a line break, and the last because it looks
3236like a number.
3237
3238@lisp
3239#@{foo bar@}#
3240
3241#@{what
3242ever@}#
3243
3244#@{4242@}#
3245@end lisp
3246
3247Although Guile provides this extended read syntax for symbols,
3248widespread usage of it is discouraged because it is not portable and not
3249very readable.
3250
3251
3252@node Symbol Uninterned
3253@subsubsection Uninterned Symbols
3254
3255What makes symbols useful is that they are automatically kept unique.
3256There are no two symbols that are distinct objects but have the same
3257name. But of course, there is no rule without exception. In addition
3258to the normal symbols that have been discussed up to now, you can also
3259create special @dfn{uninterned} symbols that behave slightly
3260differently.
3261
3262To understand what is different about them and why they might be useful,
3263we look at how normal symbols are actually kept unique.
3264
3265Whenever Guile wants to find the symbol with a specific name, for
3266example during @code{read} or when executing @code{string->symbol}, it
3267first looks into a table of all existing symbols to find out whether a
3268symbol with the given name already exists. When this is the case, Guile
3269just returns that symbol. When not, a new symbol with the name is
3270created and entered into the table so that it can be found later.
3271
3272Sometimes you might want to create a symbol that is guaranteed `fresh',
3273i.e. a symbol that did not exist previously. You might also want to
3274somehow guarantee that no one else will ever unintentionally stumble
3275across your symbol in the future. These properties of a symbol are
3276often needed when generating code during macro expansion. When
3277introducing new temporary variables, you want to guarantee that they
3278don't conflict with variables in other people's code.
3279
3280The simplest way to arrange for this is to create a new symbol but
3281not enter it into the global table of all symbols. That way, no one
3282will ever get access to your symbol by chance. Symbols that are not in
3283the table are called @dfn{uninterned}. Of course, symbols that
3284@emph{are} in the table are called @dfn{interned}.
3285
3286You create new uninterned symbols with the function @code{make-symbol}.
3287You can test whether a symbol is interned or not with
3288@code{symbol-interned?}.
3289
3290Uninterned symbols break the rule that the name of a symbol uniquely
3291identifies the symbol object. Because of this, they can not be written
3292out and read back in like interned symbols. Currently, Guile has no
3293support for reading uninterned symbols. Note that the function
3294@code{gensym} does not return uninterned symbols for this reason.
3295
3296@deffn {Scheme Procedure} make-symbol name
3297@deffnx {C Function} scm_make_symbol (name)
3298Return a new uninterned symbol with the name @var{name}. The returned
3299symbol is guaranteed to be unique and future calls to
3300@code{string->symbol} will not return it.
3301@end deffn
3302
3303@deffn {Scheme Procedure} symbol-interned? symbol
3304@deffnx {C Function} scm_symbol_interned_p (symbol)
3305Return @code{#t} if @var{symbol} is interned, otherwise return
3306@code{#f}.
3307@end deffn
3308
3309For example:
3310
3311@lisp
3312(define foo-1 (string->symbol "foo"))
3313(define foo-2 (string->symbol "foo"))
3314(define foo-3 (make-symbol "foo"))
3315(define foo-4 (make-symbol "foo"))
3316
3317(eq? foo-1 foo-2)
3318@result{} #t
3319; Two interned symbols with the same name are the same object,
3320
3321(eq? foo-1 foo-3)
3322@result{} #f
3323; but a call to make-symbol with the same name returns a
3324; distinct object.
3325
3326(eq? foo-3 foo-4)
3327@result{} #f
3328; A call to make-symbol always returns a new object, even for
3329; the same name.
3330
3331foo-3
3332@result{} #<uninterned-symbol foo 8085290>
3333; Uninterned symbols print differently from interned symbols,
3334
3335(symbol? foo-3)
3336@result{} #t
3337; but they are still symbols,
3338
3339(symbol-interned? foo-3)
3340@result{} #f
3341; just not interned.
3342@end lisp
3343
3344
3345@node Keywords
3346@subsection Keywords
3347@tpindex Keywords
3348
3349Keywords are self-evaluating objects with a convenient read syntax that
3350makes them easy to type.
3351
3352Guile's keyword support conforms to R5RS, and adds a (switchable) read
3353syntax extension to permit keywords to begin with @code{:} as well as
3354@code{#:}.
3355
3356@menu
3357* Why Use Keywords?:: Motivation for keyword usage.
3358* Coding With Keywords:: How to use keywords.
3359* Keyword Read Syntax:: Read syntax for keywords.
3360* Keyword Procedures:: Procedures for dealing with keywords.
3361* Keyword Primitives:: The underlying primitive procedures.
3362@end menu
3363
3364@node Why Use Keywords?
3365@subsubsection Why Use Keywords?
3366
3367Keywords are useful in contexts where a program or procedure wants to be
3368able to accept a large number of optional arguments without making its
3369interface unmanageable.
3370
3371To illustrate this, consider a hypothetical @code{make-window}
3372procedure, which creates a new window on the screen for drawing into
3373using some graphical toolkit. There are many parameters that the caller
3374might like to specify, but which could also be sensibly defaulted, for
3375example:
3376
3377@itemize @bullet
3378@item
3379color depth -- Default: the color depth for the screen
3380
3381@item
3382background color -- Default: white
3383
3384@item
3385width -- Default: 600
3386
3387@item
3388height -- Default: 400
3389@end itemize
3390
3391If @code{make-window} did not use keywords, the caller would have to
3392pass in a value for each possible argument, remembering the correct
3393argument order and using a special value to indicate the default value
3394for that argument:
3395
3396@lisp
3397(make-window 'default ;; Color depth
3398 'default ;; Background color
3399 800 ;; Width
3400 100 ;; Height
3401 @dots{}) ;; More make-window arguments
3402@end lisp
3403
3404With keywords, on the other hand, defaulted arguments are omitted, and
3405non-default arguments are clearly tagged by the appropriate keyword. As
3406a result, the invocation becomes much clearer:
3407
3408@lisp
3409(make-window #:width 800 #:height 100)
3410@end lisp
3411
3412On the other hand, for a simpler procedure with few arguments, the use
3413of keywords would be a hindrance rather than a help. The primitive
3414procedure @code{cons}, for example, would not be improved if it had to
3415be invoked as
3416
3417@lisp
3418(cons #:car x #:cdr y)
3419@end lisp
3420
3421So the decision whether to use keywords or not is purely pragmatic: use
3422them if they will clarify the procedure invocation at point of call.
3423
3424@node Coding With Keywords
3425@subsubsection Coding With Keywords
3426
3427If a procedure wants to support keywords, it should take a rest argument
3428and then use whatever means is convenient to extract keywords and their
3429corresponding arguments from the contents of that rest argument.
3430
3431The following example illustrates the principle: the code for
3432@code{make-window} uses a helper procedure called
3433@code{get-keyword-value} to extract individual keyword arguments from
3434the rest argument.
3435
3436@lisp
3437(define (get-keyword-value args keyword default)
3438 (let ((kv (memq keyword args)))
3439 (if (and kv (>= (length kv) 2))
3440 (cadr kv)
3441 default)))
3442
3443(define (make-window . args)
3444 (let ((depth (get-keyword-value args #:depth screen-depth))
3445 (bg (get-keyword-value args #:bg "white"))
3446 (width (get-keyword-value args #:width 800))
3447 (height (get-keyword-value args #:height 100))
3448 @dots{})
3449 @dots{}))
3450@end lisp
3451
3452But you don't need to write @code{get-keyword-value}. The @code{(ice-9
3453optargs)} module provides a set of powerful macros that you can use to
3454implement keyword-supporting procedures like this:
3455
3456@lisp
3457(use-modules (ice-9 optargs))
3458
3459(define (make-window . args)
3460 (let-keywords args #f ((depth screen-depth)
3461 (bg "white")
3462 (width 800)
3463 (height 100))
3464 ...))
3465@end lisp
3466
3467@noindent
3468Or, even more economically, like this:
3469
3470@lisp
3471(use-modules (ice-9 optargs))
3472
3473(define* (make-window #:key (depth screen-depth)
3474 (bg "white")
3475 (width 800)
3476 (height 100))
3477 ...)
3478@end lisp
3479
3480For further details on @code{let-keywords}, @code{define*} and other
3481facilities provided by the @code{(ice-9 optargs)} module, see
3482@ref{Optional Arguments}.
3483
3484
3485@node Keyword Read Syntax
3486@subsubsection Keyword Read Syntax
3487
3488Guile, by default, only recognizes the keyword syntax specified by R5RS.
3489A token of the form @code{#:NAME}, where @code{NAME} has the same syntax
3490as a Scheme symbol (@pxref{Symbol Read Syntax}), is the external
3491representation of the keyword named @code{NAME}. Keyword objects print
3492using this syntax as well, so values containing keyword objects can be
3493read back into Guile. When used in an expression, keywords are
3494self-quoting objects.
3495
3496If the @code{keyword} read option is set to @code{'prefix}, Guile also
3497recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
3498of the form @code{:NAME} are read as symbols, as required by R5RS.
3499
3500To enable and disable the alternative non-R5RS keyword syntax, you use
3501the @code{read-set!} procedure documented in @ref{User level options
3502interfaces} and @ref{Reader options}.
3503
3504@smalllisp
3505(read-set! keywords 'prefix)
3506
3507#:type
3508@result{}
3509#:type
3510
3511:type
3512@result{}
3513#:type
3514
3515(read-set! keywords #f)
3516
3517#:type
3518@result{}
3519#:type
3520
3521:type
3522@print{}
3523ERROR: In expression :type:
3524ERROR: Unbound variable: :type
3525ABORT: (unbound-variable)
3526@end smalllisp
3527
3528@node Keyword Procedures
3529@subsubsection Keyword Procedures
3530
3531The following procedures can be used for converting symbols to keywords
3532and back.
3533
3534@deffn {Scheme Procedure} symbol->keyword sym
3535Return a keyword with the same characters as in @var{sym}.
3536@end deffn
3537
3538@deffn {Scheme Procedure} keyword->symbol kw
3539Return a symbol with the same characters as in @var{kw}.
3540@end deffn
3541
3542
3543@node Keyword Primitives
3544@subsubsection Keyword Primitives
3545
3546Internally, a keyword is implemented as something like a tagged symbol,
3547where the tag identifies the keyword as being self-evaluating, and the
3548symbol, known as the keyword's @dfn{dash symbol} has the same name as
3549the keyword name but prefixed by a single dash. For example, the
3550keyword @code{#:name} has the corresponding dash symbol @code{-name}.
3551
3552Most keyword objects are constructed automatically by the reader when it
3553reads a token beginning with @code{#:}. However, if you need to
3554construct a keyword object programmatically, you can do so by calling
3555@code{make-keyword-from-dash-symbol} with the corresponding dash symbol
3556(as the reader does). The dash symbol for a keyword object can be
3557retrieved using the @code{keyword-dash-symbol} procedure.
3558
3559@deffn {Scheme Procedure} make-keyword-from-dash-symbol symbol
3560@deffnx {C Function} scm_make_keyword_from_dash_symbol (symbol)
3561Make a keyword object from a @var{symbol} that starts with a dash.
3562For example,
3563
3564@example
3565(make-keyword-from-dash-symbol '-foo)
3566@result{} #:foo
3567@end example
3568@end deffn
3569
3570@deffn {Scheme Procedure} keyword? obj
3571@deffnx {C Function} scm_keyword_p (obj)
3572Return @code{#t} if the argument @var{obj} is a keyword, else
3573@code{#f}.
3574@end deffn
3575
3576@deffn {Scheme Procedure} keyword-dash-symbol keyword
3577@deffnx {C Function} scm_keyword_dash_symbol (keyword)
3578Return the dash symbol for @var{keyword}.
3579This is the inverse of @code{make-keyword-from-dash-symbol}.
3580For example,
3581
3582@example
3583(keyword-dash-symbol #:foo)
3584@result{} -foo
3585@end example
3586@end deffn
3587
3588@deftypefn {C Function} SCM scm_c_make_keyword (char *@var{str})
3589Make a keyword object from a string. For example,
3590
3591@example
3592scm_c_make_keyword ("foo")
3593@result{} #:foo
3594@end example
3595@c
3596@c FIXME: What can be said about the string argument? Currently it's
3597@c not used after creation, but should that be documented?
3598@end deftypefn
3599
3600
3601@node Other Types
3602@subsection ``Functionality-Centric'' Data Types
3603
3604Procedures and macros are documented in their own chapter: see
3605@ref{Procedures and Macros}.
3606
3607Variable objects are documented as part of the description of Guile's
3608module system: see @ref{Variables}.
3609
3610Asyncs, dynamic roots and fluids are described in the chapter on
3611scheduling: see @ref{Scheduling}.
3612
3613Hooks are documented in the chapter on general utility functions: see
3614@ref{Hooks}.
3615
3616Ports are described in the chapter on I/O: see @ref{Input and Output}.
3617
3618
3619@c Local Variables:
3620@c TeX-master: "guile.texi"
3621@c End: