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