Moved SRFI-4 docs into main part. Moved bit vectors out of array
[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
6b1a6e4c
KR
895return value, or if @var{body} is empty then the result is @code{#t}.
896Each @var{clause} should be one of the following,
4fd0db14
KR
897
898@table @code
899@item (symbol expr)
900Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}.
901Like @code{let*}, that binding is available to subsequent clauses.
902@item (expr)
903Evaluate @var{expr} and check for @code{#f}.
904@item symbol
905Get the value bound to @var{symbol} and check for @code{#f}.
906@end table
a0e07ba4 907
4fd0db14
KR
908Notice that @code{(expr)} has an ``extra'' pair of parentheses, for
909instance @code{((eq? x y))}. One way to remember this is to imagine
910the @code{symbol} in @code{(symbol expr)} is omitted.
a0e07ba4 911
4fd0db14
KR
912@code{and-let*} is good for calculations where a @code{#f} value means
913termination, but where a non-@code{#f} value is going to be needed in
914subsequent expressions.
915
916The following illustrates this, it returns text between brackets
917@samp{[...]} in a string, or @code{#f} if there are no such brackets
918(ie.@: either @code{string-index} gives @code{#f}).
919
920@example
921(define (extract-brackets str)
922 (and-let* ((start (string-index str #\[))
923 (end (string-index str #\] start)))
924 (substring str (1+ start) end)))
925@end example
926
927The following shows plain variables and expressions tested too.
928@code{diagnostic-levels} is taken to be an alist associating a
929diagnostic type with a level. @code{str} is printed only if the type
930is known and its level is high enough.
931
932@example
933(define (show-diagnostic type str)
934 (and-let* (want-diagnostics
935 (level (assq-ref diagnostic-levels type))
936 ((>= level current-diagnostic-level)))
937 (display str)))
938@end example
939
940The advantage of @code{and-let*} is that an extended sequence of
941expressions and tests doesn't require lots of nesting as would arise
942from separate @code{and} and @code{let*}, or from @code{cond} with
943@code{=>}.
944
945@end deffn
a0e07ba4
NJ
946
947
948@node SRFI-4
3229f68b 949@subsection SRFI-4 - Homogeneous numeric vector datatypes
8742c48b 950@cindex SRFI-4
a0e07ba4 951
e6b226b9
MV
952The SRFI-4 procedures and data types are always available, @xref{Uniform
953Vectors}.
a0e07ba4
NJ
954
955@node SRFI-6
3229f68b 956@subsection SRFI-6 - Basic String Ports
8742c48b 957@cindex SRFI-6
a0e07ba4
NJ
958
959SRFI-6 defines the procedures @code{open-input-string},
960@code{open-output-string} and @code{get-output-string}. These
961procedures are included in the Guile core, so using this module does not
962make any difference at the moment. But it is possible that support for
963SRFI-6 will be factored out of the core library in the future, so using
964this module does not hurt, after all.
965
966@node SRFI-8
3229f68b 967@subsection SRFI-8 - receive
8742c48b 968@cindex SRFI-8
a0e07ba4
NJ
969
970@code{receive} is a syntax for making the handling of multiple-value
971procedures easier. It is documented in @xref{Multiple Values}.
972
973
974@node SRFI-9
3229f68b 975@subsection SRFI-9 - define-record-type
8742c48b 976@cindex SRFI-9
a0e07ba4 977
6afe385d
KR
978This SRFI is a syntax for defining new record types and creating
979predicate, constructor, and field getter and setter functions. In
980Guile this is simply an alternate interface to the core record
981functionality (@pxref{Records}). It can be used with,
a0e07ba4 982
6afe385d
KR
983@example
984(use-modules (srfi srfi-9))
985@end example
986
987@deffn {library syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
988@sp 1
989Create a new record type, and make various @code{define}s for using
990it. This syntax can only occur at the top-level, not nested within
991some other form.
992
993@var{type} is bound to the record type, which is as per the return
994from the core @code{make-record-type}. @var{type} also provides the
995name for the record, as per @code{record-type-name}.
996
997@var{constructor} is bound to a function to be called as
998@code{(@var{constructor} fieldval @dots{})} to create a new record of
999this type. The arguments are initial values for the fields, one
1000argument for each field, in the order they appear in the
1001@code{define-record-type} form.
1002
1003The @var{fieldname}s provide the names for the record fields, as per
1004the core @code{record-type-fields} etc, and are referred to in the
1005subsequent accessor/modifier forms.
1006
1007@var{predictate} is bound to a function to be called as
1008@code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f}
1009according to whether @var{obj} is a record of this type.
1010
1011Each @var{accessor} is bound to a function to be called
1012@code{(@var{accessor} record)} to retrieve the respective field from a
1013@var{record}. Similarly each @var{modifier} is bound to a function to
1014be called @code{(@var{modifier} record val)} to set the respective
1015field in a @var{record}.
1016@end deffn
1017
1018@noindent
1019An example will illustrate typical usage,
a0e07ba4
NJ
1020
1021@example
6afe385d
KR
1022(define-record-type employee-type
1023 (make-employee name age salary)
1024 employee?
1025 (name get-employee-name)
1026 (age get-employee-age set-employee-age)
1027 (salary get-employee-salary set-employee-salary))
a0e07ba4
NJ
1028@end example
1029
6afe385d
KR
1030This creates a new employee data type, with name, age and salary
1031fields. Accessor functions are created for each field, but no
1032modifier function for the name (the intention in this example being
1033that it's established only when an employee object is created). These
1034can all then be used as for example,
a0e07ba4
NJ
1035
1036@example
6afe385d
KR
1037employee-type @result{} #<record-type employee-type>
1038
1039(define fred (make-employee "Fred" 45 20000.00))
1040
1041(employee? fred) @result{} #t
1042(get-employee-age fred) @result{} 45
1043(set-employee-salary fred 25000.00) ;; pay rise
a0e07ba4
NJ
1044@end example
1045
6afe385d
KR
1046The functions created by @code{define-record-type} are ordinary
1047top-level @code{define}s. They can be redefined or @code{set!} as
1048desired, exported from a module, etc.
1049
a0e07ba4
NJ
1050
1051@node SRFI-10
3229f68b 1052@subsection SRFI-10 - Hash-Comma Reader Extension
8742c48b 1053@cindex SRFI-10
a0e07ba4
NJ
1054
1055@cindex hash-comma
1056@cindex #,()
633acbe2
KR
1057This SRFI implements a reader extension @code{#,()} called hash-comma.
1058It allows the reader to give new kinds of objects, for use both in
1059data and as constants or literals in source code. This feature is
1060available with
a0e07ba4 1061
633acbe2
KR
1062@example
1063(use-modules (srfi srfi-10))
1064@end example
1065
1066@noindent
1067The new read syntax is of the form
a0e07ba4
NJ
1068
1069@example
633acbe2 1070#,(@var{tag} @var{arg}@dots{})
a0e07ba4
NJ
1071@end example
1072
633acbe2
KR
1073@noindent
1074where @var{tag} is a symbol and the @var{arg}s are objects taken as
1075parameters. @var{tag}s are registered with the following procedure.
a0e07ba4 1076
633acbe2
KR
1077@deffn {Scheme Procedure} define-reader-ctor tag proc
1078Register @var{proc} as the constructor for a hash-comma read syntax
1079starting with symbol @var{tag}, ie. @nicode{#,(@var{tag} arg@dots{})}.
1080@var{proc} is called with the given arguments @code{(@var{proc}
1081arg@dots{})} and the object it returns is the result of the read.
1082@end deffn
a0e07ba4 1083
633acbe2
KR
1084@noindent
1085For example, a syntax giving a list of @var{N} copies of an object.
1086
1087@example
1088(define-reader-ctor 'repeat
1089 (lambda (obj reps)
1090 (make-list reps obj)))
1091
1092(display '#,(repeat 99 3))
1093@print{} (99 99 99)
1094@end example
1095
1096Notice the quote @nicode{'} when the @nicode{#,( )} is used. The
1097@code{repeat} handler returns a list and the program must quote to use
1098it literally, the same as any other list. Ie.
1099
1100@example
1101(display '#,(repeat 99 3))
a0e07ba4 1102@result{}
633acbe2
KR
1103(display '(99 99 99))
1104@end example
a0e07ba4 1105
633acbe2
KR
1106When a handler returns an object which is self-evaluating, like a
1107number or a string, then there's no need for quoting, just as there's
1108no need when giving those directly as literals. For example an
1109addition,
a0e07ba4 1110
633acbe2
KR
1111@example
1112(define-reader-ctor 'sum
1113 (lambda (x y)
1114 (+ x y)))
1115(display #,(sum 123 456)) @print{} 579
1116@end example
1117
1118A typical use for @nicode{#,()} is to get a read syntax for objects
1119which don't otherwise have one. For example, the following allows a
1120hash table to be given literally, with tags and values, ready for fast
1121lookup.
1122
1123@example
1124(define-reader-ctor 'hash
1125 (lambda elems
1126 (let ((table (make-hash-table)))
1127 (for-each (lambda (elem)
1128 (apply hash-set! table elem))
1129 elems)
1130 table)))
1131
1132(define (animal->family animal)
1133 (hash-ref '#,(hash ("tiger" "cat")
1134 ("lion" "cat")
1135 ("wolf" "dog"))
1136 animal))
1137
1138(animal->family "lion") @result{} "cat"
1139@end example
1140
1141Or for example the following is a syntax for a compiled regular
1142expression (@pxref{Regular Expressions}).
1143
1144@example
1145(use-modules (ice-9 regex))
1146
1147(define-reader-ctor 'regexp make-regexp)
1148
1149(define (extract-angs str)
1150 (let ((match (regexp-exec '#,(regexp "<([A-Z0-9]+)>") str)))
1151 (and match
1152 (match:substring match 1))))
1153
1154(extract-angs "foo <BAR> quux") @result{} "BAR"
1155@end example
1156
1157@sp 1
1158@nicode{#,()} is somewhat similar to @code{define-macro}
1159(@pxref{Macros}) in that handler code is run to produce a result, but
1160@nicode{#,()} operates at the read stage, so it can appear in data for
1161@code{read} (@pxref{Scheme Read}), not just in code to be executed.
1162
1163Because @nicode{#,()} is handled at read-time it has no direct access
1164to variables etc. A symbol in the arguments is just a symbol, not a
1165variable reference. The arguments are essentially constants, though
1166the handler procedure can use them in any complicated way it might
1167want.
1168
1169Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
1170globally, there's no need to use @code{(srfi srfi-10)} in later
1171modules. Similarly the tags registered are global and can be used
1172anywhere once registered.
1173
1174There's no attempt to record what previous @nicode{#,()} forms have
1175been seen, if two identical forms occur then two calls are made to the
1176handler procedure. The handler might like to maintain a cache or
1177similar to avoid making copies of large objects, depending on expected
1178usage.
1179
1180In code the best uses of @nicode{#,()} are generally when there's a
1181lot of objects of a particular kind as literals or constants. If
1182there's just a few then some local variables and initializers are
1183fine, but that becomes tedious and error prone when there's a lot, and
1184the anonymous and compact syntax of @nicode{#,()} is much better.
a0e07ba4
NJ
1185
1186
1187@node SRFI-11
3229f68b 1188@subsection SRFI-11 - let-values
8742c48b 1189@cindex SRFI-11
a0e07ba4 1190
8742c48b
KR
1191@findex let-values
1192@findex let-values*
a0e07ba4
NJ
1193This module implements the binding forms for multiple values
1194@code{let-values} and @code{let-values*}. These forms are similar to
1195@code{let} and @code{let*} (@pxref{Local Bindings}), but they support
1196binding of the values returned by multiple-valued expressions.
1197
1198Write @code{(use-modules (srfi srfi-11))} to make the bindings
1199available.
1200
1201@lisp
1202(let-values (((x y) (values 1 2))
1203 ((z f) (values 3 4)))
1204 (+ x y z f))
1205@result{}
120610
1207@end lisp
1208
1209@code{let-values} performs all bindings simultaneously, which means that
1210no expression in the binding clauses may refer to variables bound in the
1211same clause list. @code{let-values*}, on the other hand, performs the
1212bindings sequentially, just like @code{let*} does for single-valued
1213expressions.
1214
1215
1216@node SRFI-13
3229f68b 1217@subsection SRFI-13 - String Library
8742c48b 1218@cindex SRFI-13
a0e07ba4 1219
5676b4fa 1220The SRFI-13 procedures are always available, @xref{Strings}.
a0e07ba4
NJ
1221
1222@node SRFI-14
3229f68b 1223@subsection SRFI-14 - Character-set Library
8742c48b 1224@cindex SRFI-14
a0e07ba4 1225
050ab45f
MV
1226The SRFI-14 data type and procedures are always available,
1227@xref{Character Sets}.
a0e07ba4
NJ
1228
1229@node SRFI-16
3229f68b 1230@subsection SRFI-16 - case-lambda
8742c48b 1231@cindex SRFI-16
a0e07ba4
NJ
1232
1233@c FIXME::martin: Review me!
1234
8742c48b 1235@findex case-lambda
a0e07ba4
NJ
1236The syntactic form @code{case-lambda} creates procedures, just like
1237@code{lambda}, but has syntactic extensions for writing procedures of
1238varying arity easier.
1239
1240The syntax of the @code{case-lambda} form is defined in the following
1241EBNF grammar.
1242
1243@example
1244@group
1245<case-lambda>
1246 --> (case-lambda <case-lambda-clause>)
1247<case-lambda-clause>
1248 --> (<formals> <definition-or-command>*)
1249<formals>
1250 --> (<identifier>*)
1251 | (<identifier>* . <identifier>)
1252 | <identifier>
1253@end group
1254@end example
1255
1256The value returned by a @code{case-lambda} form is a procedure which
1257matches the number of actual arguments against the formals in the
1258various clauses, in order. @dfn{Formals} means a formal argument list
1259just like with @code{lambda} (@pxref{Lambda}). The first matching clause
1260is selected, the corresponding values from the actual parameter list are
1261bound to the variable names in the clauses and the body of the clause is
1262evaluated. If no clause matches, an error is signalled.
1263
1264The following (silly) definition creates a procedure @var{foo} which
1265acts differently, depending on the number of actual arguments. If one
1266argument is given, the constant @code{#t} is returned, two arguments are
1267added and if more arguments are passed, their product is calculated.
1268
1269@lisp
1270(define foo (case-lambda
1271 ((x) #t)
1272 ((x y) (+ x y))
1273 (z
1274 (apply * z))))
1275(foo 'bar)
1276@result{}
1277#t
1278(foo 2 4)
1279@result{}
12806
1281(foo 3 3 3)
1282@result{}
128327
1284(foo)
1285@result{}
12861
1287@end lisp
1288
1289The last expression evaluates to 1 because the last clause is matched,
1290@var{z} is bound to the empty list and the following multiplication,
1291applied to zero arguments, yields 1.
1292
1293
1294@node SRFI-17
3229f68b 1295@subsection SRFI-17 - Generalized set!
8742c48b 1296@cindex SRFI-17
a0e07ba4
NJ
1297
1298This is an implementation of SRFI-17: Generalized set!
1299
8742c48b 1300@findex getter-with-setter
a0e07ba4
NJ
1301It exports the Guile procedure @code{make-procedure-with-setter} under
1302the SRFI name @code{getter-with-setter} and exports the standard
1303procedures @code{car}, @code{cdr}, @dots{}, @code{cdddr},
1304@code{string-ref} and @code{vector-ref} as procedures with setters, as
1305required by the SRFI.
1306
1307SRFI-17 was heavily criticized during its discussion period but it was
1308finalized anyway. One issue was its concept of globally associating
1309setter @dfn{properties} with (procedure) values, which is non-Schemy.
1310For this reason, this implementation chooses not to provide a way to set
1311the setter of a procedure. In fact, @code{(set! (setter @var{proc})
1312@var{setter})} signals an error. The only way to attach a setter to a
1313procedure is to create a new object (a @dfn{procedure with setter}) via
1314the @code{getter-with-setter} procedure. This procedure is also
1315specified in the SRFI. Using it avoids the described problems.
1316
12991fed
TTN
1317
1318@node SRFI-19
3229f68b 1319@subsection SRFI-19 - Time/Date Library
8742c48b 1320@cindex SRFI-19
12991fed 1321
85600a0f
KR
1322This is an implementation of the SRFI-19 time/date library. The
1323functions and variables described here are provided by
12991fed
TTN
1324
1325@example
85600a0f 1326(use-modules (srfi srfi-19))
12991fed
TTN
1327@end example
1328
85600a0f
KR
1329@menu
1330* SRFI-19 Introduction::
1331* SRFI-19 Time::
1332* SRFI-19 Date::
1333* SRFI-19 Time/Date conversions::
1334* SRFI-19 Date to string::
1335* SRFI-19 String to date::
1336@end menu
12991fed 1337
85600a0f 1338@node SRFI-19 Introduction
3229f68b 1339@subsubsection SRFI-19 Introduction
85600a0f
KR
1340
1341@cindex universal time
1342@cindex atomic time
1343@cindex UTC
1344@cindex TAI
1345This module implements time and date representations and calculations,
1346in various time systems, including universal time (UTC) and atomic
1347time (TAI).
1348
1349For those not familiar with these time systems, TAI is based on a
1350fixed length second derived from oscillations of certain atoms. UTC
1351differs from TAI by an integral number of seconds, which is increased
1352or decreased at announced times to keep UTC aligned to a mean solar
1353day (the orbit and rotation of the earth are not quite constant).
1354
1355@cindex leap second
1356So far, only increases in the TAI
1357@tex
1358$\leftrightarrow$
1359@end tex
1360@ifnottex
1361<->
1362@end ifnottex
1363UTC difference have been needed. Such an increase is a ``leap
1364second'', an extra second of TAI introduced at the end of a UTC day.
1365When working entirely within UTC this is never seen, every day simply
1366has 86400 seconds. But when converting from TAI to a UTC date, an
1367extra 23:59:60 is present, where normally a day would end at 23:59:59.
1368Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
1369seconds.
1370
1371@cindex system clock
1372In the current implementation, the system clock is assumed to be UTC,
1373and a table of leap seconds in the code converts to TAI. See comments
1374in @file{srfi-19.scm} for how to update this table.
1375
1376@cindex julian day
1377@cindex modified julian day
1378Also, for those not familiar with the terminology, a @dfn{Julian Day}
1379is a real number which is a count of days and fraction of a day, in
1380UTC, starting from -4713-01-01T12:00:00Z, ie.@: midday Monday 1 Jan
13814713 B.C. And a @dfn{Modified Julian Day} is the same, but starting
1382from 1858-11-17T00:00:00Z, ie.@: midnight 17 November 1858 UTC.
1383
1384@c The SRFI-1 spec says -4714-11-24T12:00:00Z (November 24, -4714 at
1385@c noon, UTC), but this is incorrect. It looks like it might have
1386@c arisen from the code incorrectly treating years a multiple of 100
1387@c but not 400 prior to 1582 as leap years, where instead the Julian
1388@c calendar should be used so all multiples of 4 before 1582 are leap
1389@c years.
1390
1391
1392@node SRFI-19 Time
3229f68b 1393@subsubsection SRFI-19 Time
85600a0f
KR
1394@cindex time
1395
1396A @dfn{time} object has type, seconds and nanoseconds fields
1397representing a point in time starting from some epoch. This is an
1398arbitrary point in time, not just a time of day. Although times are
1399represented in nanoseconds, the actual resolution may be lower.
1400
1401The following variables hold the possible time types. For instance
1402@code{(current-time time-process)} would give the current CPU process
1403time.
1404
1405@defvar time-utc
1406Universal Coordinated Time (UTC).
1407@cindex UTC
1408@end defvar
12991fed 1409
85600a0f
KR
1410@defvar time-tai
1411International Atomic Time (TAI).
1412@cindex TAI
1413@end defvar
12991fed 1414
85600a0f
KR
1415@defvar time-monotonic
1416Monotonic time, meaning a monotonically increasing time starting from
1417an unspecified epoch.
12991fed 1418
85600a0f
KR
1419Note that in the current implementation @code{time-monotonic} is the
1420same as @code{time-tai}, and unfortunately is therefore affected by
1421adjustments to the system clock. Perhaps this will change in the
1422future.
1423@end defvar
12991fed 1424
85600a0f
KR
1425@defvar time-duration
1426A duration, meaning simply a difference between two times.
1427@end defvar
12991fed 1428
85600a0f
KR
1429@defvar time-process
1430CPU time spent in the current process, starting from when the process
1431began.
1432@cindex process time
1433@end defvar
12991fed 1434
85600a0f
KR
1435@defvar time-thread
1436CPU time spent in the current thread. Not currently implemented.
1437@cindex thread time
1438@end defvar
12991fed 1439
85600a0f
KR
1440@sp 1
1441@defun time? obj
1442Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
1443@end defun
1444
1445@defun make-time type nanoseconds seconds
1446Create a time object with the given @var{type}, @var{seconds} and
1447@var{nanoseconds}.
1448@end defun
1449
1450@defun time-type time
1451@defunx time-nanosecond time
1452@defunx time-second time
1453@defunx set-time-type! time type
1454@defunx set-time-nanosecond! time nsec
1455@defunx set-time-second! time sec
1456Get or set the type, seconds or nanoseconds fields of a time object.
1457
1458@code{set-time-type!} merely changes the field, it doesn't convert the
1459time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
1460@end defun
1461
1462@defun copy-time time
1463Return a new time object, which is a copy of the given @var{time}.
1464@end defun
1465
1466@defun current-time [type]
1467Return the current time of the given @var{type}. The default
1468@var{type} is @code{time-utc}.
1469
1470Note that the name @code{current-time} conflicts with the Guile core
1471@code{current-time} function (@pxref{Time}). Applications wanting to
1472use both will need to use a different name for one of them.
1473@end defun
1474
1475@defun time-resolution [type]
1476Return the resolution, in nanoseconds, of the given time @var{type}.
1477The default @var{type} is @code{time-utc}.
1478@end defun
1479
1480@defun time<=? t1 t2
1481@defunx time<? t1 t2
1482@defunx time=? t1 t2
1483@defunx time>=? t1 t2
1484@defunx time>? t1 t2
1485Return @code{#t} or @code{#f} according to the respective relation
1486between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
1487must be the same time type.
1488@end defun
1489
1490@defun time-difference t1 t2
1491@defunx time-difference! t1 t2
1492Return a time object of type @code{time-duration} representing the
1493period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
1494the same time type.
1495
1496@code{time-difference} returns a new time object,
1497@code{time-difference!} may modify @var{t1} to form its return.
1498@end defun
1499
1500@defun add-duration time duration
1501@defunx add-duration! time duration
1502@defunx subtract-duration time duration
1503@defunx subtract-duration! time duration
1504Return a time object which is @var{time} with the given @var{duration}
1505added or subtracted. @var{duration} must be a time object of type
1506@code{time-duration}.
1507
1508@code{add-duration} and @code{subtract-duration} return a new time
1509object. @code{add-duration!} and @code{subtract-duration!} may modify
1510the given @var{time} to form their return.
1511@end defun
1512
1513
1514@node SRFI-19 Date
3229f68b 1515@subsubsection SRFI-19 Date
85600a0f
KR
1516@cindex date
1517
1518A @dfn{date} object represents a date in the Gregorian calendar and a
1519time of day on that date in some timezone.
1520
1521The fields are year, month, day, hour, minute, second, nanoseconds and
1522timezone. A date object is immutable, its fields can be read but they
1523cannot be modified once the object is created.
1524
1525@defun date? obj
1526Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
1527@end defun
1528
1529@defun make-date nsecs seconds minutes hours date month year zone-offset
1530Create a new date object.
1531@c
1532@c FIXME: What can we say about the ranges of the values. The
1533@c current code looks it doesn't normalize, but expects then in their
1534@c usual range already.
1535@c
1536@end defun
1537
1538@defun date-nanosecond date
1539Nanoseconds, 0 to 999999999.
1540@end defun
1541
1542@defun date-second date
1543Seconds, 0 to 60. 0 to 59 is the usual range, 60 is for a leap second.
1544@end defun
1545
1546@defun date-minute date
1547Minutes, 0 to 59.
1548@end defun
1549
1550@defun date-hour date
1551Hour, 0 to 23.
1552@end defun
1553
1554@defun date-day date
1555Day of the month, 1 to 31 (or less, according to the month).
1556@end defun
1557
1558@defun date-month date
1559Month, 1 to 12.
1560@end defun
1561
1562@defun date-year date
1563Year, eg.@: 2003.
1564@end defun
1565
1566@defun date-zone-offset date
1567Time zone, an integer number of seconds east of Greenwich.
1568@end defun
1569
1570@defun date-year-day date
1571Day of the year, starting from 1 for 1st January.
1572@end defun
1573
1574@defun date-week-day date
1575Day of the week, starting from 0 for Sunday.
1576@end defun
1577
1578@defun date-week-number date dstartw
1579Week of the year, ignoring a first partial week. @var{dstartw} is the
1580day of the week which is taken to start a week, 0 for Sunday, 1 for
1581Monday, etc.
1582@c
1583@c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
1584@c The code looks like it's 0, if that's the correct intention.
1585@c
1586@end defun
1587
1588@c The SRFI text doesn't actually give the default for tz-offset, but
1589@c the reference implementation has the local timezone and the
1590@c conversions functions all specify that, so it should be ok to
1591@c document it here.
1592@c
1593@defun current-date [tz-offset]
1594Return a date object representing the current date/time UTC.
1595@var{tz-offset} is seconds east of Greenwich, and defaults to the
1596local timezone.
1597@end defun
1598
1599@defun current-julian-day
1600@cindex julian day
1601Return the current Julian Day.
1602@end defun
1603
1604@defun current-modified-julian-day
1605@cindex modified julian day
1606Return the current Modified Julian Day.
1607@end defun
1608
1609
1610@node SRFI-19 Time/Date conversions
3229f68b 1611@subsubsection SRFI-19 Time/Date conversions
85600a0f
KR
1612
1613@defun date->julian-day date
1614@defunx date->modified-julian-day date
1615@defunx date->time-monotonic date
1616@defunx date->time-tai date
1617@defunx date->time-utc date
1618@end defun
1619@defun julian-day->date jdn [tz-offset]
1620@defunx julian-day->time-monotonic jdn
1621@defunx julian-day->time-tai jdn
1622@defunx julian-day->time-utc jdn
1623@end defun
1624@defun modified-julian-day->date jdn [tz-offset]
1625@defunx modified-julian-day->time-monotonic jdn
1626@defunx modified-julian-day->time-tai jdn
1627@defunx modified-julian-day->time-utc jdn
1628@end defun
1629@defun time-monotonic->date time [tz-offset]
1630@defunx time-monotonic->time-tai time
1631@defunx time-monotonic->time-tai! time
1632@defunx time-monotonic->time-utc time
1633@defunx time-monotonic->time-utc! time
1634@end defun
1635@defun time-tai->date time [tz-offset]
1636@defunx time-tai->julian-day time
1637@defunx time-tai->modified-julian-day time
1638@defunx time-tai->time-monotonic time
1639@defunx time-tai->time-monotonic! time
1640@defunx time-tai->time-utc time
1641@defunx time-tai->time-utc! time
1642@end defun
1643@defun time-utc->date time [tz-offset]
1644@defunx time-utc->julian-day time
1645@defunx time-utc->modified-julian-day time
1646@defunx time-utc->time-monotonic time
1647@defunx time-utc->time-monotonic! time
1648@defunx time-utc->time-tai time
1649@defunx time-utc->time-tai! time
1650@sp 1
1651Convert between dates, times and days of the respective types. For
1652instance @code{time-tai->time-utc} accepts a @var{time} object of type
1653@code{time-tai} and returns an object of type @code{time-utc}.
1654
1655For conversions to dates, @var{tz-offset} is seconds east of
1656Greenwich. The default is the local timezone.
1657
1658The @code{!} variants may modify their @var{time} argument to form
1659their return. The plain functions create a new object.
1660@end defun
1661
1662@node SRFI-19 Date to string
3229f68b 1663@subsubsection SRFI-19 Date to string
85600a0f
KR
1664@cindex date to string
1665
1666@defun date->string date [format]
1667Convert a date to a string under the control of a format.
1668@var{format} should be a string containing @samp{~} escapes, which
1669will be expanded as per the following conversion table. The default
1670@var{format} is @samp{~c}, a locale-dependent date and time.
1671
1672Many of these conversion characters are the same as POSIX
1673@code{strftime} (@pxref{Time}), but there are some extras and some
1674variations.
1675
1676@multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
1677@item @nicode{~~} @tab literal ~
1678@item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
1679@item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
1680@item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
1681@item @nicode{~B} @tab locale full month, eg.@: @samp{January}
1682@item @nicode{~c} @tab locale date and time, eg.@: @*
1683@samp{Fri Jul 14 20:28:42-0400 2000}
1684@item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
1685
1686@c Spec says d/m/y, reference implementation says m/d/y.
1687@c Apparently the reference code was the intention, but would like to
1688@c see an errata published for the spec before contradicting it here.
1689@c
1690@c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
1691
1692@item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
1693@item @nicode{~f} @tab seconds and fractional seconds,
1694with locale decimal point, eg.@: @samp{5.2}
1695@item @nicode{~h} @tab same as @nicode{~b}
1696@item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
1697@item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
1698@item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
1699@item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
1700@item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
1701@item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
1702@item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
1703@item @nicode{~n} @tab newline
1704@item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
1705@item @nicode{~p} @tab locale AM or PM
1706@item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
1707@item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
1708@item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
1709(usual limit is 59, 60 is a leap second)
1710@item @nicode{~t} @tab horizontal tab character
1711@item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
1712@item @nicode{~U} @tab week of year, Sunday first day of week,
1713@samp{00} to @samp{52}
1714@item @nicode{~V} @tab week of year, Monday first day of week,
1715@samp{01} to @samp{53}
1716@item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
1717@item @nicode{~W} @tab week of year, Monday first day of week,
1718@samp{00} to @samp{52}
1719
1720@c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
1721@c date. The reference code has ~x as the locale date and ~X as a
1722@c locale time. The rule is apparently that the code should be
1723@c believed, but would like to see an errata for the spec before
1724@c contradicting it here.
1725@c
1726@c @item @nicode{~x} @tab week of year, Monday as first day of week,
1727@c @samp{00} to @samp{53}
1728@c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
1729
1730@item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
1731@item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
1732@item @nicode{~z} @tab time zone, RFC-822 style
1733@item @nicode{~Z} @tab time zone symbol (not currently implemented)
1734@item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
1735@item @nicode{~2} @tab ISO-8601 time+zone, @samp{~k:~M:~S~z}
1736@item @nicode{~3} @tab ISO-8601 time, @samp{~k:~M:~S}
1737@item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~k:~M:~S~z}
1738@item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~k:~M:~S}
1739@end multitable
1740@end defun
1741
1742Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
1743described here, since the specification and reference implementation
1744differ.
1745
1746Currently Guile doesn't implement any localizations for the above, all
1747outputs are in English, and the @samp{~c} conversion is POSIX
1748@code{ctime} style @samp{~a ~b ~d ~H:~M:~S~z ~Y}. This may change in
1749the future.
1750
1751
1752@node SRFI-19 String to date
3229f68b 1753@subsubsection SRFI-19 String to date
85600a0f
KR
1754@cindex string to date
1755
1756@c FIXME: Can we say what happens when an incomplete date is
1757@c converted? Ie. fields left as 0, or what? The spec seems to be
1758@c silent on this.
1759
1760@defun string->date input template
1761Convert an @var{input} string to a date under the control of a
1762@var{template} string. Return a newly created date object.
1763
1764Literal characters in @var{template} must match characters in
1765@var{input} and @samp{~} escapes must match the input forms described
1766in the table below. ``Skip to'' means characters up to one of the
1767given type are ignored, or ``no skip'' for no skipping. ``Read'' is
1768what's then read, and ``Set'' is the field affected in the date
1769object.
1770
1771For example @samp{~Y} skips input characters until a digit is reached,
1772at which point it expects a year and stores that to the year field of
1773the date.
1774
1775@multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
1776@item
1777@tab Skip to
1778@tab Read
1779@tab Set
1780
1781@item @nicode{~~}
1782@tab no skip
1783@tab literal ~
1784@tab nothing
1785
1786@item @nicode{~a}
1787@tab @nicode{char-alphabetic?}
1788@tab locale abbreviated weekday name
1789@tab nothing
1790
1791@item @nicode{~A}
1792@tab @nicode{char-alphabetic?}
1793@tab locale full weekday name
1794@tab nothing
1795
1796@c Note that the SRFI spec says that ~b and ~B don't set anything,
1797@c but that looks like a mistake. The reference implementation sets
1798@c the month field, which seems sensible and is what we describe
1799@c here.
1800
1801@item @nicode{~b}
1802@tab @nicode{char-alphabetic?}
1803@tab locale abbreviated month name
1804@tab @nicode{date-month}
1805
1806@item @nicode{~B}
1807@tab @nicode{char-alphabetic?}
1808@tab locale full month name
1809@tab @nicode{date-month}
1810
1811@item @nicode{~d}
1812@tab @nicode{char-numeric?}
1813@tab day of month
1814@tab @nicode{date-day}
1815
1816@item @nicode{~e}
1817@tab no skip
1818@tab day of month, blank padded
1819@tab @nicode{date-day}
1820
1821@item @nicode{~h}
1822@tab same as @samp{~b}
1823
1824@item @nicode{~H}
1825@tab @nicode{char-numeric?}
1826@tab hour
1827@tab @nicode{date-hour}
1828
1829@item @nicode{~k}
1830@tab no skip
1831@tab hour, blank padded
1832@tab @nicode{date-hour}
1833
1834@item @nicode{~m}
1835@tab @nicode{char-numeric?}
1836@tab month
1837@tab @nicode{date-month}
1838
1839@item @nicode{~M}
1840@tab @nicode{char-numeric?}
1841@tab minute
1842@tab @nicode{date-minute}
1843
1844@item @nicode{~S}
1845@tab @nicode{char-numeric?}
1846@tab second
1847@tab @nicode{date-second}
1848
1849@item @nicode{~y}
1850@tab no skip
1851@tab 2-digit year
1852@tab @nicode{date-year} within 50 years
1853
1854@item @nicode{~Y}
1855@tab @nicode{char-numeric?}
1856@tab year
1857@tab @nicode{date-year}
1858
1859@item @nicode{~z}
1860@tab no skip
1861@tab time zone
1862@tab date-zone-offset
1863@end multitable
1864
1865Notice that the weekday matching forms don't affect the date object
1866returned, instead the weekday will be derived from the day, month and
1867year.
1868
1869Currently Guile doesn't implement any localizations for the above,
1870month and weekday names are always expected in English. This may
1871change in the future.
1872@end defun
12991fed 1873
1de8c1ae 1874
b0b55bd6 1875@node SRFI-26
3229f68b 1876@subsection SRFI-26 - specializing parameters
1de8c1ae
KR
1877@cindex SRFI-26
1878
1879This SRFI provides a syntax for conveniently specializing selected
1880parameters of a function. It can be used with,
1881
1882@example
1883(use-modules (srfi srfi-26))
1884@end example
1885
1886@deffn {library syntax} cut slot @dots{}
1887@deffnx {library syntax} cute slot @dots{}
1888Return a new procedure which will make a call (@var{slot} @dots{}) but
1889with selected parameters specialized to given expressions.
1890
1891An example will illustrate the idea. The following is a
1892specialization of @code{write}, sending output to
1893@code{my-output-port},
1894
1895@example
1896(cut write <> my-output-port)
1897@result{}
1898(lambda (obj) (write obj my-output-port))
1899@end example
1900
1901The special symbol @code{<>} indicates a slot to be filled by an
1902argument to the new procedure. @code{my-output-port} on the other
1903hand is an expression to be evaluated and passed, ie.@: it specializes
1904the behaviour of @code{write}.
1905
1906@table @nicode
1907@item <>
1908A slot to be filled by an argument from the created procedure.
1909Arguments are assigned to @code{<>} slots in the order they appear in
1910the @code{cut} form, there's no way to re-arrange arguments.
1911
1912The first argument to @code{cut} is usually a procedure (or expression
1913giving a procedure), but @code{<>} is allowed there too. For example,
1914
1915@example
1916(cut <> 1 2 3)
1917@result{}
1918(lambda (proc) (proc 1 2 3))
1919@end example
1920
1921@item <...>
1922A slot to be filled by all remaining arguments from the new procedure.
1923This can only occur at the end of a @code{cut} form.
1924
1925For example, a procedure taking a variable number of arguments like
1926@code{max} but in addition enforcing a lower bound,
1927
1928@example
1929(define my-lower-bound 123)
1930
1931(cut max my-lower-bound <...>)
1932@result{}
1933(lambda arglist (apply max my-lower-bound arglist))
1934@end example
1935@end table
1936
1937For @code{cut} the specializing expressions are evaluated each time
1938the new procedure is called. For @code{cute} they're evaluated just
1939once, when the new procedure is created. The name @code{cute} stands
1940for ``@code{cut} with evaluated arguments''. In all cases the
1941evaluations take place in an unspecified order.
1942
1943The following illustrates the difference between @code{cut} and
1944@code{cute},
1945
1946@example
1947(cut format <> "the time is ~s" (current-time))
1948@result{}
1949(lambda (port) (format port "the time is ~s" (current-time)))
1950
1951(cute format <> "the time is ~s" (current-time))
1952@result{}
1953(let ((val (current-time)))
1954 (lambda (port) (format port "the time is ~s" val))
1955@end example
1956
1957(There's no provision for a mixture of @code{cut} and @code{cute}
1958where some expressions would be evaluated every time but others
1959evaluated only once.)
1960
1961@code{cut} is really just a shorthand for the sort of @code{lambda}
1962forms shown in the above examples. But notice @code{cut} avoids the
1963need to name unspecialized parameters, and is more compact. Use in
1964functional programming style or just with @code{map}, @code{for-each}
1965or similar is typical.
1966
1967@example
1968(map (cut * 2 <>) '(1 2 3 4))
1969
1970(for-each (cut write <> my-port) my-list)
1971@end example
1972@end deffn
b0b55bd6 1973
8638c417
RB
1974@node SRFI-31
1975@subsection SRFI-31 - A special form `rec' for recursive evaluation
1976@cindex SRFI-31
1977@findex rec
1978
1979SRFI-31 defines a special form that can be used to create
1980self-referential expressions more conveniently. The syntax is as
1981follows:
1982
1983@example
1984@group
1985<rec expression> --> (rec <variable> <expression>)
1986<rec expression> --> (rec (<variable>+) <body>)
1987@end group
1988@end example
1989
1990The first syntax can be used to create self-referential expressions,
1991for example:
1992
1993@lisp
1994 guile> (define tmp (rec ones (cons 1 (delay ones))))
1995@end lisp
1996
1997The second syntax can be used to create anonymous recursive functions:
1998
1999@lisp
2000 guile> (define tmp (rec (display-n item n)
2001 (if (positive? n)
2002 (begin (display n) (display-n (- n 1))))))
2003 guile> (tmp 42 3)
2004 424242
2005 guile>
2006@end lisp
12991fed
TTN
2007
2008@c srfi-modules.texi ends here
193239f1
KR
2009
2010@c Local Variables:
2011@c TeX-master: "guile.texi"
2012@c End: