64943c362bb3d20bb29a0b7cd9ea913eb8f220f8
[bpt/guile.git] / doc / ref / srfi-modules.texi
1 @page
2 @node SRFI Support
3 @chapter SRFI Support Modules
4 @cindex SRFI
5
6 SRFI is an acronym for Scheme Request For Implementation. The SRFI
7 documents define a lot of syntactic and procedure extensions to standard
8 Scheme as defined in R5RS.
9
10 Guile has support for a number of SRFIs. This chapter gives an overview
11 over the available SRFIs and some usage hints. For complete
12 documentation, design rationales and further examples, we advise you to
13 get the relevant SRFI documents from the SRFI home page
14 @url{http://srfi.schemers.org}.
15
16 @menu
17 * About SRFI Usage:: What to know about Guile's SRFI support.
18 * SRFI-0:: cond-expand
19 * SRFI-1:: List library.
20 * SRFI-2:: and-let*.
21 * SRFI-4:: Homogeneous numeric vector datatypes.
22 * SRFI-6:: Basic String Ports.
23 * SRFI-8:: receive.
24 * SRFI-9:: define-record-type.
25 * SRFI-10:: Hash-Comma Reader Extension.
26 * SRFI-11:: let-values and let-values*.
27 * SRFI-13:: String library.
28 * SRFI-14:: Character-set library.
29 * SRFI-16:: case-lambda
30 * SRFI-17:: Generalized set!
31 * SRFI-19:: Time/Date library.
32 @end menu
33
34
35 @node About SRFI Usage
36 @section About SRFI Usage
37
38 @c FIXME::martin: Review me!
39
40 SRFI support in Guile is currently implemented partly in the core
41 library, and partly as add-on modules. That means that some SRFIs are
42 automatically available when the interpreter is started, whereas the
43 other SRFIs require you to use the appropriate support module
44 explicitly.
45
46 There are several reasons for this inconsistency. First, the feature
47 checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be
48 available immediately, because it must be there when the user wants to
49 check for the Scheme implementation, that is, before she can know that
50 it is safe to use @code{use-modules} to load SRFI support modules. The
51 second reason is that some features defined in SRFIs had been
52 implemented in Guile before the developers started to add SRFI
53 implementations as modules (for example SRFI-6 (@pxref{SRFI-6})). In
54 the future, it is possible that SRFIs in the core library might be
55 factored out into separate modules, requiring explicit module loading
56 when they are needed. So you should be prepared to have to use
57 @code{use-modules} someday in the future to access SRFI-6 bindings. If
58 you want, you can do that already. We have included the module
59 @code{(srfi srfi-6)} in the distribution, which currently does nothing,
60 but ensures that you can write future-safe code.
61
62 Generally, support for a specific SRFI is made available by using
63 modules named @code{(srfi srfi-@var{number})}, where @var{number} is the
64 number of the SRFI needed. Another possibility is to use the command
65 line option @code{--use-srfi}, which will load the necessary modules
66 automatically (@pxref{Invoking Guile}).
67
68
69 @node SRFI-0
70 @section SRFI-0 - cond-expand
71 @cindex SRFI-0
72
73 @c FIXME::martin: Review me!
74
75 SRFI-0 defines a means for checking whether a Scheme implementation has
76 support for a specified feature. The syntactic form @code{cond-expand},
77 which implements this means, has the following syntax.
78
79 @example
80 @group
81 <cond-expand>
82 --> (cond-expand <cond-expand-clause>+)
83 | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
84 <cond-expand-clause>
85 --> (<feature-requirement> <command-or-definition>*)
86 <feature-requirement>
87 --> <feature-identifier>
88 | (and <feature-requirement>*)
89 | (or <feature-requirement>*)
90 | (not <feature-requirement>)
91 <feature-identifier>
92 --> <a symbol which is the name or alias of a SRFI>
93 @end group
94 @end example
95
96 When evaluated, this form checks all clauses in order, until it finds
97 one whose feature requirement is satisfied. Then the form expands into
98 the commands or definitions in the clause. A requirement is tested as
99 follows:
100
101 @itemize @bullet
102 @item
103 If it is a symbol, it is satisfied if the feature identifier is
104 supported.
105
106 @item
107 If it is an @code{and} form, all requirements must be satisfied. If no
108 requirements are given, it is satisfied, too.
109
110 @item
111 If it is an @code{or} form, at least one of the requirements must be
112 satisfied. If no requirements are given, it is not satisfied.
113
114 @item
115 If it is a @code{not} form, the feature requirement must @emph{not} be
116 satisfied.
117
118 @item
119 If the feature requirement is the keyword @code{else} and it is the last
120 clause, it is satisfied if no prior clause matched.
121 @end itemize
122
123 If no clause is satisfied, an error is signalled.
124
125 Since @code{cond-expand} is needed to tell what a Scheme implementation
126 provides, it must be accessible without using any
127 implementation-dependent operations, such as @code{use-modules} in
128 Guile. Thus, it is not necessary to use any module to get access to
129 this form.
130
131 Currently, the feature identifiers @code{guile}, @code{r5rs} and
132 @code{srfi-0} are supported. The other SRFIs are not in that list by
133 default, because the SRFI modules must be explicitly used before their
134 exported bindings can be used.
135
136 So if a Scheme program wishes to use SRFI-8, it has two possibilities:
137 First, it can check whether the running Scheme implementation is Guile,
138 and if it is, it can use the appropriate module:
139
140 @lisp
141 (cond-expand
142 (guile
143 (use-modules (srfi srfi-8)))
144 (srfi-8
145 #t))
146 ;; otherwise fail.
147 @end lisp
148
149 The other possibility is to use the @code{--use-srfi} command line
150 option when invoking Guile (@pxref{Invoking Guile}). When you do that,
151 the specified SRFI support modules will be loaded and add their feature
152 identifier to the list of symbols checked by @code{cond-expand}.
153
154 So, if you invoke Guile like this:
155
156 @example
157 $ guile --use-srfi=8
158 @end example
159
160 the following snippet will expand to @code{'hooray}.
161
162 @lisp
163 (cond-expand (srfi-8 'hooray))
164 @end lisp
165
166
167 @node SRFI-1
168 @section SRFI-1 - List library
169 @cindex SRFI-1
170
171 @c FIXME::martin: Review me!
172
173 The list library defined in SRFI-1 contains a lot of useful list
174 processing procedures for construction, examining, destructuring and
175 manipulating lists and pairs.
176
177 Since SRFI-1 also defines some procedures which are already contained
178 in R5RS and thus are supported by the Guile core library, some list
179 and pair procedures which appear in the SRFI-1 document may not appear
180 in this section. So when looking for a particular list/pair
181 processing procedure, you should also have a look at the sections
182 @ref{Lists} and @ref{Pairs}.
183
184 @menu
185 * SRFI-1 Constructors:: Constructing new lists.
186 * SRFI-1 Predicates:: Testing list for specific properties.
187 * SRFI-1 Selectors:: Selecting elements from lists.
188 * SRFI-1 Length Append etc:: Length calculation and list appending.
189 * SRFI-1 Fold and Map:: Higher-order list processing.
190 * SRFI-1 Filtering and Partitioning:: Filter lists based on predicates.
191 * SRFI-1 Searching:: Search for elements.
192 * SRFI-1 Deleting:: Delete elements from lists.
193 * SRFI-1 Association Lists:: Handle association lists.
194 * SRFI-1 Set Operations:: Use lists for representing sets.
195 @end menu
196
197 @node SRFI-1 Constructors
198 @subsection Constructors
199
200 @c FIXME::martin: Review me!
201
202 New lists can be constructed by calling one of the following
203 procedures.
204
205 @deffn {Scheme Procedure} xcons d a
206 Like @code{cons}, but with interchanged arguments. Useful mostly when
207 passed to higher-order procedures.
208 @end deffn
209
210 @deffn {Scheme Procedure} list-tabulate n init-proc
211 Return an @var{n}-element list, where each list element is produced by
212 applying the procedure @var{init-proc} to the corresponding list
213 index. The order in which @var{init-proc} is applied to the indices
214 is not specified.
215 @end deffn
216
217 @deffn {Scheme Procedure} circular-list elt1 elt2 @dots{}
218 Return a circular list containing the given arguments @var{elt1}
219 @var{elt2} @dots{}.
220 @end deffn
221
222 @deffn {Scheme Procedure} iota count [start step]
223 Return a list containing @var{count} elements, where each element is
224 calculated as follows:
225
226 @var{start} + (@var{count} - 1) * @var{step}
227
228 @var{start} defaults to 0 and @var{step} defaults to 1.
229 @end deffn
230
231
232 @node SRFI-1 Predicates
233 @subsection Predicates
234
235 @c FIXME::martin: Review me!
236
237 The procedures in this section test specific properties of lists.
238
239 @deffn {Scheme Procedure} proper-list? obj
240 Return @code{#t} if @var{obj} is a proper list, that is a finite list,
241 terminated with the empty list. Otherwise, return @code{#f}.
242 @end deffn
243
244 @deffn {Scheme Procedure} circular-list? obj
245 Return @code{#t} if @var{obj} is a circular list, otherwise return
246 @code{#f}.
247 @end deffn
248
249 @deffn {Scheme Procedure} dotted-list? obj
250 Return @code{#t} if @var{obj} is a dotted list, return @code{#f}
251 otherwise. A dotted list is a finite list which is not terminated by
252 the empty list, but some other value.
253 @end deffn
254
255 @deffn {Scheme Procedure} null-list? lst
256 Return @code{#t} if @var{lst} is the empty list @code{()}, @code{#f}
257 otherwise. If something else than a proper or circular list is passed
258 as @var{lst}, an error is signalled. This procedure is recommended
259 for checking for the end of a list in contexts where dotted lists are
260 not allowed.
261 @end deffn
262
263 @deffn {Scheme Procedure} not-pair? obj
264 Return @code{#t} is @var{obj} is not a pair, @code{#f} otherwise.
265 This is shorthand notation @code{(not (pair? @var{obj}))} and is
266 supposed to be used for end-of-list checking in contexts where dotted
267 lists are allowed.
268 @end deffn
269
270 @deffn {Scheme Procedure} list= elt= list1 @dots{}
271 Return @code{#t} if all argument lists are equal, @code{#f} otherwise.
272 List equality is determined by testing whether all lists have the same
273 length and the corresponding elements are equal in the sense of the
274 equality predicate @var{elt=}. If no or only one list is given,
275 @code{#t} is returned.
276 @end deffn
277
278
279 @node SRFI-1 Selectors
280 @subsection Selectors
281
282 @c FIXME::martin: Review me!
283
284 @deffn {Scheme Procedure} first pair
285 @deffnx {Scheme Procedure} second pair
286 @deffnx {Scheme Procedure} third pair
287 @deffnx {Scheme Procedure} fourth pair
288 @deffnx {Scheme Procedure} fifth pair
289 @deffnx {Scheme Procedure} sixth pair
290 @deffnx {Scheme Procedure} seventh pair
291 @deffnx {Scheme Procedure} eighth pair
292 @deffnx {Scheme Procedure} ninth pair
293 @deffnx {Scheme Procedure} tenth pair
294 These are synonyms for @code{car}, @code{cadr}, @code{caddr}, @dots{}.
295 @end deffn
296
297 @deffn {Scheme Procedure} car+cdr pair
298 Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.
299 @end deffn
300
301 @deffn {Scheme Procedure} take lst i
302 @deffnx {Scheme Procedure} take! lst i
303 Return a list containing the first @var{i} elements of @var{lst}.
304
305 @code{take!} may modify the structure of the argument list @var{lst}
306 in order to produce the result.
307 @end deffn
308
309 @deffn {Scheme Procedure} drop lst i
310 Return a list containing all but the first @var{i} elements of
311 @var{lst}.
312 @end deffn
313
314 @deffn {Scheme Procedure} take-right lst i
315 Return the a list containing the @var{i} last elements of @var{lst}.
316 @end deffn
317
318 @deffn {Scheme Procedure} drop-right lst i
319 @deffnx {Scheme Procedure} drop-right! lst i
320 Return the a list containing all but the @var{i} last elements of
321 @var{lst}.
322
323 @code{drop-right!} may modify the structure of the argument list
324 @var{lst} in order to produce the result.
325 @end deffn
326
327 @deffn {Scheme Procedure} split-at lst i
328 @deffnx {Scheme Procedure} split-at! lst i
329 Return two values, a list containing the first @var{i} elements of the
330 list @var{lst} and a list containing the remaining elements.
331
332 @code{split-at!} may modify the structure of the argument list
333 @var{lst} in order to produce the result.
334 @end deffn
335
336 @deffn {Scheme Procedure} last lst
337 Return the last element of the non-empty, finite list @var{lst}.
338 @end deffn
339
340
341 @node SRFI-1 Length Append etc
342 @subsection Length, Append, Concatenate, etc.
343
344 @c FIXME::martin: Review me!
345
346 @deffn {Scheme Procedure} length+ lst
347 Return the length of the argument list @var{lst}. When @var{lst} is a
348 circular list, @code{#f} is returned.
349 @end deffn
350
351 @deffn {Scheme Procedure} concatenate list-of-lists
352 @deffnx {Scheme Procedure} concatenate! list-of-lists
353 Construct a list by appending all lists in @var{list-of-lists}.
354
355 @code{concatenate!} may modify the structure of the given lists in
356 order to produce the result.
357 @end deffn
358
359 @deffn {Scheme Procedure} append-reverse rev-head tail
360 @deffnx {Scheme Procedure} append-reverse! rev-head tail
361 Reverse @var{rev-head}, append @var{tail} and return the result. This
362 is equivalent to @code{(append (reverse @var{rev-head}) @var{tail})},
363 but more efficient.
364
365 @code{append-reverse!} may modify @var{rev-head} in order to produce
366 the result.
367 @end deffn
368
369 @deffn {Scheme Procedure} zip lst1 lst2 @dots{}
370 Return a list as long as the shortest of the argument lists, where
371 each element is a list. The first list contains the first elements of
372 the argument lists, the second list contains the second elements, and
373 so on.
374 @end deffn
375
376 @deffn {Scheme Procedure} unzip1 lst
377 @deffnx {Scheme Procedure} unzip2 lst
378 @deffnx {Scheme Procedure} unzip3 lst
379 @deffnx {Scheme Procedure} unzip4 lst
380 @deffnx {Scheme Procedure} unzip5 lst
381 @code{unzip1} takes a list of lists, and returns a list containing the
382 first elements of each list, @code{unzip2} returns two lists, the
383 first containing the first elements of each lists and the second
384 containing the second elements of each lists, and so on.
385 @end deffn
386
387 @deffn {Scheme Procedure} count pred lst1 @dots{} lstN
388 Return a count of the number of times @var{pred} returns true when
389 called on elements from the given lists.
390
391 @var{pred} is called with @var{N} parameters @code{(@var{pred}
392 @var{elem1} @dots{} @var{elemN})}, each element being from the
393 corresponding @var{lst1} @dots{} @var{lstN}. The first call is with
394 the first element of each list, the second with the second element
395 from each, and so on.
396
397 Counting stops when the end of the shortest list is reached. At least
398 one list must be non-circular.
399 @end deffn
400
401
402 @node SRFI-1 Fold and Map
403 @subsection Fold, Unfold & Map
404
405 @c FIXME::martin: Review me!
406
407 @deffn {Scheme Procedure} fold kons knil lst1 lst2 @dots{}
408 Fold the procedure @var{kons} across all elements of @var{lst1},
409 @var{lst2}, @dots{}. Produce the result of
410
411 @code{(@var{kons} @var{en1} @var{en2} @dots{} (@var{kons} @var{e21}
412 @var{e22} (@var{kons} @var{e11} @var{e12} @var{knil})))},
413
414 if @var{enm} are the elements of the lists @var{lst1}, @var{lst2},
415 @dots{}.
416 @end deffn
417
418 @deffn {Scheme Procedure} fold-right kons knil lst1 lst2 @dots{}
419 Similar to @code{fold}, but applies @var{kons} in right-to-left order
420 to the list elements, that is:
421
422 @code{(@var{kons} @var{e11} @var{e12}(@var{kons} @var{e21}
423 @var{e22} @dots{} (@var{kons} @var{en1} @var{en2} @var{knil})))},
424 @end deffn
425
426 @deffn {Scheme Procedure} pair-fold kons knil lst1 lst2 @dots{}
427 Like @code{fold}, but apply @var{kons} to the pairs of the list
428 instead of the list elements.
429 @end deffn
430
431 @deffn {Scheme Procedure} pair-fold-right kons knil lst1 lst2 @dots{}
432 Like @code{fold-right}, but apply @var{kons} to the pairs of the list
433 instead of the list elements.
434 @end deffn
435
436 @deffn {Scheme Procedure} reduce f ridentity lst
437 @code{reduce} is a variant of @code{fold}. If @var{lst} is
438 @code{()}, @var{ridentity} is returned. Otherwise, @code{(fold f (car
439 @var{lst}) (cdr @var{lst}))} is returned.
440 @end deffn
441
442 @deffn {Scheme Procedure} reduce-right f ridentity lst
443 This is the @code{fold-right} variant of @code{reduce}.
444 @end deffn
445
446 @deffn {Scheme Procedure} unfold p f g seed [tail-gen]
447 @code{unfold} is defined as follows:
448
449 @lisp
450 (unfold p f g seed) =
451 (if (p seed) (tail-gen seed)
452 (cons (f seed)
453 (unfold p f g (g seed))))
454 @end lisp
455
456 @table @var
457 @item p
458 Determines when to stop unfolding.
459
460 @item f
461 Maps each seed value to the corresponding list element.
462
463 @item g
464 Maps each seed value to next seed valu.
465
466 @item seed
467 The state value for the unfold.
468
469 @item tail-gen
470 Creates the tail of the list; defaults to @code{(lambda (x) '())}.
471 @end table
472
473 @var{g} produces a series of seed values, which are mapped to list
474 elements by @var{f}. These elements are put into a list in
475 left-to-right order, and @var{p} tells when to stop unfolding.
476 @end deffn
477
478 @deffn {Scheme Procedure} unfold-right p f g seed [tail]
479 Construct a list with the following loop.
480
481 @lisp
482 (let lp ((seed seed) (lis tail))
483 (if (p seed) lis
484 (lp (g seed)
485 (cons (f seed) lis))))
486 @end lisp
487
488 @table @var
489 @item p
490 Determines when to stop unfolding.
491
492 @item f
493 Maps each seed value to the corresponding list element.
494
495 @item g
496 Maps each seed value to next seed valu.
497
498 @item seed
499 The state value for the unfold.
500
501 @item tail-gen
502 Creates the tail of the list; defaults to @code{(lambda (x) '())}.
503 @end table
504
505 @end deffn
506
507 @deffn {Scheme Procedure} map f lst1 lst2 @dots{}
508 Map the procedure over the list(s) @var{lst1}, @var{lst2}, @dots{} and
509 return a list containing the results of the procedure applications.
510 This procedure is extended with respect to R5RS, because the argument
511 lists may have different lengths. The result list will have the same
512 length as the shortest argument lists. The order in which @var{f}
513 will be applied to the list element(s) is not specified.
514 @end deffn
515
516 @deffn {Scheme Procedure} for-each f lst1 lst2 @dots{}
517 Apply the procedure @var{f} to each pair of corresponding elements of
518 the list(s) @var{lst1}, @var{lst2}, @dots{}. The return value is not
519 specified. This procedure is extended with respect to R5RS, because
520 the argument lists may have different lengths. The shortest argument
521 list determines the number of times @var{f} is called. @var{f} will
522 be applied to the list elements in left-to-right order.
523
524 @end deffn
525
526 @deffn {Scheme Procedure} append-map f lst1 lst2 @dots{}
527 @deffnx {Scheme Procedure} append-map! f lst1 lst2 @dots{}
528 Equivalent to
529
530 @lisp
531 (apply append (map f clist1 clist2 ...))
532 @end lisp
533
534 and
535
536 @lisp
537 (apply append! (map f clist1 clist2 ...))
538 @end lisp
539
540 Map @var{f} over the elements of the lists, just as in the @code{map}
541 function. However, the results of the applications are appended
542 together to make the final result. @code{append-map} uses
543 @code{append} to append the results together; @code{append-map!} uses
544 @code{append!}.
545
546 The dynamic order in which the various applications of @var{f} are
547 made is not specified.
548 @end deffn
549
550 @deffn {Scheme Procedure} map! f lst1 lst2 @dots{}
551 Linear-update variant of @code{map} -- @code{map!} is allowed, but not
552 required, to alter the cons cells of @var{lst1} to construct the
553 result list.
554
555 The dynamic order in which the various applications of @var{f} are
556 made is not specified. In the n-ary case, @var{lst2}, @var{lst3},
557 @dots{} must have at least as many elements as @var{lst1}.
558 @end deffn
559
560 @deffn {Scheme Procedure} pair-for-each f lst1 lst2 @dots{}
561 Like @code{for-each}, but applies the procedure @var{f} to the pairs
562 from which the argument lists are constructed, instead of the list
563 elements. The return value is not specified.
564 @end deffn
565
566 @deffn {Scheme Procedure} filter-map f lst1 lst2 @dots{}
567 Like @code{map}, but only results from the applications of @var{f}
568 which are true are saved in the result list.
569 @end deffn
570
571
572 @node SRFI-1 Filtering and Partitioning
573 @subsection Filtering and Partitioning
574
575 @c FIXME::martin: Review me!
576
577 Filtering means to collect all elements from a list which satisfy a
578 specific condition. Partitioning a list means to make two groups of
579 list elements, one which contains the elements satisfying a condition,
580 and the other for the elements which don't.
581
582 @deffn {Scheme Procedure} filter pred lst
583 @deffnx {Scheme Procedure} filter! pred lst
584 Return a list containing all elements from @var{lst} which satisfy the
585 predicate @var{pred}. The elements in the result list have the same
586 order as in @var{lst}. The order in which @var{pred} is applied to
587 the list elements is not specified.
588
589 @code{filter!} is allowed, but not required to modify the structure of
590 @end deffn
591
592 @deffn {Scheme Procedure} partition pred lst
593 @deffnx {Scheme Procedure} partition! pred lst
594 Return two lists, one containing all elements from @var{lst} which
595 satisfy the predicate @var{pred}, and one list containing the elements
596 which do not satisfy the predicated. The elements in the result lists
597 have the same order as in @var{lst}. The order in which @var{pred} is
598 applied to the list elements is not specified.
599
600 @code{partition!} is allowed, but not required to modify the structure of
601 the input list.
602 @end deffn
603
604 @deffn {Scheme Procedure} remove pred lst
605 @deffnx {Scheme Procedure} remove! pred lst
606 Return a list containing all elements from @var{lst} which do not
607 satisfy the predicate @var{pred}. The elements in the result list
608 have the same order as in @var{lst}. The order in which @var{pred} is
609 applied to the list elements is not specified.
610
611 @code{remove!} is allowed, but not required to modify the structure of
612 the input list.
613 @end deffn
614
615
616 @node SRFI-1 Searching
617 @subsection Searching
618
619 @c FIXME::martin: Review me!
620
621 The procedures for searching elements in lists either accept a
622 predicate or a comparison object for determining which elements are to
623 be searched.
624
625 @deffn {Scheme Procedure} find pred lst
626 Return the first element of @var{lst} which satisfies the predicate
627 @var{pred} and @code{#f} if no such element is found.
628 @end deffn
629
630 @deffn {Scheme Procedure} find-tail pred lst
631 Return the first pair of @var{lst} whose @sc{car} satisfies the
632 predicate @var{pred} and @code{#f} if no such element is found.
633 @end deffn
634
635 @deffn {Scheme Procedure} take-while pred lst
636 @deffnx {Scheme Procedure} take-while! pred lst
637 Return the longest initial prefix of @var{lst} whose elements all
638 satisfy the predicate @var{pred}.
639
640 @code{take-while!} is allowed, but not required to modify the input
641 list while producing the result.
642 @end deffn
643
644 @deffn {Scheme Procedure} drop-while pred lst
645 Drop the longest initial prefix of @var{lst} whose elements all
646 satisfy the predicate @var{pred}.
647 @end deffn
648
649 @deffn {Scheme Procedure} span pred lst
650 @deffnx {Scheme Procedure} span! pred lst
651 @deffnx {Scheme Procedure} break pred lst
652 @deffnx {Scheme Procedure} break! pred lst
653 @code{span} splits the list @var{lst} into the longest initial prefix
654 whose elements all satisfy the predicate @var{pred}, and the remaining
655 tail. @code{break} inverts the sense of the predicate.
656
657 @code{span!} and @code{break!} are allowed, but not required to modify
658 the structure of the input list @var{lst} in order to produce the
659 result.
660 @end deffn
661
662 @deffn {Scheme Procedure} any pred lst1 lst2 @dots{}
663 Apply @var{pred} across the lists and return a true value if the
664 predicate returns true for any of the list elements(s); return
665 @code{#f} otherwise. The true value returned is always the result of
666 the first successful application of @var{pred}.
667 @end deffn
668
669 @deffn {Scheme Procedure} every pred lst1 lst2 @dots{}
670 Apply @var{pred} across the lists and return a true value if the
671 predicate returns true for every of the list elements(s); return
672 @code{#f} otherwise. The true value returned is always the result of
673 the final successful application of @var{pred}.
674 @end deffn
675
676 @deffn {Scheme Procedure} list-index pred lst1 lst2 @dots{}
677 Return the index of the leftmost element that satisfies @var{pred}.
678 @end deffn
679
680 @deffn {Scheme Procedure} member x lst [=]
681 Return the first sublist of @var{lst} whose @sc{car} is equal to
682 @var{x}. If @var{x} does no appear in @var{lst}, return @code{#f}.
683 Equality is determined by the equality predicate @var{=}, or
684 @code{equal?} if @var{=} is not given.
685 @end deffn
686
687
688 @node SRFI-1 Deleting
689 @subsection Deleting
690
691 @c FIXME::martin: Review me!
692
693 The procedures for deleting elements from a list either accept a
694 predicate or a comparison object for determining which elements are to
695 be removed.
696
697 @deffn {Scheme Procedure} delete x lst [=]
698 @deffnx {Scheme Procedure} delete! x lst [=]
699 Return a list containing all elements from @var{lst}, but without the
700 elements equal to @var{x}. Equality is determined by the equality
701 predicate @var{=}, which defaults to @code{equal?} if not given.
702
703 @code{delete!} is allowed, but not required to modify the structure of
704 the argument list in order to produce the result.
705 @end deffn
706
707 @deffn {Scheme Procedure} delete-duplicates lst [=]
708 @deffnx {Scheme Procedure} delete-duplicates! lst [=]
709 Return a list containing all elements from @var{lst}, but without
710 duplicate elements. Equality of elements is determined by the
711 equality predicate @var{=}, which defaults to @code{equal?} if not
712 given.
713
714 @code{delete-duplicates!} is allowed, but not required to modify the
715 structure of the argument list in order to produce the result.
716 @end deffn
717
718
719 @node SRFI-1 Association Lists
720 @subsection Association Lists
721
722 @c FIXME::martin: Review me!
723
724 Association lists are described in detail in section @ref{Association
725 Lists}. The present section only documents the additional procedures
726 for dealing with association lists defined by SRFI-1.
727
728 @deffn {Scheme Procedure} assoc key alist [=]
729 Return the pair from @var{alist} which matches @var{key}. Equality is
730 determined by @var{=}, which defaults to @code{equal?} if not given.
731 @var{alist} must be an association lists---a list of pairs.
732 @end deffn
733
734 @deffn {Scheme Procedure} alist-cons key datum alist
735 Equivalent to
736
737 @lisp
738 (cons (cons @var{key} @var{datum}) @var{alist})
739 @end lisp
740
741 This procedure is used to coons a new pair onto an existing
742 association list.
743 @end deffn
744
745 @deffn {Scheme Procedure} alist-copy alist
746 Return a newly allocated copy of @var{alist}, that means that the
747 spine of the list as well as the pairs are copied.
748 @end deffn
749
750 @deffn {Scheme Procedure} alist-delete key alist [=]
751 @deffnx {Scheme Procedure} alist-delete! key alist [=]
752 Return a list containing the pairs of @var{alist}, but without the
753 pairs whose @sc{cars} are equal to @var{key}. Equality is determined
754 by @var{=}, which defaults to @code{equal?} if not given.
755
756 @code{alist-delete!} is allowed, but not required to modify the
757 structure of the list @var{alist} in order to produce the result.
758 @end deffn
759
760
761 @node SRFI-1 Set Operations
762 @subsection Set Operations on Lists
763
764 @c FIXME::martin: Review me!
765
766 Lists can be used for representing sets of objects. The procedures
767 documented in this section can be used for such set representations.
768 Man combining several sets or adding elements, they make sure that no
769 object is contained more than once in a given list. Please note that
770 lists are not a too efficient implementation method for sets, so if
771 you need high performance, you should think about implementing a
772 custom data structure for representing sets, such as trees, bitsets,
773 hash tables or something similar.
774
775 All these procedures accept an equality predicate as the first
776 argument. This predicate is used for testing the objects in the list
777 sets for sameness.
778
779 @deffn {Scheme Procedure} lset<= = list1 @dots{}
780 Return @code{#t} if every @var{listi} is a subset of @var{listi+1},
781 otherwise return @code{#f}. Returns @code{#t} if called with less
782 than two arguments. @var{=} is used for testing element equality.
783 @end deffn
784
785 @deffn {Scheme Procedure} lset= = list1 list2 @dots{}
786 Return @code{#t} if all argument lists are equal. @var{=} is used for
787 testing element equality.
788 @end deffn
789
790 @deffn {Scheme Procedure} lset-adjoin = list elt1 @dots{}
791 @deffnx {Scheme Procedure} lset-adjoin! = list elt1 @dots{}
792 Add all @var{elts} to the list @var{list}, suppressing duplicates and
793 return the resulting list. @code{lset-adjoin!} is allowed, but not
794 required to modify its first argument. @var{=} is used for testing
795 element equality.
796 @end deffn
797
798 @deffn {Scheme Procedure} lset-union = list1 @dots{}
799 @deffnx {Scheme Procedure} lset-union! = list1 @dots{}
800 Return the union of all argument list sets. The union is the set of
801 all elements which appear in any of the argument sets.
802 @code{lset-union!} is allowed, but not required to modify its first
803 argument. @var{=} is used for testing element equality.
804 @end deffn
805
806 @deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{}
807 @deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{}
808 Return the intersection of all argument list sets. The intersection
809 is the set containing all elements which appear in all argument sets.
810 @code{lset-intersection!} is allowed, but not required to modify its
811 first argument. @var{=} is used for testing element equality.
812 @end deffn
813
814 @deffn {Scheme Procedure} lset-difference = list1 list2 @dots{}
815 @deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{}
816 Return the difference of all argument list sets. The difference is
817 the the set containing all elements of the first list which do not
818 appear in the other lists. @code{lset-difference!} is allowed, but
819 not required to modify its first argument. @var{=} is used for testing
820 element equality.
821 @end deffn
822
823 @deffn {Scheme Procedure} lset-xor = list1 @dots{}
824 @deffnx {Scheme Procedure} lset-xor! = list1 @dots{}
825 Return the set containing all elements which appear in the first
826 argument list set, but not in the second; or, more generally: which
827 appear in an odd number of sets. @code{lset-xor!} is allowed, but
828 not required to modify its first argument. @var{=} is used for testing
829 element equality.
830 @end deffn
831
832 @deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{}
833 @deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{}
834 Return two values, the difference and the intersection of the argument
835 list sets. This works like a combination of @code{lset-difference} and
836 @code{lset-intersection}, but is more efficient.
837 @code{lset-diff+intersection!} is allowed, but not required to modify
838 its first argument. @var{=} is used for testing element equality. You
839 have to use some means to deal with the multiple values these
840 procedures return (@pxref{Multiple Values}).
841 @end deffn
842
843
844 @node SRFI-2
845 @section SRFI-2 - and-let*
846 @cindex SRFI-2
847
848 @c FIXME::martin: Review me!
849
850 @findex and-let*
851 The syntactic form @code{and-let*} combines the conditional evaluation
852 form @code{and} with the binding form @var{let*}. Each argument
853 expression will be evaluated sequentially, bound to a variable (if a
854 variable name is given), but only as long as no expression returns
855 the false value @code{#f}.
856
857 Use @code{(use-modules (srfi srfi-2)} to access this syntax form.
858
859 A short example will demonstrate how it works. In the first expression,
860 @var{x} will get bound to 1, but the next expression (@code{#f}) is
861 false, so evaluation of the form is stopped, and @code{#f} is returned.
862 In the next expression, @var{x} is bound to 1, @var{y} is bound to
863 @code{#t} and since no expression in the binding section was false, the
864 body of the @code{and-let*} expression is evaluated, which in this case
865 returns the value of @var{x}.
866
867 @lisp
868 (and-let* ((x 1) (y #f)) 42)
869 @result{}
870 #f
871 (and-let* ((x 1) (y #t)) x)
872 @result{}
873 1
874 @end lisp
875
876
877 @node SRFI-4
878 @section SRFI-4 - Homogeneous numeric vector datatypes.
879 @cindex SRFI-4
880
881 @c FIXME::martin: Review me!
882
883 SRFI-4 defines a set of datatypes for vectors whose elements are all
884 of the same numeric type. Vectors for signed and unsigned exact
885 integer or inexact real numbers in several precisions are available.
886
887 Procedures similar to the vector procedures (@pxref{Vectors}) are
888 provided for handling these homogeneous vectors, but they are distinct
889 datatypes.
890
891 The reason for providing this set of datatypes is that with the
892 limitation (all elements must have the same type), it is possible to
893 implement them much more memory-efficient than normal, heterogenous
894 vectors.
895
896 If you want to use these datatypes and the corresponding procedures,
897 you have to use the module @code{(srfi srfi-4)}.
898
899 Ten vector data types are provided: Unsigned and signed integer values
900 with 8, 16, 32 and 64 bits and floating point values with 32 and 64
901 bits. In the following descriptions, the tags @code{u8}, @code{s8},
902 @code{u16}, @code{s16}, @code{u32}, @code{s32}, @code{u64},
903 @code{s64}, @code{f32}, @code{f64}, respectively, are used for
904 denoting the various types.
905
906 @menu
907 * SRFI-4 - Read Syntax:: How to write homogeneous vector literals.
908 * SRFI-4 - Procedures:: Available homogeneous vector procedures.
909 @end menu
910
911
912 @node SRFI-4 - Read Syntax
913 @subsection SRFI-4 - Read Syntax
914
915 Homogeneous numeric vectors have an external representation (read
916 syntax) similar to normal Scheme vectors, but with an additional tag
917 telling the vector's type.
918
919 @lisp
920 #u16(1 2 3)
921 @end lisp
922
923 denotes a homogeneous numeric vector of three elements, which are the
924 values 1, 2 and 3, represented as 16-bit unsigned integers.
925 Correspondingly,
926
927 @lisp
928 #f64(3.1415 2.71)
929 @end lisp
930
931 denotes a vector of two elements, which are the values 3.1415 and
932 2.71, represented as floating-point values of 64 bit precision.
933
934 Please note that the read syntax for floating-point vectors conflicts
935 with Standard Scheme, because there @code{#f} is defined to be the
936 literal false value. That means, that with the loaded SRFI-4 module,
937 it is not possible to enter some list like
938
939 @lisp
940 '(1 #f3)
941 @end lisp
942
943 and hope that it will be parsed as a three-element list with the
944 elements 1, @code{#f} and 3. In normal use, this should be no
945 problem, because people tend to terminate tokens sensibly when writing
946 Scheme expressions.
947
948 @node SRFI-4 - Procedures
949 @subsection SRFI-4 Procedures
950
951 The procedures listed in this section are provided for all homogeneous
952 numeric vector datatypes. For brevity, they are not all documented,
953 but a summary of the procedures is given. In the following
954 descriptions, you can replace @code{TAG} by any of the datatype
955 indicators @code{u8}, @code{s8}, @code{u16}, @code{s16}, @code{u32},
956 @code{s32}, @code{u64}, @code{s64}, @code{f32} and @code{f64}.
957
958 For example, you can use the procedures @code{u8vector?},
959 @code{make-s8vector}, @code{u16vector}, @code{u32vector-length},
960 @code{s64vector-ref}, @code{f32vector-set!} or @code{f64vector->list}.
961
962 @deffn {Scheme Procedure} TAGvector? obj
963 Return @code{#t} if @var{obj} is a homogeneous numeric vector of type
964 @code{TAG}.
965 @end deffn
966
967 @deffn {Scheme Procedure} make-TAGvector n [value]
968 Create a newly allocated homogeneous numeric vector of type
969 @code{TAG}, which can hold @var{n} elements. If @var{value} is given,
970 the vector is initialized with the value, otherwise, the contents of
971 the returned vector is not specified.
972 @end deffn
973
974 @deffn {Scheme Procedure} TAGvector value1 @dots{}
975 Create a newly allocated homogeneous numeric vector of type
976 @code{TAG}. The returned vector is as long as the number of arguments
977 given, and is initialized with the argument values.
978 @end deffn
979
980 @deffn {Scheme Procedure} TAGvector-length TAGvec
981 Return the number of elements in @var{TAGvec}.
982 @end deffn
983
984 @deffn {Scheme Procedure} TAGvector-ref TAGvec i
985 Return the element at index @var{i} in @var{TAGvec}.
986 @end deffn
987
988 @deffn {Scheme Procedure} TAGvector-ref TAGvec i value
989 Set the element at index @var{i} in @var{TAGvec} to @var{value}. The
990 return value is not specified.
991 @end deffn
992
993 @deffn {Scheme Procedure} TAGvector->list TAGvec
994 Return a newly allocated list holding all elements of @var{TAGvec}.
995 @end deffn
996
997 @deffn {Scheme Procedure} list->TAGvector lst
998 Return a newly allocated homogeneous numeric vector of type @code{TAG},
999 initialized with the elements of the list @var{lst}.
1000 @end deffn
1001
1002
1003 @node SRFI-6
1004 @section SRFI-6 - Basic String Ports
1005 @cindex SRFI-6
1006
1007 SRFI-6 defines the procedures @code{open-input-string},
1008 @code{open-output-string} and @code{get-output-string}. These
1009 procedures are included in the Guile core, so using this module does not
1010 make any difference at the moment. But it is possible that support for
1011 SRFI-6 will be factored out of the core library in the future, so using
1012 this module does not hurt, after all.
1013
1014 @node SRFI-8
1015 @section SRFI-8 - receive
1016 @cindex SRFI-8
1017
1018 @code{receive} is a syntax for making the handling of multiple-value
1019 procedures easier. It is documented in @xref{Multiple Values}.
1020
1021
1022 @node SRFI-9
1023 @section SRFI-9 - define-record-type
1024 @cindex SRFI-9
1025
1026 This is the SRFI way for defining record types. The Guile
1027 implementation is a layer above Guile's normal record construction
1028 procedures (@pxref{Records}). The nice thing about this kind of record
1029 definition method is that no new names are implicitly created, all
1030 constructor, accessor and predicates are explicitly given. This reduces
1031 the risk of variable capture.
1032
1033 The syntax of a record type definition is:
1034
1035 @example
1036 @group
1037 <record type definition>
1038 -> (define-record-type <type name>
1039 (<constructor name> <field tag> ...)
1040 <predicate name>
1041 <field spec> ...)
1042 <field spec> -> (<field tag> <accessor name>)
1043 -> (<field tag> <accessor name> <modifier name>)
1044 <field tag> -> <identifier>
1045 <... name> -> <identifier>
1046 @end group
1047 @end example
1048
1049 Usage example:
1050
1051 @example
1052 guile> (use-modules (srfi srfi-9))
1053 guile> (define-record-type :foo (make-foo x) foo?
1054 (x get-x) (y get-y set-y!))
1055 guile> (define f (make-foo 1))
1056 guile> f
1057 #<:foo x: 1 y: #f>
1058 guile> (get-x f)
1059 1
1060 guile> (set-y! f 2)
1061 2
1062 guile> (get-y f)
1063 2
1064 guile> f
1065 #<:foo x: 1 y: 2>
1066 guile> (foo? f)
1067 #t
1068 guile> (foo? 1)
1069 #f
1070 @end example
1071
1072
1073 @node SRFI-10
1074 @section SRFI-10 - Hash-Comma Reader Extension
1075 @cindex SRFI-10
1076
1077 @cindex hash-comma
1078 @cindex #,()
1079 The module @code{(srfi srfi-10)} implements the syntax extension
1080 @code{#,()}, also called hash-comma, which is defined in SRFI-10.
1081
1082 The support for SRFI-10 consists of the procedure
1083 @code{define-reader-ctor} for defining new reader constructors and the
1084 read syntax form
1085
1086 @example
1087 #,(@var{ctor} @var{datum} ...)
1088 @end example
1089
1090 where @var{ctor} must be a symbol for which a read constructor was
1091 defined previously, using @code{define-reader-ctor}.
1092
1093 Example:
1094
1095 @lisp
1096 (use-modules (ice-9 rdelim)) ; for read-line
1097 (define-reader-ctor 'file open-input-file)
1098 (define f '#,(file "/etc/passwd"))
1099 (read-line f)
1100 @result{}
1101 "root:x:0:0:root:/root:/bin/bash"
1102 @end lisp
1103
1104 Please note the quote before the @code{#,(file ...)} expression. This
1105 is necessary because ports are not self-evaluating in Guile.
1106
1107 @deffn {Scheme Procedure} define-reader-ctor symbol proc
1108 Define @var{proc} as the reader constructor for hash-comma forms with a
1109 tag @var{symbol}. @var{proc} will be applied to the datum(s) following
1110 the tag in the hash-comma expression after the complete form has been
1111 read in. The result of @var{proc} is returned by the Scheme reader.
1112 @end deffn
1113
1114
1115 @node SRFI-11
1116 @section SRFI-11 - let-values
1117 @cindex SRFI-11
1118
1119 @findex let-values
1120 @findex let-values*
1121 This module implements the binding forms for multiple values
1122 @code{let-values} and @code{let-values*}. These forms are similar to
1123 @code{let} and @code{let*} (@pxref{Local Bindings}), but they support
1124 binding of the values returned by multiple-valued expressions.
1125
1126 Write @code{(use-modules (srfi srfi-11))} to make the bindings
1127 available.
1128
1129 @lisp
1130 (let-values (((x y) (values 1 2))
1131 ((z f) (values 3 4)))
1132 (+ x y z f))
1133 @result{}
1134 10
1135 @end lisp
1136
1137 @code{let-values} performs all bindings simultaneously, which means that
1138 no expression in the binding clauses may refer to variables bound in the
1139 same clause list. @code{let-values*}, on the other hand, performs the
1140 bindings sequentially, just like @code{let*} does for single-valued
1141 expressions.
1142
1143
1144 @node SRFI-13
1145 @section SRFI-13 - String Library
1146 @cindex SRFI-13
1147
1148 In this section, we will describe all procedures defined in SRFI-13
1149 (string library) and implemented by the module @code{(srfi srfi-13)}.
1150
1151 Note that only the procedures from SRFI-13 are documented here which are
1152 not already contained in Guile. For procedures not documented here
1153 please refer to the relevant chapters in the Guile Reference Manual, for
1154 example the documentation of strings and string procedures
1155 (@pxref{Strings}).
1156
1157 All of the procedures defined in SRFI-13, which are not already
1158 included in the Guile core library, are implemented in the module
1159 @code{(srfi srfi-13)}. The procedures which are both in Guile and in
1160 SRFI-13 are slightly extended in this module. Their bindings
1161 overwrite those in the Guile core.
1162
1163 The procedures which are defined in the section @emph{Low-level
1164 procedures} of SRFI-13 for parsing optional string indices, substring
1165 specification checking and Knuth-Morris-Pratt-Searching are not
1166 implemented.
1167
1168 The procedures @code{string-contains} and @code{string-contains-ci} are
1169 not implemented very efficiently at the moment. This will be changed as
1170 soon as possible.
1171
1172 @menu
1173 * Loading SRFI-13:: How to load SRFI-13 support.
1174 * SRFI-13 Predicates:: String predicates.
1175 * SRFI-13 Constructors:: String constructing procedures.
1176 * SRFI-13 List/String Conversion:: Conversion from/to lists.
1177 * SRFI-13 Selection:: Selection portions of strings.
1178 * SRFI-13 Modification:: Modify strings in-place.
1179 * SRFI-13 Comparison:: Compare strings.
1180 * SRFI-13 Prefixes/Suffixes:: Detect common pre-/suffixes.
1181 * SRFI-13 Searching:: Searching for substrings.
1182 * SRFI-13 Case Mapping:: Mapping to lower-/upper-case.
1183 * SRFI-13 Reverse/Append:: Reverse and append strings.
1184 * SRFI-13 Fold/Unfold/Map:: Construct/deconstruct strings.
1185 * SRFI-13 Replicate/Rotate:: Replicate and rotate portions of strings.
1186 * SRFI-13 Miscellaneous:: Left-over string procedures.
1187 * SRFI-13 Filtering/Deleting:: Filter and delete characters from strings.
1188 @end menu
1189
1190
1191 @node Loading SRFI-13
1192 @subsection Loading SRFI-13
1193
1194 When Guile is properly installed, SRFI-13 support can be loaded into a
1195 running Guile by using the @code{(srfi srfi-13)} module.
1196
1197 @example
1198 $ guile
1199 guile> (use-modules (srfi srfi-13))
1200 guile>
1201 @end example
1202
1203 When this step causes any errors, Guile is not properly installed.
1204
1205 One possible reason is that Guile cannot find either the Scheme module
1206 file @file{srfi-13.scm}, or it cannot find the shared object file
1207 @file{libguile-srfi-srfi-13-14.so}. Make sure that the former is in the
1208 Guile load path and that the latter is either installed in some default
1209 location like @file{/usr/local/lib} or that the directory it was
1210 installed to is in your @code{LTDL_LIBRARY_PATH}. The same applies to
1211 @file{srfi-14.scm}.
1212
1213 Now you can test whether the SRFI-13 procedures are working by calling
1214 the @code{string-concatenate} procedure.
1215
1216 @example
1217 guile> (string-concatenate '("Hello" " " "World!"))
1218 "Hello World!"
1219 @end example
1220
1221 @node SRFI-13 Predicates
1222 @subsection Predicates
1223
1224 In addition to the primitives @code{string?} and @code{string-null?},
1225 which are already in the Guile core, the string predicates
1226 @code{string-any} and @code{string-every} are defined by SRFI-13.
1227
1228 @deffn {Scheme Procedure} string-any pred s [start end]
1229 Check if the predicate @var{pred} is true for any character in
1230 the string @var{s}, proceeding from left (index @var{start}) to
1231 right (index @var{end}). If @code{string-any} returns true,
1232 the returned true value is the one produced by the first
1233 successful application of @var{pred}.
1234 @end deffn
1235
1236 @deffn {Scheme Procedure} string-every pred s [start end]
1237 Check if the predicate @var{pred} is true for every character
1238 in the string @var{s}, proceeding from left (index @var{start})
1239 to right (index @var{end}). If @code{string-every} returns
1240 true, the returned true value is the one produced by the final
1241 application of @var{pred} to the last character of @var{s}.
1242 @end deffn
1243
1244
1245 @c ===================================================================
1246
1247 @node SRFI-13 Constructors
1248 @subsection Constructors
1249
1250 SRFI-13 defines several procedures for constructing new strings. In
1251 addition to @code{make-string} and @code{string} (available in the Guile
1252 core library), the procedure @code{string-tabulate} does exist.
1253
1254 @deffn {Scheme Procedure} string-tabulate proc len
1255 @var{proc} is an integer->char procedure. Construct a string
1256 of size @var{len} by applying @var{proc} to each index to
1257 produce the corresponding string element. The order in which
1258 @var{proc} is applied to the indices is not specified.
1259 @end deffn
1260
1261
1262 @c ===================================================================
1263
1264 @node SRFI-13 List/String Conversion
1265 @subsection List/String Conversion
1266
1267 The procedure @code{string->list} is extended by SRFI-13, that is why it
1268 is included in @code{(srfi srfi-13)}. The other procedures are new.
1269 The Guile core already contains the procedure @code{list->string} for
1270 converting a list of characters into a string (@pxref{List/String
1271 Conversion}).
1272
1273 @deffn {Scheme Procedure} string->list str [start end]
1274 Convert the string @var{str} into a list of characters.
1275 @end deffn
1276
1277 @deffn {Scheme Procedure} reverse-list->string chrs
1278 An efficient implementation of @code{(compose string->list
1279 reverse)}:
1280
1281 @smalllisp
1282 (reverse-list->string '(#\a #\B #\c)) @result{} "cBa"
1283 @end smalllisp
1284 @end deffn
1285
1286 @deffn {Scheme Procedure} string-join ls [delimiter grammar]
1287 Append the string in the string list @var{ls}, using the string
1288 @var{delim} as a delimiter between the elements of @var{ls}.
1289 @var{grammar} is a symbol which specifies how the delimiter is
1290 placed between the strings, and defaults to the symbol
1291 @code{infix}.
1292
1293 @table @code
1294 @item infix
1295 Insert the separator between list elements. An empty string
1296 will produce an empty list.
1297
1298 @item string-infix
1299 Like @code{infix}, but will raise an error if given the empty
1300 list.
1301
1302 @item suffix
1303 Insert the separator after every list element.
1304
1305 @item prefix
1306 Insert the separator before each list element.
1307 @end table
1308 @end deffn
1309
1310
1311 @c ===================================================================
1312
1313 @node SRFI-13 Selection
1314 @subsection Selection
1315
1316 These procedures are called @dfn{selectors}, because they access
1317 information about the string or select pieces of a given string.
1318
1319 Additional selector procedures are documented in the Strings section
1320 (@pxref{String Selection}), like @code{string-length} or
1321 @code{string-ref}.
1322
1323 @code{string-copy} is also available in core Guile, but this version
1324 accepts additional start/end indices.
1325
1326 @deffn {Scheme Procedure} string-copy str [start end]
1327 Return a freshly allocated copy of the string @var{str}. If
1328 given, @var{start} and @var{end} delimit the portion of
1329 @var{str} which is copied.
1330 @end deffn
1331
1332 @deffn {Scheme Procedure} substring/shared str start [end]
1333 Like @code{substring}, but the result may share memory with the
1334 argument @var{str}.
1335 @end deffn
1336
1337 @deffn {Scheme Procedure} string-copy! target tstart s [start end]
1338 Copy the sequence of characters from index range [@var{start},
1339 @var{end}) in string @var{s} to string @var{target}, beginning
1340 at index @var{tstart}. The characters are copied left-to-right
1341 or right-to-left as needed - the copy is guaranteed to work,
1342 even if @var{target} and @var{s} are the same string. It is an
1343 error if the copy operation runs off the end of the target
1344 string.
1345 @end deffn
1346
1347 @deffn {Scheme Procedure} string-take s n
1348 @deffnx {Scheme Procedure} string-take-right s n
1349 Return the @var{n} first/last characters of @var{s}.
1350 @end deffn
1351
1352 @deffn {Scheme Procedure} string-drop s n
1353 @deffnx {Scheme Procedure} string-drop-right s n
1354 Return all but the first/last @var{n} characters of @var{s}.
1355 @end deffn
1356
1357 @deffn {Scheme Procedure} string-pad s len [chr start end]
1358 @deffnx {Scheme Procedure} string-pad-right s len [chr start end]
1359 Take that characters from @var{start} to @var{end} from the
1360 string @var{s} and return a new string, right(left)-padded by the
1361 character @var{chr} to length @var{len}. If the resulting
1362 string is longer than @var{len}, it is truncated on the right (left).
1363 @end deffn
1364
1365 @deffn {Scheme Procedure} string-trim s [char_pred start end]
1366 @deffnx {Scheme Procedure} string-trim-right s [char_pred start end]
1367 @deffnx {Scheme Procedure} string-trim-both s [char_pred start end]
1368 Trim @var{s} by skipping over all characters on the left/right/both
1369 sides of the string that satisfy the parameter @var{char_pred}:
1370
1371 @itemize @bullet
1372 @item
1373 if it is the character @var{ch}, characters equal to
1374 @var{ch} are trimmed,
1375
1376 @item
1377 if it is a procedure @var{pred} characters that
1378 satisfy @var{pred} are trimmed,
1379
1380 @item
1381 if it is a character set, characters in that set are trimmed.
1382 @end itemize
1383
1384 If called without a @var{char_pred} argument, all whitespace is
1385 trimmed.
1386 @end deffn
1387
1388
1389 @c ===================================================================
1390
1391 @node SRFI-13 Modification
1392 @subsection Modification
1393
1394 The procedure @code{string-fill!} is extended from R5RS because it
1395 accepts optional start/end indices. This bindings shadows the procedure
1396 of the same name in the Guile core. The second modification procedure
1397 @code{string-set!} is documented in the Strings section (@pxref{String
1398 Modification}).
1399
1400 @deffn {Scheme Procedure} string-fill! str chr [start end]
1401 Stores @var{chr} in every element of the given @var{str} and
1402 returns an unspecified value.
1403 @end deffn
1404
1405
1406 @c ===================================================================
1407
1408 @node SRFI-13 Comparison
1409 @subsection Comparison
1410
1411 The procedures in this section are used for comparing strings in
1412 different ways. The comparison predicates differ from those in R5RS in
1413 that they do not only return @code{#t} or @code{#f}, but the mismatch
1414 index in the case of a true return value.
1415
1416 @code{string-hash} and @code{string-hash-ci} are for calculating hash
1417 values for strings, useful for implementing fast lookup mechanisms.
1418
1419 @deffn {Scheme Procedure} string-compare s1 s2 proc_lt proc_eq proc_gt [start1 end1 start2 end2]
1420 @deffnx {Scheme Procedure} string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 end1 start2 end2]
1421 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
1422 mismatch index, depending upon whether @var{s1} is less than,
1423 equal to, or greater than @var{s2}. The mismatch index is the
1424 largest index @var{i} such that for every 0 <= @var{j} <
1425 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] - that is,
1426 @var{i} is the first position that does not match. The
1427 character comparison is done case-insensitively.
1428 @end deffn
1429
1430 @deffn {Scheme Procedure} string= s1 s2 [start1 end1 start2 end2]
1431 @deffnx {Scheme Procedure} string<> s1 s2 [start1 end1 start2 end2]
1432 @deffnx {Scheme Procedure} string< s1 s2 [start1 end1 start2 end2]
1433 @deffnx {Scheme Procedure} string> s1 s2 [start1 end1 start2 end2]
1434 @deffnx {Scheme Procedure} string<= s1 s2 [start1 end1 start2 end2]
1435 @deffnx {Scheme Procedure} string>= s1 s2 [start1 end1 start2 end2]
1436 Compare @var{s1} and @var{s2} and return @code{#f} if the predicate
1437 fails. Otherwise, the mismatch index is returned (or @var{end1} in the
1438 case of @code{string=}.
1439 @end deffn
1440
1441 @deffn {Scheme Procedure} string-ci= s1 s2 [start1 end1 start2 end2]
1442 @deffnx {Scheme Procedure} string-ci<> s1 s2 [start1 end1 start2 end2]
1443 @deffnx {Scheme Procedure} string-ci< s1 s2 [start1 end1 start2 end2]
1444 @deffnx {Scheme Procedure} string-ci> s1 s2 [start1 end1 start2 end2]
1445 @deffnx {Scheme Procedure} string-ci<= s1 s2 [start1 end1 start2 end2]
1446 @deffnx {Scheme Procedure} string-ci>= s1 s2 [start1 end1 start2 end2]
1447 Compare @var{s1} and @var{s2} and return @code{#f} if the predicate
1448 fails. Otherwise, the mismatch index is returned (or @var{end1} in the
1449 case of @code{string=}. These are the case-insensitive variants.
1450 @end deffn
1451
1452 @deffn {Scheme Procedure} string-hash s [bound start end]
1453 @deffnx {Scheme Procedure} string-hash-ci s [bound start end]
1454 Return a hash value of the string @var{s} in the range 0 @dots{}
1455 @var{bound} - 1. @code{string-hash-ci} is the case-insensitive variant.
1456 @end deffn
1457
1458
1459 @c ===================================================================
1460
1461 @node SRFI-13 Prefixes/Suffixes
1462 @subsection Prefixes/Suffixes
1463
1464 Using these procedures you can determine whether a given string is a
1465 prefix or suffix of another string or how long a common prefix/suffix
1466 is.
1467
1468 @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 end1 start2 end2]
1469 @deffnx {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 end1 start2 end2]
1470 @deffnx {Scheme Procedure} string-suffix-length s1 s2 [start1 end1 start2 end2]
1471 @deffnx {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 end1 start2 end2]
1472 Return the length of the longest common prefix/suffix of the two
1473 strings. @code{string-prefix-length-ci} and
1474 @code{string-suffix-length-ci} are the case-insensitive variants.
1475 @end deffn
1476
1477 @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 end1 start2 end2]
1478 @deffnx {Scheme Procedure} string-prefix-ci? s1 s2 [start1 end1 start2 end2]
1479 @deffnx {Scheme Procedure} string-suffix? s1 s2 [start1 end1 start2 end2]
1480 @deffnx {Scheme Procedure} string-suffix-ci? s1 s2 [start1 end1 start2 end2]
1481 Is @var{s1} a prefix/suffix of @var{s2}. @code{string-prefix-ci?} and
1482 @code{string-suffix-ci?} are the case-insensitive variants.
1483 @end deffn
1484
1485
1486 @c ===================================================================
1487
1488 @node SRFI-13 Searching
1489 @subsection Searching
1490
1491 Use these procedures to find out whether a string contains a given
1492 character or a given substring, or a character from a set of characters.
1493
1494 @deffn {Scheme Procedure} string-index s char_pred [start end]
1495 @deffnx {Scheme Procedure} string-index-right s char_pred [start end]
1496 Search through the string @var{s} from left to right (right to left),
1497 returning the index of the first (last) occurrence of a character which
1498
1499 @itemize @bullet
1500 @item
1501 equals @var{char_pred}, if it is character,
1502
1503 @item
1504 satisfies the predicate @var{char_pred}, if it is a
1505 procedure,
1506
1507 @item
1508 is in the set @var{char_pred}, if it is a character set.
1509 @end itemize
1510 @end deffn
1511
1512 @deffn {Scheme Procedure} string-skip s char_pred [start end]
1513 @deffnx {Scheme Procedure} string-skip-right s char_pred [start end]
1514 Search through the string @var{s} from left to right (right to left),
1515 returning the index of the first (last) occurrence of a character which
1516
1517 @itemize @bullet
1518 @item
1519 does not equal @var{char_pred}, if it is character,
1520
1521 @item
1522 does not satisfy the predicate @var{char_pred}, if it is
1523 a procedure.
1524
1525 @item
1526 is not in the set if @var{char_pred} is a character set.
1527 @end itemize
1528 @end deffn
1529
1530 @deffn {Scheme Procedure} string-count s char_pred [start end]
1531 Return the count of the number of characters in the string
1532 @var{s} which
1533
1534 @itemize @bullet
1535 @item
1536 equals @var{char_pred}, if it is character,
1537
1538 @item
1539 satisfies the predicate @var{char_pred}, if it is a procedure.
1540
1541 @item
1542 is in the set @var{char_pred}, if it is a character set.
1543 @end itemize
1544 @end deffn
1545
1546 @deffn {Scheme Procedure} string-contains s1 s2 [start1 end1 start2 end2]
1547 @deffnx {Scheme Procedure} string-contains-ci s1 s2 [start1 end1 start2 end2]
1548 Does string @var{s1} contain string @var{s2}? Return the index
1549 in @var{s1} where @var{s2} occurs as a substring, or false.
1550 The optional start/end indices restrict the operation to the
1551 indicated substrings.
1552
1553 @code{string-contains-ci} is the case-insensitive variant.
1554 @end deffn
1555
1556
1557 @c ===================================================================
1558
1559 @node SRFI-13 Case Mapping
1560 @subsection Alphabetic Case Mapping
1561
1562 These procedures convert the alphabetic case of strings. They are
1563 similar to the procedures in the Guile core, but are extended to handle
1564 optional start/end indices.
1565
1566 @deffn {Scheme Procedure} string-upcase s [start end]
1567 @deffnx {Scheme Procedure} string-upcase! s [start end]
1568 Upcase every character in @var{s}. @code{string-upcase!} is the
1569 side-effecting variant.
1570 @end deffn
1571
1572 @deffn {Scheme Procedure} string-downcase s [start end]
1573 @deffnx {Scheme Procedure} string-downcase! s [start end]
1574 Downcase every character in @var{s}. @code{string-downcase!} is the
1575 side-effecting variant.
1576 @end deffn
1577
1578 @deffn {Scheme Procedure} string-titlecase s [start end]
1579 @deffnx {Scheme Procedure} string-titlecase! s [start end]
1580 Upcase every first character in every word in @var{s}, downcase the
1581 other characters. @code{string-titlecase!} is the side-effecting
1582 variant.
1583 @end deffn
1584
1585
1586 @c ===================================================================
1587
1588 @node SRFI-13 Reverse/Append
1589 @subsection Reverse/Append
1590
1591 One appending procedure, @code{string-append} is the same in R5RS and in
1592 SRFI-13, so it is not redefined.
1593
1594 @deffn {Scheme Procedure} string-reverse str [start end]
1595 @deffnx {Scheme Procedure} string-reverse! str [start end]
1596 Reverse the string @var{str}. The optional arguments
1597 @var{start} and @var{end} delimit the region of @var{str} to
1598 operate on.
1599
1600 @code{string-reverse!} modifies the argument string and returns an
1601 unspecified value.
1602 @end deffn
1603
1604 @deffn {Scheme Procedure} string-append/shared ls @dots{}
1605 Like @code{string-append}, but the result may share memory
1606 with the argument strings.
1607 @end deffn
1608
1609 @deffn {Scheme Procedure} string-concatenate ls
1610 Append the elements of @var{ls} (which must be strings)
1611 together into a single string. Guaranteed to return a freshly
1612 allocated string.
1613 @end deffn
1614
1615 @deffn {Scheme Procedure} string-concatenate/shared ls
1616 Like @code{string-concatenate}, but the result may share memory
1617 with the strings in the list @var{ls}.
1618 @end deffn
1619
1620 @deffn {Scheme Procedure} string-concatenate-reverse ls final_string end
1621 Without optional arguments, this procedure is equivalent to
1622
1623 @smalllisp
1624 (string-concatenate (reverse ls))
1625 @end smalllisp
1626
1627 If the optional argument @var{final_string} is specified, it is
1628 consed onto the beginning to @var{ls} before performing the
1629 list-reverse and string-concatenate operations. If @var{end}
1630 is given, only the characters of @var{final_string} up to index
1631 @var{end} are used.
1632
1633 Guaranteed to return a freshly allocated string.
1634 @end deffn
1635
1636 @deffn {Scheme Procedure} string-concatenate-reverse/shared ls final_string end
1637 Like @code{string-concatenate-reverse}, but the result may
1638 share memory with the the strings in the @var{ls} arguments.
1639 @end deffn
1640
1641
1642 @c ===================================================================
1643
1644 @node SRFI-13 Fold/Unfold/Map
1645 @subsection Fold/Unfold/Map
1646
1647 @code{string-map}, @code{string-for-each} etc. are for iterating over
1648 the characters a string is composed of. The fold and unfold procedures
1649 are list iterators and constructors.
1650
1651 @deffn {Scheme Procedure} string-map proc s [start end]
1652 @var{proc} is a char->char procedure, it is mapped over
1653 @var{s}. The order in which the procedure is applied to the
1654 string elements is not specified.
1655 @end deffn
1656
1657 @deffn {Scheme Procedure} string-map! proc s [start end]
1658 @var{proc} is a char->char procedure, it is mapped over
1659 @var{s}. The order in which the procedure is applied to the
1660 string elements is not specified. The string @var{s} is
1661 modified in-place, the return value is not specified.
1662 @end deffn
1663
1664 @deffn {Scheme Procedure} string-fold kons knil s [start end]
1665 @deffnx {Scheme Procedure} string-fold-right kons knil s [start end]
1666 Fold @var{kons} over the characters of @var{s}, with @var{knil} as the
1667 terminating element, from left to right (or right to left, for
1668 @code{string-fold-right}). @var{kons} must expect two arguments: The
1669 actual character and the last result of @var{kons}' application.
1670 @end deffn
1671
1672 @deffn {Scheme Procedure} string-unfold p f g seed [base make_final]
1673 @deffnx {Scheme Procedure} string-unfold-right p f g seed [base make_final]
1674 These are the fundamental string constructors.
1675 @itemize @bullet
1676 @item @var{g} is used to generate a series of @emph{seed}
1677 values from the initial @var{seed}: @var{seed}, (@var{g}
1678 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
1679 @dots{}
1680 @item @var{p} tells us when to stop - when it returns true
1681 when applied to one of these seed values.
1682 @item @var{f} maps each seed value to the corresponding
1683 character in the result string. These chars are assembled into the
1684 string in a left-to-right (right-to-left) order.
1685 @item @var{base} is the optional initial/leftmost (rightmost)
1686 portion of the constructed string; it default to the empty string.
1687 @item @var{make_final} is applied to the terminal seed
1688 value (on which @var{p} returns true) to produce the final/rightmost
1689 (leftmost) portion of the constructed string. It defaults to
1690 @code{(lambda (x) "")}.
1691 @end itemize
1692 @end deffn
1693
1694 @deffn {Scheme Procedure} string-for-each proc s [start end]
1695 @var{proc} is mapped over @var{s} in left-to-right order. The
1696 return value is not specified.
1697 @end deffn
1698
1699
1700 @c ===================================================================
1701
1702 @node SRFI-13 Replicate/Rotate
1703 @subsection Replicate/Rotate
1704
1705 These procedures are special substring procedures, which can also be
1706 used for replicating strings. They are a bit tricky to use, but
1707 consider this code fragment, which replicates the input string
1708 @code{"foo"} so often that the resulting string has a length of six.
1709
1710 @lisp
1711 (xsubstring "foo" 0 6)
1712 @result{}
1713 "foofoo"
1714 @end lisp
1715
1716 @deffn {Scheme Procedure} xsubstring s from [to start end]
1717 This is the @emph{extended substring} procedure that implements
1718 replicated copying of a substring of some string.
1719
1720 @var{s} is a string, @var{start} and @var{end} are optional
1721 arguments that demarcate a substring of @var{s}, defaulting to
1722 0 and the length of @var{s}. Replicate this substring up and
1723 down index space, in both the positive and negative directions.
1724 @code{xsubstring} returns the substring of this string
1725 beginning at index @var{from}, and ending at @var{to}, which
1726 defaults to @var{from} + (@var{end} - @var{start}).
1727 @end deffn
1728
1729 @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto start end]
1730 Exactly the same as @code{xsubstring}, but the extracted text
1731 is written into the string @var{target} starting at index
1732 @var{tstart}. The operation is not defined if @code{(eq?
1733 @var{target} @var{s})} or these arguments share storage - you
1734 cannot copy a string on top of itself.
1735 @end deffn
1736
1737
1738 @c ===================================================================
1739
1740 @node SRFI-13 Miscellaneous
1741 @subsection Miscellaneous
1742
1743 @code{string-replace} is for replacing a portion of a string with
1744 another string and @code{string-tokenize} splits a string into a list of
1745 strings, breaking it up at a specified character.
1746
1747 @deffn {Scheme Procedure} string-replace s1 s2 [start1 end1 start2 end2]
1748 Return the string @var{s1}, but with the characters
1749 @var{start1} @dots{} @var{end1} replaced by the characters
1750 @var{start2} @dots{} @var{end2} from @var{s2}.
1751 @end deffn
1752
1753 @deffn {Scheme Procedure} string-tokenize s [token-set start end]
1754 Split the string @var{s} into a list of substrings, where each
1755 substring is a maximal non-empty contiguous sequence of characters
1756 from the character set @var{token_set}, which defaults to an
1757 equivalent of @code{char-set:graphic}. If @var{start} or @var{end}
1758 indices are provided, they restrict @code{string-tokenize} to
1759 operating on the indicated substring of @var{s}.
1760 @end deffn
1761
1762
1763 @c ===================================================================
1764
1765 @node SRFI-13 Filtering/Deleting
1766 @subsection Filtering/Deleting
1767
1768 @dfn{Filtering} means to remove all characters from a string which do
1769 not match a given criteria, @dfn{deleting} means the opposite.
1770
1771 @deffn {Scheme Procedure} string-filter s char_pred [start end]
1772 Filter the string @var{s}, retaining only those characters that
1773 satisfy the @var{char_pred} argument. If the argument is a
1774 procedure, it is applied to each character as a predicate, if
1775 it is a character, it is tested for equality and if it is a
1776 character set, it is tested for membership.
1777 @end deffn
1778
1779 @deffn {Scheme Procedure} string-delete s char_pred [start end]
1780 Filter the string @var{s}, retaining only those characters that
1781 do not satisfy the @var{char_pred} argument. If the argument
1782 is a procedure, it is applied to each character as a predicate,
1783 if it is a character, it is tested for equality and if it is a
1784 character set, it is tested for membership.
1785 @end deffn
1786
1787
1788 @node SRFI-14
1789 @section SRFI-14 - Character-set Library
1790 @cindex SRFI-14
1791
1792 SRFI-14 defines the data type @dfn{character set}, and also defines a
1793 lot of procedures for handling this character type, and a few standard
1794 character sets like whitespace, alphabetic characters and others.
1795
1796 All procedures from SRFI-14 (character-set library) are implemented in
1797 the module @code{(srfi srfi-14)}, as well as the standard variables
1798 @code{char-set:letter}, @code{char-set:digit} etc.
1799
1800 @menu
1801 * Loading SRFI-14:: How to make charsets available.
1802 * SRFI-14 Character Set Data Type:: Underlying data type for charsets.
1803 * SRFI-14 Predicates/Comparison:: Charset predicates.
1804 * SRFI-14 Iterating Over Character Sets:: Enumerate charset elements.
1805 * SRFI-14 Creating Character Sets:: Making new charsets.
1806 * SRFI-14 Querying Character Sets:: Test charsets for membership etc.
1807 * SRFI-14 Character-Set Algebra:: Calculating new charsets.
1808 * SRFI-14 Standard Character Sets:: Variables containing predefined charsets.
1809 @end menu
1810
1811
1812 @node Loading SRFI-14
1813 @subsection Loading SRFI-14
1814
1815 When Guile is properly installed, SRFI-14 support can be loaded into a
1816 running Guile by using the @code{(srfi srfi-14)} module.
1817
1818 @example
1819 $ guile
1820 guile> (use-modules (srfi srfi-14))
1821 guile> (char-set-union (char-set #\f #\o #\o) (string->char-set "bar"))
1822 #<charset @{#\a #\b #\f #\o #\r@}>
1823 guile>
1824 @end example
1825
1826
1827 @node SRFI-14 Character Set Data Type
1828 @subsection Character Set Data Type
1829
1830 The data type @dfn{charset} implements sets of characters
1831 (@pxref{Characters}). Because the internal representation of character
1832 sets is not visible to the user, a lot of procedures for handling them
1833 are provided.
1834
1835 Character sets can be created, extended, tested for the membership of a
1836 characters and be compared to other character sets.
1837
1838 The Guile implementation of character sets deals with 8-bit characters.
1839 In the standard variables, only the ASCII part of the character range is
1840 really used, so that for example @dfn{Umlaute} and other accented
1841 characters are not considered to be letters. In the future, as Guile
1842 may get support for international character sets, this will change, so
1843 don't rely on these ``features''.
1844
1845
1846 @c ===================================================================
1847
1848 @node SRFI-14 Predicates/Comparison
1849 @subsection Predicates/Comparison
1850
1851 Use these procedures for testing whether an object is a character set,
1852 or whether several character sets are equal or subsets of each other.
1853 @code{char-set-hash} can be used for calculating a hash value, maybe for
1854 usage in fast lookup procedures.
1855
1856 @deffn {Scheme Procedure} char-set? obj
1857 Return @code{#t} if @var{obj} is a character set, @code{#f}
1858 otherwise.
1859 @end deffn
1860
1861 @deffn {Scheme Procedure} char-set= cs1 @dots{}
1862 Return @code{#t} if all given character sets are equal.
1863 @end deffn
1864
1865 @deffn {Scheme Procedure} char-set<= cs1 @dots{}
1866 Return @code{#t} if every character set @var{cs}i is a subset
1867 of character set @var{cs}i+1.
1868 @end deffn
1869
1870 @deffn {Scheme Procedure} char-set-hash cs [bound]
1871 Compute a hash value for the character set @var{cs}. If
1872 @var{bound} is given and not @code{#f}, it restricts the
1873 returned value to the range 0 @dots{} @var{bound - 1}.
1874 @end deffn
1875
1876
1877 @c ===================================================================
1878
1879 @node SRFI-14 Iterating Over Character Sets
1880 @subsection Iterating Over Character Sets
1881
1882 Character set cursors are a means for iterating over the members of a
1883 character sets. After creating a character set cursor with
1884 @code{char-set-cursor}, a cursor can be dereferenced with
1885 @code{char-set-ref}, advanced to the next member with
1886 @code{char-set-cursor-next}. Whether a cursor has passed past the last
1887 element of the set can be checked with @code{end-of-char-set?}.
1888
1889 Additionally, mapping and (un-)folding procedures for character sets are
1890 provided.
1891
1892 @deffn {Scheme Procedure} char-set-cursor cs
1893 Return a cursor into the character set @var{cs}.
1894 @end deffn
1895
1896 @deffn {Scheme Procedure} char-set-ref cs cursor
1897 Return the character at the current cursor position
1898 @var{cursor} in the character set @var{cs}. It is an error to
1899 pass a cursor for which @code{end-of-char-set?} returns true.
1900 @end deffn
1901
1902 @deffn {Scheme Procedure} char-set-cursor-next cs cursor
1903 Advance the character set cursor @var{cursor} to the next
1904 character in the character set @var{cs}. It is an error if the
1905 cursor given satisfies @code{end-of-char-set?}.
1906 @end deffn
1907
1908 @deffn {Scheme Procedure} end-of-char-set? cursor
1909 Return @code{#t} if @var{cursor} has reached the end of a
1910 character set, @code{#f} otherwise.
1911 @end deffn
1912
1913 @deffn {Scheme Procedure} char-set-fold kons knil cs
1914 Fold the procedure @var{kons} over the character set @var{cs},
1915 initializing it with @var{knil}.
1916 @end deffn
1917
1918 @deffn {Scheme Procedure} char-set-unfold p f g seed [base_cs]
1919 @deffnx {Scheme Procedure} char-set-unfold! p f g seed base_cs
1920 This is a fundamental constructor for character sets.
1921 @itemize @bullet
1922 @item @var{g} is used to generate a series of ``seed'' values
1923 from the initial seed: @var{seed}, (@var{g} @var{seed}),
1924 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
1925 @item @var{p} tells us when to stop -- when it returns true
1926 when applied to one of the seed values.
1927 @item @var{f} maps each seed value to a character. These
1928 characters are added to the base character set @var{base_cs} to
1929 form the result; @var{base_cs} defaults to the empty set.
1930 @end itemize
1931
1932 @code{char-set-unfold!} is the side-effecting variant.
1933 @end deffn
1934
1935 @deffn {Scheme Procedure} char-set-for-each proc cs
1936 Apply @var{proc} to every character in the character set
1937 @var{cs}. The return value is not specified.
1938 @end deffn
1939
1940 @deffn {Scheme Procedure} char-set-map proc cs
1941 Map the procedure @var{proc} over every character in @var{cs}.
1942 @var{proc} must be a character -> character procedure.
1943 @end deffn
1944
1945
1946 @c ===================================================================
1947
1948 @node SRFI-14 Creating Character Sets
1949 @subsection Creating Character Sets
1950
1951 New character sets are produced with these procedures.
1952
1953 @deffn {Scheme Procedure} char-set-copy cs
1954 Return a newly allocated character set containing all
1955 characters in @var{cs}.
1956 @end deffn
1957
1958 @deffn {Scheme Procedure} char-set char1 @dots{}
1959 Return a character set containing all given characters.
1960 @end deffn
1961
1962 @deffn {Scheme Procedure} list->char-set char_list [base_cs]
1963 @deffnx {Scheme Procedure} list->char-set! char_list base_cs
1964 Convert the character list @var{list} to a character set. If
1965 the character set @var{base_cs} is given, the character in this
1966 set are also included in the result.
1967
1968 @code{list->char-set!} is the side-effecting variant.
1969 @end deffn
1970
1971 @deffn {Scheme Procedure} string->char-set s [base_cs]
1972 @deffnx {Scheme Procedure} string->char-set! s base_cs
1973 Convert the string @var{str} to a character set. If the
1974 character set @var{base_cs} is given, the characters in this
1975 set are also included in the result.
1976
1977 @code{string->char-set!} is the side-effecting variant.
1978 @end deffn
1979
1980 @deffn {Scheme Procedure} char-set-filter pred cs [base_cs]
1981 @deffnx {Scheme Procedure} char-set-filter! pred cs base_cs
1982 Return a character set containing every character from @var{cs}
1983 so that it satisfies @var{pred}. If provided, the characters
1984 from @var{base_cs} are added to the result.
1985
1986 @code{char-set-filter!} is the side-effecting variant.
1987 @end deffn
1988
1989 @deffn {Scheme Procedure} ucs-range->char-set lower upper [error? base_cs]
1990 @deffnx {Scheme Procedure} uce-range->char-set! lower upper error? base_cs
1991 Return a character set containing all characters whose
1992 character codes lie in the half-open range
1993 [@var{lower},@var{upper}).
1994
1995 If @var{error} is a true value, an error is signalled if the
1996 specified range contains characters which are not contained in
1997 the implemented character range. If @var{error} is @code{#f},
1998 these characters are silently left out of the resulting
1999 character set.
2000
2001 The characters in @var{base_cs} are added to the result, if
2002 given.
2003
2004 @code{ucs-range->char-set!} is the side-effecting variant.
2005 @end deffn
2006
2007 @deffn {Scheme Procedure} ->char-set x
2008 Coerce @var{x} into a character set. @var{x} may be a string, a
2009 character or a character set.
2010 @end deffn
2011
2012
2013 @c ===================================================================
2014
2015 @node SRFI-14 Querying Character Sets
2016 @subsection Querying Character Sets
2017
2018 Access the elements and other information of a character set with these
2019 procedures.
2020
2021 @deffn {Scheme Procedure} char-set-size cs
2022 Return the number of elements in character set @var{cs}.
2023 @end deffn
2024
2025 @deffn {Scheme Procedure} char-set-count pred cs
2026 Return the number of the elements int the character set
2027 @var{cs} which satisfy the predicate @var{pred}.
2028 @end deffn
2029
2030 @deffn {Scheme Procedure} char-set->list cs
2031 Return a list containing the elements of the character set
2032 @var{cs}.
2033 @end deffn
2034
2035 @deffn {Scheme Procedure} char-set->string cs
2036 Return a string containing the elements of the character set
2037 @var{cs}. The order in which the characters are placed in the
2038 string is not defined.
2039 @end deffn
2040
2041 @deffn {Scheme Procedure} char-set-contains? cs char
2042 Return @code{#t} iff the character @var{ch} is contained in the
2043 character set @var{cs}.
2044 @end deffn
2045
2046 @deffn {Scheme Procedure} char-set-every pred cs
2047 Return a true value if every character in the character set
2048 @var{cs} satisfies the predicate @var{pred}.
2049 @end deffn
2050
2051 @deffn {Scheme Procedure} char-set-any pred cs
2052 Return a true value if any character in the character set
2053 @var{cs} satisfies the predicate @var{pred}.
2054 @end deffn
2055
2056
2057 @c ===================================================================
2058
2059 @node SRFI-14 Character-Set Algebra
2060 @subsection Character-Set Algebra
2061
2062 Character sets can be manipulated with the common set algebra operation,
2063 such as union, complement, intersection etc. All of these procedures
2064 provide side-effecting variants, which modify their character set
2065 argument(s).
2066
2067 @deffn {Scheme Procedure} char-set-adjoin cs char1 @dots{}
2068 @deffnx {Scheme Procedure} char-set-adjoin! cs char1 @dots{}
2069 Add all character arguments to the first argument, which must
2070 be a character set.
2071 @end deffn
2072
2073 @deffn {Scheme Procedure} char-set-delete cs char1 @dots{}
2074 @deffnx {Scheme Procedure} char-set-delete! cs char1 @dots{}
2075 Delete all character arguments from the first argument, which
2076 must be a character set.
2077 @end deffn
2078
2079 @deffn {Scheme Procedure} char-set-complement cs
2080 @deffnx {Scheme Procedure} char-set-complement! cs
2081 Return the complement of the character set @var{cs}.
2082 @end deffn
2083
2084 @deffn {Scheme Procedure} char-set-union cs1 @dots{}
2085 @deffnx {Scheme Procedure} char-set-union! cs1 @dots{}
2086 Return the union of all argument character sets.
2087 @end deffn
2088
2089 @deffn {Scheme Procedure} char-set-intersection cs1 @dots{}
2090 @deffnx {Scheme Procedure} char-set-intersection! cs1 @dots{}
2091 Return the intersection of all argument character sets.
2092 @end deffn
2093
2094 @deffn {Scheme Procedure} char-set-difference cs1 @dots{}
2095 @deffnx {Scheme Procedure} char-set-difference! cs1 @dots{}
2096 Return the difference of all argument character sets.
2097 @end deffn
2098
2099 @deffn {Scheme Procedure} char-set-xor cs1 @dots{}
2100 @deffnx {Scheme Procedure} char-set-xor! cs1 @dots{}
2101 Return the exclusive-or of all argument character sets.
2102 @end deffn
2103
2104 @deffn {Scheme Procedure} char-set-diff+intersection cs1 @dots{}
2105 @deffnx {Scheme Procedure} char-set-diff+intersection! cs1 @dots{}
2106 Return the difference and the intersection of all argument
2107 character sets.
2108 @end deffn
2109
2110
2111 @c ===================================================================
2112
2113 @node SRFI-14 Standard Character Sets
2114 @subsection Standard Character Sets
2115
2116 In order to make the use of the character set data type and procedures
2117 useful, several predefined character set variables exist.
2118
2119 @defvar char-set:lower-case
2120 All lower-case characters.
2121 @end defvar
2122
2123 @defvar char-set:upper-case
2124 All upper-case characters.
2125 @end defvar
2126
2127 @defvar char-set:title-case
2128 This is empty, because ASCII has no titlecase characters.
2129 @end defvar
2130
2131 @defvar char-set:letter
2132 All letters, e.g. the union of @code{char-set:lower-case} and
2133 @code{char-set:upper-case}.
2134 @end defvar
2135
2136 @defvar char-set:digit
2137 All digits.
2138 @end defvar
2139
2140 @defvar char-set:letter+digit
2141 The union of @code{char-set:letter} and @code{char-set:digit}.
2142 @end defvar
2143
2144 @defvar char-set:graphic
2145 All characters which would put ink on the paper.
2146 @end defvar
2147
2148 @defvar char-set:printing
2149 The union of @code{char-set:graphic} and @code{char-set:whitespace}.
2150 @end defvar
2151
2152 @defvar char-set:whitespace
2153 All whitespace characters.
2154 @end defvar
2155
2156 @defvar char-set:blank
2157 All horizontal whitespace characters, that is @code{#\space} and
2158 @code{#\tab}.
2159 @end defvar
2160
2161 @defvar char-set:iso-control
2162 The ISO control characters with the codes 0--31 and 127.
2163 @end defvar
2164
2165 @defvar char-set:punctuation
2166 The characters @code{!"#%&'()*,-./:;?@@[\\]_@{@}}
2167 @end defvar
2168
2169 @defvar char-set:symbol
2170 The characters @code{$+<=>^`|~}.
2171 @end defvar
2172
2173 @defvar char-set:hex-digit
2174 The hexadecimal digits @code{0123456789abcdefABCDEF}.
2175 @end defvar
2176
2177 @defvar char-set:ascii
2178 All ASCII characters.
2179 @end defvar
2180
2181 @defvar char-set:empty
2182 The empty character set.
2183 @end defvar
2184
2185 @defvar char-set:full
2186 This character set contains all possible characters.
2187 @end defvar
2188
2189 @node SRFI-16
2190 @section SRFI-16 - case-lambda
2191 @cindex SRFI-16
2192
2193 @c FIXME::martin: Review me!
2194
2195 @findex case-lambda
2196 The syntactic form @code{case-lambda} creates procedures, just like
2197 @code{lambda}, but has syntactic extensions for writing procedures of
2198 varying arity easier.
2199
2200 The syntax of the @code{case-lambda} form is defined in the following
2201 EBNF grammar.
2202
2203 @example
2204 @group
2205 <case-lambda>
2206 --> (case-lambda <case-lambda-clause>)
2207 <case-lambda-clause>
2208 --> (<formals> <definition-or-command>*)
2209 <formals>
2210 --> (<identifier>*)
2211 | (<identifier>* . <identifier>)
2212 | <identifier>
2213 @end group
2214 @end example
2215
2216 The value returned by a @code{case-lambda} form is a procedure which
2217 matches the number of actual arguments against the formals in the
2218 various clauses, in order. @dfn{Formals} means a formal argument list
2219 just like with @code{lambda} (@pxref{Lambda}). The first matching clause
2220 is selected, the corresponding values from the actual parameter list are
2221 bound to the variable names in the clauses and the body of the clause is
2222 evaluated. If no clause matches, an error is signalled.
2223
2224 The following (silly) definition creates a procedure @var{foo} which
2225 acts differently, depending on the number of actual arguments. If one
2226 argument is given, the constant @code{#t} is returned, two arguments are
2227 added and if more arguments are passed, their product is calculated.
2228
2229 @lisp
2230 (define foo (case-lambda
2231 ((x) #t)
2232 ((x y) (+ x y))
2233 (z
2234 (apply * z))))
2235 (foo 'bar)
2236 @result{}
2237 #t
2238 (foo 2 4)
2239 @result{}
2240 6
2241 (foo 3 3 3)
2242 @result{}
2243 27
2244 (foo)
2245 @result{}
2246 1
2247 @end lisp
2248
2249 The last expression evaluates to 1 because the last clause is matched,
2250 @var{z} is bound to the empty list and the following multiplication,
2251 applied to zero arguments, yields 1.
2252
2253
2254 @node SRFI-17
2255 @section SRFI-17 - Generalized set!
2256 @cindex SRFI-17
2257
2258 This is an implementation of SRFI-17: Generalized set!
2259
2260 @findex getter-with-setter
2261 It exports the Guile procedure @code{make-procedure-with-setter} under
2262 the SRFI name @code{getter-with-setter} and exports the standard
2263 procedures @code{car}, @code{cdr}, @dots{}, @code{cdddr},
2264 @code{string-ref} and @code{vector-ref} as procedures with setters, as
2265 required by the SRFI.
2266
2267 SRFI-17 was heavily criticized during its discussion period but it was
2268 finalized anyway. One issue was its concept of globally associating
2269 setter @dfn{properties} with (procedure) values, which is non-Schemy.
2270 For this reason, this implementation chooses not to provide a way to set
2271 the setter of a procedure. In fact, @code{(set! (setter @var{proc})
2272 @var{setter})} signals an error. The only way to attach a setter to a
2273 procedure is to create a new object (a @dfn{procedure with setter}) via
2274 the @code{getter-with-setter} procedure. This procedure is also
2275 specified in the SRFI. Using it avoids the described problems.
2276
2277
2278 @node SRFI-19
2279 @section SRFI-19 - Time/Date Library
2280 @cindex SRFI-19
2281
2282 This is an implementation of SRFI-19: Time/Date Library
2283
2284 It depends on SRFIs: 6 (@pxref{SRFI-6}), 8 (@pxref{SRFI-8}),
2285 9 (@pxref{SRFI-9}).
2286
2287 This section documents constants and procedure signatures.
2288
2289 @menu
2290 * SRFI-19 Constants::
2291 * SRFI-19 Current time and clock resolution::
2292 * SRFI-19 Time object and accessors::
2293 * SRFI-19 Time comparison procedures::
2294 * SRFI-19 Time arithmetic procedures::
2295 * SRFI-19 Date object and accessors::
2296 * SRFI-19 Time/Date/Julian Day/Modified Julian Day converters::
2297 * SRFI-19 Date to string/string to date converters::
2298 @end menu
2299
2300 @node SRFI-19 Constants
2301 @subsection SRFI-19 Constants
2302
2303 All these are bound to their symbol names:
2304
2305 @example
2306 time-duration
2307 time-monotonic
2308 time-process
2309 time-tai
2310 time-thread
2311 time-utc
2312 @end example
2313
2314 @node SRFI-19 Current time and clock resolution
2315 @subsection SRFI-19 Current time and clock resolution
2316
2317 @example
2318 (current-date . tz-offset)
2319 (current-julian-day)
2320 (current-modified-julian-day)
2321 (current-time . clock-type)
2322 (time-resolution . clock-type)
2323 @end example
2324
2325 @node SRFI-19 Time object and accessors
2326 @subsection SRFI-19 Time object and accessors
2327
2328 @example
2329 (make-time type nanosecond second)
2330 (time? obj)
2331 (time-type time)
2332 (time-nanosecond time)
2333 (time-second time)
2334 (set-time-type! time type)
2335 (set-time-nanosecond! time nsec)
2336 (set-time-second! time sec)
2337 (copy-time time)
2338 @end example
2339
2340 @node SRFI-19 Time comparison procedures
2341 @subsection SRFI-19 Time comparison procedures
2342
2343 Args are all @code{time} values.
2344
2345 @example
2346 (time<=? t1 t2)
2347 (time<? t1 t2)
2348 (time=? t1 t2)
2349 (time>=? t1 t2)
2350 (time>? t1 t2)
2351 @end example
2352
2353 @node SRFI-19 Time arithmetic procedures
2354 @subsection SRFI-19 Time arithmetic procedures
2355
2356 The @code{foo!} variants modify in place. Time difference
2357 is expressed in @code{time-duration} values.
2358
2359 @example
2360 (time-difference t1 t2)
2361 (time-difference! t1 t2)
2362 (add-duration time duration)
2363 (add-duration! time duration)
2364 (subtract-duration time duration)
2365 (subtract-duration! time duration)
2366 @end example
2367
2368 @node SRFI-19 Date object and accessors
2369 @subsection SRFI-19 Date object and accessors
2370
2371 @example
2372 (make-date nsecs seconds minutes hours
2373 date month year offset)
2374 (date? obj)
2375 (date-nanosecond date)
2376 (date-second date)
2377 (date-minute date)
2378 (date-hour date)
2379 (date-day date)
2380 (date-month date)
2381 (date-year date)
2382 (date-zone-offset date)
2383 (date-year-day date)
2384 (date-week-day date)
2385 (date-week-number date day-of-week-starting-week)
2386 @end example
2387
2388 @node SRFI-19 Time/Date/Julian Day/Modified Julian Day converters
2389 @subsection SRFI-19 Time/Date/Julian Day/Modified Julian Day converters
2390
2391 @example
2392 (date->julian-day date)
2393 (date->modified-julian-day date)
2394 (date->time-monotonic date)
2395 (date->time-tai date)
2396 (date->time-utc date)
2397 (julian-day->date jdn . tz-offset)
2398 (julian-day->time-monotonic jdn)
2399 (julian-day->time-tai jdn)
2400 (julian-day->time-utc jdn)
2401 (modified-julian-day->date jdn . tz-offset)
2402 (modified-julian-day->time-monotonic jdn)
2403 (modified-julian-day->time-tai jdn)
2404 (modified-julian-day->time-utc jdn)
2405 (time-monotonic->date time . tz-offset)
2406 (time-monotonic->time-tai time-in)
2407 (time-monotonic->time-tai! time-in)
2408 (time-monotonic->time-utc time-in)
2409 (time-monotonic->time-utc! time-in)
2410 (time-tai->date time . tz-offset)
2411 (time-tai->julian-day time)
2412 (time-tai->modified-julian-day time)
2413 (time-tai->time-monotonic time-in)
2414 (time-tai->time-monotonic! time-in)
2415 (time-tai->time-utc time-in)
2416 (time-tai->time-utc! time-in)
2417 (time-utc->date time . tz-offset)
2418 (time-utc->julian-day time)
2419 (time-utc->modified-julian-day time)
2420 (time-utc->time-monotonic time-in)
2421 (time-utc->time-monotonic! time-in)
2422 (time-utc->time-tai time-in)
2423 (time-utc->time-tai! time-in)
2424 @end example
2425
2426 @node SRFI-19 Date to string/string to date converters
2427 @subsection SRFI-19 Date to string/string to date converters
2428
2429 @example
2430 (date->string date . format-string)
2431 (string->date input-string template-string)
2432 @end example
2433
2434 @c srfi-modules.texi ends here