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