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