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