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