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