More of:
[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
eeadfda1 40* SRFI-39:: Parameter objects
a0e07ba4
NJ
41@end menu
42
43
44@node About SRFI Usage
3229f68b 45@subsection About SRFI Usage
a0e07ba4
NJ
46
47@c FIXME::martin: Review me!
48
49SRFI support in Guile is currently implemented partly in the core
50library, and partly as add-on modules. That means that some SRFIs are
51automatically available when the interpreter is started, whereas the
52other SRFIs require you to use the appropriate support module
12991fed 53explicitly.
a0e07ba4
NJ
54
55There are several reasons for this inconsistency. First, the feature
56checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be
57available immediately, because it must be there when the user wants to
58check for the Scheme implementation, that is, before she can know that
59it is safe to use @code{use-modules} to load SRFI support modules. The
60second reason is that some features defined in SRFIs had been
61implemented in Guile before the developers started to add SRFI
62implementations as modules (for example SRFI-6 (@pxref{SRFI-6})). In
63the future, it is possible that SRFIs in the core library might be
64factored out into separate modules, requiring explicit module loading
65when they are needed. So you should be prepared to have to use
66@code{use-modules} someday in the future to access SRFI-6 bindings. If
67you want, you can do that already. We have included the module
68@code{(srfi srfi-6)} in the distribution, which currently does nothing,
69but ensures that you can write future-safe code.
70
71Generally, support for a specific SRFI is made available by using
72modules named @code{(srfi srfi-@var{number})}, where @var{number} is the
73number of the SRFI needed. Another possibility is to use the command
74line option @code{--use-srfi}, which will load the necessary modules
75automatically (@pxref{Invoking Guile}).
76
77
78@node SRFI-0
3229f68b 79@subsection SRFI-0 - cond-expand
8742c48b 80@cindex SRFI-0
a0e07ba4 81
5eef0f61
KR
82This SRFI lets a portable Scheme program test for the presence of
83certain features, and adapt itself by using different blocks of code,
84or fail if the necessary features are not available. There's no
85module to load, this is in the Guile core.
a0e07ba4 86
5eef0f61
KR
87A program designed only for Guile will generally not need this
88mechanism, such a program can of course directly use the various
89documented parts of Guile.
a0e07ba4 90
5eef0f61
KR
91@deffn syntax cond-expand (feature body@dots{}) @dots{}
92Expand to the @var{body} of the first clause whose @var{feature}
93specification is satisfied. It is an error if no @var{feature} is
a0e07ba4
NJ
94satisfied.
95
5eef0f61
KR
96Features are symbols such as @code{srfi-1}, and a feature
97specification can use @code{and}, @code{or} and @code{not} forms to
98test combinations. The last clause can be an @code{else}, to be used
99if no other passes.
a0e07ba4 100
5eef0f61
KR
101For example, define a private version of @code{alist-cons} if SRFI-1
102is not available.
a0e07ba4 103
5eef0f61
KR
104@example
105(cond-expand (srfi-1
106 )
107 (else
108 (define (alist-cons key val alist)
109 (cons (cons key val) alist))))
110@end example
a0e07ba4 111
5eef0f61
KR
112Or demand a certain set of SRFIs (list operations, string ports,
113@code{receive} and string operations), failing if they're not
114available.
a0e07ba4 115
5eef0f61
KR
116@example
117(cond-expand ((and srfi-1 srfi-6 srfi-8 srfi-13)
118 ))
119@end example
120@end deffn
a0e07ba4 121
5eef0f61
KR
122The Guile core provides features @code{guile}, @code{r5rs},
123@code{srfi-0} and @code{srfi-6} initially. Other SRFI feature symbols
124are defined once their code has been loaded with @code{use-modules},
125since only then are their bindings available.
a0e07ba4 126
5eef0f61
KR
127The @samp{--use-srfi} command line option (@pxref{Invoking Guile}) is
128a good way to load SRFIs to satisfy @code{cond-expand} when running a
129portable program.
a0e07ba4 130
5eef0f61
KR
131Testing the @code{guile} feature allows a program to adapt itself to
132the Guile module system, but still run on other Scheme systems. For
133example the following demands SRFI-8 (@code{receive}), but also knows
134how to load it with the Guile mechanism.
a0e07ba4
NJ
135
136@example
5eef0f61
KR
137(cond-expand (srfi-8
138 )
139 (guile
140 (use-modules (srfi srfi-8))))
a0e07ba4
NJ
141@end example
142
5eef0f61
KR
143It should be noted that @code{cond-expand} is separate from the
144@code{*features*} mechanism (@pxref{Feature Tracking}), feature
145symbols in one are unrelated to those in the other.
a0e07ba4
NJ
146
147
148@node SRFI-1
3229f68b 149@subsection SRFI-1 - List library
8742c48b 150@cindex SRFI-1
7c2e18cd 151@cindex list
a0e07ba4
NJ
152
153@c FIXME::martin: Review me!
154
155The list library defined in SRFI-1 contains a lot of useful list
156processing procedures for construction, examining, destructuring and
157manipulating lists and pairs.
158
159Since SRFI-1 also defines some procedures which are already contained
160in R5RS and thus are supported by the Guile core library, some list
161and pair procedures which appear in the SRFI-1 document may not appear
162in this section. So when looking for a particular list/pair
163processing procedure, you should also have a look at the sections
164@ref{Lists} and @ref{Pairs}.
165
166@menu
167* SRFI-1 Constructors:: Constructing new lists.
168* SRFI-1 Predicates:: Testing list for specific properties.
169* SRFI-1 Selectors:: Selecting elements from lists.
170* SRFI-1 Length Append etc:: Length calculation and list appending.
171* SRFI-1 Fold and Map:: Higher-order list processing.
172* SRFI-1 Filtering and Partitioning:: Filter lists based on predicates.
85a9b4ed 173* SRFI-1 Searching:: Search for elements.
a0e07ba4
NJ
174* SRFI-1 Deleting:: Delete elements from lists.
175* SRFI-1 Association Lists:: Handle association lists.
176* SRFI-1 Set Operations:: Use lists for representing sets.
177@end menu
178
179@node SRFI-1 Constructors
3229f68b 180@subsubsection Constructors
7c2e18cd 181@cindex list constructor
a0e07ba4
NJ
182
183@c FIXME::martin: Review me!
184
185New lists can be constructed by calling one of the following
186procedures.
187
8f85c0c6 188@deffn {Scheme Procedure} xcons d a
a0e07ba4
NJ
189Like @code{cons}, but with interchanged arguments. Useful mostly when
190passed to higher-order procedures.
191@end deffn
192
8f85c0c6 193@deffn {Scheme Procedure} list-tabulate n init-proc
a0e07ba4
NJ
194Return an @var{n}-element list, where each list element is produced by
195applying the procedure @var{init-proc} to the corresponding list
196index. The order in which @var{init-proc} is applied to the indices
197is not specified.
198@end deffn
199
57066448
KR
200@deffn {Scheme Procedure} list-copy lst
201Return a new list containing the elements of the list @var{lst}.
202
203This function differs from the core @code{list-copy} (@pxref{List
204Constructors}) in accepting improper lists too. And if @var{lst} is
205not a pair at all then it's treated as the final tail of an improper
206list and simply returned.
207@end deffn
208
8f85c0c6 209@deffn {Scheme Procedure} circular-list elt1 elt2 @dots{}
a0e07ba4
NJ
210Return a circular list containing the given arguments @var{elt1}
211@var{elt2} @dots{}.
212@end deffn
213
8f85c0c6 214@deffn {Scheme Procedure} iota count [start step]
256853db
KR
215Return a list containing @var{count} numbers, starting from
216@var{start} and adding @var{step} each time. The default @var{start}
217is 0, the default @var{step} is 1. For example,
a0e07ba4 218
256853db
KR
219@example
220(iota 6) @result{} (0 1 2 3 4 5)
221(iota 4 2.5 -2) @result{} (2.5 0.5 -1.5 -3.5)
222@end example
a0e07ba4 223
256853db
KR
224This function takes its name from the corresponding primitive in the
225APL language.
a0e07ba4
NJ
226@end deffn
227
228
229@node SRFI-1 Predicates
3229f68b 230@subsubsection Predicates
7c2e18cd 231@cindex list predicate
a0e07ba4
NJ
232
233@c FIXME::martin: Review me!
234
235The procedures in this section test specific properties of lists.
236
8f85c0c6 237@deffn {Scheme Procedure} proper-list? obj
a0e07ba4
NJ
238Return @code{#t} if @var{obj} is a proper list, that is a finite list,
239terminated with the empty list. Otherwise, return @code{#f}.
240@end deffn
241
8f85c0c6 242@deffn {Scheme Procedure} circular-list? obj
a0e07ba4
NJ
243Return @code{#t} if @var{obj} is a circular list, otherwise return
244@code{#f}.
245@end deffn
246
8f85c0c6 247@deffn {Scheme Procedure} dotted-list? obj
a0e07ba4
NJ
248Return @code{#t} if @var{obj} is a dotted list, return @code{#f}
249otherwise. A dotted list is a finite list which is not terminated by
250the empty list, but some other value.
251@end deffn
252
8f85c0c6 253@deffn {Scheme Procedure} null-list? lst
a0e07ba4
NJ
254Return @code{#t} if @var{lst} is the empty list @code{()}, @code{#f}
255otherwise. If something else than a proper or circular list is passed
85a9b4ed 256as @var{lst}, an error is signalled. This procedure is recommended
a0e07ba4
NJ
257for checking for the end of a list in contexts where dotted lists are
258not allowed.
259@end deffn
260
8f85c0c6 261@deffn {Scheme Procedure} not-pair? obj
a0e07ba4
NJ
262Return @code{#t} is @var{obj} is not a pair, @code{#f} otherwise.
263This is shorthand notation @code{(not (pair? @var{obj}))} and is
264supposed to be used for end-of-list checking in contexts where dotted
265lists are allowed.
266@end deffn
267
8f85c0c6 268@deffn {Scheme Procedure} list= elt= list1 @dots{}
a0e07ba4
NJ
269Return @code{#t} if all argument lists are equal, @code{#f} otherwise.
270List equality is determined by testing whether all lists have the same
271length and the corresponding elements are equal in the sense of the
272equality predicate @var{elt=}. If no or only one list is given,
273@code{#t} is returned.
274@end deffn
275
276
277@node SRFI-1 Selectors
3229f68b 278@subsubsection Selectors
7c2e18cd 279@cindex list selector
a0e07ba4
NJ
280
281@c FIXME::martin: Review me!
282
8f85c0c6
NJ
283@deffn {Scheme Procedure} first pair
284@deffnx {Scheme Procedure} second pair
285@deffnx {Scheme Procedure} third pair
286@deffnx {Scheme Procedure} fourth pair
287@deffnx {Scheme Procedure} fifth pair
288@deffnx {Scheme Procedure} sixth pair
289@deffnx {Scheme Procedure} seventh pair
290@deffnx {Scheme Procedure} eighth pair
291@deffnx {Scheme Procedure} ninth pair
292@deffnx {Scheme Procedure} tenth pair
a0e07ba4
NJ
293These are synonyms for @code{car}, @code{cadr}, @code{caddr}, @dots{}.
294@end deffn
295
8f85c0c6 296@deffn {Scheme Procedure} car+cdr pair
a0e07ba4
NJ
297Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.
298@end deffn
299
8f85c0c6
NJ
300@deffn {Scheme Procedure} take lst i
301@deffnx {Scheme Procedure} take! lst i
a0e07ba4
NJ
302Return a list containing the first @var{i} elements of @var{lst}.
303
304@code{take!} may modify the structure of the argument list @var{lst}
305in order to produce the result.
306@end deffn
307
8f85c0c6 308@deffn {Scheme Procedure} drop lst i
a0e07ba4
NJ
309Return a list containing all but the first @var{i} elements of
310@var{lst}.
311@end deffn
312
8f85c0c6 313@deffn {Scheme Procedure} take-right lst i
a0e07ba4
NJ
314Return the a list containing the @var{i} last elements of @var{lst}.
315@end deffn
316
8f85c0c6
NJ
317@deffn {Scheme Procedure} drop-right lst i
318@deffnx {Scheme Procedure} drop-right! lst i
a0e07ba4
NJ
319Return the a list containing all but the @var{i} last elements of
320@var{lst}.
321
322@code{drop-right!} may modify the structure of the argument list
323@var{lst} in order to produce the result.
324@end deffn
325
8f85c0c6
NJ
326@deffn {Scheme Procedure} split-at lst i
327@deffnx {Scheme Procedure} split-at! lst i
a0e07ba4
NJ
328Return two values, a list containing the first @var{i} elements of the
329list @var{lst} and a list containing the remaining elements.
330
331@code{split-at!} may modify the structure of the argument list
332@var{lst} in order to produce the result.
333@end deffn
334
8f85c0c6 335@deffn {Scheme Procedure} last lst
a0e07ba4
NJ
336Return the last element of the non-empty, finite list @var{lst}.
337@end deffn
338
339
340@node SRFI-1 Length Append etc
3229f68b 341@subsubsection Length, Append, Concatenate, etc.
a0e07ba4
NJ
342
343@c FIXME::martin: Review me!
344
8f85c0c6 345@deffn {Scheme Procedure} length+ lst
a0e07ba4
NJ
346Return the length of the argument list @var{lst}. When @var{lst} is a
347circular list, @code{#f} is returned.
348@end deffn
349
8f85c0c6
NJ
350@deffn {Scheme Procedure} concatenate list-of-lists
351@deffnx {Scheme Procedure} concatenate! list-of-lists
a0e07ba4
NJ
352Construct a list by appending all lists in @var{list-of-lists}.
353
354@code{concatenate!} may modify the structure of the given lists in
355order to produce the result.
a3e856f2
KR
356
357@code{concatenate} is the same as @code{(apply append
358@var{list-of-lists})}. It exists because some Scheme implementations
359have a limit on the number of arguments a function takes, which the
360@code{apply} might exceed. In Guile there is no such limit.
a0e07ba4
NJ
361@end deffn
362
8f85c0c6
NJ
363@deffn {Scheme Procedure} append-reverse rev-head tail
364@deffnx {Scheme Procedure} append-reverse! rev-head tail
a0e07ba4
NJ
365Reverse @var{rev-head}, append @var{tail} and return the result. This
366is equivalent to @code{(append (reverse @var{rev-head}) @var{tail})},
367but more efficient.
368
369@code{append-reverse!} may modify @var{rev-head} in order to produce
370the result.
371@end deffn
372
8f85c0c6 373@deffn {Scheme Procedure} zip lst1 lst2 @dots{}
a0e07ba4
NJ
374Return a list as long as the shortest of the argument lists, where
375each element is a list. The first list contains the first elements of
376the argument lists, the second list contains the second elements, and
377so on.
378@end deffn
379
8f85c0c6
NJ
380@deffn {Scheme Procedure} unzip1 lst
381@deffnx {Scheme Procedure} unzip2 lst
382@deffnx {Scheme Procedure} unzip3 lst
383@deffnx {Scheme Procedure} unzip4 lst
384@deffnx {Scheme Procedure} unzip5 lst
a0e07ba4
NJ
385@code{unzip1} takes a list of lists, and returns a list containing the
386first elements of each list, @code{unzip2} returns two lists, the
387first containing the first elements of each lists and the second
388containing the second elements of each lists, and so on.
389@end deffn
390
e508c863
KR
391@deffn {Scheme Procedure} count pred lst1 @dots{} lstN
392Return a count of the number of times @var{pred} returns true when
393called on elements from the given lists.
394
395@var{pred} is called with @var{N} parameters @code{(@var{pred}
396@var{elem1} @dots{} @var{elemN})}, each element being from the
397corresponding @var{lst1} @dots{} @var{lstN}. The first call is with
398the first element of each list, the second with the second element
399from each, and so on.
400
401Counting stops when the end of the shortest list is reached. At least
402one list must be non-circular.
403@end deffn
404
a0e07ba4
NJ
405
406@node SRFI-1 Fold and Map
3229f68b 407@subsubsection Fold, Unfold & Map
7c2e18cd
KR
408@cindex list fold
409@cindex list map
a0e07ba4
NJ
410
411@c FIXME::martin: Review me!
412
8f85c0c6 413@deffn {Scheme Procedure} fold kons knil lst1 lst2 @dots{}
a0e07ba4
NJ
414Fold the procedure @var{kons} across all elements of @var{lst1},
415@var{lst2}, @dots{}. Produce the result of
416
417@code{(@var{kons} @var{en1} @var{en2} @dots{} (@var{kons} @var{e21}
418@var{e22} (@var{kons} @var{e11} @var{e12} @var{knil})))},
419
420if @var{enm} are the elements of the lists @var{lst1}, @var{lst2},
421@dots{}.
422@end deffn
423
8f85c0c6 424@deffn {Scheme Procedure} fold-right kons knil lst1 lst2 @dots{}
a0e07ba4
NJ
425Similar to @code{fold}, but applies @var{kons} in right-to-left order
426to the list elements, that is:
427
428@code{(@var{kons} @var{e11} @var{e12}(@var{kons} @var{e21}
429@var{e22} @dots{} (@var{kons} @var{en1} @var{en2} @var{knil})))},
430@end deffn
431
8f85c0c6 432@deffn {Scheme Procedure} pair-fold kons knil lst1 lst2 @dots{}
a0e07ba4
NJ
433Like @code{fold}, but apply @var{kons} to the pairs of the list
434instead of the list elements.
435@end deffn
436
8f85c0c6 437@deffn {Scheme Procedure} pair-fold-right kons knil lst1 lst2 @dots{}
a0e07ba4
NJ
438Like @code{fold-right}, but apply @var{kons} to the pairs of the list
439instead of the list elements.
440@end deffn
441
8f85c0c6 442@deffn {Scheme Procedure} reduce f ridentity lst
1ae7b878
KR
443@code{reduce} is a variant of @code{fold}. If @var{lst} is
444@code{()}, @var{ridentity} is returned. Otherwise, @code{(fold f (car
a0e07ba4
NJ
445@var{lst}) (cdr @var{lst}))} is returned.
446@end deffn
447
8f85c0c6 448@deffn {Scheme Procedure} reduce-right f ridentity lst
b5aa0215 449This is the @code{fold-right} variant of @code{reduce}.
a0e07ba4
NJ
450@end deffn
451
8f85c0c6 452@deffn {Scheme Procedure} unfold p f g seed [tail-gen]
a0e07ba4
NJ
453@code{unfold} is defined as follows:
454
455@lisp
456(unfold p f g seed) =
457 (if (p seed) (tail-gen seed)
458 (cons (f seed)
459 (unfold p f g (g seed))))
460@end lisp
461
462@table @var
463@item p
464Determines when to stop unfolding.
465
466@item f
467Maps each seed value to the corresponding list element.
468
469@item g
470Maps each seed value to next seed valu.
471
472@item seed
473The state value for the unfold.
474
475@item tail-gen
476Creates the tail of the list; defaults to @code{(lambda (x) '())}.
477@end table
478
479@var{g} produces a series of seed values, which are mapped to list
480elements by @var{f}. These elements are put into a list in
481left-to-right order, and @var{p} tells when to stop unfolding.
482@end deffn
483
8f85c0c6 484@deffn {Scheme Procedure} unfold-right p f g seed [tail]
a0e07ba4
NJ
485Construct a list with the following loop.
486
487@lisp
488(let lp ((seed seed) (lis tail))
489 (if (p seed) lis
490 (lp (g seed)
491 (cons (f seed) lis))))
492@end lisp
493
494@table @var
495@item p
496Determines when to stop unfolding.
497
498@item f
499Maps each seed value to the corresponding list element.
500
501@item g
502Maps each seed value to next seed valu.
503
504@item seed
505The state value for the unfold.
506
507@item tail-gen
508Creates the tail of the list; defaults to @code{(lambda (x) '())}.
509@end table
510
511@end deffn
512
8f85c0c6 513@deffn {Scheme Procedure} map f lst1 lst2 @dots{}
a0e07ba4
NJ
514Map the procedure over the list(s) @var{lst1}, @var{lst2}, @dots{} and
515return a list containing the results of the procedure applications.
516This procedure is extended with respect to R5RS, because the argument
517lists may have different lengths. The result list will have the same
518length as the shortest argument lists. The order in which @var{f}
519will be applied to the list element(s) is not specified.
520@end deffn
521
8f85c0c6 522@deffn {Scheme Procedure} for-each f lst1 lst2 @dots{}
a0e07ba4
NJ
523Apply the procedure @var{f} to each pair of corresponding elements of
524the list(s) @var{lst1}, @var{lst2}, @dots{}. The return value is not
525specified. This procedure is extended with respect to R5RS, because
526the argument lists may have different lengths. The shortest argument
527list determines the number of times @var{f} is called. @var{f} will
85a9b4ed 528be applied to the list elements in left-to-right order.
a0e07ba4
NJ
529
530@end deffn
531
8f85c0c6
NJ
532@deffn {Scheme Procedure} append-map f lst1 lst2 @dots{}
533@deffnx {Scheme Procedure} append-map! f lst1 lst2 @dots{}
12991fed 534Equivalent to
a0e07ba4
NJ
535
536@lisp
12991fed 537(apply append (map f clist1 clist2 ...))
a0e07ba4
NJ
538@end lisp
539
12991fed 540and
a0e07ba4
NJ
541
542@lisp
12991fed 543(apply append! (map f clist1 clist2 ...))
a0e07ba4
NJ
544@end lisp
545
546Map @var{f} over the elements of the lists, just as in the @code{map}
547function. However, the results of the applications are appended
548together to make the final result. @code{append-map} uses
549@code{append} to append the results together; @code{append-map!} uses
550@code{append!}.
551
552The dynamic order in which the various applications of @var{f} are
553made is not specified.
554@end deffn
555
8f85c0c6 556@deffn {Scheme Procedure} map! f lst1 lst2 @dots{}
a0e07ba4
NJ
557Linear-update variant of @code{map} -- @code{map!} is allowed, but not
558required, to alter the cons cells of @var{lst1} to construct the
559result list.
560
561The dynamic order in which the various applications of @var{f} are
562made is not specified. In the n-ary case, @var{lst2}, @var{lst3},
563@dots{} must have at least as many elements as @var{lst1}.
564@end deffn
565
8f85c0c6 566@deffn {Scheme Procedure} pair-for-each f lst1 lst2 @dots{}
a0e07ba4
NJ
567Like @code{for-each}, but applies the procedure @var{f} to the pairs
568from which the argument lists are constructed, instead of the list
569elements. The return value is not specified.
570@end deffn
571
8f85c0c6 572@deffn {Scheme Procedure} filter-map f lst1 lst2 @dots{}
a0e07ba4
NJ
573Like @code{map}, but only results from the applications of @var{f}
574which are true are saved in the result list.
575@end deffn
576
577
578@node SRFI-1 Filtering and Partitioning
3229f68b 579@subsubsection Filtering and Partitioning
7c2e18cd
KR
580@cindex list filter
581@cindex list partition
a0e07ba4
NJ
582
583@c FIXME::martin: Review me!
584
585Filtering means to collect all elements from a list which satisfy a
586specific condition. Partitioning a list means to make two groups of
587list elements, one which contains the elements satisfying a condition,
588and the other for the elements which don't.
589
60e25dc4
KR
590The @code{filter} and @code{filter!} functions are implemented in the
591Guile core, @xref{List Modification}.
a0e07ba4 592
8f85c0c6
NJ
593@deffn {Scheme Procedure} partition pred lst
594@deffnx {Scheme Procedure} partition! pred lst
193239f1
KR
595Split @var{lst} into those elements which do and don't satisfy the
596predicate @var{pred}.
a0e07ba4 597
193239f1
KR
598The return is two values (@pxref{Multiple Values}), the first being a
599list of all elements from @var{lst} which satisfy @var{pred}, the
600second a list of those which do not.
601
602The elements in the result lists are in the same order as in @var{lst}
603but the order in which the calls @code{(@var{pred} elem)} are made on
604the list elements is unspecified.
605
606@code{partition} does not change @var{lst}, but one of the returned
607lists may share a tail with it. @code{partition!} may modify
608@var{lst} to construct its return.
a0e07ba4
NJ
609@end deffn
610
8f85c0c6
NJ
611@deffn {Scheme Procedure} remove pred lst
612@deffnx {Scheme Procedure} remove! pred lst
a0e07ba4
NJ
613Return a list containing all elements from @var{lst} which do not
614satisfy the predicate @var{pred}. The elements in the result list
615have the same order as in @var{lst}. The order in which @var{pred} is
616applied to the list elements is not specified.
617
618@code{remove!} is allowed, but not required to modify the structure of
619the input list.
620@end deffn
621
622
623@node SRFI-1 Searching
3229f68b 624@subsubsection Searching
7c2e18cd 625@cindex list search
a0e07ba4
NJ
626
627@c FIXME::martin: Review me!
628
629The procedures for searching elements in lists either accept a
630predicate or a comparison object for determining which elements are to
631be searched.
632
8f85c0c6 633@deffn {Scheme Procedure} find pred lst
a0e07ba4
NJ
634Return the first element of @var{lst} which satisfies the predicate
635@var{pred} and @code{#f} if no such element is found.
636@end deffn
637
8f85c0c6 638@deffn {Scheme Procedure} find-tail pred lst
a0e07ba4
NJ
639Return the first pair of @var{lst} whose @sc{car} satisfies the
640predicate @var{pred} and @code{#f} if no such element is found.
641@end deffn
642
8f85c0c6
NJ
643@deffn {Scheme Procedure} take-while pred lst
644@deffnx {Scheme Procedure} take-while! pred lst
a0e07ba4
NJ
645Return the longest initial prefix of @var{lst} whose elements all
646satisfy the predicate @var{pred}.
647
648@code{take-while!} is allowed, but not required to modify the input
649list while producing the result.
650@end deffn
651
8f85c0c6 652@deffn {Scheme Procedure} drop-while pred lst
a0e07ba4
NJ
653Drop the longest initial prefix of @var{lst} whose elements all
654satisfy the predicate @var{pred}.
655@end deffn
656
8f85c0c6
NJ
657@deffn {Scheme Procedure} span pred lst
658@deffnx {Scheme Procedure} span! pred lst
659@deffnx {Scheme Procedure} break pred lst
660@deffnx {Scheme Procedure} break! pred lst
a0e07ba4
NJ
661@code{span} splits the list @var{lst} into the longest initial prefix
662whose elements all satisfy the predicate @var{pred}, and the remaining
663tail. @code{break} inverts the sense of the predicate.
664
665@code{span!} and @code{break!} are allowed, but not required to modify
666the structure of the input list @var{lst} in order to produce the
667result.
3e73b6f9
KR
668
669Note that the name @code{break} conflicts with the @code{break}
670binding established by @code{while} (@pxref{while do}). Applications
671wanting to use @code{break} from within a @code{while} loop will need
672to make a new define under a different name.
a0e07ba4
NJ
673@end deffn
674
62705beb
KR
675@deffn {Scheme Procedure} any pred lst1 lst2 @dots{} lstN
676Test whether any set of elements from @var{lst1} @dots{} lstN
677satisfies @var{pred}. If so the return value is the return from the
678successful @var{pred} call, or if not the return is @code{#f}.
679
680Each @var{pred} call is @code{(@var{pred} @var{elem1} @dots{}
681@var{elemN})} taking an element from each @var{lst}. The calls are
682made successively for the first, second, etc elements of the lists,
683stopping when @var{pred} returns non-@code{#f}, or when the end of the
684shortest list is reached.
685
686The @var{pred} call on the last set of elements (ie.@: when the end of
687the shortest list has been reached), if that point is reached, is a
688tail call.
689@end deffn
690
691@deffn {Scheme Procedure} every pred lst1 lst2 @dots{} lstN
692Test whether every set of elements from @var{lst1} @dots{} lstN
693satisfies @var{pred}. If so the return value is the return from the
694final @var{pred} call, or if not the return is @code{#f}.
695
696Each @var{pred} call is @code{(@var{pred} @var{elem1} @dots{}
697@var{elemN})} taking an element from each @var{lst}. The calls are
698made successively for the first, second, etc elements of the lists,
699stopping if @var{pred} returns @code{#f}, or when the end of any of
700the lists is reached.
701
702The @var{pred} call on the last set of elements (ie.@: when the end of
703the shortest list has been reached) is a tail call.
704
705If one of @var{lst1} @dots{} @var{lstN} is empty then no calls to
706@var{pred} are made, and the return is @code{#t}.
a0e07ba4
NJ
707@end deffn
708
0166e7f2
KR
709@deffn {Scheme Procedure} list-index pred lst1 @dots{} lstN
710Call @var{pred} on sets of elements from the @var{lst}s, one from each
711list, starting from the first, as @code{(@var{pred} elem1 @dots{}
712elemN)}. Return the index of the first set for which @var{pred}
713returns true.
714
715The index starts from 0 for the first first set of elements.
716Searching stops when the end of the shortest @var{lst} is reached. If
717no set of elements pass then the return is @code{#f}.
718
719@example
720(list-index odd? '(2 4 6 9)) @result{} 3
721(list-index = '(1 2 3) '(3 1 2)) @result{} #f
722@end example
a0e07ba4
NJ
723@end deffn
724
8f85c0c6 725@deffn {Scheme Procedure} member x lst [=]
a0e07ba4 726Return the first sublist of @var{lst} whose @sc{car} is equal to
ca04a5ae 727@var{x}. If @var{x} does not appear in @var{lst}, return @code{#f}.
ea6ea01b 728
ca04a5ae
KR
729Equality is determined by @code{equal?}, or by the equality predicate
730@var{=} if given. @var{=} is called @code{(= @var{x} elem)},
731ie.@: with the given @var{x} first, so for example to find the first
732element greater than 5,
733
734@example
735(member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)
736@end example
737
738This version of @code{member} extends the core @code{member}
739(@pxref{List Searching}) by accepting an equality predicate.
a0e07ba4
NJ
740@end deffn
741
742
743@node SRFI-1 Deleting
3229f68b 744@subsubsection Deleting
7c2e18cd 745@cindex list delete
a0e07ba4
NJ
746
747@c FIXME::martin: Review me!
748
8f85c0c6
NJ
749@deffn {Scheme Procedure} delete x lst [=]
750@deffnx {Scheme Procedure} delete! x lst [=]
b6b9376a
KR
751Return a list containing the elements of @var{lst} but with those
752equal to @var{x} deleted. The returned elements will be in the same
753order as they were in @var{lst}.
754
755Equality is determined by the @var{=} predicate, or @code{equal?} if
756not given. An equality call is made just once for each element, but
757the order in which the calls are made on the elements is unspecified.
a0e07ba4 758
243bdb63 759The equality calls are always @code{(= x elem)}, ie.@: the given @var{x}
b6b9376a
KR
760is first. This means for instance elements greater than 5 can be
761deleted with @code{(delete 5 lst <)}.
762
763@code{delete} does not modify @var{lst}, but the return might share a
764common tail with @var{lst}. @code{delete!} may modify the structure
765of @var{lst} to construct its return.
ea6ea01b
KR
766
767These functions extend the core @code{delete} and @code{delete!} in
768accepting an equality predicate. (@pxref{List Modification})
a0e07ba4
NJ
769@end deffn
770
8f85c0c6
NJ
771@deffn {Scheme Procedure} delete-duplicates lst [=]
772@deffnx {Scheme Procedure} delete-duplicates! lst [=]
b6b9376a
KR
773Return a list containing the elements of @var{lst} but without
774duplicates.
775
776When elements are equal, only the first in @var{lst} is retained.
777Equal elements can be anywhere in @var{lst}, they don't have to be
778adjacent. The returned list will have the retained elements in the
779same order as they were in @var{lst}.
780
781Equality is determined by the @var{=} predicate, or @code{equal?} if
782not given. Calls @code{(= x y)} are made with element @var{x} being
783before @var{y} in @var{lst}. A call is made at most once for each
784combination, but the sequence of the calls across the elements is
785unspecified.
786
787@code{delete-duplicates} does not modify @var{lst}, but the return
788might share a common tail with @var{lst}. @code{delete-duplicates!}
789may modify the structure of @var{lst} to construct its return.
790
791In the worst case, this is an @math{O(N^2)} algorithm because it must
792check each element against all those preceding it. For long lists it
793is more efficient to sort and then compare only adjacent elements.
a0e07ba4
NJ
794@end deffn
795
796
797@node SRFI-1 Association Lists
3229f68b 798@subsubsection Association Lists
7c2e18cd
KR
799@cindex association list
800@cindex alist
a0e07ba4
NJ
801
802@c FIXME::martin: Review me!
803
804Association lists are described in detail in section @ref{Association
805Lists}. The present section only documents the additional procedures
806for dealing with association lists defined by SRFI-1.
807
8f85c0c6 808@deffn {Scheme Procedure} assoc key alist [=]
a0e07ba4
NJ
809Return the pair from @var{alist} which matches @var{key}. Equality is
810determined by @var{=}, which defaults to @code{equal?} if not given.
811@var{alist} must be an association lists---a list of pairs.
ea6ea01b
KR
812
813This function extends the core @code{assoc} by accepting an equality
814predicate. (@pxref{Association Lists})
a0e07ba4
NJ
815@end deffn
816
8f85c0c6 817@deffn {Scheme Procedure} alist-cons key datum alist
a0e07ba4
NJ
818Equivalent to
819
820@lisp
821(cons (cons @var{key} @var{datum}) @var{alist})
822@end lisp
823
824This procedure is used to coons a new pair onto an existing
825association list.
826@end deffn
827
8f85c0c6 828@deffn {Scheme Procedure} alist-copy alist
a0e07ba4
NJ
829Return a newly allocated copy of @var{alist}, that means that the
830spine of the list as well as the pairs are copied.
831@end deffn
832
8f85c0c6
NJ
833@deffn {Scheme Procedure} alist-delete key alist [=]
834@deffnx {Scheme Procedure} alist-delete! key alist [=]
bd35f1f0
KR
835Return a list containing the elements of @var{alist} but with those
836elements whose keys are equal to @var{key} deleted. The returned
837elements will be in the same order as they were in @var{alist}.
a0e07ba4 838
bd35f1f0
KR
839Equality is determined by the @var{=} predicate, or @code{equal?} if
840not given. The order in which elements are tested is unspecified, but
841each equality call is made @code{(= key alistkey)}, ie. the given
842@var{key} parameter is first and the key from @var{alist} second.
843This means for instance all associations with a key greater than 5 can
844be removed with @code{(alist-delete 5 alist <)}.
845
846@code{alist-delete} does not modify @var{alist}, but the return might
847share a common tail with @var{alist}. @code{alist-delete!} may modify
848the list structure of @var{alist} to construct its return.
a0e07ba4
NJ
849@end deffn
850
851
852@node SRFI-1 Set Operations
3229f68b 853@subsubsection Set Operations on Lists
7c2e18cd 854@cindex list set operation
a0e07ba4
NJ
855
856@c FIXME::martin: Review me!
857
858Lists can be used for representing sets of objects. The procedures
859documented in this section can be used for such set representations.
85a9b4ed 860Man combining several sets or adding elements, they make sure that no
a0e07ba4
NJ
861object is contained more than once in a given list. Please note that
862lists are not a too efficient implementation method for sets, so if
863you need high performance, you should think about implementing a
864custom data structure for representing sets, such as trees, bitsets,
865hash tables or something similar.
866
867All these procedures accept an equality predicate as the first
868argument. This predicate is used for testing the objects in the list
869sets for sameness.
870
8f85c0c6 871@deffn {Scheme Procedure} lset<= = list1 @dots{}
a0e07ba4
NJ
872Return @code{#t} if every @var{listi} is a subset of @var{listi+1},
873otherwise return @code{#f}. Returns @code{#t} if called with less
874than two arguments. @var{=} is used for testing element equality.
875@end deffn
876
8f85c0c6 877@deffn {Scheme Procedure} lset= = list1 list2 @dots{}
a0e07ba4
NJ
878Return @code{#t} if all argument lists are equal. @var{=} is used for
879testing element equality.
880@end deffn
881
8f85c0c6 882@deffn {Scheme Procedure} lset-adjoin = list elt1 @dots{}
a0e07ba4 883Add all @var{elts} to the list @var{list}, suppressing duplicates and
ac70289c 884return the resulting list. @var{=} is used for testing
a0e07ba4
NJ
885element equality.
886@end deffn
887
8f85c0c6
NJ
888@deffn {Scheme Procedure} lset-union = list1 @dots{}
889@deffnx {Scheme Procedure} lset-union! = list1 @dots{}
a0e07ba4
NJ
890Return the union of all argument list sets. The union is the set of
891all elements which appear in any of the argument sets.
892@code{lset-union!} is allowed, but not required to modify its first
893argument. @var{=} is used for testing element equality.
894@end deffn
895
8f85c0c6
NJ
896@deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{}
897@deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{}
a0e07ba4
NJ
898Return the intersection of all argument list sets. The intersection
899is the set containing all elements which appear in all argument sets.
900@code{lset-intersection!} is allowed, but not required to modify its
901first argument. @var{=} is used for testing element equality.
902@end deffn
903
8f85c0c6
NJ
904@deffn {Scheme Procedure} lset-difference = list1 list2 @dots{}
905@deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{}
a0e07ba4
NJ
906Return the difference of all argument list sets. The difference is
907the the set containing all elements of the first list which do not
908appear in the other lists. @code{lset-difference!} is allowed, but
909not required to modify its first argument. @var{=} is used for testing
910element equality.
911@end deffn
912
8f85c0c6
NJ
913@deffn {Scheme Procedure} lset-xor = list1 @dots{}
914@deffnx {Scheme Procedure} lset-xor! = list1 @dots{}
a0e07ba4
NJ
915Return the set containing all elements which appear in the first
916argument list set, but not in the second; or, more generally: which
917appear in an odd number of sets. @code{lset-xor!} is allowed, but
918not required to modify its first argument. @var{=} is used for testing
919element equality.
920@end deffn
921
8f85c0c6
NJ
922@deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{}
923@deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{}
a0e07ba4
NJ
924Return two values, the difference and the intersection of the argument
925list sets. This works like a combination of @code{lset-difference} and
926@code{lset-intersection}, but is more efficient.
927@code{lset-diff+intersection!} is allowed, but not required to modify
928its first argument. @var{=} is used for testing element equality. You
929have to use some means to deal with the multiple values these
930procedures return (@pxref{Multiple Values}).
931@end deffn
932
933
934@node SRFI-2
3229f68b 935@subsection SRFI-2 - and-let*
8742c48b 936@cindex SRFI-2
a0e07ba4 937
4fd0db14
KR
938@noindent
939The following syntax can be obtained with
a0e07ba4 940
4fd0db14
KR
941@lisp
942(use-modules (srfi srfi-2))
943@end lisp
a0e07ba4 944
4fd0db14
KR
945@deffn {library syntax} and-let* (clause @dots{}) body @dots{}
946A combination of @code{and} and @code{let*}.
947
948Each @var{clause} is evaluated in turn, and if @code{#f} is obtained
949then evaluation stops and @code{#f} is returned. If all are
950non-@code{#f} then @var{body} is evaluated and the last form gives the
6b1a6e4c
KR
951return value, or if @var{body} is empty then the result is @code{#t}.
952Each @var{clause} should be one of the following,
4fd0db14
KR
953
954@table @code
955@item (symbol expr)
956Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}.
957Like @code{let*}, that binding is available to subsequent clauses.
958@item (expr)
959Evaluate @var{expr} and check for @code{#f}.
960@item symbol
961Get the value bound to @var{symbol} and check for @code{#f}.
962@end table
a0e07ba4 963
4fd0db14
KR
964Notice that @code{(expr)} has an ``extra'' pair of parentheses, for
965instance @code{((eq? x y))}. One way to remember this is to imagine
966the @code{symbol} in @code{(symbol expr)} is omitted.
a0e07ba4 967
4fd0db14
KR
968@code{and-let*} is good for calculations where a @code{#f} value means
969termination, but where a non-@code{#f} value is going to be needed in
970subsequent expressions.
971
972The following illustrates this, it returns text between brackets
973@samp{[...]} in a string, or @code{#f} if there are no such brackets
974(ie.@: either @code{string-index} gives @code{#f}).
975
976@example
977(define (extract-brackets str)
978 (and-let* ((start (string-index str #\[))
979 (end (string-index str #\] start)))
980 (substring str (1+ start) end)))
981@end example
982
983The following shows plain variables and expressions tested too.
984@code{diagnostic-levels} is taken to be an alist associating a
985diagnostic type with a level. @code{str} is printed only if the type
986is known and its level is high enough.
987
988@example
989(define (show-diagnostic type str)
990 (and-let* (want-diagnostics
991 (level (assq-ref diagnostic-levels type))
992 ((>= level current-diagnostic-level)))
993 (display str)))
994@end example
995
996The advantage of @code{and-let*} is that an extended sequence of
997expressions and tests doesn't require lots of nesting as would arise
998from separate @code{and} and @code{let*}, or from @code{cond} with
999@code{=>}.
1000
1001@end deffn
a0e07ba4
NJ
1002
1003
1004@node SRFI-4
3229f68b 1005@subsection SRFI-4 - Homogeneous numeric vector datatypes
8742c48b 1006@cindex SRFI-4
a0e07ba4 1007
e6b226b9 1008The SRFI-4 procedures and data types are always available, @xref{Uniform
3dd6e0cf 1009Numeric Vectors}.
a0e07ba4
NJ
1010
1011@node SRFI-6
3229f68b 1012@subsection SRFI-6 - Basic String Ports
8742c48b 1013@cindex SRFI-6
a0e07ba4
NJ
1014
1015SRFI-6 defines the procedures @code{open-input-string},
1016@code{open-output-string} and @code{get-output-string}. These
1017procedures are included in the Guile core, so using this module does not
1018make any difference at the moment. But it is possible that support for
1019SRFI-6 will be factored out of the core library in the future, so using
1020this module does not hurt, after all.
1021
1022@node SRFI-8
3229f68b 1023@subsection SRFI-8 - receive
8742c48b 1024@cindex SRFI-8
a0e07ba4
NJ
1025
1026@code{receive} is a syntax for making the handling of multiple-value
1027procedures easier. It is documented in @xref{Multiple Values}.
1028
1029
1030@node SRFI-9
3229f68b 1031@subsection SRFI-9 - define-record-type
8742c48b 1032@cindex SRFI-9
7c2e18cd 1033@cindex record
a0e07ba4 1034
6afe385d
KR
1035This SRFI is a syntax for defining new record types and creating
1036predicate, constructor, and field getter and setter functions. In
1037Guile this is simply an alternate interface to the core record
1038functionality (@pxref{Records}). It can be used with,
a0e07ba4 1039
6afe385d
KR
1040@example
1041(use-modules (srfi srfi-9))
1042@end example
1043
1044@deffn {library syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
1045@sp 1
1046Create a new record type, and make various @code{define}s for using
1047it. This syntax can only occur at the top-level, not nested within
1048some other form.
1049
1050@var{type} is bound to the record type, which is as per the return
1051from the core @code{make-record-type}. @var{type} also provides the
1052name for the record, as per @code{record-type-name}.
1053
1054@var{constructor} is bound to a function to be called as
1055@code{(@var{constructor} fieldval @dots{})} to create a new record of
1056this type. The arguments are initial values for the fields, one
1057argument for each field, in the order they appear in the
1058@code{define-record-type} form.
1059
1060The @var{fieldname}s provide the names for the record fields, as per
1061the core @code{record-type-fields} etc, and are referred to in the
1062subsequent accessor/modifier forms.
1063
1064@var{predictate} is bound to a function to be called as
1065@code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f}
1066according to whether @var{obj} is a record of this type.
1067
1068Each @var{accessor} is bound to a function to be called
1069@code{(@var{accessor} record)} to retrieve the respective field from a
1070@var{record}. Similarly each @var{modifier} is bound to a function to
1071be called @code{(@var{modifier} record val)} to set the respective
1072field in a @var{record}.
1073@end deffn
1074
1075@noindent
1076An example will illustrate typical usage,
a0e07ba4
NJ
1077
1078@example
6afe385d
KR
1079(define-record-type employee-type
1080 (make-employee name age salary)
1081 employee?
1082 (name get-employee-name)
1083 (age get-employee-age set-employee-age)
1084 (salary get-employee-salary set-employee-salary))
a0e07ba4
NJ
1085@end example
1086
6afe385d
KR
1087This creates a new employee data type, with name, age and salary
1088fields. Accessor functions are created for each field, but no
1089modifier function for the name (the intention in this example being
1090that it's established only when an employee object is created). These
1091can all then be used as for example,
a0e07ba4
NJ
1092
1093@example
6afe385d
KR
1094employee-type @result{} #<record-type employee-type>
1095
1096(define fred (make-employee "Fred" 45 20000.00))
1097
1098(employee? fred) @result{} #t
1099(get-employee-age fred) @result{} 45
1100(set-employee-salary fred 25000.00) ;; pay rise
a0e07ba4
NJ
1101@end example
1102
6afe385d
KR
1103The functions created by @code{define-record-type} are ordinary
1104top-level @code{define}s. They can be redefined or @code{set!} as
1105desired, exported from a module, etc.
1106
a0e07ba4
NJ
1107
1108@node SRFI-10
3229f68b 1109@subsection SRFI-10 - Hash-Comma Reader Extension
8742c48b 1110@cindex SRFI-10
a0e07ba4
NJ
1111
1112@cindex hash-comma
1113@cindex #,()
633acbe2
KR
1114This SRFI implements a reader extension @code{#,()} called hash-comma.
1115It allows the reader to give new kinds of objects, for use both in
1116data and as constants or literals in source code. This feature is
1117available with
a0e07ba4 1118
633acbe2
KR
1119@example
1120(use-modules (srfi srfi-10))
1121@end example
1122
1123@noindent
1124The new read syntax is of the form
a0e07ba4
NJ
1125
1126@example
633acbe2 1127#,(@var{tag} @var{arg}@dots{})
a0e07ba4
NJ
1128@end example
1129
633acbe2
KR
1130@noindent
1131where @var{tag} is a symbol and the @var{arg}s are objects taken as
1132parameters. @var{tag}s are registered with the following procedure.
a0e07ba4 1133
633acbe2
KR
1134@deffn {Scheme Procedure} define-reader-ctor tag proc
1135Register @var{proc} as the constructor for a hash-comma read syntax
1136starting with symbol @var{tag}, ie. @nicode{#,(@var{tag} arg@dots{})}.
1137@var{proc} is called with the given arguments @code{(@var{proc}
1138arg@dots{})} and the object it returns is the result of the read.
1139@end deffn
a0e07ba4 1140
633acbe2
KR
1141@noindent
1142For example, a syntax giving a list of @var{N} copies of an object.
1143
1144@example
1145(define-reader-ctor 'repeat
1146 (lambda (obj reps)
1147 (make-list reps obj)))
1148
1149(display '#,(repeat 99 3))
1150@print{} (99 99 99)
1151@end example
1152
1153Notice the quote @nicode{'} when the @nicode{#,( )} is used. The
1154@code{repeat} handler returns a list and the program must quote to use
1155it literally, the same as any other list. Ie.
1156
1157@example
1158(display '#,(repeat 99 3))
a0e07ba4 1159@result{}
633acbe2
KR
1160(display '(99 99 99))
1161@end example
a0e07ba4 1162
633acbe2
KR
1163When a handler returns an object which is self-evaluating, like a
1164number or a string, then there's no need for quoting, just as there's
1165no need when giving those directly as literals. For example an
1166addition,
a0e07ba4 1167
633acbe2
KR
1168@example
1169(define-reader-ctor 'sum
1170 (lambda (x y)
1171 (+ x y)))
1172(display #,(sum 123 456)) @print{} 579
1173@end example
1174
1175A typical use for @nicode{#,()} is to get a read syntax for objects
1176which don't otherwise have one. For example, the following allows a
1177hash table to be given literally, with tags and values, ready for fast
1178lookup.
1179
1180@example
1181(define-reader-ctor 'hash
1182 (lambda elems
1183 (let ((table (make-hash-table)))
1184 (for-each (lambda (elem)
1185 (apply hash-set! table elem))
1186 elems)
1187 table)))
1188
1189(define (animal->family animal)
1190 (hash-ref '#,(hash ("tiger" "cat")
1191 ("lion" "cat")
1192 ("wolf" "dog"))
1193 animal))
1194
1195(animal->family "lion") @result{} "cat"
1196@end example
1197
1198Or for example the following is a syntax for a compiled regular
1199expression (@pxref{Regular Expressions}).
1200
1201@example
1202(use-modules (ice-9 regex))
1203
1204(define-reader-ctor 'regexp make-regexp)
1205
1206(define (extract-angs str)
1207 (let ((match (regexp-exec '#,(regexp "<([A-Z0-9]+)>") str)))
1208 (and match
1209 (match:substring match 1))))
1210
1211(extract-angs "foo <BAR> quux") @result{} "BAR"
1212@end example
1213
1214@sp 1
1215@nicode{#,()} is somewhat similar to @code{define-macro}
1216(@pxref{Macros}) in that handler code is run to produce a result, but
1217@nicode{#,()} operates at the read stage, so it can appear in data for
1218@code{read} (@pxref{Scheme Read}), not just in code to be executed.
1219
1220Because @nicode{#,()} is handled at read-time it has no direct access
1221to variables etc. A symbol in the arguments is just a symbol, not a
1222variable reference. The arguments are essentially constants, though
1223the handler procedure can use them in any complicated way it might
1224want.
1225
1226Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
1227globally, there's no need to use @code{(srfi srfi-10)} in later
1228modules. Similarly the tags registered are global and can be used
1229anywhere once registered.
1230
1231There's no attempt to record what previous @nicode{#,()} forms have
1232been seen, if two identical forms occur then two calls are made to the
1233handler procedure. The handler might like to maintain a cache or
1234similar to avoid making copies of large objects, depending on expected
1235usage.
1236
1237In code the best uses of @nicode{#,()} are generally when there's a
1238lot of objects of a particular kind as literals or constants. If
1239there's just a few then some local variables and initializers are
1240fine, but that becomes tedious and error prone when there's a lot, and
1241the anonymous and compact syntax of @nicode{#,()} is much better.
a0e07ba4
NJ
1242
1243
1244@node SRFI-11
3229f68b 1245@subsection SRFI-11 - let-values
8742c48b 1246@cindex SRFI-11
a0e07ba4 1247
8742c48b
KR
1248@findex let-values
1249@findex let-values*
a0e07ba4
NJ
1250This module implements the binding forms for multiple values
1251@code{let-values} and @code{let-values*}. These forms are similar to
1252@code{let} and @code{let*} (@pxref{Local Bindings}), but they support
1253binding of the values returned by multiple-valued expressions.
1254
1255Write @code{(use-modules (srfi srfi-11))} to make the bindings
1256available.
1257
1258@lisp
1259(let-values (((x y) (values 1 2))
1260 ((z f) (values 3 4)))
1261 (+ x y z f))
1262@result{}
126310
1264@end lisp
1265
1266@code{let-values} performs all bindings simultaneously, which means that
1267no expression in the binding clauses may refer to variables bound in the
1268same clause list. @code{let-values*}, on the other hand, performs the
1269bindings sequentially, just like @code{let*} does for single-valued
1270expressions.
1271
1272
1273@node SRFI-13
3229f68b 1274@subsection SRFI-13 - String Library
8742c48b 1275@cindex SRFI-13
a0e07ba4 1276
5676b4fa 1277The SRFI-13 procedures are always available, @xref{Strings}.
a0e07ba4
NJ
1278
1279@node SRFI-14
3229f68b 1280@subsection SRFI-14 - Character-set Library
8742c48b 1281@cindex SRFI-14
a0e07ba4 1282
050ab45f
MV
1283The SRFI-14 data type and procedures are always available,
1284@xref{Character Sets}.
a0e07ba4
NJ
1285
1286@node SRFI-16
3229f68b 1287@subsection SRFI-16 - case-lambda
8742c48b 1288@cindex SRFI-16
7c2e18cd
KR
1289@cindex variable arity
1290@cindex arity, variable
a0e07ba4
NJ
1291
1292@c FIXME::martin: Review me!
1293
8742c48b 1294@findex case-lambda
a0e07ba4
NJ
1295The syntactic form @code{case-lambda} creates procedures, just like
1296@code{lambda}, but has syntactic extensions for writing procedures of
1297varying arity easier.
1298
1299The syntax of the @code{case-lambda} form is defined in the following
1300EBNF grammar.
1301
1302@example
1303@group
1304<case-lambda>
1305 --> (case-lambda <case-lambda-clause>)
1306<case-lambda-clause>
1307 --> (<formals> <definition-or-command>*)
1308<formals>
1309 --> (<identifier>*)
1310 | (<identifier>* . <identifier>)
1311 | <identifier>
1312@end group
1313@end example
1314
1315The value returned by a @code{case-lambda} form is a procedure which
1316matches the number of actual arguments against the formals in the
1317various clauses, in order. @dfn{Formals} means a formal argument list
1318just like with @code{lambda} (@pxref{Lambda}). The first matching clause
1319is selected, the corresponding values from the actual parameter list are
1320bound to the variable names in the clauses and the body of the clause is
1321evaluated. If no clause matches, an error is signalled.
1322
1323The following (silly) definition creates a procedure @var{foo} which
1324acts differently, depending on the number of actual arguments. If one
1325argument is given, the constant @code{#t} is returned, two arguments are
1326added and if more arguments are passed, their product is calculated.
1327
1328@lisp
1329(define foo (case-lambda
1330 ((x) #t)
1331 ((x y) (+ x y))
1332 (z
1333 (apply * z))))
1334(foo 'bar)
1335@result{}
1336#t
1337(foo 2 4)
1338@result{}
13396
1340(foo 3 3 3)
1341@result{}
134227
1343(foo)
1344@result{}
13451
1346@end lisp
1347
1348The last expression evaluates to 1 because the last clause is matched,
1349@var{z} is bound to the empty list and the following multiplication,
1350applied to zero arguments, yields 1.
1351
1352
1353@node SRFI-17
3229f68b 1354@subsection SRFI-17 - Generalized set!
8742c48b 1355@cindex SRFI-17
a0e07ba4
NJ
1356
1357This is an implementation of SRFI-17: Generalized set!
1358
8742c48b 1359@findex getter-with-setter
a0e07ba4
NJ
1360It exports the Guile procedure @code{make-procedure-with-setter} under
1361the SRFI name @code{getter-with-setter} and exports the standard
1362procedures @code{car}, @code{cdr}, @dots{}, @code{cdddr},
1363@code{string-ref} and @code{vector-ref} as procedures with setters, as
1364required by the SRFI.
1365
1366SRFI-17 was heavily criticized during its discussion period but it was
1367finalized anyway. One issue was its concept of globally associating
1368setter @dfn{properties} with (procedure) values, which is non-Schemy.
1369For this reason, this implementation chooses not to provide a way to set
1370the setter of a procedure. In fact, @code{(set! (setter @var{proc})
1371@var{setter})} signals an error. The only way to attach a setter to a
1372procedure is to create a new object (a @dfn{procedure with setter}) via
1373the @code{getter-with-setter} procedure. This procedure is also
1374specified in the SRFI. Using it avoids the described problems.
1375
12991fed
TTN
1376
1377@node SRFI-19
3229f68b 1378@subsection SRFI-19 - Time/Date Library
8742c48b 1379@cindex SRFI-19
7c2e18cd
KR
1380@cindex time
1381@cindex date
12991fed 1382
85600a0f
KR
1383This is an implementation of the SRFI-19 time/date library. The
1384functions and variables described here are provided by
12991fed
TTN
1385
1386@example
85600a0f 1387(use-modules (srfi srfi-19))
12991fed
TTN
1388@end example
1389
85600a0f
KR
1390@menu
1391* SRFI-19 Introduction::
1392* SRFI-19 Time::
1393* SRFI-19 Date::
1394* SRFI-19 Time/Date conversions::
1395* SRFI-19 Date to string::
1396* SRFI-19 String to date::
1397@end menu
12991fed 1398
85600a0f 1399@node SRFI-19 Introduction
3229f68b 1400@subsubsection SRFI-19 Introduction
85600a0f
KR
1401
1402@cindex universal time
1403@cindex atomic time
1404@cindex UTC
1405@cindex TAI
1406This module implements time and date representations and calculations,
1407in various time systems, including universal time (UTC) and atomic
1408time (TAI).
1409
1410For those not familiar with these time systems, TAI is based on a
1411fixed length second derived from oscillations of certain atoms. UTC
1412differs from TAI by an integral number of seconds, which is increased
1413or decreased at announced times to keep UTC aligned to a mean solar
1414day (the orbit and rotation of the earth are not quite constant).
1415
1416@cindex leap second
1417So far, only increases in the TAI
1418@tex
1419$\leftrightarrow$
1420@end tex
1421@ifnottex
1422<->
1423@end ifnottex
1424UTC difference have been needed. Such an increase is a ``leap
1425second'', an extra second of TAI introduced at the end of a UTC day.
1426When working entirely within UTC this is never seen, every day simply
1427has 86400 seconds. But when converting from TAI to a UTC date, an
1428extra 23:59:60 is present, where normally a day would end at 23:59:59.
1429Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
1430seconds.
1431
1432@cindex system clock
1433In the current implementation, the system clock is assumed to be UTC,
1434and a table of leap seconds in the code converts to TAI. See comments
1435in @file{srfi-19.scm} for how to update this table.
1436
1437@cindex julian day
1438@cindex modified julian day
1439Also, for those not familiar with the terminology, a @dfn{Julian Day}
1440is a real number which is a count of days and fraction of a day, in
1441UTC, starting from -4713-01-01T12:00:00Z, ie.@: midday Monday 1 Jan
7c2e18cd
KR
14424713 B.C. A @dfn{Modified Julian Day} is the same, but starting from
14431858-11-17T00:00:00Z, ie.@: midnight 17 November 1858 UTC. That time
1444is julian day 2400000.5.
85600a0f
KR
1445
1446@c The SRFI-1 spec says -4714-11-24T12:00:00Z (November 24, -4714 at
1447@c noon, UTC), but this is incorrect. It looks like it might have
1448@c arisen from the code incorrectly treating years a multiple of 100
7c2e18cd 1449@c but not 400 prior to 1582 as non-leap years, where instead the Julian
85600a0f
KR
1450@c calendar should be used so all multiples of 4 before 1582 are leap
1451@c years.
1452
1453
1454@node SRFI-19 Time
3229f68b 1455@subsubsection SRFI-19 Time
85600a0f
KR
1456@cindex time
1457
1458A @dfn{time} object has type, seconds and nanoseconds fields
1459representing a point in time starting from some epoch. This is an
1460arbitrary point in time, not just a time of day. Although times are
1461represented in nanoseconds, the actual resolution may be lower.
1462
1463The following variables hold the possible time types. For instance
1464@code{(current-time time-process)} would give the current CPU process
1465time.
1466
1467@defvar time-utc
1468Universal Coordinated Time (UTC).
1469@cindex UTC
1470@end defvar
12991fed 1471
85600a0f
KR
1472@defvar time-tai
1473International Atomic Time (TAI).
1474@cindex TAI
1475@end defvar
12991fed 1476
85600a0f
KR
1477@defvar time-monotonic
1478Monotonic time, meaning a monotonically increasing time starting from
1479an unspecified epoch.
12991fed 1480
85600a0f
KR
1481Note that in the current implementation @code{time-monotonic} is the
1482same as @code{time-tai}, and unfortunately is therefore affected by
1483adjustments to the system clock. Perhaps this will change in the
1484future.
1485@end defvar
12991fed 1486
85600a0f
KR
1487@defvar time-duration
1488A duration, meaning simply a difference between two times.
1489@end defvar
12991fed 1490
85600a0f
KR
1491@defvar time-process
1492CPU time spent in the current process, starting from when the process
1493began.
1494@cindex process time
1495@end defvar
12991fed 1496
85600a0f
KR
1497@defvar time-thread
1498CPU time spent in the current thread. Not currently implemented.
1499@cindex thread time
1500@end defvar
12991fed 1501
85600a0f
KR
1502@sp 1
1503@defun time? obj
1504Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
1505@end defun
1506
1507@defun make-time type nanoseconds seconds
1508Create a time object with the given @var{type}, @var{seconds} and
1509@var{nanoseconds}.
1510@end defun
1511
1512@defun time-type time
1513@defunx time-nanosecond time
1514@defunx time-second time
1515@defunx set-time-type! time type
1516@defunx set-time-nanosecond! time nsec
1517@defunx set-time-second! time sec
1518Get or set the type, seconds or nanoseconds fields of a time object.
1519
1520@code{set-time-type!} merely changes the field, it doesn't convert the
1521time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
1522@end defun
1523
1524@defun copy-time time
1525Return a new time object, which is a copy of the given @var{time}.
1526@end defun
1527
1528@defun current-time [type]
1529Return the current time of the given @var{type}. The default
1530@var{type} is @code{time-utc}.
1531
1532Note that the name @code{current-time} conflicts with the Guile core
1533@code{current-time} function (@pxref{Time}). Applications wanting to
1534use both will need to use a different name for one of them.
1535@end defun
1536
1537@defun time-resolution [type]
1538Return the resolution, in nanoseconds, of the given time @var{type}.
1539The default @var{type} is @code{time-utc}.
1540@end defun
1541
1542@defun time<=? t1 t2
1543@defunx time<? t1 t2
1544@defunx time=? t1 t2
1545@defunx time>=? t1 t2
1546@defunx time>? t1 t2
1547Return @code{#t} or @code{#f} according to the respective relation
1548between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
1549must be the same time type.
1550@end defun
1551
1552@defun time-difference t1 t2
1553@defunx time-difference! t1 t2
1554Return a time object of type @code{time-duration} representing the
1555period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
1556the same time type.
1557
1558@code{time-difference} returns a new time object,
1559@code{time-difference!} may modify @var{t1} to form its return.
1560@end defun
1561
1562@defun add-duration time duration
1563@defunx add-duration! time duration
1564@defunx subtract-duration time duration
1565@defunx subtract-duration! time duration
1566Return a time object which is @var{time} with the given @var{duration}
1567added or subtracted. @var{duration} must be a time object of type
1568@code{time-duration}.
1569
1570@code{add-duration} and @code{subtract-duration} return a new time
1571object. @code{add-duration!} and @code{subtract-duration!} may modify
1572the given @var{time} to form their return.
1573@end defun
1574
1575
1576@node SRFI-19 Date
3229f68b 1577@subsubsection SRFI-19 Date
85600a0f
KR
1578@cindex date
1579
1580A @dfn{date} object represents a date in the Gregorian calendar and a
1581time of day on that date in some timezone.
1582
1583The fields are year, month, day, hour, minute, second, nanoseconds and
1584timezone. A date object is immutable, its fields can be read but they
1585cannot be modified once the object is created.
1586
1587@defun date? obj
1588Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
1589@end defun
1590
1591@defun make-date nsecs seconds minutes hours date month year zone-offset
1592Create a new date object.
1593@c
1594@c FIXME: What can we say about the ranges of the values. The
1595@c current code looks it doesn't normalize, but expects then in their
1596@c usual range already.
1597@c
1598@end defun
1599
1600@defun date-nanosecond date
1601Nanoseconds, 0 to 999999999.
1602@end defun
1603
1604@defun date-second date
7c2e18cd
KR
1605Seconds, 0 to 59, or 60 for a leap second. 60 is never seen when working
1606entirely within UTC, it's only when converting to or from TAI.
85600a0f
KR
1607@end defun
1608
1609@defun date-minute date
1610Minutes, 0 to 59.
1611@end defun
1612
1613@defun date-hour date
1614Hour, 0 to 23.
1615@end defun
1616
1617@defun date-day date
1618Day of the month, 1 to 31 (or less, according to the month).
1619@end defun
1620
1621@defun date-month date
1622Month, 1 to 12.
1623@end defun
1624
1625@defun date-year date
7c2e18cd
KR
1626Year, eg.@: 2003. Dates B.C.@: are negative, eg.@: @math{-46} is 46
1627B.C. There is no year 0, year @math{-1} is followed by year 1.
85600a0f
KR
1628@end defun
1629
1630@defun date-zone-offset date
1631Time zone, an integer number of seconds east of Greenwich.
1632@end defun
1633
1634@defun date-year-day date
1635Day of the year, starting from 1 for 1st January.
1636@end defun
1637
1638@defun date-week-day date
1639Day of the week, starting from 0 for Sunday.
1640@end defun
1641
1642@defun date-week-number date dstartw
1643Week of the year, ignoring a first partial week. @var{dstartw} is the
1644day of the week which is taken to start a week, 0 for Sunday, 1 for
1645Monday, etc.
1646@c
1647@c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
1648@c The code looks like it's 0, if that's the correct intention.
1649@c
1650@end defun
1651
1652@c The SRFI text doesn't actually give the default for tz-offset, but
1653@c the reference implementation has the local timezone and the
1654@c conversions functions all specify that, so it should be ok to
1655@c document it here.
1656@c
1657@defun current-date [tz-offset]
7c2e18cd
KR
1658Return a date object representing the current date/time, in UTC offset
1659by @var{tz-offset}. @var{tz-offset} is seconds east of Greenwich and
1660defaults to the local timezone.
85600a0f
KR
1661@end defun
1662
1663@defun current-julian-day
1664@cindex julian day
1665Return the current Julian Day.
1666@end defun
1667
1668@defun current-modified-julian-day
1669@cindex modified julian day
1670Return the current Modified Julian Day.
1671@end defun
1672
1673
1674@node SRFI-19 Time/Date conversions
3229f68b 1675@subsubsection SRFI-19 Time/Date conversions
7c2e18cd
KR
1676@cindex time conversion
1677@cindex date conversion
85600a0f
KR
1678
1679@defun date->julian-day date
1680@defunx date->modified-julian-day date
1681@defunx date->time-monotonic date
1682@defunx date->time-tai date
1683@defunx date->time-utc date
1684@end defun
1685@defun julian-day->date jdn [tz-offset]
1686@defunx julian-day->time-monotonic jdn
1687@defunx julian-day->time-tai jdn
1688@defunx julian-day->time-utc jdn
1689@end defun
1690@defun modified-julian-day->date jdn [tz-offset]
1691@defunx modified-julian-day->time-monotonic jdn
1692@defunx modified-julian-day->time-tai jdn
1693@defunx modified-julian-day->time-utc jdn
1694@end defun
1695@defun time-monotonic->date time [tz-offset]
1696@defunx time-monotonic->time-tai time
1697@defunx time-monotonic->time-tai! time
1698@defunx time-monotonic->time-utc time
1699@defunx time-monotonic->time-utc! time
1700@end defun
1701@defun time-tai->date time [tz-offset]
1702@defunx time-tai->julian-day time
1703@defunx time-tai->modified-julian-day time
1704@defunx time-tai->time-monotonic time
1705@defunx time-tai->time-monotonic! time
1706@defunx time-tai->time-utc time
1707@defunx time-tai->time-utc! time
1708@end defun
1709@defun time-utc->date time [tz-offset]
1710@defunx time-utc->julian-day time
1711@defunx time-utc->modified-julian-day time
1712@defunx time-utc->time-monotonic time
1713@defunx time-utc->time-monotonic! time
1714@defunx time-utc->time-tai time
1715@defunx time-utc->time-tai! time
1716@sp 1
1717Convert between dates, times and days of the respective types. For
1718instance @code{time-tai->time-utc} accepts a @var{time} object of type
1719@code{time-tai} and returns an object of type @code{time-utc}.
1720
1721For conversions to dates, @var{tz-offset} is seconds east of
7c2e18cd
KR
1722Greenwich. The default is the local timezone, at the given time, as
1723provided by the system.
85600a0f
KR
1724
1725The @code{!} variants may modify their @var{time} argument to form
1726their return. The plain functions create a new object.
1727@end defun
1728
1729@node SRFI-19 Date to string
3229f68b 1730@subsubsection SRFI-19 Date to string
85600a0f 1731@cindex date to string
7c2e18cd 1732@cindex string, from date
85600a0f
KR
1733
1734@defun date->string date [format]
1735Convert a date to a string under the control of a format.
1736@var{format} should be a string containing @samp{~} escapes, which
1737will be expanded as per the following conversion table. The default
1738@var{format} is @samp{~c}, a locale-dependent date and time.
1739
1740Many of these conversion characters are the same as POSIX
1741@code{strftime} (@pxref{Time}), but there are some extras and some
1742variations.
1743
1744@multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
1745@item @nicode{~~} @tab literal ~
1746@item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
1747@item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
1748@item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
1749@item @nicode{~B} @tab locale full month, eg.@: @samp{January}
1750@item @nicode{~c} @tab locale date and time, eg.@: @*
1751@samp{Fri Jul 14 20:28:42-0400 2000}
1752@item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
1753
1754@c Spec says d/m/y, reference implementation says m/d/y.
1755@c Apparently the reference code was the intention, but would like to
1756@c see an errata published for the spec before contradicting it here.
1757@c
1758@c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
1759
1760@item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
1761@item @nicode{~f} @tab seconds and fractional seconds,
1762with locale decimal point, eg.@: @samp{5.2}
1763@item @nicode{~h} @tab same as @nicode{~b}
1764@item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
1765@item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
1766@item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
1767@item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
1768@item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
1769@item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
1770@item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
1771@item @nicode{~n} @tab newline
1772@item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
1773@item @nicode{~p} @tab locale AM or PM
1774@item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
1775@item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
1776@item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
1777(usual limit is 59, 60 is a leap second)
1778@item @nicode{~t} @tab horizontal tab character
1779@item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
1780@item @nicode{~U} @tab week of year, Sunday first day of week,
1781@samp{00} to @samp{52}
1782@item @nicode{~V} @tab week of year, Monday first day of week,
1783@samp{01} to @samp{53}
1784@item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
1785@item @nicode{~W} @tab week of year, Monday first day of week,
1786@samp{00} to @samp{52}
1787
1788@c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
1789@c date. The reference code has ~x as the locale date and ~X as a
1790@c locale time. The rule is apparently that the code should be
1791@c believed, but would like to see an errata for the spec before
1792@c contradicting it here.
1793@c
1794@c @item @nicode{~x} @tab week of year, Monday as first day of week,
1795@c @samp{00} to @samp{53}
1796@c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
1797
1798@item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
1799@item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
1800@item @nicode{~z} @tab time zone, RFC-822 style
1801@item @nicode{~Z} @tab time zone symbol (not currently implemented)
1802@item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
1803@item @nicode{~2} @tab ISO-8601 time+zone, @samp{~k:~M:~S~z}
1804@item @nicode{~3} @tab ISO-8601 time, @samp{~k:~M:~S}
1805@item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~k:~M:~S~z}
1806@item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~k:~M:~S}
1807@end multitable
1808@end defun
1809
1810Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
1811described here, since the specification and reference implementation
1812differ.
1813
1814Currently Guile doesn't implement any localizations for the above, all
1815outputs are in English, and the @samp{~c} conversion is POSIX
1816@code{ctime} style @samp{~a ~b ~d ~H:~M:~S~z ~Y}. This may change in
1817the future.
1818
1819
1820@node SRFI-19 String to date
3229f68b 1821@subsubsection SRFI-19 String to date
85600a0f 1822@cindex string to date
7c2e18cd 1823@cindex date, from string
85600a0f
KR
1824
1825@c FIXME: Can we say what happens when an incomplete date is
1826@c converted? Ie. fields left as 0, or what? The spec seems to be
1827@c silent on this.
1828
1829@defun string->date input template
1830Convert an @var{input} string to a date under the control of a
1831@var{template} string. Return a newly created date object.
1832
1833Literal characters in @var{template} must match characters in
1834@var{input} and @samp{~} escapes must match the input forms described
1835in the table below. ``Skip to'' means characters up to one of the
1836given type are ignored, or ``no skip'' for no skipping. ``Read'' is
1837what's then read, and ``Set'' is the field affected in the date
1838object.
1839
1840For example @samp{~Y} skips input characters until a digit is reached,
1841at which point it expects a year and stores that to the year field of
1842the date.
1843
1844@multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
1845@item
1846@tab Skip to
1847@tab Read
1848@tab Set
1849
1850@item @nicode{~~}
1851@tab no skip
1852@tab literal ~
1853@tab nothing
1854
1855@item @nicode{~a}
1856@tab @nicode{char-alphabetic?}
1857@tab locale abbreviated weekday name
1858@tab nothing
1859
1860@item @nicode{~A}
1861@tab @nicode{char-alphabetic?}
1862@tab locale full weekday name
1863@tab nothing
1864
1865@c Note that the SRFI spec says that ~b and ~B don't set anything,
1866@c but that looks like a mistake. The reference implementation sets
1867@c the month field, which seems sensible and is what we describe
1868@c here.
1869
1870@item @nicode{~b}
1871@tab @nicode{char-alphabetic?}
1872@tab locale abbreviated month name
1873@tab @nicode{date-month}
1874
1875@item @nicode{~B}
1876@tab @nicode{char-alphabetic?}
1877@tab locale full month name
1878@tab @nicode{date-month}
1879
1880@item @nicode{~d}
1881@tab @nicode{char-numeric?}
1882@tab day of month
1883@tab @nicode{date-day}
1884
1885@item @nicode{~e}
1886@tab no skip
1887@tab day of month, blank padded
1888@tab @nicode{date-day}
1889
1890@item @nicode{~h}
1891@tab same as @samp{~b}
1892
1893@item @nicode{~H}
1894@tab @nicode{char-numeric?}
1895@tab hour
1896@tab @nicode{date-hour}
1897
1898@item @nicode{~k}
1899@tab no skip
1900@tab hour, blank padded
1901@tab @nicode{date-hour}
1902
1903@item @nicode{~m}
1904@tab @nicode{char-numeric?}
1905@tab month
1906@tab @nicode{date-month}
1907
1908@item @nicode{~M}
1909@tab @nicode{char-numeric?}
1910@tab minute
1911@tab @nicode{date-minute}
1912
1913@item @nicode{~S}
1914@tab @nicode{char-numeric?}
1915@tab second
1916@tab @nicode{date-second}
1917
1918@item @nicode{~y}
1919@tab no skip
1920@tab 2-digit year
1921@tab @nicode{date-year} within 50 years
1922
1923@item @nicode{~Y}
1924@tab @nicode{char-numeric?}
1925@tab year
1926@tab @nicode{date-year}
1927
1928@item @nicode{~z}
1929@tab no skip
1930@tab time zone
1931@tab date-zone-offset
1932@end multitable
1933
1934Notice that the weekday matching forms don't affect the date object
1935returned, instead the weekday will be derived from the day, month and
1936year.
1937
1938Currently Guile doesn't implement any localizations for the above,
1939month and weekday names are always expected in English. This may
1940change in the future.
1941@end defun
12991fed 1942
1de8c1ae 1943
b0b55bd6 1944@node SRFI-26
3229f68b 1945@subsection SRFI-26 - specializing parameters
1de8c1ae 1946@cindex SRFI-26
7c2e18cd
KR
1947@cindex parameter specialize
1948@cindex argument specialize
1949@cindex specialize parameter
1de8c1ae
KR
1950
1951This SRFI provides a syntax for conveniently specializing selected
1952parameters of a function. It can be used with,
1953
1954@example
1955(use-modules (srfi srfi-26))
1956@end example
1957
1958@deffn {library syntax} cut slot @dots{}
1959@deffnx {library syntax} cute slot @dots{}
1960Return a new procedure which will make a call (@var{slot} @dots{}) but
1961with selected parameters specialized to given expressions.
1962
1963An example will illustrate the idea. The following is a
1964specialization of @code{write}, sending output to
1965@code{my-output-port},
1966
1967@example
1968(cut write <> my-output-port)
1969@result{}
1970(lambda (obj) (write obj my-output-port))
1971@end example
1972
1973The special symbol @code{<>} indicates a slot to be filled by an
1974argument to the new procedure. @code{my-output-port} on the other
1975hand is an expression to be evaluated and passed, ie.@: it specializes
1976the behaviour of @code{write}.
1977
1978@table @nicode
1979@item <>
1980A slot to be filled by an argument from the created procedure.
1981Arguments are assigned to @code{<>} slots in the order they appear in
1982the @code{cut} form, there's no way to re-arrange arguments.
1983
1984The first argument to @code{cut} is usually a procedure (or expression
1985giving a procedure), but @code{<>} is allowed there too. For example,
1986
1987@example
1988(cut <> 1 2 3)
1989@result{}
1990(lambda (proc) (proc 1 2 3))
1991@end example
1992
1993@item <...>
1994A slot to be filled by all remaining arguments from the new procedure.
1995This can only occur at the end of a @code{cut} form.
1996
1997For example, a procedure taking a variable number of arguments like
1998@code{max} but in addition enforcing a lower bound,
1999
2000@example
2001(define my-lower-bound 123)
2002
2003(cut max my-lower-bound <...>)
2004@result{}
2005(lambda arglist (apply max my-lower-bound arglist))
2006@end example
2007@end table
2008
2009For @code{cut} the specializing expressions are evaluated each time
2010the new procedure is called. For @code{cute} they're evaluated just
2011once, when the new procedure is created. The name @code{cute} stands
2012for ``@code{cut} with evaluated arguments''. In all cases the
2013evaluations take place in an unspecified order.
2014
2015The following illustrates the difference between @code{cut} and
2016@code{cute},
2017
2018@example
2019(cut format <> "the time is ~s" (current-time))
2020@result{}
2021(lambda (port) (format port "the time is ~s" (current-time)))
2022
2023(cute format <> "the time is ~s" (current-time))
2024@result{}
2025(let ((val (current-time)))
2026 (lambda (port) (format port "the time is ~s" val))
2027@end example
2028
2029(There's no provision for a mixture of @code{cut} and @code{cute}
2030where some expressions would be evaluated every time but others
2031evaluated only once.)
2032
2033@code{cut} is really just a shorthand for the sort of @code{lambda}
2034forms shown in the above examples. But notice @code{cut} avoids the
2035need to name unspecialized parameters, and is more compact. Use in
2036functional programming style or just with @code{map}, @code{for-each}
2037or similar is typical.
2038
2039@example
2040(map (cut * 2 <>) '(1 2 3 4))
2041
2042(for-each (cut write <> my-port) my-list)
2043@end example
2044@end deffn
b0b55bd6 2045
8638c417
RB
2046@node SRFI-31
2047@subsection SRFI-31 - A special form `rec' for recursive evaluation
2048@cindex SRFI-31
7c2e18cd 2049@cindex recursive expression
8638c417
RB
2050@findex rec
2051
2052SRFI-31 defines a special form that can be used to create
2053self-referential expressions more conveniently. The syntax is as
2054follows:
2055
2056@example
2057@group
2058<rec expression> --> (rec <variable> <expression>)
2059<rec expression> --> (rec (<variable>+) <body>)
2060@end group
2061@end example
2062
2063The first syntax can be used to create self-referential expressions,
2064for example:
2065
2066@lisp
2067 guile> (define tmp (rec ones (cons 1 (delay ones))))
2068@end lisp
2069
2070The second syntax can be used to create anonymous recursive functions:
2071
2072@lisp
2073 guile> (define tmp (rec (display-n item n)
2074 (if (positive? n)
2075 (begin (display n) (display-n (- n 1))))))
2076 guile> (tmp 42 3)
2077 424242
2078 guile>
2079@end lisp
12991fed 2080
eeadfda1
KR
2081
2082@node SRFI-39
2083@subsection SRFI-39 - Parameters
2084@cindex SRFI-39
2085@cindex parameter object
2086@tindex Parameter
2087
2088This SRFI provides parameter objects, which implement dynamically
2089bound locations for values. The functions below are available from
2090
2091@example
2092(use-modules (srfi srfi-39))
2093@end example
2094
2095A parameter object is a procedure. Called with no arguments it
2096returns its value, called with one argument it sets the value.
2097
2098@example
2099(define my-param (make-parameter 123))
2100(my-param) @result{} 123
2101(my-param 456)
2102(my-param) @result{} 456
2103@end example
2104
2105The @code{parameterize} special form establishes new locations for
2106parameters, those new locations having effect within the dynamic scope
2107of the @code{parameterize} body. Leaving restores the previous
2108locations, or re-entering through a saved continuation will again use
2109the new locations.
2110
2111@example
2112(parameterize ((my-param 789))
2113 (my-param) @result{} 789
2114 )
2115(my-param) @result{} 456
2116@end example
2117
2118Parameters are like dynamically bound variables in other Lisp dialets.
2119They allow an application to establish parameter settings (as the name
2120suggests) just for the execution of a particular bit of code,
2121restoring when done. Examples of such parameters might be
2122case-sensitivity for a search, or a prompt for user input.
2123
2124Global variables are not as good as parameter objects for this sort of
2125thing. Changes to them are visible to all threads, but in Guile
2126parameter object locations are per-thread, thereby truely limiting the
2127effect of @code{parameterize} to just its dynamic execution.
2128
2129Passing arguments to functions is thread-safe, but that soon becomes
2130tedious when there's more than a few or when they need to pass down
2131through several layers of calls before reaching the point they should
2132affect. And introducing a new setting to existing code is often
2133easier with a parameter object than adding arguments.
2134
2135
2136@sp 1
2137@defun make-parameter init [converter]
2138Return a new parameter object, with initial value @var{init}.
2139
2140A parameter object is a procedure. When called @code{(param)} it
2141returns its value, or a call @code{(param val)} sets its value. For
2142example,
2143
2144@example
2145(define my-param (make-parameter 123))
2146(my-param) @result{} 123
2147
2148(my-param 456)
2149(my-param) @result{} 456
2150@end example
2151
2152If a @var{converter} is given, then a call @code{(@var{converter}
2153val)} is made for each value set, its return is the value stored.
2154Such a call is made for the @var{init} initial value too.
2155
2156A @var{converter} allows values to be validated, or put into a
2157canonical form. For example,
2158
2159@example
2160(define my-param (make-parameter 123
2161 (lambda (val)
2162 (if (not (number? val))
2163 (error "must be a number"))
2164 (inexact->exact val))))
2165(my-param 0.75)
2166(my-param) @result{} 3/4
2167@end example
2168@end defun
2169
2170@deffn {library syntax} parameterize ((param value) @dots{}) body @dots{}
2171Establish a new dynamic scope with the given @var{param}s bound to new
2172locations and set to the given @var{value}s. @var{body} is evaluated
2173in that environment, the result is the return from the last form in
2174@var{body}.
2175
2176Each @var{param} is an expression which is evaluated to get the
2177parameter object. Often this will just be the name of a variable
2178holding the object, but it can be anything that evaluates to a
2179parameter.
2180
2181The @var{param} expressions and @var{value} expressions are all
2182evaluated before establishing the new dynamic bindings, and they're
2183evaluated in an unspecified order.
2184
2185For example,
2186
2187@example
2188(define prompt (make-parameter "Type something: "))
2189(define (get-input)
2190 (display (prompt))
2191 ...)
2192
2193(parameterize ((prompt "Type a number: "))
2194 (get-input)
2195 ...)
2196@end example
2197@end deffn
2198
2199@deffn {Parameter object} current-input-port [new-port]
2200@deffnx {Parameter object} current-output-port [new-port]
2201@deffnx {Parameter object} current-error-port [new-port]
2202This SRFI extends the core @code{current-input-port} and
2203@code{current-output-port}, making them parameter objects. The
2204Guile-specific @code{current-error-port} is extended too, for
2205consistency. (@pxref{Default Ports}.)
2206
2207This is an upwardly compatible extension, a plain call like
2208@code{(current-input-port)} still returns the current input port, and
2209@code{set-current-input-port} can still be used. But the port can now
2210also be set with @code{(current-input-port my-port)} and bound
2211dynamically with @code{parameterize}.
2212@end deffn
2213
2214@defun with-parameters* param-list value-list thunk
2215Establish a new dynamic scope, as per @code{parameterize} above,
2216taking parameters from @var{param-list} and corresponding values from
2217@var{values-list}. A call @code{(@var{thunk})} is made in the new
2218scope and the result from that @var{thunk} is the return from
2219@code{with-parameters*}.
2220
2221This function is a Guile-specific addition to the SRFI, it's similar
2222to the core @code{with-fluids*} (@pxref{Fluids}).
2223@end defun
2224
2225
2226@sp 1
2227Parameter objects are implemented using fluids (@pxref{Fluids}), so
2228each dynamic root has it's own parameter locations. That includes the
2229separate locations when outside any @code{parameterize} form. When a
2230parameter is created it gets a separate initial location in each
2231dynamic root, all initialized to the given @var{init} value.
2232
2233As alluded to above, because each thread is a separate dynamic root,
2234each thread has it's own locations behind parameter objects, and
2235changes in one thread are not visible to any other. When a new
2236dynamic root or thread is created, the values of parameters in the
2237originating context are copied, into new locations.
2238
2239SRFI-39 doesn't specify the interaction between parameter objects and
2240threads, so the threading behaviour described here should be regarded
2241as Guile-specific.
2242
2243
12991fed 2244@c srfi-modules.texi ends here
193239f1
KR
2245
2246@c Local Variables:
2247@c TeX-master: "guile.texi"
2248@c End: