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