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