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