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