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