5 |
This chapter describes those of Guile's simple data types which are |
This chapter describes those of Guile's simple data types which are |
6 |
primarily used for their role as items of generic data. By |
primarily used for their role as items of generic data. By |
7 |
@dfn{simple} we mean data types that are not primarily used as |
@dfn{simple} we mean data types that are not primarily used as |
8 |
containers to hold other data --- i.e. pairs, lists, vectors and so on. |
containers to hold other data --- i.e.@: pairs, lists, vectors and so on. |
9 |
For the documentation of such @dfn{compound} data types, see |
For the documentation of such @dfn{compound} data types, see |
10 |
@ref{Compound Data Types}. |
@ref{Compound Data Types}. |
11 |
|
|
12 |
One of the great strengths of Scheme is that there is no straightforward |
One of the great strengths of Scheme is that there is no straightforward |
13 |
distinction between ``data'' and ``functionality''. For example, |
distinction between ``data'' and ``functionality''. For example, |
14 |
Guile's support for dynamic linking could be described |
Guile's support for dynamic linking could be described: |
15 |
|
|
16 |
@itemize @bullet |
@itemize @bullet |
17 |
@item |
@item |
59 |
|
|
60 |
@lisp |
@lisp |
61 |
(<= 3 8) |
(<= 3 8) |
62 |
@result{} |
@result{} #t |
|
#t |
|
63 |
|
|
64 |
(<= 3 -3) |
(<= 3 -3) |
65 |
@result{} |
@result{} #f |
|
#f |
|
66 |
|
|
67 |
(equal? "house" "houses") |
(equal? "house" "houses") |
68 |
@result{} |
@result{} #f |
|
#f |
|
69 |
|
|
70 |
(eq? #f #f) |
(eq? #f #f) |
71 |
@result{} |
@result{} |
79 |
|
|
80 |
@lisp |
@lisp |
81 |
(if #t "yes" "no") |
(if #t "yes" "no") |
82 |
@result{} |
@result{} "yes" |
|
"yes" |
|
83 |
|
|
84 |
(if 0 "yes" "no") |
(if 0 "yes" "no") |
85 |
@result{} |
@result{} "yes" |
|
"yes" |
|
86 |
|
|
87 |
(if #f "yes" "no") |
(if #f "yes" "no") |
88 |
@result{} |
@result{} "no" |
|
"no" |
|
89 |
@end lisp |
@end lisp |
90 |
|
|
91 |
A result of this asymmetry is that typical Scheme source code more often |
A result of this asymmetry is that typical Scheme source code more often |
127 |
|
|
128 |
You may also find it illuminating to read R5RS's presentation of numbers |
You may also find it illuminating to read R5RS's presentation of numbers |
129 |
in Scheme, which is particularly clear and accessible: see |
in Scheme, which is particularly clear and accessible: see |
130 |
@xref{Numbers,,,r5rs}. |
@ref{Numbers,,,r5rs,R5RS}. |
131 |
|
|
132 |
@menu |
@menu |
133 |
* Numerical Tower:: Scheme's numerical "tower". |
* Numerical Tower:: Scheme's numerical "tower". |
155 |
Scheme's numerical ``tower'' consists of the following categories of |
Scheme's numerical ``tower'' consists of the following categories of |
156 |
numbers: |
numbers: |
157 |
|
|
158 |
@itemize @bullet |
@table @dfn |
159 |
@item |
@item integers |
160 |
integers (whole numbers) |
Whole numbers, positive or negative; e.g.@: --5, 0, 18. |
161 |
|
|
162 |
@item |
@item rationals |
163 |
rationals (the set of numbers that can be expressed as P/Q where P and Q |
The set of numbers that can be expressed as @math{@var{p}/@var{q}} |
164 |
are integers) |
where @var{p} and @var{q} are integers; e.g.@: @math{9/16} works, but |
165 |
|
pi (an irrational number) doesn't. These include integers |
166 |
@item |
(@math{@var{n}/1}). |
167 |
real numbers (the set of numbers that describes all possible positions |
|
168 |
along a one dimensional line) |
@item real numbers |
169 |
|
The set of numbers that describes all possible positions along a |
170 |
@item |
one-dimensional line. This includes rationals as well as irrational |
171 |
complex numbers (the set of numbers that describes all possible |
numbers. |
172 |
positions in a two dimensional space) |
|
173 |
@end itemize |
@item complex numbers |
174 |
|
The set of numbers that describes all possible positions in a two |
175 |
|
dimensional 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 |
180 |
|
|
181 |
It is called a tower because each category ``sits on'' the one that |
It is called a tower because each category ``sits on'' the one that |
182 |
follows it, in the sense that every integer is also a rational, every |
follows it, in the sense that every integer is also a rational, every |
200 |
|
|
201 |
@lisp |
@lisp |
202 |
(number? 3) |
(number? 3) |
203 |
@result{} |
@result{} #t |
|
#t |
|
204 |
|
|
205 |
(number? "hello there!") |
(number? "hello there!") |
206 |
@result{} |
@result{} #f |
|
#f |
|
207 |
|
|
208 |
(define pi 3.141592654) |
(define pi 3.141592654) |
209 |
(number? pi) |
(number? pi) |
210 |
@result{} |
@result{} #t |
|
#t |
|
211 |
@end lisp |
@end lisp |
212 |
|
|
213 |
The next few subsections document each of Guile's numerical data types |
The next few subsections document each of Guile's numerical data types |
221 |
@rnindex integer? |
@rnindex integer? |
222 |
|
|
223 |
Integers are whole numbers, that is numbers with no fractional part, |
Integers are whole numbers, that is numbers with no fractional part, |
224 |
such as 2, 83 and -3789. |
such as 2, 83, and @minus{}3789. |
225 |
|
|
226 |
Integers in Guile can be arbitrarily big, as shown by the following |
Integers in Guile can be arbitrarily big, as shown by the following |
227 |
example. |
example. |
234 |
(loop (- n 1) (* product n))))) |
(loop (- n 1) (* product n))))) |
235 |
|
|
236 |
(factorial 3) |
(factorial 3) |
237 |
@result{} |
@result{} 6 |
|
6 |
|
238 |
|
|
239 |
(factorial 20) |
(factorial 20) |
240 |
@result{} |
@result{} 2432902008176640000 |
|
2432902008176640000 |
|
241 |
|
|
242 |
(- (factorial 45)) |
(- (factorial 45)) |
243 |
@result{} |
@result{} -119622220865480194561963161495657715064383733760000000000 |
|
-119622220865480194561963161495657715064383733760000000000 |
|
244 |
@end lisp |
@end lisp |
245 |
|
|
246 |
Readers whose background is in programming languages where integers are |
Readers whose background is in programming languages where integers are |
253 |
form. Conversion between these two representations is automatic and |
form. Conversion between these two representations is automatic and |
254 |
completely invisible to the Scheme level programmer. |
completely invisible to the Scheme level programmer. |
255 |
|
|
256 |
The infinities @code{+inf.0} and @code{-inf.0} are considered to be |
The infinities @samp{+inf.0} and @samp{-inf.0} are considered to be |
257 |
inexact integers. They are explained in detail in the next section, |
inexact integers. They are explained in detail in the next section, |
258 |
together with reals and rationals. |
together with reals and rationals. |
259 |
|
|
266 |
|
|
267 |
@lisp |
@lisp |
268 |
(integer? 487) |
(integer? 487) |
269 |
@result{} |
@result{} #t |
|
#t |
|
270 |
|
|
271 |
(integer? -3.4) |
(integer? -3.4) |
272 |
@result{} |
@result{} #f |
|
#f |
|
273 |
|
|
274 |
(integer? +inf.0) |
(integer? +inf.0) |
275 |
@result{} |
@result{} #t |
|
#t |
|
276 |
@end lisp |
@end lisp |
277 |
@end deffn |
@end deffn |
278 |
|
|
288 |
Mathematically, the real numbers are the set of numbers that describe |
Mathematically, the real numbers are the set of numbers that describe |
289 |
all possible points along a continuous, infinite, one-dimensional line. |
all possible points along a continuous, infinite, one-dimensional line. |
290 |
The rational numbers are the set of all numbers that can be written as |
The rational numbers are the set of all numbers that can be written as |
291 |
fractions P/Q, where P and Q are integers. All rational numbers are |
fractions @var{p}/@var{q}, where @var{p} and @var{q} are integers. |
292 |
also real, but there are real numbers that are not rational, for example |
All rational numbers are also real, but there are real numbers that |
293 |
the square root of 2, and pi. |
are not rational, for example the square root of 2, and pi. |
294 |
|
|
295 |
Guile represents both real and rational numbers approximately using a |
Guile represents both real and rational numbers approximately using a |
296 |
floating point encoding with limited precision. Even though the actual |
floating point encoding with limited precision. Even though the actual |
309 |
The limited precision of Guile's encoding means that any ``real'' number |
The limited precision of Guile's encoding means that any ``real'' number |
310 |
in Guile can be written in a rational form, by multiplying and then dividing |
in Guile can be written in a rational form, by multiplying and then dividing |
311 |
by sufficient powers of 10 (or in fact, 2). For example, |
by sufficient powers of 10 (or in fact, 2). For example, |
312 |
@code{-0.00000142857931198} is the same as @code{142857931198} divided by |
@samp{-0.00000142857931198} is the same as @minus{}142857931198 divided by |
313 |
@code{100000000000000000}. In Guile's current incarnation, therefore, |
100000000000000000. In Guile's current incarnation, therefore, the |
314 |
the @code{rational?} and @code{real?} predicates are equivalent. |
@code{rational?} and @code{real?} predicates are equivalent. |
315 |
|
|
316 |
Another aspect of this equivalence is that Guile currently does not |
Another aspect of this equivalence is that Guile currently does not |
317 |
preserve the exactness that is possible with rational arithmetic. |
preserve the exactness that is possible with rational arithmetic. |
335 |
extension to the usual Scheme syntax. |
extension to the usual Scheme syntax. |
336 |
|
|
337 |
Dividing zero by zero yields something that is not a number at all: |
Dividing zero by zero yields something that is not a number at all: |
338 |
@samp{+nan.0}. This is the special 'not a number' value. |
@samp{+nan.0}. This is the special `not a number' value. |
339 |
|
|
340 |
On platforms that follow IEEE 754 for their floating point arithmetic, |
On platforms that follow @acronym{IEEE} 754 for their floating point |
341 |
the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values are |
arithmetic, the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values |
342 |
implemented using the corresponding IEEE 754 values. They behave in |
are implemented using the corresponding @acronym{IEEE} 754 values. |
343 |
arithmetic operations like IEEE 754 describes it, i.e., @code{(= |
They behave in arithmetic operations like @acronym{IEEE} 754 describes |
344 |
+nan.0 +nan.0) @result{#f}}. |
it, i.e., @code{(= +nan.0 +nan.0) @result{#f}}. |
345 |
|
|
346 |
The infinities are inexact integers and are considered to be both even |
The infinities are inexact integers and are considered to be both even |
347 |
and odd. While @samp{+nan.0} is not @code{=} to itself, it is |
and odd. While @samp{+nan.0} is not @code{=} to itself, it is |
428 |
|
|
429 |
R5RS requires that a calculation involving inexact numbers always |
R5RS requires that a calculation involving inexact numbers always |
430 |
produces an inexact result. To meet this requirement, Guile |
produces an inexact result. To meet this requirement, Guile |
431 |
distinguishes between an exact integer value such as @code{5} and the |
distinguishes between an exact integer value such as @samp{5} and the |
432 |
corresponding inexact real value which, to the limited precision |
corresponding inexact real value which, to the limited precision |
433 |
available, has no fractional part, and is printed as @code{5.0}. Guile |
available, has no fractional part, and is printed as @samp{5.0}. Guile |
434 |
will only convert the latter value to the former when forced to do so by |
will only convert the latter value to the former when forced to do so by |
435 |
an invocation of the @code{inexact->exact} procedure. |
an invocation of the @code{inexact->exact} procedure. |
436 |
|
|
465 |
base in which the integer is encoded, and a code indicating whether |
base in which the integer is encoded, and a code indicating whether |
466 |
the number is exact or inexact. The supported base codes are: |
the number is exact or inexact. The supported base codes are: |
467 |
|
|
468 |
@itemize @bullet |
@table @code |
469 |
@item |
@item #b |
470 |
@code{#b}, @code{#B} --- the integer is written in binary (base 2) |
@itemx #B |
471 |
|
the integer is written in binary (base 2) |
472 |
@item |
|
473 |
@code{#o}, @code{#O} --- the integer is written in octal (base 8) |
@item #o |
474 |
|
@itemx #O |
475 |
@item |
the integer is written in octal (base 8) |
476 |
@code{#d}, @code{#D} --- the integer is written in decimal (base 10) |
|
477 |
|
@item #d |
478 |
@item |
@itemx #D |
479 |
@code{#x}, @code{#X} --- the integer is written in hexadecimal (base 16). |
the integer is written in decimal (base 10) |
480 |
@end itemize |
|
481 |
|
@item #x |
482 |
|
@itemx #X |
483 |
|
the integer is written in hexadecimal (base 16) |
484 |
|
@end table |
485 |
|
|
486 |
If the base code is omitted, the integer is assumed to be decimal. The |
If the base code is omitted, the integer is assumed to be decimal. The |
487 |
following examples show how these base codes are used. |
following examples show how these base codes are used. |
488 |
|
|
489 |
@lisp |
@lisp |
490 |
-13 |
-13 |
491 |
@result{} |
@result{} -13 |
|
-13 |
|
492 |
|
|
493 |
#d-13 |
#d-13 |
494 |
@result{} |
@result{} -13 |
|
-13 |
|
495 |
|
|
496 |
#x-13 |
#x-13 |
497 |
@result{} |
@result{} -19 |
|
-19 |
|
498 |
|
|
499 |
#b+1101 |
#b+1101 |
500 |
@result{} |
@result{} 13 |
|
13 |
|
501 |
|
|
502 |
#o377 |
#o377 |
503 |
@result{} |
@result{} 255 |
|
255 |
|
504 |
@end lisp |
@end lisp |
505 |
|
|
506 |
The codes for indicating exactness (which can, incidentally, be applied |
The codes for indicating exactness (which can, incidentally, be applied |
507 |
to all numerical values) are: |
to all numerical values) are: |
508 |
|
|
509 |
@itemize @bullet |
@table @code |
510 |
@item |
@item #e |
511 |
@code{#e}, @code{#E} --- the number is exact |
@itemx #E |
512 |
|
the number is exact |
513 |
@item |
|
514 |
@code{#i}, @code{#I} --- the number is inexact. |
@item #i |
515 |
@end itemize |
@itemx #I |
516 |
|
the number is inexact. |
517 |
|
@end table |
518 |
|
|
519 |
If the exactness indicator is omitted, the integer is assumed to be exact, |
If the exactness indicator is omitted, the integer is assumed to be exact, |
520 |
since Guile's internal representation for integers is always exact. |
since Guile's internal representation for integers is always exact. |
521 |
Real numbers have limited precision similar to the precision of the |
Real numbers have limited precision similar to the precision of the |
522 |
@code{double} type in C. A consequence of the limited precision is that |
@code{double} type in C. A consequence of the limited precision is that |
523 |
all real numbers in Guile are also rational, since any number R with a |
all real numbers in Guile are also rational, since any number @var{r} with a |
524 |
limited number of decimal places, say N, can be made into an integer by |
limited number of decimal places, say @var{n}, can be made into an integer by |
525 |
multiplying by 10^N. |
multiplying by @math{10^n}. |
526 |
|
|
527 |
Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for |
Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for |
528 |
plus and minus infinity, respectively. The value must be written |
plus and minus infinity, respectively. The value must be written |
529 |
exactly as shown, that is, the always must have a sign and exactly one |
exactly as shown, that is, the always must have a sign and exactly one |
530 |
zero digit after the decimal point. It also understands @samp{+nan.0} |
zero digit after the decimal point. It also understands @samp{+nan.0} |
531 |
and @samp{-nan.0} for the special 'not-a-number' value. The sign is |
and @samp{-nan.0} for the special `not-a-number' value. The sign is |
532 |
ignored for 'not-a-number' and the value is always printed as @samp{+nan.0}. |
ignored for `not-a-number' and the value is always printed as @samp{+nan.0}. |
533 |
|
|
534 |
@node Integer Operations |
@node Integer Operations |
535 |
@subsection Operations on Integer Values |
@subsection Operations on Integer Values |
955 |
|
|
956 |
@c begin (texi-doc-string "guile" "$atan") |
@c begin (texi-doc-string "guile" "$atan") |
957 |
@deffn {Scheme Procedure} $atan x |
@deffn {Scheme Procedure} $atan x |
958 |
Return the arctangent of @var{x} in the range -PI/2 to PI/2. |
Return the arctangent of @var{x} in the range @minus{}@math{PI/2} to |
959 |
|
@math{PI/2}. |
960 |
@end deffn |
@end deffn |
961 |
|
|
962 |
@deffn {Scheme Procedure} $atan2 x y |
@deffn {Scheme Procedure} $atan2 x y |
1051 |
@subsection Bitwise Operations |
@subsection Bitwise Operations |
1052 |
|
|
1053 |
@deffn {Scheme Procedure} logand n1 n2 |
@deffn {Scheme Procedure} logand n1 n2 |
1054 |
Return the bitwise AND of the integer arguments. |
Return the bitwise @sc{and} of the integer arguments. |
1055 |
|
|
1056 |
@lisp |
@lisp |
1057 |
(logand) @result{} -1 |
(logand) @result{} -1 |
1061 |
@end deffn |
@end deffn |
1062 |
|
|
1063 |
@deffn {Scheme Procedure} logior n1 n2 |
@deffn {Scheme Procedure} logior n1 n2 |
1064 |
Return the bitwise OR of the integer arguments. |
Return the bitwise @sc{or} of the integer arguments. |
1065 |
|
|
1066 |
@lisp |
@lisp |
1067 |
(logior) @result{} 0 |
(logior) @result{} 0 |
1071 |
@end deffn |
@end deffn |
1072 |
|
|
1073 |
@deffn {Scheme Procedure} logxor n1 n2 |
@deffn {Scheme Procedure} logxor n1 n2 |
1074 |
Return the bitwise XOR of the integer arguments. A bit is |
Return the bitwise @sc{xor} of the integer arguments. A bit is |
1075 |
set in the result if it is set in an odd number of arguments. |
set in the result if it is set in an odd number of arguments. |
1076 |
@lisp |
@lisp |
1077 |
(logxor) @result{} 0 |
(logxor) @result{} 0 |
1119 |
|
|
1120 |
@deffn {Scheme Procedure} ash n cnt |
@deffn {Scheme Procedure} ash n cnt |
1121 |
@deffnx {C Function} scm_ash (n, cnt) |
@deffnx {C Function} scm_ash (n, cnt) |
1122 |
The function ash performs an arithmetic shift left by @var{cnt} |
The function @code{ash} performs an arithmetic shift left by @var{cnt} |
1123 |
bits (or shift right, if @var{cnt} is negative). 'Arithmetic' |
bits (or shift right, if @var{cnt} is negative). `Arithmetic' |
1124 |
means, that the function does not guarantee to keep the bit |
means that the function does not guarantee to keep the bit |
1125 |
structure of @var{n}, but rather guarantees that the result |
structure of @var{n}, but rather guarantees that the result |
1126 |
will always be rounded towards minus infinity. Therefore, the |
will always be rounded towards minus infinity. Therefore, the |
1127 |
results of ash and a corresponding bitwise shift will differ if |
results of @code{ash} and a corresponding bitwise shift will differ if |
1128 |
@var{n} is negative. |
@var{n} is negative. |
1129 |
|
|
1130 |
Formally, the function returns an integer equivalent to |
Formally, the function returns an integer equivalent to |
1205 |
|
|
1206 |
@deffn {Scheme Procedure} random n [state] |
@deffn {Scheme Procedure} random n [state] |
1207 |
@deffnx {C Function} scm_random (n, state) |
@deffnx {C Function} scm_random (n, state) |
1208 |
Return a number in [0, N). |
Return a number in [0, @var{n}). |
1209 |
|
|
1210 |
Accepts a positive integer or real n and returns a |
Accepts a positive integer or real n and returns a |
1211 |
number of the same type between zero (inclusive) and |
number of the same type between zero (inclusive) and |
1212 |
N (exclusive). The values returned have a uniform |
@var{n} (exclusive). The values returned have a uniform |
1213 |
distribution. |
distribution. |
1214 |
|
|
1215 |
The optional argument @var{state} must be of the type produced |
The optional argument @var{state} must be of the type produced |
1222 |
@deffn {Scheme Procedure} random:exp [state] |
@deffn {Scheme Procedure} random:exp [state] |
1223 |
@deffnx {C Function} scm_random_exp (state) |
@deffnx {C Function} scm_random_exp (state) |
1224 |
Return an inexact real in an exponential distribution with mean |
Return an inexact real in an exponential distribution with mean |
1225 |
1. For an exponential distribution with mean u use (* u |
1. For an exponential distribution with mean @var{u} use @code{(* |
1226 |
(random:exp)). |
@var{u} (random:exp))}. |
1227 |
@end deffn |
@end deffn |
1228 |
|
|
1229 |
@deffn {Scheme Procedure} random:hollow-sphere! v [state] |
@deffn {Scheme Procedure} random:hollow-sphere! vect [state] |
1230 |
@deffnx {C Function} scm_random_hollow_sphere_x (v, state) |
@deffnx {C Function} scm_random_hollow_sphere_x (vect, state) |
1231 |
Fills vect with inexact real random numbers |
Fills @var{vect} with inexact real random numbers the sum of whose |
1232 |
the sum of whose squares is equal to 1.0. |
squares is equal to 1.0. Thinking of @var{vect} as coordinates in |
1233 |
Thinking of vect as coordinates in space of |
space of dimension @var{n} @math{=} @code{(vector-length @var{vect})}, |
1234 |
dimension n = (vector-length vect), the coordinates |
the coordinates are uniformly distributed over the surface of the unit |
1235 |
are uniformly distributed over the surface of the |
n-sphere. |
|
unit n-sphere. |
|
1236 |
@end deffn |
@end deffn |
1237 |
|
|
1238 |
@deffn {Scheme Procedure} random:normal [state] |
@deffn {Scheme Procedure} random:normal [state] |
1239 |
@deffnx {C Function} scm_random_normal (state) |
@deffnx {C Function} scm_random_normal (state) |
1240 |
Return an inexact real in a normal distribution. The |
Return an inexact real in a normal distribution. The distribution |
1241 |
distribution used has mean 0 and standard deviation 1. For a |
used has mean 0 and standard deviation 1. For a normal distribution |
1242 |
normal distribution with mean m and standard deviation d use |
with mean @var{m} and standard deviation @var{d} use @code{(+ @var{m} |
1243 |
@code{(+ m (* d (random:normal)))}. |
(* @var{d} (random:normal)))}. |
1244 |
@end deffn |
@end deffn |
1245 |
|
|
1246 |
@deffn {Scheme Procedure} random:normal-vector! v [state] |
@deffn {Scheme Procedure} random:normal-vector! vect [state] |
1247 |
@deffnx {C Function} scm_random_normal_vector_x (v, state) |
@deffnx {C Function} scm_random_normal_vector_x (vect, state) |
1248 |
Fills vect with inexact real random numbers that are |
Fills @var{vect} with inexact real random numbers that are |
1249 |
independent and standard normally distributed |
independent and standard normally distributed |
1250 |
(i.e., with mean 0 and variance 1). |
(i.e., with mean 0 and variance 1). |
1251 |
@end deffn |
@end deffn |
1252 |
|
|
1253 |
@deffn {Scheme Procedure} random:solid-sphere! v [state] |
@deffn {Scheme Procedure} random:solid-sphere! vect [state] |
1254 |
@deffnx {C Function} scm_random_solid_sphere_x (v, state) |
@deffnx {C Function} scm_random_solid_sphere_x (vect, state) |
1255 |
Fills vect with inexact real random numbers |
Fills @var{vect} with inexact real random numbers the sum of whose |
1256 |
the sum of whose squares is less than 1.0. |
squares is less than 1.0. Thinking of @var{vect} as coordinates in |
1257 |
Thinking of vect as coordinates in space of |
space of dimension @var{n} @math{=} @code{(vector-length @var{vect})}, |
1258 |
dimension n = (vector-length vect), the coordinates |
the coordinates are uniformly distributed within the unit |
1259 |
are uniformly distributed within the unit n-sphere. |
@var{n}-sphere. The sum of the squares of the numbers is returned. |
1260 |
The sum of the squares of the numbers is returned. |
@c FIXME: What does this mean, particularly the n-sphere part? |
1261 |
@end deffn |
@end deffn |
1262 |
|
|
1263 |
@deffn {Scheme Procedure} random:uniform [state] |
@deffn {Scheme Procedure} random:uniform [state] |
1276 |
@section Characters |
@section Characters |
1277 |
@tpindex Characters |
@tpindex Characters |
1278 |
|
|
1279 |
Most of the characters in the ASCII character set may be referred to by |
@noindent |
1280 |
name: for example, @code{#\tab}, @code{#\esc}, @code{#\stx}, and so on. |
[@strong{FIXME}: how do you specify regular (non-control) characters?] |
1281 |
The following table describes the ASCII names for each character. |
|
1282 |
|
Most of the ``control characters'' (those below codepoint 32) in the |
1283 |
|
@acronym{ASCII} character set, as well as the space, may be referred |
1284 |
|
to by name: for example, @code{#\tab}, @code{#\esc}, @code{#\stx}, and |
1285 |
|
so on. The following table describes the @acronym{ASCII} names for |
1286 |
|
each character. |
1287 |
|
|
1288 |
@multitable @columnfractions .25 .25 .25 .25 |
@multitable @columnfractions .25 .25 .25 .25 |
1289 |
@item 0 = @code{#\nul} |
@item 0 = @code{#\nul} |
1321 |
@item 32 = @code{#\sp} |
@item 32 = @code{#\sp} |
1322 |
@end multitable |
@end multitable |
1323 |
|
|
1324 |
The @code{delete} character (octal 177) may be referred to with the name |
The ``delete'' character (octal 177) may be referred to with the name |
1325 |
@code{#\del}. |
@code{#\del}. |
1326 |
|
|
1327 |
Several characters have more than one name: |
Several characters have more than one name: |
1328 |
|
|
1329 |
@itemize @bullet |
@multitable {@code{#\backspace}} {Original} |
1330 |
@item |
@item Alias @tab Original |
1331 |
@code{#\space}, @code{#\sp} |
@item @code{#\space} @tab @code{#\sp} |
1332 |
@item |
@item @code{#\newline} @tab @code{#\nl} |
1333 |
@code{#\newline}, @code{#\nl} |
@item @code{#\tab} @tab @code{#\ht} |
1334 |
@item |
@item @code{#\backspace} @tab @code{#\bs} |
1335 |
@code{#\tab}, @code{#\ht} |
@item @code{#\return} @tab @code{#\cr} |
1336 |
@item |
@item @code{#\page} @tab @code{#\np} |
1337 |
@code{#\backspace}, @code{#\bs} |
@item @code{#\null} @tab @code{#\nul} |
1338 |
@item |
@end multitable |
|
@code{#\return}, @code{#\cr} |
|
|
@item |
|
|
@code{#\page}, @code{#\np} |
|
|
@item |
|
|
@code{#\null}, @code{#\nul} |
|
|
@end itemize |
|
1339 |
|
|
1340 |
@rnindex char? |
@rnindex char? |
1341 |
@deffn {Scheme Procedure} char? x |
@deffn {Scheme Procedure} char? x |
1350 |
|
|
1351 |
@rnindex char<? |
@rnindex char<? |
1352 |
@deffn {Scheme Procedure} char<? x y |
@deffn {Scheme Procedure} char<? x y |
1353 |
Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence, |
Return @code{#t} iff @var{x} is less than @var{y} in the @acronym{ASCII} sequence, |
1354 |
else @code{#f}. |
else @code{#f}. |
1355 |
@end deffn |
@end deffn |
1356 |
|
|
1357 |
@rnindex char<=? |
@rnindex char<=? |
1358 |
@deffn {Scheme Procedure} char<=? x y |
@deffn {Scheme Procedure} char<=? x y |
1359 |
Return @code{#t} iff @var{x} is less than or equal to @var{y} in the |
Return @code{#t} iff @var{x} is less than or equal to @var{y} in the |
1360 |
ASCII sequence, else @code{#f}. |
@acronym{ASCII} sequence, else @code{#f}. |
1361 |
@end deffn |
@end deffn |
1362 |
|
|
1363 |
@rnindex char>? |
@rnindex char>? |
1364 |
@deffn {Scheme Procedure} char>? x y |
@deffn {Scheme Procedure} char>? x y |
1365 |
Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII |
Return @code{#t} iff @var{x} is greater than @var{y} in the @acronym{ASCII} |
1366 |
sequence, else @code{#f}. |
sequence, else @code{#f}. |
1367 |
@end deffn |
@end deffn |
1368 |
|
|
1369 |
@rnindex char>=? |
@rnindex char>=? |
1370 |
@deffn {Scheme Procedure} char>=? x y |
@deffn {Scheme Procedure} char>=? x y |
1371 |
Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the |
Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the |
1372 |
ASCII sequence, else @code{#f}. |
@acronym{ASCII} sequence, else @code{#f}. |
1373 |
@end deffn |
@end deffn |
1374 |
|
|
1375 |
@rnindex char-ci=? |
@rnindex char-ci=? |
1380 |
|
|
1381 |
@rnindex char-ci<? |
@rnindex char-ci<? |
1382 |
@deffn {Scheme Procedure} char-ci<? x y |
@deffn {Scheme Procedure} char-ci<? x y |
1383 |
Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence |
Return @code{#t} iff @var{x} is less than @var{y} in the @acronym{ASCII} sequence |
1384 |
ignoring case, else @code{#f}. |
ignoring case, else @code{#f}. |
1385 |
@end deffn |
@end deffn |
1386 |
|
|
1387 |
@rnindex char-ci<=? |
@rnindex char-ci<=? |
1388 |
@deffn {Scheme Procedure} char-ci<=? x y |
@deffn {Scheme Procedure} char-ci<=? x y |
1389 |
Return @code{#t} iff @var{x} is less than or equal to @var{y} in the |
Return @code{#t} iff @var{x} is less than or equal to @var{y} in the |
1390 |
ASCII sequence ignoring case, else @code{#f}. |
@acronym{ASCII} sequence ignoring case, else @code{#f}. |
1391 |
@end deffn |
@end deffn |
1392 |
|
|
1393 |
@rnindex char-ci>? |
@rnindex char-ci>? |
1394 |
@deffn {Scheme Procedure} char-ci>? x y |
@deffn {Scheme Procedure} char-ci>? x y |
1395 |
Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII |
Return @code{#t} iff @var{x} is greater than @var{y} in the @acronym{ASCII} |
1396 |
sequence ignoring case, else @code{#f}. |
sequence ignoring case, else @code{#f}. |
1397 |
@end deffn |
@end deffn |
1398 |
|
|
1399 |
@rnindex char-ci>=? |
@rnindex char-ci>=? |
1400 |
@deffn {Scheme Procedure} char-ci>=? x y |
@deffn {Scheme Procedure} char-ci>=? x y |
1401 |
Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the |
Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the |
1402 |
ASCII sequence ignoring case, else @code{#f}. |
@acronym{ASCII} sequence ignoring case, else @code{#f}. |
1403 |
@end deffn |
@end deffn |
1404 |
|
|
1405 |
@rnindex char-alphabetic? |
@rnindex char-alphabetic? |
1406 |
@deffn {Scheme Procedure} char-alphabetic? chr |
@deffn {Scheme Procedure} char-alphabetic? chr |
1407 |
@deffnx {C Function} scm_char_alphabetic_p (chr) |
@deffnx {C Function} scm_char_alphabetic_p (chr) |
1408 |
Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}. |
Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}. |
1409 |
Alphabetic means the same thing as the isalpha C library function. |
Alphabetic means the same thing as the @code{isalpha} C library function. |
1410 |
@end deffn |
@end deffn |
1411 |
|
|
1412 |
@rnindex char-numeric? |
@rnindex char-numeric? |
1413 |
@deffn {Scheme Procedure} char-numeric? chr |
@deffn {Scheme Procedure} char-numeric? chr |
1414 |
@deffnx {C Function} scm_char_numeric_p (chr) |
@deffnx {C Function} scm_char_numeric_p (chr) |
1415 |
Return @code{#t} iff @var{chr} is numeric, else @code{#f}. |
Return @code{#t} iff @var{chr} is numeric, else @code{#f}. |
1416 |
Numeric means the same thing as the isdigit C library function. |
Numeric means the same thing as the @code{isdigit} C library function. |
1417 |
@end deffn |
@end deffn |
1418 |
|
|
1419 |
@rnindex char-whitespace? |
@rnindex char-whitespace? |
1420 |
@deffn {Scheme Procedure} char-whitespace? chr |
@deffn {Scheme Procedure} char-whitespace? chr |
1421 |
@deffnx {C Function} scm_char_whitespace_p (chr) |
@deffnx {C Function} scm_char_whitespace_p (chr) |
1422 |
Return @code{#t} iff @var{chr} is whitespace, else @code{#f}. |
Return @code{#t} iff @var{chr} is whitespace, else @code{#f}. |
1423 |
Whitespace means the same thing as the isspace C library function. |
Whitespace means the same thing as the @code{isspace} C library function. |
1424 |
@end deffn |
@end deffn |
1425 |
|
|
1426 |
@rnindex char-upper-case? |
@rnindex char-upper-case? |
1427 |
@deffn {Scheme Procedure} char-upper-case? chr |
@deffn {Scheme Procedure} char-upper-case? chr |
1428 |
@deffnx {C Function} scm_char_upper_case_p (chr) |
@deffnx {C Function} scm_char_upper_case_p (chr) |
1429 |
Return @code{#t} iff @var{chr} is uppercase, else @code{#f}. |
Return @code{#t} iff @var{chr} is uppercase, else @code{#f}. |
1430 |
Uppercase means the same thing as the isupper C library function. |
Uppercase means the same thing as the @code{isupper} C library function. |
1431 |
@end deffn |
@end deffn |
1432 |
|
|
1433 |
@rnindex char-lower-case? |
@rnindex char-lower-case? |
1434 |
@deffn {Scheme Procedure} char-lower-case? chr |
@deffn {Scheme Procedure} char-lower-case? chr |
1435 |
@deffnx {C Function} scm_char_lower_case_p (chr) |
@deffnx {C Function} scm_char_lower_case_p (chr) |
1436 |
Return @code{#t} iff @var{chr} is lowercase, else @code{#f}. |
Return @code{#t} iff @var{chr} is lowercase, else @code{#f}. |
1437 |
Lowercase means the same thing as the islower C library function. |
Lowercase means the same thing as the @code{islower} C library function. |
1438 |
@end deffn |
@end deffn |
1439 |
|
|
1440 |
@deffn {Scheme Procedure} char-is-both? chr |
@deffn {Scheme Procedure} char-is-both? chr |
1441 |
@deffnx {C Function} scm_char_is_both_p (chr) |
@deffnx {C Function} scm_char_is_both_p (chr) |
1442 |
Return @code{#t} iff @var{chr} is either uppercase or lowercase, else @code{#f}. |
Return @code{#t} iff @var{chr} is either uppercase or lowercase, else |
1443 |
Uppercase and lowercase are as defined by the isupper and islower |
@code{#f}. Uppercase and lowercase are as defined by the |
1444 |
C library functions. |
@code{isupper} and @code{islower} C library functions. |
1445 |
@end deffn |
@end deffn |
1446 |
|
|
1447 |
@rnindex char->integer |
@rnindex char->integer |
1448 |
@deffn {Scheme Procedure} char->integer chr |
@deffn {Scheme Procedure} char->integer chr |
1449 |
@deffnx {C Function} scm_char_to_integer (chr) |
@deffnx {C Function} scm_char_to_integer (chr) |
1450 |
Return the number corresponding to ordinal position of @var{chr} in the |
Return the number corresponding to ordinal position of @var{chr} in the |
1451 |
ASCII sequence. |
@acronym{ASCII} sequence. |
1452 |
@end deffn |
@end deffn |
1453 |
|
|
1454 |
@rnindex integer->char |
@rnindex integer->char |
1455 |
@deffn {Scheme Procedure} integer->char n |
@deffn {Scheme Procedure} integer->char n |
1456 |
@deffnx {C Function} scm_integer_to_char (n) |
@deffnx {C Function} scm_integer_to_char (n) |
1457 |
Return the character at position @var{n} in the ASCII sequence. |
Return the character at position @var{n} in the @acronym{ASCII} sequence. |
1458 |
@end deffn |
@end deffn |
1459 |
|
|
1460 |
@rnindex char-upcase |
@rnindex char-upcase |
1469 |
Return the lowercase character version of @var{chr}. |
Return the lowercase character version of @var{chr}. |
1470 |
@end deffn |
@end deffn |
1471 |
|
|
1472 |
|
@xref{Classification of Characters,,,libc,GNU C Library Reference |
1473 |
|
Manual}, for information about the @code{is*} Standard C functions |
1474 |
|
mentioned above. |
1475 |
|
|
1476 |
|
|
1477 |
@node Strings |
@node Strings |
1478 |
@section Strings |
@section Strings |
1480 |
|
|
1481 |
Strings are fixed-length sequences of characters. They can be created |
Strings are fixed-length sequences of characters. They can be created |
1482 |
by calling constructor procedures, but they can also literally get |
by calling constructor procedures, but they can also literally get |
1483 |
entered at the REPL or in Scheme source files. |
entered at the @acronym{REPL} or in Scheme source files. |
1484 |
|
|
1485 |
Guile provides a rich set of string processing procedures, because text |
Guile provides a rich set of string processing procedures, because text |
1486 |
handling is very important when Guile is used as a scripting language. |
handling is very important when Guile is used as a scripting language. |
1488 |
Strings always carry the information about how many characters they are |
Strings always carry the information about how many characters they are |
1489 |
composed of with them, so there is no special end-of-string character, |
composed of with them, so there is no special end-of-string character, |
1490 |
like in C. That means that Scheme strings can contain any character, |
like in C. That means that Scheme strings can contain any character, |
1491 |
even the NUL character @code{'\0'}. But note: Since most operating |
even the @samp{NUL} character @samp{\0}. But note: Since most operating |
1492 |
system calls dealing with strings (such as for file operations) expect |
system calls dealing with strings (such as for file operations) expect |
1493 |
strings to be zero-terminated, they might do unexpected things when |
strings to be zero-terminated, they might do unexpected things when |
1494 |
called with string containing unusual characters. |
called with string containing unusual characters. |
1510 |
@subsection String Read Syntax |
@subsection String Read Syntax |
1511 |
|
|
1512 |
The read syntax for strings is an arbitrarily long sequence of |
The read syntax for strings is an arbitrarily long sequence of |
1513 |
characters enclosed in double quotes (@code{"}). @footnote{Actually, the |
characters enclosed in double quotes (@code{"}).@footnote{Actually, |
1514 |
current implementation restricts strings to a length of 2^24 |
the current implementation restricts strings to a length of |
1515 |
characters.} If you want to insert a double quote character into a |
@math{2^24}, or 16,777,216, characters. Sorry.} If you want to |
1516 |
string literal, it must be prefixed with a backslash @code{\} character |
insert a double quote character into a string literal, it must be |
1517 |
(called an @dfn{escape character}). |
prefixed with a backslash @samp{\} character (called an @dfn{escape |
1518 |
|
character}). |
1519 |
|
|
1520 |
The following are examples of string literals: |
The following are examples of string literals: |
1521 |
|
|
1652 |
@var{str} must be a string, @var{start} and @var{end} must be |
@var{str} must be a string, @var{start} and @var{end} must be |
1653 |
exact integers satisfying: |
exact integers satisfying: |
1654 |
|
|
1655 |
0 <= @var{start} <= @var{end} <= (string-length @var{str}). |
0 <= @var{start} <= @var{end} <= @code{(string-length @var{str})}. |
1656 |
@end deffn |
@end deffn |
1657 |
|
|
1658 |
@node String Modification |
@node String Modification |
2099 |
@end deffn |
@end deffn |
2100 |
|
|
2101 |
The following example takes a regular expression that matches a standard |
The following example takes a regular expression that matches a standard |
2102 |
YYYYMMDD-format date such as @code{"20020828"}. The |
@sc{yyyymmdd}-format date such as @code{"20020828"}. The |
2103 |
@code{regexp-substitute} call returns a string computed from the |
@code{regexp-substitute} call returns a string computed from the |
2104 |
information in the match structure, consisting of the fields and text |
information in the match structure, consisting of the fields and text |
2105 |
from the original string reordered and reformatted. |
from the original string reordered and reformatted. |
2120 |
@var{regexp} string describing a regular expression, and a @var{target} |
@var{regexp} string describing a regular expression, and a @var{target} |
2121 |
string which should be matched against this regular expression. |
string which should be matched against this regular expression. |
2122 |
|
|
2123 |
Each @var{item} behaves as in @var{regexp-substitute}, with the |
Each @var{item} behaves as in @code{regexp-substitute}, with the |
2124 |
following exceptions: |
following exceptions: |
2125 |
|
|
2126 |
@itemize @bullet |
@itemize @bullet |