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