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