*** empty log message ***
[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
NJ
726Return the first sublist of @var{lst} whose @sc{car} is equal to
727@var{x}. If @var{x} does no appear in @var{lst}, return @code{#f}.
728Equality is determined by the equality predicate @var{=}, or
729@code{equal?} if @var{=} is not given.
ea6ea01b
KR
730
731This function extends the core @code{member} by accepting an equality
732predicate. (@pxref{List Searching})
a0e07ba4
NJ
733@end deffn
734
735
736@node SRFI-1 Deleting
3229f68b 737@subsubsection Deleting
7c2e18cd 738@cindex list delete
a0e07ba4
NJ
739
740@c FIXME::martin: Review me!
741
8f85c0c6
NJ
742@deffn {Scheme Procedure} delete x lst [=]
743@deffnx {Scheme Procedure} delete! x lst [=]
b6b9376a
KR
744Return a list containing the elements of @var{lst} but with those
745equal to @var{x} deleted. The returned elements will be in the same
746order as they were in @var{lst}.
747
748Equality is determined by the @var{=} predicate, or @code{equal?} if
749not given. An equality call is made just once for each element, but
750the order in which the calls are made on the elements is unspecified.
a0e07ba4 751
243bdb63 752The equality calls are always @code{(= x elem)}, ie.@: the given @var{x}
b6b9376a
KR
753is first. This means for instance elements greater than 5 can be
754deleted with @code{(delete 5 lst <)}.
755
756@code{delete} does not modify @var{lst}, but the return might share a
757common tail with @var{lst}. @code{delete!} may modify the structure
758of @var{lst} to construct its return.
ea6ea01b
KR
759
760These functions extend the core @code{delete} and @code{delete!} in
761accepting an equality predicate. (@pxref{List Modification})
a0e07ba4
NJ
762@end deffn
763
8f85c0c6
NJ
764@deffn {Scheme Procedure} delete-duplicates lst [=]
765@deffnx {Scheme Procedure} delete-duplicates! lst [=]
b6b9376a
KR
766Return a list containing the elements of @var{lst} but without
767duplicates.
768
769When elements are equal, only the first in @var{lst} is retained.
770Equal elements can be anywhere in @var{lst}, they don't have to be
771adjacent. The returned list will have the retained elements in the
772same order as they were in @var{lst}.
773
774Equality is determined by the @var{=} predicate, or @code{equal?} if
775not given. Calls @code{(= x y)} are made with element @var{x} being
776before @var{y} in @var{lst}. A call is made at most once for each
777combination, but the sequence of the calls across the elements is
778unspecified.
779
780@code{delete-duplicates} does not modify @var{lst}, but the return
781might share a common tail with @var{lst}. @code{delete-duplicates!}
782may modify the structure of @var{lst} to construct its return.
783
784In the worst case, this is an @math{O(N^2)} algorithm because it must
785check each element against all those preceding it. For long lists it
786is more efficient to sort and then compare only adjacent elements.
a0e07ba4
NJ
787@end deffn
788
789
790@node SRFI-1 Association Lists
3229f68b 791@subsubsection Association Lists
7c2e18cd
KR
792@cindex association list
793@cindex alist
a0e07ba4
NJ
794
795@c FIXME::martin: Review me!
796
797Association lists are described in detail in section @ref{Association
798Lists}. The present section only documents the additional procedures
799for dealing with association lists defined by SRFI-1.
800
8f85c0c6 801@deffn {Scheme Procedure} assoc key alist [=]
a0e07ba4
NJ
802Return the pair from @var{alist} which matches @var{key}. Equality is
803determined by @var{=}, which defaults to @code{equal?} if not given.
804@var{alist} must be an association lists---a list of pairs.
ea6ea01b
KR
805
806This function extends the core @code{assoc} by accepting an equality
807predicate. (@pxref{Association Lists})
a0e07ba4
NJ
808@end deffn
809
8f85c0c6 810@deffn {Scheme Procedure} alist-cons key datum alist
a0e07ba4
NJ
811Equivalent to
812
813@lisp
814(cons (cons @var{key} @var{datum}) @var{alist})
815@end lisp
816
817This procedure is used to coons a new pair onto an existing
818association list.
819@end deffn
820
8f85c0c6 821@deffn {Scheme Procedure} alist-copy alist
a0e07ba4
NJ
822Return a newly allocated copy of @var{alist}, that means that the
823spine of the list as well as the pairs are copied.
824@end deffn
825
8f85c0c6
NJ
826@deffn {Scheme Procedure} alist-delete key alist [=]
827@deffnx {Scheme Procedure} alist-delete! key alist [=]
bd35f1f0
KR
828Return a list containing the elements of @var{alist} but with those
829elements whose keys are equal to @var{key} deleted. The returned
830elements will be in the same order as they were in @var{alist}.
a0e07ba4 831
bd35f1f0
KR
832Equality is determined by the @var{=} predicate, or @code{equal?} if
833not given. The order in which elements are tested is unspecified, but
834each equality call is made @code{(= key alistkey)}, ie. the given
835@var{key} parameter is first and the key from @var{alist} second.
836This means for instance all associations with a key greater than 5 can
837be removed with @code{(alist-delete 5 alist <)}.
838
839@code{alist-delete} does not modify @var{alist}, but the return might
840share a common tail with @var{alist}. @code{alist-delete!} may modify
841the list structure of @var{alist} to construct its return.
a0e07ba4
NJ
842@end deffn
843
844
845@node SRFI-1 Set Operations
3229f68b 846@subsubsection Set Operations on Lists
7c2e18cd 847@cindex list set operation
a0e07ba4
NJ
848
849@c FIXME::martin: Review me!
850
851Lists can be used for representing sets of objects. The procedures
852documented in this section can be used for such set representations.
85a9b4ed 853Man combining several sets or adding elements, they make sure that no
a0e07ba4
NJ
854object is contained more than once in a given list. Please note that
855lists are not a too efficient implementation method for sets, so if
856you need high performance, you should think about implementing a
857custom data structure for representing sets, such as trees, bitsets,
858hash tables or something similar.
859
860All these procedures accept an equality predicate as the first
861argument. This predicate is used for testing the objects in the list
862sets for sameness.
863
8f85c0c6 864@deffn {Scheme Procedure} lset<= = list1 @dots{}
a0e07ba4
NJ
865Return @code{#t} if every @var{listi} is a subset of @var{listi+1},
866otherwise return @code{#f}. Returns @code{#t} if called with less
867than two arguments. @var{=} is used for testing element equality.
868@end deffn
869
8f85c0c6 870@deffn {Scheme Procedure} lset= = list1 list2 @dots{}
a0e07ba4
NJ
871Return @code{#t} if all argument lists are equal. @var{=} is used for
872testing element equality.
873@end deffn
874
8f85c0c6
NJ
875@deffn {Scheme Procedure} lset-adjoin = list elt1 @dots{}
876@deffnx {Scheme Procedure} lset-adjoin! = list elt1 @dots{}
a0e07ba4
NJ
877Add all @var{elts} to the list @var{list}, suppressing duplicates and
878return the resulting list. @code{lset-adjoin!} is allowed, but not
879required to modify its first argument. @var{=} is used for testing
880element equality.
881@end deffn
882
8f85c0c6
NJ
883@deffn {Scheme Procedure} lset-union = list1 @dots{}
884@deffnx {Scheme Procedure} lset-union! = list1 @dots{}
a0e07ba4
NJ
885Return the union of all argument list sets. The union is the set of
886all elements which appear in any of the argument sets.
887@code{lset-union!} is allowed, but not required to modify its first
888argument. @var{=} is used for testing element equality.
889@end deffn
890
8f85c0c6
NJ
891@deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{}
892@deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{}
a0e07ba4
NJ
893Return the intersection of all argument list sets. The intersection
894is the set containing all elements which appear in all argument sets.
895@code{lset-intersection!} is allowed, but not required to modify its
896first argument. @var{=} is used for testing element equality.
897@end deffn
898
8f85c0c6
NJ
899@deffn {Scheme Procedure} lset-difference = list1 list2 @dots{}
900@deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{}
a0e07ba4
NJ
901Return the difference of all argument list sets. The difference is
902the the set containing all elements of the first list which do not
903appear in the other lists. @code{lset-difference!} is allowed, but
904not required to modify its first argument. @var{=} is used for testing
905element equality.
906@end deffn
907
8f85c0c6
NJ
908@deffn {Scheme Procedure} lset-xor = list1 @dots{}
909@deffnx {Scheme Procedure} lset-xor! = list1 @dots{}
a0e07ba4
NJ
910Return the set containing all elements which appear in the first
911argument list set, but not in the second; or, more generally: which
912appear in an odd number of sets. @code{lset-xor!} is allowed, but
913not required to modify its first argument. @var{=} is used for testing
914element equality.
915@end deffn
916
8f85c0c6
NJ
917@deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{}
918@deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{}
a0e07ba4
NJ
919Return two values, the difference and the intersection of the argument
920list sets. This works like a combination of @code{lset-difference} and
921@code{lset-intersection}, but is more efficient.
922@code{lset-diff+intersection!} is allowed, but not required to modify
923its first argument. @var{=} is used for testing element equality. You
924have to use some means to deal with the multiple values these
925procedures return (@pxref{Multiple Values}).
926@end deffn
927
928
929@node SRFI-2
3229f68b 930@subsection SRFI-2 - and-let*
8742c48b 931@cindex SRFI-2
a0e07ba4 932
4fd0db14
KR
933@noindent
934The following syntax can be obtained with
a0e07ba4 935
4fd0db14
KR
936@lisp
937(use-modules (srfi srfi-2))
938@end lisp
a0e07ba4 939
4fd0db14
KR
940@deffn {library syntax} and-let* (clause @dots{}) body @dots{}
941A combination of @code{and} and @code{let*}.
942
943Each @var{clause} is evaluated in turn, and if @code{#f} is obtained
944then evaluation stops and @code{#f} is returned. If all are
945non-@code{#f} then @var{body} is evaluated and the last form gives the
6b1a6e4c
KR
946return value, or if @var{body} is empty then the result is @code{#t}.
947Each @var{clause} should be one of the following,
4fd0db14
KR
948
949@table @code
950@item (symbol expr)
951Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}.
952Like @code{let*}, that binding is available to subsequent clauses.
953@item (expr)
954Evaluate @var{expr} and check for @code{#f}.
955@item symbol
956Get the value bound to @var{symbol} and check for @code{#f}.
957@end table
a0e07ba4 958
4fd0db14
KR
959Notice that @code{(expr)} has an ``extra'' pair of parentheses, for
960instance @code{((eq? x y))}. One way to remember this is to imagine
961the @code{symbol} in @code{(symbol expr)} is omitted.
a0e07ba4 962
4fd0db14
KR
963@code{and-let*} is good for calculations where a @code{#f} value means
964termination, but where a non-@code{#f} value is going to be needed in
965subsequent expressions.
966
967The following illustrates this, it returns text between brackets
968@samp{[...]} in a string, or @code{#f} if there are no such brackets
969(ie.@: either @code{string-index} gives @code{#f}).
970
971@example
972(define (extract-brackets str)
973 (and-let* ((start (string-index str #\[))
974 (end (string-index str #\] start)))
975 (substring str (1+ start) end)))
976@end example
977
978The following shows plain variables and expressions tested too.
979@code{diagnostic-levels} is taken to be an alist associating a
980diagnostic type with a level. @code{str} is printed only if the type
981is known and its level is high enough.
982
983@example
984(define (show-diagnostic type str)
985 (and-let* (want-diagnostics
986 (level (assq-ref diagnostic-levels type))
987 ((>= level current-diagnostic-level)))
988 (display str)))
989@end example
990
991The advantage of @code{and-let*} is that an extended sequence of
992expressions and tests doesn't require lots of nesting as would arise
993from separate @code{and} and @code{let*}, or from @code{cond} with
994@code{=>}.
995
996@end deffn
a0e07ba4
NJ
997
998
999@node SRFI-4
3229f68b 1000@subsection SRFI-4 - Homogeneous numeric vector datatypes
8742c48b 1001@cindex SRFI-4
a0e07ba4 1002
e6b226b9 1003The SRFI-4 procedures and data types are always available, @xref{Uniform
3dd6e0cf 1004Numeric Vectors}.
a0e07ba4
NJ
1005
1006@node SRFI-6
3229f68b 1007@subsection SRFI-6 - Basic String Ports
8742c48b 1008@cindex SRFI-6
a0e07ba4
NJ
1009
1010SRFI-6 defines the procedures @code{open-input-string},
1011@code{open-output-string} and @code{get-output-string}. These
1012procedures are included in the Guile core, so using this module does not
1013make any difference at the moment. But it is possible that support for
1014SRFI-6 will be factored out of the core library in the future, so using
1015this module does not hurt, after all.
1016
1017@node SRFI-8
3229f68b 1018@subsection SRFI-8 - receive
8742c48b 1019@cindex SRFI-8
a0e07ba4
NJ
1020
1021@code{receive} is a syntax for making the handling of multiple-value
1022procedures easier. It is documented in @xref{Multiple Values}.
1023
1024
1025@node SRFI-9
3229f68b 1026@subsection SRFI-9 - define-record-type
8742c48b 1027@cindex SRFI-9
7c2e18cd 1028@cindex record
a0e07ba4 1029
6afe385d
KR
1030This SRFI is a syntax for defining new record types and creating
1031predicate, constructor, and field getter and setter functions. In
1032Guile this is simply an alternate interface to the core record
1033functionality (@pxref{Records}). It can be used with,
a0e07ba4 1034
6afe385d
KR
1035@example
1036(use-modules (srfi srfi-9))
1037@end example
1038
1039@deffn {library syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
1040@sp 1
1041Create a new record type, and make various @code{define}s for using
1042it. This syntax can only occur at the top-level, not nested within
1043some other form.
1044
1045@var{type} is bound to the record type, which is as per the return
1046from the core @code{make-record-type}. @var{type} also provides the
1047name for the record, as per @code{record-type-name}.
1048
1049@var{constructor} is bound to a function to be called as
1050@code{(@var{constructor} fieldval @dots{})} to create a new record of
1051this type. The arguments are initial values for the fields, one
1052argument for each field, in the order they appear in the
1053@code{define-record-type} form.
1054
1055The @var{fieldname}s provide the names for the record fields, as per
1056the core @code{record-type-fields} etc, and are referred to in the
1057subsequent accessor/modifier forms.
1058
1059@var{predictate} is bound to a function to be called as
1060@code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f}
1061according to whether @var{obj} is a record of this type.
1062
1063Each @var{accessor} is bound to a function to be called
1064@code{(@var{accessor} record)} to retrieve the respective field from a
1065@var{record}. Similarly each @var{modifier} is bound to a function to
1066be called @code{(@var{modifier} record val)} to set the respective
1067field in a @var{record}.
1068@end deffn
1069
1070@noindent
1071An example will illustrate typical usage,
a0e07ba4
NJ
1072
1073@example
6afe385d
KR
1074(define-record-type employee-type
1075 (make-employee name age salary)
1076 employee?
1077 (name get-employee-name)
1078 (age get-employee-age set-employee-age)
1079 (salary get-employee-salary set-employee-salary))
a0e07ba4
NJ
1080@end example
1081
6afe385d
KR
1082This creates a new employee data type, with name, age and salary
1083fields. Accessor functions are created for each field, but no
1084modifier function for the name (the intention in this example being
1085that it's established only when an employee object is created). These
1086can all then be used as for example,
a0e07ba4
NJ
1087
1088@example
6afe385d
KR
1089employee-type @result{} #<record-type employee-type>
1090
1091(define fred (make-employee "Fred" 45 20000.00))
1092
1093(employee? fred) @result{} #t
1094(get-employee-age fred) @result{} 45
1095(set-employee-salary fred 25000.00) ;; pay rise
a0e07ba4
NJ
1096@end example
1097
6afe385d
KR
1098The functions created by @code{define-record-type} are ordinary
1099top-level @code{define}s. They can be redefined or @code{set!} as
1100desired, exported from a module, etc.
1101
a0e07ba4
NJ
1102
1103@node SRFI-10
3229f68b 1104@subsection SRFI-10 - Hash-Comma Reader Extension
8742c48b 1105@cindex SRFI-10
a0e07ba4
NJ
1106
1107@cindex hash-comma
1108@cindex #,()
633acbe2
KR
1109This SRFI implements a reader extension @code{#,()} called hash-comma.
1110It allows the reader to give new kinds of objects, for use both in
1111data and as constants or literals in source code. This feature is
1112available with
a0e07ba4 1113
633acbe2
KR
1114@example
1115(use-modules (srfi srfi-10))
1116@end example
1117
1118@noindent
1119The new read syntax is of the form
a0e07ba4
NJ
1120
1121@example
633acbe2 1122#,(@var{tag} @var{arg}@dots{})
a0e07ba4
NJ
1123@end example
1124
633acbe2
KR
1125@noindent
1126where @var{tag} is a symbol and the @var{arg}s are objects taken as
1127parameters. @var{tag}s are registered with the following procedure.
a0e07ba4 1128
633acbe2
KR
1129@deffn {Scheme Procedure} define-reader-ctor tag proc
1130Register @var{proc} as the constructor for a hash-comma read syntax
1131starting with symbol @var{tag}, ie. @nicode{#,(@var{tag} arg@dots{})}.
1132@var{proc} is called with the given arguments @code{(@var{proc}
1133arg@dots{})} and the object it returns is the result of the read.
1134@end deffn
a0e07ba4 1135
633acbe2
KR
1136@noindent
1137For example, a syntax giving a list of @var{N} copies of an object.
1138
1139@example
1140(define-reader-ctor 'repeat
1141 (lambda (obj reps)
1142 (make-list reps obj)))
1143
1144(display '#,(repeat 99 3))
1145@print{} (99 99 99)
1146@end example
1147
1148Notice the quote @nicode{'} when the @nicode{#,( )} is used. The
1149@code{repeat} handler returns a list and the program must quote to use
1150it literally, the same as any other list. Ie.
1151
1152@example
1153(display '#,(repeat 99 3))
a0e07ba4 1154@result{}
633acbe2
KR
1155(display '(99 99 99))
1156@end example
a0e07ba4 1157
633acbe2
KR
1158When a handler returns an object which is self-evaluating, like a
1159number or a string, then there's no need for quoting, just as there's
1160no need when giving those directly as literals. For example an
1161addition,
a0e07ba4 1162
633acbe2
KR
1163@example
1164(define-reader-ctor 'sum
1165 (lambda (x y)
1166 (+ x y)))
1167(display #,(sum 123 456)) @print{} 579
1168@end example
1169
1170A typical use for @nicode{#,()} is to get a read syntax for objects
1171which don't otherwise have one. For example, the following allows a
1172hash table to be given literally, with tags and values, ready for fast
1173lookup.
1174
1175@example
1176(define-reader-ctor 'hash
1177 (lambda elems
1178 (let ((table (make-hash-table)))
1179 (for-each (lambda (elem)
1180 (apply hash-set! table elem))
1181 elems)
1182 table)))
1183
1184(define (animal->family animal)
1185 (hash-ref '#,(hash ("tiger" "cat")
1186 ("lion" "cat")
1187 ("wolf" "dog"))
1188 animal))
1189
1190(animal->family "lion") @result{} "cat"
1191@end example
1192
1193Or for example the following is a syntax for a compiled regular
1194expression (@pxref{Regular Expressions}).
1195
1196@example
1197(use-modules (ice-9 regex))
1198
1199(define-reader-ctor 'regexp make-regexp)
1200
1201(define (extract-angs str)
1202 (let ((match (regexp-exec '#,(regexp "<([A-Z0-9]+)>") str)))
1203 (and match
1204 (match:substring match 1))))
1205
1206(extract-angs "foo <BAR> quux") @result{} "BAR"
1207@end example
1208
1209@sp 1
1210@nicode{#,()} is somewhat similar to @code{define-macro}
1211(@pxref{Macros}) in that handler code is run to produce a result, but
1212@nicode{#,()} operates at the read stage, so it can appear in data for
1213@code{read} (@pxref{Scheme Read}), not just in code to be executed.
1214
1215Because @nicode{#,()} is handled at read-time it has no direct access
1216to variables etc. A symbol in the arguments is just a symbol, not a
1217variable reference. The arguments are essentially constants, though
1218the handler procedure can use them in any complicated way it might
1219want.
1220
1221Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
1222globally, there's no need to use @code{(srfi srfi-10)} in later
1223modules. Similarly the tags registered are global and can be used
1224anywhere once registered.
1225
1226There's no attempt to record what previous @nicode{#,()} forms have
1227been seen, if two identical forms occur then two calls are made to the
1228handler procedure. The handler might like to maintain a cache or
1229similar to avoid making copies of large objects, depending on expected
1230usage.
1231
1232In code the best uses of @nicode{#,()} are generally when there's a
1233lot of objects of a particular kind as literals or constants. If
1234there's just a few then some local variables and initializers are
1235fine, but that becomes tedious and error prone when there's a lot, and
1236the anonymous and compact syntax of @nicode{#,()} is much better.
a0e07ba4
NJ
1237
1238
1239@node SRFI-11
3229f68b 1240@subsection SRFI-11 - let-values
8742c48b 1241@cindex SRFI-11
a0e07ba4 1242
8742c48b
KR
1243@findex let-values
1244@findex let-values*
a0e07ba4
NJ
1245This module implements the binding forms for multiple values
1246@code{let-values} and @code{let-values*}. These forms are similar to
1247@code{let} and @code{let*} (@pxref{Local Bindings}), but they support
1248binding of the values returned by multiple-valued expressions.
1249
1250Write @code{(use-modules (srfi srfi-11))} to make the bindings
1251available.
1252
1253@lisp
1254(let-values (((x y) (values 1 2))
1255 ((z f) (values 3 4)))
1256 (+ x y z f))
1257@result{}
125810
1259@end lisp
1260
1261@code{let-values} performs all bindings simultaneously, which means that
1262no expression in the binding clauses may refer to variables bound in the
1263same clause list. @code{let-values*}, on the other hand, performs the
1264bindings sequentially, just like @code{let*} does for single-valued
1265expressions.
1266
1267
1268@node SRFI-13
3229f68b 1269@subsection SRFI-13 - String Library
8742c48b 1270@cindex SRFI-13
a0e07ba4 1271
5676b4fa 1272The SRFI-13 procedures are always available, @xref{Strings}.
a0e07ba4
NJ
1273
1274@node SRFI-14
3229f68b 1275@subsection SRFI-14 - Character-set Library
8742c48b 1276@cindex SRFI-14
a0e07ba4 1277
050ab45f
MV
1278The SRFI-14 data type and procedures are always available,
1279@xref{Character Sets}.
a0e07ba4
NJ
1280
1281@node SRFI-16
3229f68b 1282@subsection SRFI-16 - case-lambda
8742c48b 1283@cindex SRFI-16
7c2e18cd
KR
1284@cindex variable arity
1285@cindex arity, variable
a0e07ba4
NJ
1286
1287@c FIXME::martin: Review me!
1288
8742c48b 1289@findex case-lambda
a0e07ba4
NJ
1290The syntactic form @code{case-lambda} creates procedures, just like
1291@code{lambda}, but has syntactic extensions for writing procedures of
1292varying arity easier.
1293
1294The syntax of the @code{case-lambda} form is defined in the following
1295EBNF grammar.
1296
1297@example
1298@group
1299<case-lambda>
1300 --> (case-lambda <case-lambda-clause>)
1301<case-lambda-clause>
1302 --> (<formals> <definition-or-command>*)
1303<formals>
1304 --> (<identifier>*)
1305 | (<identifier>* . <identifier>)
1306 | <identifier>
1307@end group
1308@end example
1309
1310The value returned by a @code{case-lambda} form is a procedure which
1311matches the number of actual arguments against the formals in the
1312various clauses, in order. @dfn{Formals} means a formal argument list
1313just like with @code{lambda} (@pxref{Lambda}). The first matching clause
1314is selected, the corresponding values from the actual parameter list are
1315bound to the variable names in the clauses and the body of the clause is
1316evaluated. If no clause matches, an error is signalled.
1317
1318The following (silly) definition creates a procedure @var{foo} which
1319acts differently, depending on the number of actual arguments. If one
1320argument is given, the constant @code{#t} is returned, two arguments are
1321added and if more arguments are passed, their product is calculated.
1322
1323@lisp
1324(define foo (case-lambda
1325 ((x) #t)
1326 ((x y) (+ x y))
1327 (z
1328 (apply * z))))
1329(foo 'bar)
1330@result{}
1331#t
1332(foo 2 4)
1333@result{}
13346
1335(foo 3 3 3)
1336@result{}
133727
1338(foo)
1339@result{}
13401
1341@end lisp
1342
1343The last expression evaluates to 1 because the last clause is matched,
1344@var{z} is bound to the empty list and the following multiplication,
1345applied to zero arguments, yields 1.
1346
1347
1348@node SRFI-17
3229f68b 1349@subsection SRFI-17 - Generalized set!
8742c48b 1350@cindex SRFI-17
a0e07ba4
NJ
1351
1352This is an implementation of SRFI-17: Generalized set!
1353
8742c48b 1354@findex getter-with-setter
a0e07ba4
NJ
1355It exports the Guile procedure @code{make-procedure-with-setter} under
1356the SRFI name @code{getter-with-setter} and exports the standard
1357procedures @code{car}, @code{cdr}, @dots{}, @code{cdddr},
1358@code{string-ref} and @code{vector-ref} as procedures with setters, as
1359required by the SRFI.
1360
1361SRFI-17 was heavily criticized during its discussion period but it was
1362finalized anyway. One issue was its concept of globally associating
1363setter @dfn{properties} with (procedure) values, which is non-Schemy.
1364For this reason, this implementation chooses not to provide a way to set
1365the setter of a procedure. In fact, @code{(set! (setter @var{proc})
1366@var{setter})} signals an error. The only way to attach a setter to a
1367procedure is to create a new object (a @dfn{procedure with setter}) via
1368the @code{getter-with-setter} procedure. This procedure is also
1369specified in the SRFI. Using it avoids the described problems.
1370
12991fed
TTN
1371
1372@node SRFI-19
3229f68b 1373@subsection SRFI-19 - Time/Date Library
8742c48b 1374@cindex SRFI-19
7c2e18cd
KR
1375@cindex time
1376@cindex date
12991fed 1377
85600a0f
KR
1378This is an implementation of the SRFI-19 time/date library. The
1379functions and variables described here are provided by
12991fed
TTN
1380
1381@example
85600a0f 1382(use-modules (srfi srfi-19))
12991fed
TTN
1383@end example
1384
85600a0f
KR
1385@menu
1386* SRFI-19 Introduction::
1387* SRFI-19 Time::
1388* SRFI-19 Date::
1389* SRFI-19 Time/Date conversions::
1390* SRFI-19 Date to string::
1391* SRFI-19 String to date::
1392@end menu
12991fed 1393
85600a0f 1394@node SRFI-19 Introduction
3229f68b 1395@subsubsection SRFI-19 Introduction
85600a0f
KR
1396
1397@cindex universal time
1398@cindex atomic time
1399@cindex UTC
1400@cindex TAI
1401This module implements time and date representations and calculations,
1402in various time systems, including universal time (UTC) and atomic
1403time (TAI).
1404
1405For those not familiar with these time systems, TAI is based on a
1406fixed length second derived from oscillations of certain atoms. UTC
1407differs from TAI by an integral number of seconds, which is increased
1408or decreased at announced times to keep UTC aligned to a mean solar
1409day (the orbit and rotation of the earth are not quite constant).
1410
1411@cindex leap second
1412So far, only increases in the TAI
1413@tex
1414$\leftrightarrow$
1415@end tex
1416@ifnottex
1417<->
1418@end ifnottex
1419UTC difference have been needed. Such an increase is a ``leap
1420second'', an extra second of TAI introduced at the end of a UTC day.
1421When working entirely within UTC this is never seen, every day simply
1422has 86400 seconds. But when converting from TAI to a UTC date, an
1423extra 23:59:60 is present, where normally a day would end at 23:59:59.
1424Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
1425seconds.
1426
1427@cindex system clock
1428In the current implementation, the system clock is assumed to be UTC,
1429and a table of leap seconds in the code converts to TAI. See comments
1430in @file{srfi-19.scm} for how to update this table.
1431
1432@cindex julian day
1433@cindex modified julian day
1434Also, for those not familiar with the terminology, a @dfn{Julian Day}
1435is a real number which is a count of days and fraction of a day, in
1436UTC, starting from -4713-01-01T12:00:00Z, ie.@: midday Monday 1 Jan
7c2e18cd
KR
14374713 B.C. A @dfn{Modified Julian Day} is the same, but starting from
14381858-11-17T00:00:00Z, ie.@: midnight 17 November 1858 UTC. That time
1439is julian day 2400000.5.
85600a0f
KR
1440
1441@c The SRFI-1 spec says -4714-11-24T12:00:00Z (November 24, -4714 at
1442@c noon, UTC), but this is incorrect. It looks like it might have
1443@c arisen from the code incorrectly treating years a multiple of 100
7c2e18cd 1444@c but not 400 prior to 1582 as non-leap years, where instead the Julian
85600a0f
KR
1445@c calendar should be used so all multiples of 4 before 1582 are leap
1446@c years.
1447
1448
1449@node SRFI-19 Time
3229f68b 1450@subsubsection SRFI-19 Time
85600a0f
KR
1451@cindex time
1452
1453A @dfn{time} object has type, seconds and nanoseconds fields
1454representing a point in time starting from some epoch. This is an
1455arbitrary point in time, not just a time of day. Although times are
1456represented in nanoseconds, the actual resolution may be lower.
1457
1458The following variables hold the possible time types. For instance
1459@code{(current-time time-process)} would give the current CPU process
1460time.
1461
1462@defvar time-utc
1463Universal Coordinated Time (UTC).
1464@cindex UTC
1465@end defvar
12991fed 1466
85600a0f
KR
1467@defvar time-tai
1468International Atomic Time (TAI).
1469@cindex TAI
1470@end defvar
12991fed 1471
85600a0f
KR
1472@defvar time-monotonic
1473Monotonic time, meaning a monotonically increasing time starting from
1474an unspecified epoch.
12991fed 1475
85600a0f
KR
1476Note that in the current implementation @code{time-monotonic} is the
1477same as @code{time-tai}, and unfortunately is therefore affected by
1478adjustments to the system clock. Perhaps this will change in the
1479future.
1480@end defvar
12991fed 1481
85600a0f
KR
1482@defvar time-duration
1483A duration, meaning simply a difference between two times.
1484@end defvar
12991fed 1485
85600a0f
KR
1486@defvar time-process
1487CPU time spent in the current process, starting from when the process
1488began.
1489@cindex process time
1490@end defvar
12991fed 1491
85600a0f
KR
1492@defvar time-thread
1493CPU time spent in the current thread. Not currently implemented.
1494@cindex thread time
1495@end defvar
12991fed 1496
85600a0f
KR
1497@sp 1
1498@defun time? obj
1499Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
1500@end defun
1501
1502@defun make-time type nanoseconds seconds
1503Create a time object with the given @var{type}, @var{seconds} and
1504@var{nanoseconds}.
1505@end defun
1506
1507@defun time-type time
1508@defunx time-nanosecond time
1509@defunx time-second time
1510@defunx set-time-type! time type
1511@defunx set-time-nanosecond! time nsec
1512@defunx set-time-second! time sec
1513Get or set the type, seconds or nanoseconds fields of a time object.
1514
1515@code{set-time-type!} merely changes the field, it doesn't convert the
1516time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
1517@end defun
1518
1519@defun copy-time time
1520Return a new time object, which is a copy of the given @var{time}.
1521@end defun
1522
1523@defun current-time [type]
1524Return the current time of the given @var{type}. The default
1525@var{type} is @code{time-utc}.
1526
1527Note that the name @code{current-time} conflicts with the Guile core
1528@code{current-time} function (@pxref{Time}). Applications wanting to
1529use both will need to use a different name for one of them.
1530@end defun
1531
1532@defun time-resolution [type]
1533Return the resolution, in nanoseconds, of the given time @var{type}.
1534The default @var{type} is @code{time-utc}.
1535@end defun
1536
1537@defun time<=? t1 t2
1538@defunx time<? t1 t2
1539@defunx time=? t1 t2
1540@defunx time>=? t1 t2
1541@defunx time>? t1 t2
1542Return @code{#t} or @code{#f} according to the respective relation
1543between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
1544must be the same time type.
1545@end defun
1546
1547@defun time-difference t1 t2
1548@defunx time-difference! t1 t2
1549Return a time object of type @code{time-duration} representing the
1550period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
1551the same time type.
1552
1553@code{time-difference} returns a new time object,
1554@code{time-difference!} may modify @var{t1} to form its return.
1555@end defun
1556
1557@defun add-duration time duration
1558@defunx add-duration! time duration
1559@defunx subtract-duration time duration
1560@defunx subtract-duration! time duration
1561Return a time object which is @var{time} with the given @var{duration}
1562added or subtracted. @var{duration} must be a time object of type
1563@code{time-duration}.
1564
1565@code{add-duration} and @code{subtract-duration} return a new time
1566object. @code{add-duration!} and @code{subtract-duration!} may modify
1567the given @var{time} to form their return.
1568@end defun
1569
1570
1571@node SRFI-19 Date
3229f68b 1572@subsubsection SRFI-19 Date
85600a0f
KR
1573@cindex date
1574
1575A @dfn{date} object represents a date in the Gregorian calendar and a
1576time of day on that date in some timezone.
1577
1578The fields are year, month, day, hour, minute, second, nanoseconds and
1579timezone. A date object is immutable, its fields can be read but they
1580cannot be modified once the object is created.
1581
1582@defun date? obj
1583Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
1584@end defun
1585
1586@defun make-date nsecs seconds minutes hours date month year zone-offset
1587Create a new date object.
1588@c
1589@c FIXME: What can we say about the ranges of the values. The
1590@c current code looks it doesn't normalize, but expects then in their
1591@c usual range already.
1592@c
1593@end defun
1594
1595@defun date-nanosecond date
1596Nanoseconds, 0 to 999999999.
1597@end defun
1598
1599@defun date-second date
7c2e18cd
KR
1600Seconds, 0 to 59, or 60 for a leap second. 60 is never seen when working
1601entirely within UTC, it's only when converting to or from TAI.
85600a0f
KR
1602@end defun
1603
1604@defun date-minute date
1605Minutes, 0 to 59.
1606@end defun
1607
1608@defun date-hour date
1609Hour, 0 to 23.
1610@end defun
1611
1612@defun date-day date
1613Day of the month, 1 to 31 (or less, according to the month).
1614@end defun
1615
1616@defun date-month date
1617Month, 1 to 12.
1618@end defun
1619
1620@defun date-year date
7c2e18cd
KR
1621Year, eg.@: 2003. Dates B.C.@: are negative, eg.@: @math{-46} is 46
1622B.C. There is no year 0, year @math{-1} is followed by year 1.
85600a0f
KR
1623@end defun
1624
1625@defun date-zone-offset date
1626Time zone, an integer number of seconds east of Greenwich.
1627@end defun
1628
1629@defun date-year-day date
1630Day of the year, starting from 1 for 1st January.
1631@end defun
1632
1633@defun date-week-day date
1634Day of the week, starting from 0 for Sunday.
1635@end defun
1636
1637@defun date-week-number date dstartw
1638Week of the year, ignoring a first partial week. @var{dstartw} is the
1639day of the week which is taken to start a week, 0 for Sunday, 1 for
1640Monday, etc.
1641@c
1642@c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
1643@c The code looks like it's 0, if that's the correct intention.
1644@c
1645@end defun
1646
1647@c The SRFI text doesn't actually give the default for tz-offset, but
1648@c the reference implementation has the local timezone and the
1649@c conversions functions all specify that, so it should be ok to
1650@c document it here.
1651@c
1652@defun current-date [tz-offset]
7c2e18cd
KR
1653Return a date object representing the current date/time, in UTC offset
1654by @var{tz-offset}. @var{tz-offset} is seconds east of Greenwich and
1655defaults to the local timezone.
85600a0f
KR
1656@end defun
1657
1658@defun current-julian-day
1659@cindex julian day
1660Return the current Julian Day.
1661@end defun
1662
1663@defun current-modified-julian-day
1664@cindex modified julian day
1665Return the current Modified Julian Day.
1666@end defun
1667
1668
1669@node SRFI-19 Time/Date conversions
3229f68b 1670@subsubsection SRFI-19 Time/Date conversions
7c2e18cd
KR
1671@cindex time conversion
1672@cindex date conversion
85600a0f
KR
1673
1674@defun date->julian-day date
1675@defunx date->modified-julian-day date
1676@defunx date->time-monotonic date
1677@defunx date->time-tai date
1678@defunx date->time-utc date
1679@end defun
1680@defun julian-day->date jdn [tz-offset]
1681@defunx julian-day->time-monotonic jdn
1682@defunx julian-day->time-tai jdn
1683@defunx julian-day->time-utc jdn
1684@end defun
1685@defun modified-julian-day->date jdn [tz-offset]
1686@defunx modified-julian-day->time-monotonic jdn
1687@defunx modified-julian-day->time-tai jdn
1688@defunx modified-julian-day->time-utc jdn
1689@end defun
1690@defun time-monotonic->date time [tz-offset]
1691@defunx time-monotonic->time-tai time
1692@defunx time-monotonic->time-tai! time
1693@defunx time-monotonic->time-utc time
1694@defunx time-monotonic->time-utc! time
1695@end defun
1696@defun time-tai->date time [tz-offset]
1697@defunx time-tai->julian-day time
1698@defunx time-tai->modified-julian-day time
1699@defunx time-tai->time-monotonic time
1700@defunx time-tai->time-monotonic! time
1701@defunx time-tai->time-utc time
1702@defunx time-tai->time-utc! time
1703@end defun
1704@defun time-utc->date time [tz-offset]
1705@defunx time-utc->julian-day time
1706@defunx time-utc->modified-julian-day time
1707@defunx time-utc->time-monotonic time
1708@defunx time-utc->time-monotonic! time
1709@defunx time-utc->time-tai time
1710@defunx time-utc->time-tai! time
1711@sp 1
1712Convert between dates, times and days of the respective types. For
1713instance @code{time-tai->time-utc} accepts a @var{time} object of type
1714@code{time-tai} and returns an object of type @code{time-utc}.
1715
1716For conversions to dates, @var{tz-offset} is seconds east of
7c2e18cd
KR
1717Greenwich. The default is the local timezone, at the given time, as
1718provided by the system.
85600a0f
KR
1719
1720The @code{!} variants may modify their @var{time} argument to form
1721their return. The plain functions create a new object.
1722@end defun
1723
1724@node SRFI-19 Date to string
3229f68b 1725@subsubsection SRFI-19 Date to string
85600a0f 1726@cindex date to string
7c2e18cd 1727@cindex string, from date
85600a0f
KR
1728
1729@defun date->string date [format]
1730Convert a date to a string under the control of a format.
1731@var{format} should be a string containing @samp{~} escapes, which
1732will be expanded as per the following conversion table. The default
1733@var{format} is @samp{~c}, a locale-dependent date and time.
1734
1735Many of these conversion characters are the same as POSIX
1736@code{strftime} (@pxref{Time}), but there are some extras and some
1737variations.
1738
1739@multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
1740@item @nicode{~~} @tab literal ~
1741@item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
1742@item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
1743@item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
1744@item @nicode{~B} @tab locale full month, eg.@: @samp{January}
1745@item @nicode{~c} @tab locale date and time, eg.@: @*
1746@samp{Fri Jul 14 20:28:42-0400 2000}
1747@item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
1748
1749@c Spec says d/m/y, reference implementation says m/d/y.
1750@c Apparently the reference code was the intention, but would like to
1751@c see an errata published for the spec before contradicting it here.
1752@c
1753@c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
1754
1755@item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
1756@item @nicode{~f} @tab seconds and fractional seconds,
1757with locale decimal point, eg.@: @samp{5.2}
1758@item @nicode{~h} @tab same as @nicode{~b}
1759@item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
1760@item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
1761@item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
1762@item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
1763@item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
1764@item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
1765@item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
1766@item @nicode{~n} @tab newline
1767@item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
1768@item @nicode{~p} @tab locale AM or PM
1769@item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
1770@item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
1771@item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
1772(usual limit is 59, 60 is a leap second)
1773@item @nicode{~t} @tab horizontal tab character
1774@item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
1775@item @nicode{~U} @tab week of year, Sunday first day of week,
1776@samp{00} to @samp{52}
1777@item @nicode{~V} @tab week of year, Monday first day of week,
1778@samp{01} to @samp{53}
1779@item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
1780@item @nicode{~W} @tab week of year, Monday first day of week,
1781@samp{00} to @samp{52}
1782
1783@c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
1784@c date. The reference code has ~x as the locale date and ~X as a
1785@c locale time. The rule is apparently that the code should be
1786@c believed, but would like to see an errata for the spec before
1787@c contradicting it here.
1788@c
1789@c @item @nicode{~x} @tab week of year, Monday as first day of week,
1790@c @samp{00} to @samp{53}
1791@c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
1792
1793@item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
1794@item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
1795@item @nicode{~z} @tab time zone, RFC-822 style
1796@item @nicode{~Z} @tab time zone symbol (not currently implemented)
1797@item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
1798@item @nicode{~2} @tab ISO-8601 time+zone, @samp{~k:~M:~S~z}
1799@item @nicode{~3} @tab ISO-8601 time, @samp{~k:~M:~S}
1800@item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~k:~M:~S~z}
1801@item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~k:~M:~S}
1802@end multitable
1803@end defun
1804
1805Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
1806described here, since the specification and reference implementation
1807differ.
1808
1809Currently Guile doesn't implement any localizations for the above, all
1810outputs are in English, and the @samp{~c} conversion is POSIX
1811@code{ctime} style @samp{~a ~b ~d ~H:~M:~S~z ~Y}. This may change in
1812the future.
1813
1814
1815@node SRFI-19 String to date
3229f68b 1816@subsubsection SRFI-19 String to date
85600a0f 1817@cindex string to date
7c2e18cd 1818@cindex date, from string
85600a0f
KR
1819
1820@c FIXME: Can we say what happens when an incomplete date is
1821@c converted? Ie. fields left as 0, or what? The spec seems to be
1822@c silent on this.
1823
1824@defun string->date input template
1825Convert an @var{input} string to a date under the control of a
1826@var{template} string. Return a newly created date object.
1827
1828Literal characters in @var{template} must match characters in
1829@var{input} and @samp{~} escapes must match the input forms described
1830in the table below. ``Skip to'' means characters up to one of the
1831given type are ignored, or ``no skip'' for no skipping. ``Read'' is
1832what's then read, and ``Set'' is the field affected in the date
1833object.
1834
1835For example @samp{~Y} skips input characters until a digit is reached,
1836at which point it expects a year and stores that to the year field of
1837the date.
1838
1839@multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
1840@item
1841@tab Skip to
1842@tab Read
1843@tab Set
1844
1845@item @nicode{~~}
1846@tab no skip
1847@tab literal ~
1848@tab nothing
1849
1850@item @nicode{~a}
1851@tab @nicode{char-alphabetic?}
1852@tab locale abbreviated weekday name
1853@tab nothing
1854
1855@item @nicode{~A}
1856@tab @nicode{char-alphabetic?}
1857@tab locale full weekday name
1858@tab nothing
1859
1860@c Note that the SRFI spec says that ~b and ~B don't set anything,
1861@c but that looks like a mistake. The reference implementation sets
1862@c the month field, which seems sensible and is what we describe
1863@c here.
1864
1865@item @nicode{~b}
1866@tab @nicode{char-alphabetic?}
1867@tab locale abbreviated month name
1868@tab @nicode{date-month}
1869
1870@item @nicode{~B}
1871@tab @nicode{char-alphabetic?}
1872@tab locale full month name
1873@tab @nicode{date-month}
1874
1875@item @nicode{~d}
1876@tab @nicode{char-numeric?}
1877@tab day of month
1878@tab @nicode{date-day}
1879
1880@item @nicode{~e}
1881@tab no skip
1882@tab day of month, blank padded
1883@tab @nicode{date-day}
1884
1885@item @nicode{~h}
1886@tab same as @samp{~b}
1887
1888@item @nicode{~H}
1889@tab @nicode{char-numeric?}
1890@tab hour
1891@tab @nicode{date-hour}
1892
1893@item @nicode{~k}
1894@tab no skip
1895@tab hour, blank padded
1896@tab @nicode{date-hour}
1897
1898@item @nicode{~m}
1899@tab @nicode{char-numeric?}
1900@tab month
1901@tab @nicode{date-month}
1902
1903@item @nicode{~M}
1904@tab @nicode{char-numeric?}
1905@tab minute
1906@tab @nicode{date-minute}
1907
1908@item @nicode{~S}
1909@tab @nicode{char-numeric?}
1910@tab second
1911@tab @nicode{date-second}
1912
1913@item @nicode{~y}
1914@tab no skip
1915@tab 2-digit year
1916@tab @nicode{date-year} within 50 years
1917
1918@item @nicode{~Y}
1919@tab @nicode{char-numeric?}
1920@tab year
1921@tab @nicode{date-year}
1922
1923@item @nicode{~z}
1924@tab no skip
1925@tab time zone
1926@tab date-zone-offset
1927@end multitable
1928
1929Notice that the weekday matching forms don't affect the date object
1930returned, instead the weekday will be derived from the day, month and
1931year.
1932
1933Currently Guile doesn't implement any localizations for the above,
1934month and weekday names are always expected in English. This may
1935change in the future.
1936@end defun
12991fed 1937
1de8c1ae 1938
b0b55bd6 1939@node SRFI-26
3229f68b 1940@subsection SRFI-26 - specializing parameters
1de8c1ae 1941@cindex SRFI-26
7c2e18cd
KR
1942@cindex parameter specialize
1943@cindex argument specialize
1944@cindex specialize parameter
1de8c1ae
KR
1945
1946This SRFI provides a syntax for conveniently specializing selected
1947parameters of a function. It can be used with,
1948
1949@example
1950(use-modules (srfi srfi-26))
1951@end example
1952
1953@deffn {library syntax} cut slot @dots{}
1954@deffnx {library syntax} cute slot @dots{}
1955Return a new procedure which will make a call (@var{slot} @dots{}) but
1956with selected parameters specialized to given expressions.
1957
1958An example will illustrate the idea. The following is a
1959specialization of @code{write}, sending output to
1960@code{my-output-port},
1961
1962@example
1963(cut write <> my-output-port)
1964@result{}
1965(lambda (obj) (write obj my-output-port))
1966@end example
1967
1968The special symbol @code{<>} indicates a slot to be filled by an
1969argument to the new procedure. @code{my-output-port} on the other
1970hand is an expression to be evaluated and passed, ie.@: it specializes
1971the behaviour of @code{write}.
1972
1973@table @nicode
1974@item <>
1975A slot to be filled by an argument from the created procedure.
1976Arguments are assigned to @code{<>} slots in the order they appear in
1977the @code{cut} form, there's no way to re-arrange arguments.
1978
1979The first argument to @code{cut} is usually a procedure (or expression
1980giving a procedure), but @code{<>} is allowed there too. For example,
1981
1982@example
1983(cut <> 1 2 3)
1984@result{}
1985(lambda (proc) (proc 1 2 3))
1986@end example
1987
1988@item <...>
1989A slot to be filled by all remaining arguments from the new procedure.
1990This can only occur at the end of a @code{cut} form.
1991
1992For example, a procedure taking a variable number of arguments like
1993@code{max} but in addition enforcing a lower bound,
1994
1995@example
1996(define my-lower-bound 123)
1997
1998(cut max my-lower-bound <...>)
1999@result{}
2000(lambda arglist (apply max my-lower-bound arglist))
2001@end example
2002@end table
2003
2004For @code{cut} the specializing expressions are evaluated each time
2005the new procedure is called. For @code{cute} they're evaluated just
2006once, when the new procedure is created. The name @code{cute} stands
2007for ``@code{cut} with evaluated arguments''. In all cases the
2008evaluations take place in an unspecified order.
2009
2010The following illustrates the difference between @code{cut} and
2011@code{cute},
2012
2013@example
2014(cut format <> "the time is ~s" (current-time))
2015@result{}
2016(lambda (port) (format port "the time is ~s" (current-time)))
2017
2018(cute format <> "the time is ~s" (current-time))
2019@result{}
2020(let ((val (current-time)))
2021 (lambda (port) (format port "the time is ~s" val))
2022@end example
2023
2024(There's no provision for a mixture of @code{cut} and @code{cute}
2025where some expressions would be evaluated every time but others
2026evaluated only once.)
2027
2028@code{cut} is really just a shorthand for the sort of @code{lambda}
2029forms shown in the above examples. But notice @code{cut} avoids the
2030need to name unspecialized parameters, and is more compact. Use in
2031functional programming style or just with @code{map}, @code{for-each}
2032or similar is typical.
2033
2034@example
2035(map (cut * 2 <>) '(1 2 3 4))
2036
2037(for-each (cut write <> my-port) my-list)
2038@end example
2039@end deffn
b0b55bd6 2040
8638c417
RB
2041@node SRFI-31
2042@subsection SRFI-31 - A special form `rec' for recursive evaluation
2043@cindex SRFI-31
7c2e18cd 2044@cindex recursive expression
8638c417
RB
2045@findex rec
2046
2047SRFI-31 defines a special form that can be used to create
2048self-referential expressions more conveniently. The syntax is as
2049follows:
2050
2051@example
2052@group
2053<rec expression> --> (rec <variable> <expression>)
2054<rec expression> --> (rec (<variable>+) <body>)
2055@end group
2056@end example
2057
2058The first syntax can be used to create self-referential expressions,
2059for example:
2060
2061@lisp
2062 guile> (define tmp (rec ones (cons 1 (delay ones))))
2063@end lisp
2064
2065The second syntax can be used to create anonymous recursive functions:
2066
2067@lisp
2068 guile> (define tmp (rec (display-n item n)
2069 (if (positive? n)
2070 (begin (display n) (display-n (- n 1))))))
2071 guile> (tmp 42 3)
2072 424242
2073 guile>
2074@end lisp
12991fed 2075
eeadfda1
KR
2076
2077@node SRFI-39
2078@subsection SRFI-39 - Parameters
2079@cindex SRFI-39
2080@cindex parameter object
2081@tindex Parameter
2082
2083This SRFI provides parameter objects, which implement dynamically
2084bound locations for values. The functions below are available from
2085
2086@example
2087(use-modules (srfi srfi-39))
2088@end example
2089
2090A parameter object is a procedure. Called with no arguments it
2091returns its value, called with one argument it sets the value.
2092
2093@example
2094(define my-param (make-parameter 123))
2095(my-param) @result{} 123
2096(my-param 456)
2097(my-param) @result{} 456
2098@end example
2099
2100The @code{parameterize} special form establishes new locations for
2101parameters, those new locations having effect within the dynamic scope
2102of the @code{parameterize} body. Leaving restores the previous
2103locations, or re-entering through a saved continuation will again use
2104the new locations.
2105
2106@example
2107(parameterize ((my-param 789))
2108 (my-param) @result{} 789
2109 )
2110(my-param) @result{} 456
2111@end example
2112
2113Parameters are like dynamically bound variables in other Lisp dialets.
2114They allow an application to establish parameter settings (as the name
2115suggests) just for the execution of a particular bit of code,
2116restoring when done. Examples of such parameters might be
2117case-sensitivity for a search, or a prompt for user input.
2118
2119Global variables are not as good as parameter objects for this sort of
2120thing. Changes to them are visible to all threads, but in Guile
2121parameter object locations are per-thread, thereby truely limiting the
2122effect of @code{parameterize} to just its dynamic execution.
2123
2124Passing arguments to functions is thread-safe, but that soon becomes
2125tedious when there's more than a few or when they need to pass down
2126through several layers of calls before reaching the point they should
2127affect. And introducing a new setting to existing code is often
2128easier with a parameter object than adding arguments.
2129
2130
2131@sp 1
2132@defun make-parameter init [converter]
2133Return a new parameter object, with initial value @var{init}.
2134
2135A parameter object is a procedure. When called @code{(param)} it
2136returns its value, or a call @code{(param val)} sets its value. For
2137example,
2138
2139@example
2140(define my-param (make-parameter 123))
2141(my-param) @result{} 123
2142
2143(my-param 456)
2144(my-param) @result{} 456
2145@end example
2146
2147If a @var{converter} is given, then a call @code{(@var{converter}
2148val)} is made for each value set, its return is the value stored.
2149Such a call is made for the @var{init} initial value too.
2150
2151A @var{converter} allows values to be validated, or put into a
2152canonical form. For example,
2153
2154@example
2155(define my-param (make-parameter 123
2156 (lambda (val)
2157 (if (not (number? val))
2158 (error "must be a number"))
2159 (inexact->exact val))))
2160(my-param 0.75)
2161(my-param) @result{} 3/4
2162@end example
2163@end defun
2164
2165@deffn {library syntax} parameterize ((param value) @dots{}) body @dots{}
2166Establish a new dynamic scope with the given @var{param}s bound to new
2167locations and set to the given @var{value}s. @var{body} is evaluated
2168in that environment, the result is the return from the last form in
2169@var{body}.
2170
2171Each @var{param} is an expression which is evaluated to get the
2172parameter object. Often this will just be the name of a variable
2173holding the object, but it can be anything that evaluates to a
2174parameter.
2175
2176The @var{param} expressions and @var{value} expressions are all
2177evaluated before establishing the new dynamic bindings, and they're
2178evaluated in an unspecified order.
2179
2180For example,
2181
2182@example
2183(define prompt (make-parameter "Type something: "))
2184(define (get-input)
2185 (display (prompt))
2186 ...)
2187
2188(parameterize ((prompt "Type a number: "))
2189 (get-input)
2190 ...)
2191@end example
2192@end deffn
2193
2194@deffn {Parameter object} current-input-port [new-port]
2195@deffnx {Parameter object} current-output-port [new-port]
2196@deffnx {Parameter object} current-error-port [new-port]
2197This SRFI extends the core @code{current-input-port} and
2198@code{current-output-port}, making them parameter objects. The
2199Guile-specific @code{current-error-port} is extended too, for
2200consistency. (@pxref{Default Ports}.)
2201
2202This is an upwardly compatible extension, a plain call like
2203@code{(current-input-port)} still returns the current input port, and
2204@code{set-current-input-port} can still be used. But the port can now
2205also be set with @code{(current-input-port my-port)} and bound
2206dynamically with @code{parameterize}.
2207@end deffn
2208
2209@defun with-parameters* param-list value-list thunk
2210Establish a new dynamic scope, as per @code{parameterize} above,
2211taking parameters from @var{param-list} and corresponding values from
2212@var{values-list}. A call @code{(@var{thunk})} is made in the new
2213scope and the result from that @var{thunk} is the return from
2214@code{with-parameters*}.
2215
2216This function is a Guile-specific addition to the SRFI, it's similar
2217to the core @code{with-fluids*} (@pxref{Fluids}).
2218@end defun
2219
2220
2221@sp 1
2222Parameter objects are implemented using fluids (@pxref{Fluids}), so
2223each dynamic root has it's own parameter locations. That includes the
2224separate locations when outside any @code{parameterize} form. When a
2225parameter is created it gets a separate initial location in each
2226dynamic root, all initialized to the given @var{init} value.
2227
2228As alluded to above, because each thread is a separate dynamic root,
2229each thread has it's own locations behind parameter objects, and
2230changes in one thread are not visible to any other. When a new
2231dynamic root or thread is created, the values of parameters in the
2232originating context are copied, into new locations.
2233
2234SRFI-39 doesn't specify the interaction between parameter objects and
2235threads, so the threading behaviour described here should be regarded
2236as Guile-specific.
2237
2238
12991fed 2239@c srfi-modules.texi ends here
193239f1
KR
2240
2241@c Local Variables:
2242@c TeX-master: "guile.texi"
2243@c End: