Merge remote-tracking branch 'origin/stable-2.0'
[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.
531c9f1d 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011
2da09c3f
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
a0e07ba4 7@node SRFI Support
3229f68b 8@section SRFI Support Modules
8742c48b 9@cindex SRFI
a0e07ba4
NJ
10
11SRFI is an acronym for Scheme Request For Implementation. The SRFI
12documents define a lot of syntactic and procedure extensions to standard
13Scheme as defined in R5RS.
14
15Guile has support for a number of SRFIs. This chapter gives an overview
16over the available SRFIs and some usage hints. For complete
17documentation, design rationales and further examples, we advise you to
18get the relevant SRFI documents from the SRFI home page
4b4b1e0b 19@url{http://srfi.schemers.org/}.
a0e07ba4
NJ
20
21@menu
22* About SRFI Usage:: What to know about Guile's SRFI support.
23* SRFI-0:: cond-expand
24* SRFI-1:: List library.
25* SRFI-2:: and-let*.
26* SRFI-4:: Homogeneous numeric vector datatypes.
27* SRFI-6:: Basic String Ports.
28* SRFI-8:: receive.
29* SRFI-9:: define-record-type.
30* SRFI-10:: Hash-Comma Reader Extension.
c010924a 31* SRFI-11:: let-values and let*-values.
a0e07ba4
NJ
32* SRFI-13:: String library.
33* SRFI-14:: Character-set library.
34* SRFI-16:: case-lambda
35* SRFI-17:: Generalized set!
e68f492a 36* SRFI-18:: Multithreading support
bfc9c8e0 37* SRFI-19:: Time/Date library.
8e9af854 38* SRFI-23:: Error reporting
1de8c1ae 39* SRFI-26:: Specializing parameters
56ec46a7 40* SRFI-27:: Sources of Random Bits
620c8965 41* SRFI-30:: Nested multi-line block comments
8638c417 42* SRFI-31:: A special form `rec' for recursive evaluation
f50ca8da
LC
43* SRFI-34:: Exception handling.
44* SRFI-35:: Conditions.
d4c38221 45* SRFI-37:: args-fold program argument processor
12708eeb 46* SRFI-38:: External Representation for Data With Shared Structure
eeadfda1 47* SRFI-39:: Parameter objects
fdc8fd46 48* SRFI-42:: Eager comprehensions
f16a2007 49* SRFI-45:: Primitives for expressing iterative lazy algorithms
4ea9becb 50* SRFI-55:: Requiring Features.
8503beb8 51* SRFI-60:: Integers as bits.
43ed3b69 52* SRFI-61:: A more general `cond' clause
8175a07e 53* SRFI-67:: Compare procedures
1317062f 54* SRFI-69:: Basic hash tables.
189681f5 55* SRFI-88:: Keyword objects.
922d417b 56* SRFI-98:: Accessing environment variables.
a0e07ba4
NJ
57@end menu
58
59
60@node About SRFI Usage
3229f68b 61@subsection About SRFI Usage
a0e07ba4
NJ
62
63@c FIXME::martin: Review me!
64
65SRFI support in Guile is currently implemented partly in the core
66library, and partly as add-on modules. That means that some SRFIs are
67automatically available when the interpreter is started, whereas the
68other SRFIs require you to use the appropriate support module
12991fed 69explicitly.
a0e07ba4
NJ
70
71There are several reasons for this inconsistency. First, the feature
72checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be
73available immediately, because it must be there when the user wants to
74check for the Scheme implementation, that is, before she can know that
75it is safe to use @code{use-modules} to load SRFI support modules. The
76second reason is that some features defined in SRFIs had been
77implemented in Guile before the developers started to add SRFI
78implementations as modules (for example SRFI-6 (@pxref{SRFI-6})). In
79the future, it is possible that SRFIs in the core library might be
80factored out into separate modules, requiring explicit module loading
81when they are needed. So you should be prepared to have to use
82@code{use-modules} someday in the future to access SRFI-6 bindings. If
83you want, you can do that already. We have included the module
84@code{(srfi srfi-6)} in the distribution, which currently does nothing,
85but ensures that you can write future-safe code.
86
87Generally, support for a specific SRFI is made available by using
88modules named @code{(srfi srfi-@var{number})}, where @var{number} is the
89number of the SRFI needed. Another possibility is to use the command
90line option @code{--use-srfi}, which will load the necessary modules
91automatically (@pxref{Invoking Guile}).
92
93
94@node SRFI-0
3229f68b 95@subsection SRFI-0 - cond-expand
8742c48b 96@cindex SRFI-0
a0e07ba4 97
5eef0f61
KR
98This SRFI lets a portable Scheme program test for the presence of
99certain features, and adapt itself by using different blocks of code,
100or fail if the necessary features are not available. There's no
101module to load, this is in the Guile core.
a0e07ba4 102
5eef0f61
KR
103A program designed only for Guile will generally not need this
104mechanism, such a program can of course directly use the various
105documented parts of Guile.
a0e07ba4 106
5eef0f61
KR
107@deffn syntax cond-expand (feature body@dots{}) @dots{}
108Expand to the @var{body} of the first clause whose @var{feature}
109specification is satisfied. It is an error if no @var{feature} is
a0e07ba4
NJ
110satisfied.
111
5eef0f61
KR
112Features are symbols such as @code{srfi-1}, and a feature
113specification can use @code{and}, @code{or} and @code{not} forms to
114test combinations. The last clause can be an @code{else}, to be used
115if no other passes.
a0e07ba4 116
5eef0f61
KR
117For example, define a private version of @code{alist-cons} if SRFI-1
118is not available.
a0e07ba4 119
5eef0f61
KR
120@example
121(cond-expand (srfi-1
122 )
123 (else
124 (define (alist-cons key val alist)
125 (cons (cons key val) alist))))
126@end example
a0e07ba4 127
5eef0f61
KR
128Or demand a certain set of SRFIs (list operations, string ports,
129@code{receive} and string operations), failing if they're not
130available.
a0e07ba4 131
5eef0f61
KR
132@example
133(cond-expand ((and srfi-1 srfi-6 srfi-8 srfi-13)
134 ))
135@end example
136@end deffn
a0e07ba4 137
f38d22c5
KR
138@noindent
139The Guile core has the following features,
140
141@example
142guile
60c8ad9e 143guile-2 ;; starting from Guile 2.x
f38d22c5
KR
144r5rs
145srfi-0
146srfi-4
147srfi-6
148srfi-13
149srfi-14
150@end example
151
152Other SRFI feature symbols are defined once their code has been loaded
153with @code{use-modules}, since only then are their bindings available.
a0e07ba4 154
5eef0f61
KR
155The @samp{--use-srfi} command line option (@pxref{Invoking Guile}) is
156a good way to load SRFIs to satisfy @code{cond-expand} when running a
157portable program.
a0e07ba4 158
5eef0f61
KR
159Testing the @code{guile} feature allows a program to adapt itself to
160the Guile module system, but still run on other Scheme systems. For
161example the following demands SRFI-8 (@code{receive}), but also knows
162how to load it with the Guile mechanism.
a0e07ba4
NJ
163
164@example
5eef0f61
KR
165(cond-expand (srfi-8
166 )
167 (guile
168 (use-modules (srfi srfi-8))))
a0e07ba4
NJ
169@end example
170
60c8ad9e
LC
171@cindex @code{guile-2} SRFI-0 feature
172@cindex portability between 2.0 and older versions
173Likewise, testing the @code{guile-2} feature allows code to be portable
0c81a0c1
AW
174between Guile 2.@var{x} and previous versions of Guile. For instance, it
175makes it possible to write code that accounts for Guile 2.@var{x}'s compiler,
60c8ad9e
LC
176yet be correctly interpreted on 1.8 and earlier versions:
177
178@example
179(cond-expand (guile-2 (eval-when (compile)
180 ;; This must be evaluated at compile time.
181 (fluid-set! current-reader my-reader)))
182 (guile
183 ;; Earlier versions of Guile do not have a
184 ;; separate compilation phase.
185 (fluid-set! current-reader my-reader)))
186@end example
187
5eef0f61
KR
188It should be noted that @code{cond-expand} is separate from the
189@code{*features*} mechanism (@pxref{Feature Tracking}), feature
190symbols in one are unrelated to those in the other.
a0e07ba4
NJ
191
192
193@node SRFI-1
3229f68b 194@subsection SRFI-1 - List library
8742c48b 195@cindex SRFI-1
7c2e18cd 196@cindex list
a0e07ba4
NJ
197
198@c FIXME::martin: Review me!
199
200The list library defined in SRFI-1 contains a lot of useful list
201processing procedures for construction, examining, destructuring and
202manipulating lists and pairs.
203
204Since SRFI-1 also defines some procedures which are already contained
205in R5RS and thus are supported by the Guile core library, some list
206and pair procedures which appear in the SRFI-1 document may not appear
207in this section. So when looking for a particular list/pair
208processing procedure, you should also have a look at the sections
209@ref{Lists} and @ref{Pairs}.
210
211@menu
212* SRFI-1 Constructors:: Constructing new lists.
213* SRFI-1 Predicates:: Testing list for specific properties.
214* SRFI-1 Selectors:: Selecting elements from lists.
215* SRFI-1 Length Append etc:: Length calculation and list appending.
216* SRFI-1 Fold and Map:: Higher-order list processing.
217* SRFI-1 Filtering and Partitioning:: Filter lists based on predicates.
85a9b4ed 218* SRFI-1 Searching:: Search for elements.
a0e07ba4
NJ
219* SRFI-1 Deleting:: Delete elements from lists.
220* SRFI-1 Association Lists:: Handle association lists.
221* SRFI-1 Set Operations:: Use lists for representing sets.
222@end menu
223
224@node SRFI-1 Constructors
3229f68b 225@subsubsection Constructors
7c2e18cd 226@cindex list constructor
a0e07ba4
NJ
227
228@c FIXME::martin: Review me!
229
230New lists can be constructed by calling one of the following
231procedures.
232
8f85c0c6 233@deffn {Scheme Procedure} xcons d a
a0e07ba4
NJ
234Like @code{cons}, but with interchanged arguments. Useful mostly when
235passed to higher-order procedures.
236@end deffn
237
8f85c0c6 238@deffn {Scheme Procedure} list-tabulate n init-proc
a0e07ba4
NJ
239Return an @var{n}-element list, where each list element is produced by
240applying the procedure @var{init-proc} to the corresponding list
241index. The order in which @var{init-proc} is applied to the indices
242is not specified.
243@end deffn
244
57066448
KR
245@deffn {Scheme Procedure} list-copy lst
246Return a new list containing the elements of the list @var{lst}.
247
248This function differs from the core @code{list-copy} (@pxref{List
249Constructors}) in accepting improper lists too. And if @var{lst} is
250not a pair at all then it's treated as the final tail of an improper
251list and simply returned.
252@end deffn
253
8f85c0c6 254@deffn {Scheme Procedure} circular-list elt1 elt2 @dots{}
a0e07ba4
NJ
255Return a circular list containing the given arguments @var{elt1}
256@var{elt2} @dots{}.
257@end deffn
258
8f85c0c6 259@deffn {Scheme Procedure} iota count [start step]
256853db
KR
260Return a list containing @var{count} numbers, starting from
261@var{start} and adding @var{step} each time. The default @var{start}
262is 0, the default @var{step} is 1. For example,
a0e07ba4 263
256853db
KR
264@example
265(iota 6) @result{} (0 1 2 3 4 5)
266(iota 4 2.5 -2) @result{} (2.5 0.5 -1.5 -3.5)
267@end example
a0e07ba4 268
256853db
KR
269This function takes its name from the corresponding primitive in the
270APL language.
a0e07ba4
NJ
271@end deffn
272
273
274@node SRFI-1 Predicates
3229f68b 275@subsubsection Predicates
7c2e18cd 276@cindex list predicate
a0e07ba4
NJ
277
278@c FIXME::martin: Review me!
279
280The procedures in this section test specific properties of lists.
281
8f85c0c6 282@deffn {Scheme Procedure} proper-list? obj
f18f87aa
KR
283Return @code{#t} if @var{obj} is a proper list, or @code{#f}
284otherwise. This is the same as the core @code{list?} (@pxref{List
285Predicates}).
286
287A proper list is a list which ends with the empty list @code{()} in
288the usual way. The empty list @code{()} itself is a proper list too.
289
290@example
291(proper-list? '(1 2 3)) @result{} #t
292(proper-list? '()) @result{} #t
293@end example
a0e07ba4
NJ
294@end deffn
295
8f85c0c6 296@deffn {Scheme Procedure} circular-list? obj
f18f87aa
KR
297Return @code{#t} if @var{obj} is a circular list, or @code{#f}
298otherwise.
299
300A circular list is a list where at some point the @code{cdr} refers
301back to a previous pair in the list (either the start or some later
302point), so that following the @code{cdr}s takes you around in a
303circle, with no end.
304
305@example
306(define x (list 1 2 3 4))
307(set-cdr! (last-pair x) (cddr x))
308x @result{} (1 2 3 4 3 4 3 4 ...)
309(circular-list? x) @result{} #t
310@end example
a0e07ba4
NJ
311@end deffn
312
8f85c0c6 313@deffn {Scheme Procedure} dotted-list? obj
f18f87aa
KR
314Return @code{#t} if @var{obj} is a dotted list, or @code{#f}
315otherwise.
316
317A dotted list is a list where the @code{cdr} of the last pair is not
318the empty list @code{()}. Any non-pair @var{obj} is also considered a
319dotted list, with length zero.
320
321@example
322(dotted-list? '(1 2 . 3)) @result{} #t
323(dotted-list? 99) @result{} #t
324@end example
a0e07ba4
NJ
325@end deffn
326
f18f87aa
KR
327It will be noted that any Scheme object passes exactly one of the
328above three tests @code{proper-list?}, @code{circular-list?} and
329@code{dotted-list?}. Non-lists are @code{dotted-list?}, finite lists
330are either @code{proper-list?} or @code{dotted-list?}, and infinite
331lists are @code{circular-list?}.
332
333@sp 1
8f85c0c6 334@deffn {Scheme Procedure} null-list? lst
a0e07ba4
NJ
335Return @code{#t} if @var{lst} is the empty list @code{()}, @code{#f}
336otherwise. If something else than a proper or circular list is passed
85a9b4ed 337as @var{lst}, an error is signalled. This procedure is recommended
a0e07ba4
NJ
338for checking for the end of a list in contexts where dotted lists are
339not allowed.
340@end deffn
341
8f85c0c6 342@deffn {Scheme Procedure} not-pair? obj
a0e07ba4
NJ
343Return @code{#t} is @var{obj} is not a pair, @code{#f} otherwise.
344This is shorthand notation @code{(not (pair? @var{obj}))} and is
345supposed to be used for end-of-list checking in contexts where dotted
346lists are allowed.
347@end deffn
348
8f85c0c6 349@deffn {Scheme Procedure} list= elt= list1 @dots{}
a0e07ba4
NJ
350Return @code{#t} if all argument lists are equal, @code{#f} otherwise.
351List equality is determined by testing whether all lists have the same
352length and the corresponding elements are equal in the sense of the
353equality predicate @var{elt=}. If no or only one list is given,
354@code{#t} is returned.
355@end deffn
356
357
358@node SRFI-1 Selectors
3229f68b 359@subsubsection Selectors
7c2e18cd 360@cindex list selector
a0e07ba4
NJ
361
362@c FIXME::martin: Review me!
363
8f85c0c6
NJ
364@deffn {Scheme Procedure} first pair
365@deffnx {Scheme Procedure} second pair
366@deffnx {Scheme Procedure} third pair
367@deffnx {Scheme Procedure} fourth pair
368@deffnx {Scheme Procedure} fifth pair
369@deffnx {Scheme Procedure} sixth pair
370@deffnx {Scheme Procedure} seventh pair
371@deffnx {Scheme Procedure} eighth pair
372@deffnx {Scheme Procedure} ninth pair
373@deffnx {Scheme Procedure} tenth pair
a0e07ba4
NJ
374These are synonyms for @code{car}, @code{cadr}, @code{caddr}, @dots{}.
375@end deffn
376
8f85c0c6 377@deffn {Scheme Procedure} car+cdr pair
a0e07ba4
NJ
378Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.
379@end deffn
380
8f85c0c6
NJ
381@deffn {Scheme Procedure} take lst i
382@deffnx {Scheme Procedure} take! lst i
a0e07ba4
NJ
383Return a list containing the first @var{i} elements of @var{lst}.
384
385@code{take!} may modify the structure of the argument list @var{lst}
386in order to produce the result.
387@end deffn
388
8f85c0c6 389@deffn {Scheme Procedure} drop lst i
a0e07ba4
NJ
390Return a list containing all but the first @var{i} elements of
391@var{lst}.
392@end deffn
393
8f85c0c6 394@deffn {Scheme Procedure} take-right lst i
ecb87335 395Return a list containing the @var{i} last elements of @var{lst}.
64bf8517 396The return shares a common tail with @var{lst}.
a0e07ba4
NJ
397@end deffn
398
8f85c0c6
NJ
399@deffn {Scheme Procedure} drop-right lst i
400@deffnx {Scheme Procedure} drop-right! lst i
ecb87335 401Return a list containing all but the @var{i} last elements of
a0e07ba4
NJ
402@var{lst}.
403
64bf8517
KR
404@code{drop-right} always returns a new list, even when @var{i} is
405zero. @code{drop-right!} may modify the structure of the argument
406list @var{lst} in order to produce the result.
a0e07ba4
NJ
407@end deffn
408
8f85c0c6
NJ
409@deffn {Scheme Procedure} split-at lst i
410@deffnx {Scheme Procedure} split-at! lst i
a0e07ba4
NJ
411Return two values, a list containing the first @var{i} elements of the
412list @var{lst} and a list containing the remaining elements.
413
414@code{split-at!} may modify the structure of the argument list
415@var{lst} in order to produce the result.
416@end deffn
417
8f85c0c6 418@deffn {Scheme Procedure} last lst
a0e07ba4
NJ
419Return the last element of the non-empty, finite list @var{lst}.
420@end deffn
421
422
423@node SRFI-1 Length Append etc
3229f68b 424@subsubsection Length, Append, Concatenate, etc.
a0e07ba4
NJ
425
426@c FIXME::martin: Review me!
427
8f85c0c6 428@deffn {Scheme Procedure} length+ lst
a0e07ba4
NJ
429Return the length of the argument list @var{lst}. When @var{lst} is a
430circular list, @code{#f} is returned.
431@end deffn
432
8f85c0c6
NJ
433@deffn {Scheme Procedure} concatenate list-of-lists
434@deffnx {Scheme Procedure} concatenate! list-of-lists
a0e07ba4
NJ
435Construct a list by appending all lists in @var{list-of-lists}.
436
437@code{concatenate!} may modify the structure of the given lists in
438order to produce the result.
a3e856f2
KR
439
440@code{concatenate} is the same as @code{(apply append
441@var{list-of-lists})}. It exists because some Scheme implementations
442have a limit on the number of arguments a function takes, which the
443@code{apply} might exceed. In Guile there is no such limit.
a0e07ba4
NJ
444@end deffn
445
8f85c0c6
NJ
446@deffn {Scheme Procedure} append-reverse rev-head tail
447@deffnx {Scheme Procedure} append-reverse! rev-head tail
23f2b9a3
KR
448Reverse @var{rev-head}, append @var{tail} to it, and return the
449result. This is equivalent to @code{(append (reverse @var{rev-head})
450@var{tail})}, but its implementation is more efficient.
451
452@example
453(append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)
454@end example
a0e07ba4
NJ
455
456@code{append-reverse!} may modify @var{rev-head} in order to produce
457the result.
458@end deffn
459
8f85c0c6 460@deffn {Scheme Procedure} zip lst1 lst2 @dots{}
a0e07ba4
NJ
461Return a list as long as the shortest of the argument lists, where
462each element is a list. The first list contains the first elements of
463the argument lists, the second list contains the second elements, and
464so on.
465@end deffn
466
8f85c0c6
NJ
467@deffn {Scheme Procedure} unzip1 lst
468@deffnx {Scheme Procedure} unzip2 lst
469@deffnx {Scheme Procedure} unzip3 lst
470@deffnx {Scheme Procedure} unzip4 lst
471@deffnx {Scheme Procedure} unzip5 lst
a0e07ba4
NJ
472@code{unzip1} takes a list of lists, and returns a list containing the
473first elements of each list, @code{unzip2} returns two lists, the
474first containing the first elements of each lists and the second
475containing the second elements of each lists, and so on.
476@end deffn
477
e508c863
KR
478@deffn {Scheme Procedure} count pred lst1 @dots{} lstN
479Return a count of the number of times @var{pred} returns true when
480called on elements from the given lists.
481
482@var{pred} is called with @var{N} parameters @code{(@var{pred}
483@var{elem1} @dots{} @var{elemN})}, each element being from the
484corresponding @var{lst1} @dots{} @var{lstN}. The first call is with
485the first element of each list, the second with the second element
486from each, and so on.
487
488Counting stops when the end of the shortest list is reached. At least
489one list must be non-circular.
490@end deffn
491
a0e07ba4
NJ
492
493@node SRFI-1 Fold and Map
3229f68b 494@subsubsection Fold, Unfold & Map
7c2e18cd
KR
495@cindex list fold
496@cindex list map
a0e07ba4
NJ
497
498@c FIXME::martin: Review me!
499
1e181a08
KR
500@deffn {Scheme Procedure} fold proc init lst1 @dots{} lstN
501@deffnx {Scheme Procedure} fold-right proc init lst1 @dots{} lstN
502Apply @var{proc} to the elements of @var{lst1} @dots{} @var{lstN} to
503build a result, and return that result.
a0e07ba4 504
1e181a08
KR
505Each @var{proc} call is @code{(@var{proc} @var{elem1} @dots{}
506@var{elemN} @var{previous})}, where @var{elem1} is from @var{lst1},
507through @var{elemN} from @var{lstN}. @var{previous} is the return
508from the previous call to @var{proc}, or the given @var{init} for the
509first call. If any list is empty, just @var{init} is returned.
a0e07ba4 510
1e181a08
KR
511@code{fold} works through the list elements from first to last. The
512following shows a list reversal and the calls it makes,
a0e07ba4 513
1e181a08
KR
514@example
515(fold cons '() '(1 2 3))
a0e07ba4 516
1e181a08
KR
517(cons 1 '())
518(cons 2 '(1))
519(cons 3 '(2 1)
520@result{} (3 2 1)
521@end example
a0e07ba4 522
1e181a08
KR
523@code{fold-right} works through the list elements from last to first,
524ie.@: from the right. So for example the following finds the longest
525string, and the last among equal longest,
526
527@example
528(fold-right (lambda (str prev)
529 (if (> (string-length str) (string-length prev))
530 str
531 prev))
532 ""
533 '("x" "abc" "xyz" "jk"))
534@result{} "xyz"
535@end example
a0e07ba4 536
1e181a08
KR
537If @var{lst1} through @var{lstN} have different lengths, @code{fold}
538stops when the end of the shortest is reached; @code{fold-right}
539commences at the last element of the shortest. Ie.@: elements past
540the length of the shortest are ignored in the other @var{lst}s. At
541least one @var{lst} must be non-circular.
542
543@code{fold} should be preferred over @code{fold-right} if the order of
544processing doesn't matter, or can be arranged either way, since
545@code{fold} is a little more efficient.
546
547The way @code{fold} builds a result from iterating is quite general,
548it can do more than other iterations like say @code{map} or
549@code{filter}. The following for example removes adjacent duplicate
550elements from a list,
551
552@example
553(define (delete-adjacent-duplicates lst)
554 (fold-right (lambda (elem ret)
555 (if (equal? elem (first ret))
556 ret
557 (cons elem ret)))
558 (list (last lst))
559 lst))
560(delete-adjacent-duplicates '(1 2 3 3 4 4 4 5))
561@result{} (1 2 3 4 5)
562@end example
563
564Clearly the same sort of thing can be done with a @code{for-each} and
5f708db6
KR
565a variable in which to build the result, but a self-contained
566@var{proc} can be re-used in multiple contexts, where a
567@code{for-each} would have to be written out each time.
a0e07ba4
NJ
568@end deffn
569
1e181a08
KR
570@deffn {Scheme Procedure} pair-fold proc init lst1 @dots{} lstN
571@deffnx {Scheme Procedure} pair-fold-right proc init lst1 @dots{} lstN
572The same as @code{fold} and @code{fold-right}, but apply @var{proc} to
573the pairs of the lists instead of the list elements.
a0e07ba4
NJ
574@end deffn
575
5f708db6
KR
576@deffn {Scheme Procedure} reduce proc default lst
577@deffnx {Scheme Procedure} reduce-right proc default lst
578@code{reduce} is a variant of @code{fold}, where the first call to
579@var{proc} is on two elements from @var{lst}, rather than one element
580and a given initial value.
1e181a08 581
5f708db6
KR
582If @var{lst} is empty, @code{reduce} returns @var{default} (this is
583the only use for @var{default}). If @var{lst} has just one element
584then that's the return value. Otherwise @var{proc} is called on the
585elements of @var{lst}.
1e181a08 586
5f708db6
KR
587Each @var{proc} call is @code{(@var{proc} @var{elem} @var{previous})},
588where @var{elem} is from @var{lst} (the second and subsequent elements
589of @var{lst}), and @var{previous} is the return from the previous call
590to @var{proc}. The first element of @var{lst} is the @var{previous}
591for the first call to @var{proc}.
1e181a08 592
5f708db6
KR
593For example, the following adds a list of numbers, the calls made to
594@code{+} are shown. (Of course @code{+} accepts multiple arguments
595and can add a list directly, with @code{apply}.)
1e181a08
KR
596
597@example
5f708db6
KR
598(reduce + 0 '(5 6 7)) @result{} 18
599
600(+ 6 5) @result{} 11
601(+ 7 11) @result{} 18
1e181a08
KR
602@end example
603
5f708db6
KR
604@code{reduce} can be used instead of @code{fold} where the @var{init}
605value is an ``identity'', meaning a value which under @var{proc}
606doesn't change the result, in this case 0 is an identity since
607@code{(+ 5 0)} is just 5. @code{reduce} avoids that unnecessary call.
1e181a08
KR
608
609@code{reduce-right} is a similar variation on @code{fold-right},
5f708db6
KR
610working from the end (ie.@: the right) of @var{lst}. The last element
611of @var{lst} is the @var{previous} for the first call to @var{proc},
612and the @var{elem} values go from the second last.
1e181a08
KR
613
614@code{reduce} should be preferred over @code{reduce-right} if the
615order of processing doesn't matter, or can be arranged either way,
616since @code{reduce} is a little more efficient.
a0e07ba4
NJ
617@end deffn
618
8f85c0c6 619@deffn {Scheme Procedure} unfold p f g seed [tail-gen]
a0e07ba4
NJ
620@code{unfold} is defined as follows:
621
622@lisp
623(unfold p f g seed) =
624 (if (p seed) (tail-gen seed)
625 (cons (f seed)
626 (unfold p f g (g seed))))
627@end lisp
628
629@table @var
630@item p
631Determines when to stop unfolding.
632
633@item f
634Maps each seed value to the corresponding list element.
635
636@item g
ecb87335 637Maps each seed value to next seed value.
a0e07ba4
NJ
638
639@item seed
640The state value for the unfold.
641
642@item tail-gen
643Creates the tail of the list; defaults to @code{(lambda (x) '())}.
644@end table
645
646@var{g} produces a series of seed values, which are mapped to list
647elements by @var{f}. These elements are put into a list in
648left-to-right order, and @var{p} tells when to stop unfolding.
649@end deffn
650
8f85c0c6 651@deffn {Scheme Procedure} unfold-right p f g seed [tail]
a0e07ba4
NJ
652Construct a list with the following loop.
653
654@lisp
655(let lp ((seed seed) (lis tail))
656 (if (p seed) lis
657 (lp (g seed)
658 (cons (f seed) lis))))
659@end lisp
660
661@table @var
662@item p
663Determines when to stop unfolding.
664
665@item f
666Maps each seed value to the corresponding list element.
667
668@item g
ecb87335 669Maps each seed value to next seed value.
a0e07ba4
NJ
670
671@item seed
672The state value for the unfold.
673
674@item tail-gen
675Creates the tail of the list; defaults to @code{(lambda (x) '())}.
676@end table
677
678@end deffn
679
8f85c0c6 680@deffn {Scheme Procedure} map f lst1 lst2 @dots{}
a0e07ba4
NJ
681Map the procedure over the list(s) @var{lst1}, @var{lst2}, @dots{} and
682return a list containing the results of the procedure applications.
683This procedure is extended with respect to R5RS, because the argument
684lists may have different lengths. The result list will have the same
685length as the shortest argument lists. The order in which @var{f}
686will be applied to the list element(s) is not specified.
687@end deffn
688
8f85c0c6 689@deffn {Scheme Procedure} for-each f lst1 lst2 @dots{}
a0e07ba4
NJ
690Apply the procedure @var{f} to each pair of corresponding elements of
691the list(s) @var{lst1}, @var{lst2}, @dots{}. The return value is not
692specified. This procedure is extended with respect to R5RS, because
693the argument lists may have different lengths. The shortest argument
694list determines the number of times @var{f} is called. @var{f} will
85a9b4ed 695be applied to the list elements in left-to-right order.
a0e07ba4
NJ
696
697@end deffn
698
8f85c0c6
NJ
699@deffn {Scheme Procedure} append-map f lst1 lst2 @dots{}
700@deffnx {Scheme Procedure} append-map! f lst1 lst2 @dots{}
12991fed 701Equivalent to
a0e07ba4
NJ
702
703@lisp
12991fed 704(apply append (map f clist1 clist2 ...))
a0e07ba4
NJ
705@end lisp
706
12991fed 707and
a0e07ba4
NJ
708
709@lisp
12991fed 710(apply append! (map f clist1 clist2 ...))
a0e07ba4
NJ
711@end lisp
712
713Map @var{f} over the elements of the lists, just as in the @code{map}
714function. However, the results of the applications are appended
715together to make the final result. @code{append-map} uses
716@code{append} to append the results together; @code{append-map!} uses
717@code{append!}.
718
719The dynamic order in which the various applications of @var{f} are
720made is not specified.
721@end deffn
722
8f85c0c6 723@deffn {Scheme Procedure} map! f lst1 lst2 @dots{}
a0e07ba4
NJ
724Linear-update variant of @code{map} -- @code{map!} is allowed, but not
725required, to alter the cons cells of @var{lst1} to construct the
726result list.
727
728The dynamic order in which the various applications of @var{f} are
729made is not specified. In the n-ary case, @var{lst2}, @var{lst3},
730@dots{} must have at least as many elements as @var{lst1}.
731@end deffn
732
8f85c0c6 733@deffn {Scheme Procedure} pair-for-each f lst1 lst2 @dots{}
a0e07ba4
NJ
734Like @code{for-each}, but applies the procedure @var{f} to the pairs
735from which the argument lists are constructed, instead of the list
736elements. The return value is not specified.
737@end deffn
738
8f85c0c6 739@deffn {Scheme Procedure} filter-map f lst1 lst2 @dots{}
a0e07ba4
NJ
740Like @code{map}, but only results from the applications of @var{f}
741which are true are saved in the result list.
742@end deffn
743
744
745@node SRFI-1 Filtering and Partitioning
3229f68b 746@subsubsection Filtering and Partitioning
7c2e18cd
KR
747@cindex list filter
748@cindex list partition
a0e07ba4
NJ
749
750@c FIXME::martin: Review me!
751
752Filtering means to collect all elements from a list which satisfy a
753specific condition. Partitioning a list means to make two groups of
754list elements, one which contains the elements satisfying a condition,
755and the other for the elements which don't.
756
60e25dc4
KR
757The @code{filter} and @code{filter!} functions are implemented in the
758Guile core, @xref{List Modification}.
a0e07ba4 759
8f85c0c6
NJ
760@deffn {Scheme Procedure} partition pred lst
761@deffnx {Scheme Procedure} partition! pred lst
193239f1
KR
762Split @var{lst} into those elements which do and don't satisfy the
763predicate @var{pred}.
a0e07ba4 764
193239f1
KR
765The return is two values (@pxref{Multiple Values}), the first being a
766list of all elements from @var{lst} which satisfy @var{pred}, the
767second a list of those which do not.
768
769The elements in the result lists are in the same order as in @var{lst}
770but the order in which the calls @code{(@var{pred} elem)} are made on
771the list elements is unspecified.
772
773@code{partition} does not change @var{lst}, but one of the returned
774lists may share a tail with it. @code{partition!} may modify
775@var{lst} to construct its return.
a0e07ba4
NJ
776@end deffn
777
8f85c0c6
NJ
778@deffn {Scheme Procedure} remove pred lst
779@deffnx {Scheme Procedure} remove! pred lst
a0e07ba4
NJ
780Return a list containing all elements from @var{lst} which do not
781satisfy the predicate @var{pred}. The elements in the result list
782have the same order as in @var{lst}. The order in which @var{pred} is
783applied to the list elements is not specified.
784
785@code{remove!} is allowed, but not required to modify the structure of
786the input list.
787@end deffn
788
789
790@node SRFI-1 Searching
3229f68b 791@subsubsection Searching
7c2e18cd 792@cindex list search
a0e07ba4
NJ
793
794@c FIXME::martin: Review me!
795
796The procedures for searching elements in lists either accept a
797predicate or a comparison object for determining which elements are to
798be searched.
799
8f85c0c6 800@deffn {Scheme Procedure} find pred lst
a0e07ba4
NJ
801Return the first element of @var{lst} which satisfies the predicate
802@var{pred} and @code{#f} if no such element is found.
803@end deffn
804
8f85c0c6 805@deffn {Scheme Procedure} find-tail pred lst
a0e07ba4
NJ
806Return the first pair of @var{lst} whose @sc{car} satisfies the
807predicate @var{pred} and @code{#f} if no such element is found.
808@end deffn
809
8f85c0c6
NJ
810@deffn {Scheme Procedure} take-while pred lst
811@deffnx {Scheme Procedure} take-while! pred lst
a0e07ba4
NJ
812Return the longest initial prefix of @var{lst} whose elements all
813satisfy the predicate @var{pred}.
814
815@code{take-while!} is allowed, but not required to modify the input
816list while producing the result.
817@end deffn
818
8f85c0c6 819@deffn {Scheme Procedure} drop-while pred lst
a0e07ba4
NJ
820Drop the longest initial prefix of @var{lst} whose elements all
821satisfy the predicate @var{pred}.
822@end deffn
823
8f85c0c6
NJ
824@deffn {Scheme Procedure} span pred lst
825@deffnx {Scheme Procedure} span! pred lst
826@deffnx {Scheme Procedure} break pred lst
827@deffnx {Scheme Procedure} break! pred lst
a0e07ba4
NJ
828@code{span} splits the list @var{lst} into the longest initial prefix
829whose elements all satisfy the predicate @var{pred}, and the remaining
830tail. @code{break} inverts the sense of the predicate.
831
832@code{span!} and @code{break!} are allowed, but not required to modify
833the structure of the input list @var{lst} in order to produce the
834result.
3e73b6f9
KR
835
836Note that the name @code{break} conflicts with the @code{break}
837binding established by @code{while} (@pxref{while do}). Applications
838wanting to use @code{break} from within a @code{while} loop will need
839to make a new define under a different name.
a0e07ba4
NJ
840@end deffn
841
62705beb
KR
842@deffn {Scheme Procedure} any pred lst1 lst2 @dots{} lstN
843Test whether any set of elements from @var{lst1} @dots{} lstN
844satisfies @var{pred}. If so the return value is the return from the
845successful @var{pred} call, or if not the return is @code{#f}.
846
847Each @var{pred} call is @code{(@var{pred} @var{elem1} @dots{}
848@var{elemN})} taking an element from each @var{lst}. The calls are
849made successively for the first, second, etc elements of the lists,
850stopping when @var{pred} returns non-@code{#f}, or when the end of the
851shortest list is reached.
852
853The @var{pred} call on the last set of elements (ie.@: when the end of
854the shortest list has been reached), if that point is reached, is a
855tail call.
856@end deffn
857
858@deffn {Scheme Procedure} every pred lst1 lst2 @dots{} lstN
859Test whether every set of elements from @var{lst1} @dots{} lstN
860satisfies @var{pred}. If so the return value is the return from the
861final @var{pred} call, or if not the return is @code{#f}.
862
863Each @var{pred} call is @code{(@var{pred} @var{elem1} @dots{}
864@var{elemN})} taking an element from each @var{lst}. The calls are
865made successively for the first, second, etc elements of the lists,
866stopping if @var{pred} returns @code{#f}, or when the end of any of
867the lists is reached.
868
869The @var{pred} call on the last set of elements (ie.@: when the end of
870the shortest list has been reached) is a tail call.
871
872If one of @var{lst1} @dots{} @var{lstN} is empty then no calls to
873@var{pred} are made, and the return is @code{#t}.
a0e07ba4
NJ
874@end deffn
875
0166e7f2 876@deffn {Scheme Procedure} list-index pred lst1 @dots{} lstN
d1736abf
KR
877Return the index of the first set of elements, one from each of
878@var{lst1}@dots{}@var{lstN}, which satisfies @var{pred}.
879
880@var{pred} is called as @code{(@var{pred} elem1 @dots{} elemN)}.
881Searching stops when the end of the shortest @var{lst} is reached.
882The return index starts from 0 for the first set of elements. If no
883set of elements pass then the return is @code{#f}.
0166e7f2
KR
884
885@example
886(list-index odd? '(2 4 6 9)) @result{} 3
887(list-index = '(1 2 3) '(3 1 2)) @result{} #f
888@end example
a0e07ba4
NJ
889@end deffn
890
8f85c0c6 891@deffn {Scheme Procedure} member x lst [=]
a0e07ba4 892Return the first sublist of @var{lst} whose @sc{car} is equal to
ca04a5ae 893@var{x}. If @var{x} does not appear in @var{lst}, return @code{#f}.
ea6ea01b 894
ca04a5ae
KR
895Equality is determined by @code{equal?}, or by the equality predicate
896@var{=} if given. @var{=} is called @code{(= @var{x} elem)},
897ie.@: with the given @var{x} first, so for example to find the first
898element greater than 5,
899
900@example
901(member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)
902@end example
903
904This version of @code{member} extends the core @code{member}
905(@pxref{List Searching}) by accepting an equality predicate.
a0e07ba4
NJ
906@end deffn
907
908
909@node SRFI-1 Deleting
3229f68b 910@subsubsection Deleting
7c2e18cd 911@cindex list delete
a0e07ba4 912
8f85c0c6
NJ
913@deffn {Scheme Procedure} delete x lst [=]
914@deffnx {Scheme Procedure} delete! x lst [=]
b6b9376a
KR
915Return a list containing the elements of @var{lst} but with those
916equal to @var{x} deleted. The returned elements will be in the same
917order as they were in @var{lst}.
918
919Equality is determined by the @var{=} predicate, or @code{equal?} if
920not given. An equality call is made just once for each element, but
921the order in which the calls are made on the elements is unspecified.
a0e07ba4 922
243bdb63 923The equality calls are always @code{(= x elem)}, ie.@: the given @var{x}
b6b9376a
KR
924is first. This means for instance elements greater than 5 can be
925deleted with @code{(delete 5 lst <)}.
926
927@code{delete} does not modify @var{lst}, but the return might share a
928common tail with @var{lst}. @code{delete!} may modify the structure
929of @var{lst} to construct its return.
ea6ea01b 930
4eb21177
KR
931These functions extend the core @code{delete} and @code{delete!}
932(@pxref{List Modification}) in accepting an equality predicate. See
933also @code{lset-difference} (@pxref{SRFI-1 Set Operations}) for
934deleting multiple elements from a list.
a0e07ba4
NJ
935@end deffn
936
8f85c0c6
NJ
937@deffn {Scheme Procedure} delete-duplicates lst [=]
938@deffnx {Scheme Procedure} delete-duplicates! lst [=]
b6b9376a
KR
939Return a list containing the elements of @var{lst} but without
940duplicates.
941
942When elements are equal, only the first in @var{lst} is retained.
943Equal elements can be anywhere in @var{lst}, they don't have to be
944adjacent. The returned list will have the retained elements in the
945same order as they were in @var{lst}.
946
947Equality is determined by the @var{=} predicate, or @code{equal?} if
948not given. Calls @code{(= x y)} are made with element @var{x} being
949before @var{y} in @var{lst}. A call is made at most once for each
950combination, but the sequence of the calls across the elements is
951unspecified.
952
953@code{delete-duplicates} does not modify @var{lst}, but the return
954might share a common tail with @var{lst}. @code{delete-duplicates!}
955may modify the structure of @var{lst} to construct its return.
956
957In the worst case, this is an @math{O(N^2)} algorithm because it must
958check each element against all those preceding it. For long lists it
959is more efficient to sort and then compare only adjacent elements.
a0e07ba4
NJ
960@end deffn
961
962
963@node SRFI-1 Association Lists
3229f68b 964@subsubsection Association Lists
7c2e18cd
KR
965@cindex association list
966@cindex alist
a0e07ba4
NJ
967
968@c FIXME::martin: Review me!
969
970Association lists are described in detail in section @ref{Association
971Lists}. The present section only documents the additional procedures
972for dealing with association lists defined by SRFI-1.
973
8f85c0c6 974@deffn {Scheme Procedure} assoc key alist [=]
23f2b9a3
KR
975Return the pair from @var{alist} which matches @var{key}. This
976extends the core @code{assoc} (@pxref{Retrieving Alist Entries}) by
977taking an optional @var{=} comparison procedure.
978
979The default comparison is @code{equal?}. If an @var{=} parameter is
679cceed 980given it's called @code{(@var{=} @var{key} @var{alistcar})}, i.e.@: the
23f2b9a3
KR
981given target @var{key} is the first argument, and a @code{car} from
982@var{alist} is second.
ea6ea01b 983
23f2b9a3
KR
984For example a case-insensitive string lookup,
985
986@example
987(assoc "yy" '(("XX" . 1) ("YY" . 2)) string-ci=?)
988@result{} ("YY" . 2)
989@end example
a0e07ba4
NJ
990@end deffn
991
8f85c0c6 992@deffn {Scheme Procedure} alist-cons key datum alist
5e5999f9
KR
993Cons a new association @var{key} and @var{datum} onto @var{alist} and
994return the result. This is equivalent to
a0e07ba4
NJ
995
996@lisp
997(cons (cons @var{key} @var{datum}) @var{alist})
998@end lisp
999
5e5999f9
KR
1000@code{acons} (@pxref{Adding or Setting Alist Entries}) in the Guile
1001core does the same thing.
a0e07ba4
NJ
1002@end deffn
1003
8f85c0c6 1004@deffn {Scheme Procedure} alist-copy alist
a0e07ba4
NJ
1005Return a newly allocated copy of @var{alist}, that means that the
1006spine of the list as well as the pairs are copied.
1007@end deffn
1008
8f85c0c6
NJ
1009@deffn {Scheme Procedure} alist-delete key alist [=]
1010@deffnx {Scheme Procedure} alist-delete! key alist [=]
bd35f1f0
KR
1011Return a list containing the elements of @var{alist} but with those
1012elements whose keys are equal to @var{key} deleted. The returned
1013elements will be in the same order as they were in @var{alist}.
a0e07ba4 1014
bd35f1f0
KR
1015Equality is determined by the @var{=} predicate, or @code{equal?} if
1016not given. The order in which elements are tested is unspecified, but
679cceed 1017each equality call is made @code{(= key alistkey)}, i.e.@: the given
bd35f1f0
KR
1018@var{key} parameter is first and the key from @var{alist} second.
1019This means for instance all associations with a key greater than 5 can
1020be removed with @code{(alist-delete 5 alist <)}.
1021
1022@code{alist-delete} does not modify @var{alist}, but the return might
1023share a common tail with @var{alist}. @code{alist-delete!} may modify
1024the list structure of @var{alist} to construct its return.
a0e07ba4
NJ
1025@end deffn
1026
1027
1028@node SRFI-1 Set Operations
3229f68b 1029@subsubsection Set Operations on Lists
7c2e18cd 1030@cindex list set operation
a0e07ba4 1031
4eb21177
KR
1032Lists can be used to represent sets of objects. The procedures in
1033this section operate on such lists as sets.
1034
1035Note that lists are not an efficient way to implement large sets. The
9aa0c3dd 1036procedures here typically take time @math{@var{m}@cross{}@var{n}} when
4eb21177
KR
1037operating on @var{m} and @var{n} element lists. Other data structures
1038like trees, bitsets (@pxref{Bit Vectors}) or hash tables (@pxref{Hash
1039Tables}) are faster.
1040
1041All these procedures take an equality predicate as the first argument.
1042This predicate is used for testing the objects in the list sets for
1043sameness. This predicate must be consistent with @code{eq?}
1044(@pxref{Equality}) in the sense that if two list elements are
1045@code{eq?} then they must also be equal under the predicate. This
1046simply means a given object must be equal to itself.
a0e07ba4 1047
4eb21177
KR
1048@deffn {Scheme Procedure} lset<= = list1 list2 @dots{}
1049Return @code{#t} if each list is a subset of the one following it.
1050Ie.@: @var{list1} a subset of @var{list2}, @var{list2} a subset of
1051@var{list3}, etc, for as many lists as given. If only one list or no
1052lists are given then the return is @code{#t}.
1053
1054A list @var{x} is a subset of @var{y} if each element of @var{x} is
1055equal to some element in @var{y}. Elements are compared using the
1056given @var{=} procedure, called as @code{(@var{=} xelem yelem)}.
1057
1058@example
1059(lset<= eq?) @result{} #t
1060(lset<= eqv? '(1 2 3) '(1)) @result{} #f
1061(lset<= eqv? '(1 3 2) '(4 3 1 2)) @result{} #t
1062@end example
a0e07ba4
NJ
1063@end deffn
1064
8f85c0c6 1065@deffn {Scheme Procedure} lset= = list1 list2 @dots{}
4eb21177
KR
1066Return @code{#t} if all argument lists are set-equal. @var{list1} is
1067compared to @var{list2}, @var{list2} to @var{list3}, etc, for as many
1068lists as given. If only one list or no lists are given then the
1069return is @code{#t}.
1070
1071Two lists @var{x} and @var{y} are set-equal if each element of @var{x}
1072is equal to some element of @var{y} and conversely each element of
1073@var{y} is equal to some element of @var{x}. The order of the
1074elements in the lists doesn't matter. Element equality is determined
1075with the given @var{=} procedure, called as @code{(@var{=} xelem
1076yelem)}, but exactly which calls are made is unspecified.
1077
1078@example
1079(lset= eq?) @result{} #t
1080(lset= eqv? '(1 2 3) '(3 2 1)) @result{} #t
1081(lset= string-ci=? '("a" "A" "b") '("B" "b" "a")) @result{} #t
1082@end example
a0e07ba4
NJ
1083@end deffn
1084
4eb21177
KR
1085@deffn {Scheme Procedure} lset-adjoin = list elem1 @dots{}
1086Add to @var{list} any of the given @var{elem}s not already in the
1087list. @var{elem}s are @code{cons}ed onto the start of @var{list} (so
1088the return shares a common tail with @var{list}), but the order
1089they're added is unspecified.
1090
1091The given @var{=} procedure is used for comparing elements, called as
1092@code{(@var{=} listelem elem)}, ie.@: the second argument is one of
1093the given @var{elem} parameters.
1094
1095@example
1096(lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)
1097@end example
a0e07ba4
NJ
1098@end deffn
1099
4eb21177
KR
1100@deffn {Scheme Procedure} lset-union = list1 list2 @dots{}
1101@deffnx {Scheme Procedure} lset-union! = list1 list2 @dots{}
1102Return the union of the argument list sets. The result is built by
1103taking the union of @var{list1} and @var{list2}, then the union of
1104that with @var{list3}, etc, for as many lists as given. For one list
1105argument that list itself is the result, for no list arguments the
1106result is the empty list.
1107
1108The union of two lists @var{x} and @var{y} is formed as follows. If
1109@var{x} is empty then the result is @var{y}. Otherwise start with
1110@var{x} as the result and consider each @var{y} element (from first to
1111last). A @var{y} element not equal to something already in the result
1112is @code{cons}ed onto the result.
1113
1114The given @var{=} procedure is used for comparing elements, called as
1115@code{(@var{=} relem yelem)}. The first argument is from the result
1116accumulated so far, and the second is from the list being union-ed in.
1117But exactly which calls are made is otherwise unspecified.
1118
1119Notice that duplicate elements in @var{list1} (or the first non-empty
1120list) are preserved, but that repeated elements in subsequent lists
1121are only added once.
1122
1123@example
1124(lset-union eqv?) @result{} ()
1125(lset-union eqv? '(1 2 3)) @result{} (1 2 3)
1126(lset-union eqv? '(1 2 1 3) '(2 4 5) '(5)) @result{} (5 4 1 2 1 3)
1127@end example
1128
1129@code{lset-union} doesn't change the given lists but the result may
1130share a tail with the first non-empty list. @code{lset-union!} can
1131modify all of the given lists to form the result.
a0e07ba4
NJ
1132@end deffn
1133
8f85c0c6
NJ
1134@deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{}
1135@deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{}
4eb21177
KR
1136Return the intersection of @var{list1} with the other argument lists,
1137meaning those elements of @var{list1} which are also in all of
1138@var{list2} etc. For one list argument, just that list is returned.
1139
1140The test for an element of @var{list1} to be in the return is simply
1141that it's equal to some element in each of @var{list2} etc. Notice
1142this means an element appearing twice in @var{list1} but only once in
1143each of @var{list2} etc will go into the return twice. The return has
1144its elements in the same order as they were in @var{list1}.
1145
1146The given @var{=} procedure is used for comparing elements, called as
1147@code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
1148and the second is from one of the subsequent lists. But exactly which
1149calls are made and in what order is unspecified.
1150
1151@example
1152(lset-intersection eqv? '(x y)) @result{} (x y)
1153(lset-intersection eqv? '(1 2 3) '(4 3 2)) @result{} (2 3)
1154(lset-intersection eqv? '(1 1 2 2) '(1 2) '(2 1) '(2)) @result{} (2 2)
1155@end example
1156
1157The return from @code{lset-intersection} may share a tail with
1158@var{list1}. @code{lset-intersection!} may modify @var{list1} to form
1159its result.
a0e07ba4
NJ
1160@end deffn
1161
8f85c0c6
NJ
1162@deffn {Scheme Procedure} lset-difference = list1 list2 @dots{}
1163@deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{}
4eb21177
KR
1164Return @var{list1} with any elements in @var{list2}, @var{list3} etc
1165removed (ie.@: subtracted). For one list argument, just that list is
1166returned.
1167
1168The given @var{=} procedure is used for comparing elements, called as
1169@code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
1170and the second from one of the subsequent lists. But exactly which
1171calls are made and in what order is unspecified.
a0e07ba4 1172
4eb21177
KR
1173@example
1174(lset-difference eqv? '(x y)) @result{} (x y)
1175(lset-difference eqv? '(1 2 3) '(3 1)) @result{} (2)
1176(lset-difference eqv? '(1 2 3) '(3) '(2)) @result{} (1)
1177@end example
1178
1179The return from @code{lset-difference} may share a tail with
1180@var{list1}. @code{lset-difference!} may modify @var{list1} to form
1181its result.
a0e07ba4
NJ
1182@end deffn
1183
8f85c0c6
NJ
1184@deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{}
1185@deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{}
4eb21177
KR
1186Return two values (@pxref{Multiple Values}), the difference and
1187intersection of the argument lists as per @code{lset-difference} and
1188@code{lset-intersection} above.
1189
1190For two list arguments this partitions @var{list1} into those elements
1191of @var{list1} which are in @var{list2} and not in @var{list2}. (But
1192for more than two arguments there can be elements of @var{list1} which
1193are neither part of the difference nor the intersection.)
1194
1195One of the return values from @code{lset-diff+intersection} may share
1196a tail with @var{list1}. @code{lset-diff+intersection!} may modify
1197@var{list1} to form its results.
1198@end deffn
1199
1200@deffn {Scheme Procedure} lset-xor = list1 list2 @dots{}
1201@deffnx {Scheme Procedure} lset-xor! = list1 list2 @dots{}
1202Return an XOR of the argument lists. For two lists this means those
1203elements which are in exactly one of the lists. For more than two
1204lists it means those elements which appear in an odd number of the
1205lists.
1206
1207To be precise, the XOR of two lists @var{x} and @var{y} is formed by
1208taking those elements of @var{x} not equal to any element of @var{y},
1209plus those elements of @var{y} not equal to any element of @var{x}.
1210Equality is determined with the given @var{=} procedure, called as
1211@code{(@var{=} e1 e2)}. One argument is from @var{x} and the other
1212from @var{y}, but which way around is unspecified. Exactly which
1213calls are made is also unspecified, as is the order of the elements in
1214the result.
1215
1216@example
1217(lset-xor eqv? '(x y)) @result{} (x y)
1218(lset-xor eqv? '(1 2 3) '(4 3 2)) @result{} (4 1)
1219@end example
1220
1221The return from @code{lset-xor} may share a tail with one of the list
1222arguments. @code{lset-xor!} may modify @var{list1} to form its
1223result.
a0e07ba4
NJ
1224@end deffn
1225
1226
1227@node SRFI-2
3229f68b 1228@subsection SRFI-2 - and-let*
8742c48b 1229@cindex SRFI-2
a0e07ba4 1230
4fd0db14
KR
1231@noindent
1232The following syntax can be obtained with
a0e07ba4 1233
4fd0db14
KR
1234@lisp
1235(use-modules (srfi srfi-2))
1236@end lisp
a0e07ba4 1237
4fd0db14
KR
1238@deffn {library syntax} and-let* (clause @dots{}) body @dots{}
1239A combination of @code{and} and @code{let*}.
1240
1241Each @var{clause} is evaluated in turn, and if @code{#f} is obtained
1242then evaluation stops and @code{#f} is returned. If all are
1243non-@code{#f} then @var{body} is evaluated and the last form gives the
6b1a6e4c
KR
1244return value, or if @var{body} is empty then the result is @code{#t}.
1245Each @var{clause} should be one of the following,
4fd0db14
KR
1246
1247@table @code
1248@item (symbol expr)
1249Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}.
1250Like @code{let*}, that binding is available to subsequent clauses.
1251@item (expr)
1252Evaluate @var{expr} and check for @code{#f}.
1253@item symbol
1254Get the value bound to @var{symbol} and check for @code{#f}.
1255@end table
a0e07ba4 1256
4fd0db14
KR
1257Notice that @code{(expr)} has an ``extra'' pair of parentheses, for
1258instance @code{((eq? x y))}. One way to remember this is to imagine
1259the @code{symbol} in @code{(symbol expr)} is omitted.
a0e07ba4 1260
4fd0db14
KR
1261@code{and-let*} is good for calculations where a @code{#f} value means
1262termination, but where a non-@code{#f} value is going to be needed in
1263subsequent expressions.
1264
1265The following illustrates this, it returns text between brackets
1266@samp{[...]} in a string, or @code{#f} if there are no such brackets
1267(ie.@: either @code{string-index} gives @code{#f}).
1268
1269@example
1270(define (extract-brackets str)
1271 (and-let* ((start (string-index str #\[))
1272 (end (string-index str #\] start)))
1273 (substring str (1+ start) end)))
1274@end example
1275
1276The following shows plain variables and expressions tested too.
1277@code{diagnostic-levels} is taken to be an alist associating a
1278diagnostic type with a level. @code{str} is printed only if the type
1279is known and its level is high enough.
1280
1281@example
1282(define (show-diagnostic type str)
1283 (and-let* (want-diagnostics
1284 (level (assq-ref diagnostic-levels type))
1285 ((>= level current-diagnostic-level)))
1286 (display str)))
1287@end example
1288
1289The advantage of @code{and-let*} is that an extended sequence of
1290expressions and tests doesn't require lots of nesting as would arise
1291from separate @code{and} and @code{let*}, or from @code{cond} with
1292@code{=>}.
1293
1294@end deffn
a0e07ba4
NJ
1295
1296
1297@node SRFI-4
3229f68b 1298@subsection SRFI-4 - Homogeneous numeric vector datatypes
8742c48b 1299@cindex SRFI-4
a0e07ba4 1300
27219b32
AW
1301SRFI-4 provides an interface to uniform numeric vectors: vectors whose elements
1302are all of a single numeric type. Guile offers uniform numeric vectors for
1303signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of
1304floating point values, and, as an extension to SRFI-4, complex floating-point
1305numbers of these two sizes.
1306
1307The standard SRFI-4 procedures and data types may be included via loading the
1308appropriate module:
1309
1310@example
1311(use-modules (srfi srfi-4))
1312@end example
1313
1314This module is currently a part of the default Guile environment, but it is a
1315good practice to explicitly import the module. In the future, using SRFI-4
1316procedures without importing the SRFI-4 module will cause a deprecation message
1317to be printed. (Of course, one may call the C functions at any time. Would that
1318C had modules!)
1319
1320@menu
1321* SRFI-4 Overview:: The warp and weft of uniform numeric vectors.
1322* SRFI-4 API:: Uniform vectors, from Scheme and from C.
1323* SRFI-4 Generic Operations:: The general, operating on the specific.
1324* SRFI-4 and Bytevectors:: SRFI-4 vectors are backed by bytevectors.
1325* SRFI-4 Extensions:: Guile-specific extensions to the standard.
1326@end menu
1327
1328@node SRFI-4 Overview
1329@subsubsection SRFI-4 - Overview
1330
1331Uniform numeric vectors can be useful since they consume less memory
1332than the non-uniform, general vectors. Also, since the types they can
1333store correspond directly to C types, it is easier to work with them
1334efficiently on a low level. Consider image processing as an example,
1335where you want to apply a filter to some image. While you could store
1336the pixels of an image in a general vector and write a general
1337convolution function, things are much more efficient with uniform
1338vectors: the convolution function knows that all pixels are unsigned
13398-bit values (say), and can use a very tight inner loop.
1340
1341This is implemented in Scheme by having the compiler notice calls to the SRFI-4
1342accessors, and inline them to appropriate compiled code. From C you have access
1343to the raw array; functions for efficiently working with uniform numeric vectors
1344from C are listed at the end of this section.
1345
1346Uniform numeric vectors are the special case of one dimensional uniform
1347numeric arrays.
1348
1349There are 12 standard kinds of uniform numeric vectors, and they all have their
1350own complement of constructors, accessors, and so on. Procedures that operate on
1351a specific kind of uniform numeric vector have a ``tag'' in their name,
1352indicating the element type.
1353
1354@table @nicode
1355@item u8
1356unsigned 8-bit integers
1357
1358@item s8
1359signed 8-bit integers
1360
1361@item u16
1362unsigned 16-bit integers
1363
1364@item s16
1365signed 16-bit integers
1366
1367@item u32
1368unsigned 32-bit integers
1369
1370@item s32
1371signed 32-bit integers
1372
1373@item u64
1374unsigned 64-bit integers
1375
1376@item s64
1377signed 64-bit integers
1378
1379@item f32
1380the C type @code{float}
1381
1382@item f64
1383the C type @code{double}
1384
1385@end table
1386
1387In addition, Guile supports uniform arrays of complex numbers, with the
1388nonstandard tags:
1389
1390@table @nicode
1391
1392@item c32
1393complex numbers in rectangular form with the real and imaginary part
1394being a @code{float}
1395
1396@item c64
1397complex numbers in rectangular form with the real and imaginary part
1398being a @code{double}
1399
1400@end table
1401
1402The external representation (ie.@: read syntax) for these vectors is
1403similar to normal Scheme vectors, but with an additional tag from the
1404tables above indicating the vector's type. For example,
1405
1406@lisp
1407#u16(1 2 3)
1408#f64(3.1415 2.71)
1409@end lisp
1410
1411Note that the read syntax for floating-point here conflicts with
1412@code{#f} for false. In Standard Scheme one can write @code{(1 #f3)}
1413for a three element list @code{(1 #f 3)}, but for Guile @code{(1 #f3)}
1414is invalid. @code{(1 #f 3)} is almost certainly what one should write
1415anyway to make the intention clear, so this is rarely a problem.
1416
1417
1418@node SRFI-4 API
1419@subsubsection SRFI-4 - API
1420
1421Note that the @nicode{c32} and @nicode{c64} functions are only available from
1422@nicode{(srfi srfi-4 gnu)}.
1423
1424@deffn {Scheme Procedure} u8vector? obj
1425@deffnx {Scheme Procedure} s8vector? obj
1426@deffnx {Scheme Procedure} u16vector? obj
1427@deffnx {Scheme Procedure} s16vector? obj
1428@deffnx {Scheme Procedure} u32vector? obj
1429@deffnx {Scheme Procedure} s32vector? obj
1430@deffnx {Scheme Procedure} u64vector? obj
1431@deffnx {Scheme Procedure} s64vector? obj
1432@deffnx {Scheme Procedure} f32vector? obj
1433@deffnx {Scheme Procedure} f64vector? obj
1434@deffnx {Scheme Procedure} c32vector? obj
1435@deffnx {Scheme Procedure} c64vector? obj
1436@deffnx {C Function} scm_u8vector_p (obj)
1437@deffnx {C Function} scm_s8vector_p (obj)
1438@deffnx {C Function} scm_u16vector_p (obj)
1439@deffnx {C Function} scm_s16vector_p (obj)
1440@deffnx {C Function} scm_u32vector_p (obj)
1441@deffnx {C Function} scm_s32vector_p (obj)
1442@deffnx {C Function} scm_u64vector_p (obj)
1443@deffnx {C Function} scm_s64vector_p (obj)
1444@deffnx {C Function} scm_f32vector_p (obj)
1445@deffnx {C Function} scm_f64vector_p (obj)
1446@deffnx {C Function} scm_c32vector_p (obj)
1447@deffnx {C Function} scm_c64vector_p (obj)
1448Return @code{#t} if @var{obj} is a homogeneous numeric vector of the
1449indicated type.
1450@end deffn
1451
1452@deffn {Scheme Procedure} make-u8vector n [value]
1453@deffnx {Scheme Procedure} make-s8vector n [value]
1454@deffnx {Scheme Procedure} make-u16vector n [value]
1455@deffnx {Scheme Procedure} make-s16vector n [value]
1456@deffnx {Scheme Procedure} make-u32vector n [value]
1457@deffnx {Scheme Procedure} make-s32vector n [value]
1458@deffnx {Scheme Procedure} make-u64vector n [value]
1459@deffnx {Scheme Procedure} make-s64vector n [value]
1460@deffnx {Scheme Procedure} make-f32vector n [value]
1461@deffnx {Scheme Procedure} make-f64vector n [value]
1462@deffnx {Scheme Procedure} make-c32vector n [value]
1463@deffnx {Scheme Procedure} make-c64vector n [value]
1464@deffnx {C Function} scm_make_u8vector n [value]
1465@deffnx {C Function} scm_make_s8vector n [value]
1466@deffnx {C Function} scm_make_u16vector n [value]
1467@deffnx {C Function} scm_make_s16vector n [value]
1468@deffnx {C Function} scm_make_u32vector n [value]
1469@deffnx {C Function} scm_make_s32vector n [value]
1470@deffnx {C Function} scm_make_u64vector n [value]
1471@deffnx {C Function} scm_make_s64vector n [value]
1472@deffnx {C Function} scm_make_f32vector n [value]
1473@deffnx {C Function} scm_make_f64vector n [value]
1474@deffnx {C Function} scm_make_c32vector n [value]
1475@deffnx {C Function} scm_make_c64vector n [value]
1476Return a newly allocated homogeneous numeric vector holding @var{n}
1477elements of the indicated type. If @var{value} is given, the vector
1478is initialized with that value, otherwise the contents are
1479unspecified.
1480@end deffn
1481
1482@deffn {Scheme Procedure} u8vector value @dots{}
1483@deffnx {Scheme Procedure} s8vector value @dots{}
1484@deffnx {Scheme Procedure} u16vector value @dots{}
1485@deffnx {Scheme Procedure} s16vector value @dots{}
1486@deffnx {Scheme Procedure} u32vector value @dots{}
1487@deffnx {Scheme Procedure} s32vector value @dots{}
1488@deffnx {Scheme Procedure} u64vector value @dots{}
1489@deffnx {Scheme Procedure} s64vector value @dots{}
1490@deffnx {Scheme Procedure} f32vector value @dots{}
1491@deffnx {Scheme Procedure} f64vector value @dots{}
1492@deffnx {Scheme Procedure} c32vector value @dots{}
1493@deffnx {Scheme Procedure} c64vector value @dots{}
1494@deffnx {C Function} scm_u8vector (values)
1495@deffnx {C Function} scm_s8vector (values)
1496@deffnx {C Function} scm_u16vector (values)
1497@deffnx {C Function} scm_s16vector (values)
1498@deffnx {C Function} scm_u32vector (values)
1499@deffnx {C Function} scm_s32vector (values)
1500@deffnx {C Function} scm_u64vector (values)
1501@deffnx {C Function} scm_s64vector (values)
1502@deffnx {C Function} scm_f32vector (values)
1503@deffnx {C Function} scm_f64vector (values)
1504@deffnx {C Function} scm_c32vector (values)
1505@deffnx {C Function} scm_c64vector (values)
1506Return a newly allocated homogeneous numeric vector of the indicated
1507type, holding the given parameter @var{value}s. The vector length is
1508the number of parameters given.
1509@end deffn
1510
1511@deffn {Scheme Procedure} u8vector-length vec
1512@deffnx {Scheme Procedure} s8vector-length vec
1513@deffnx {Scheme Procedure} u16vector-length vec
1514@deffnx {Scheme Procedure} s16vector-length vec
1515@deffnx {Scheme Procedure} u32vector-length vec
1516@deffnx {Scheme Procedure} s32vector-length vec
1517@deffnx {Scheme Procedure} u64vector-length vec
1518@deffnx {Scheme Procedure} s64vector-length vec
1519@deffnx {Scheme Procedure} f32vector-length vec
1520@deffnx {Scheme Procedure} f64vector-length vec
1521@deffnx {Scheme Procedure} c32vector-length vec
1522@deffnx {Scheme Procedure} c64vector-length vec
1523@deffnx {C Function} scm_u8vector_length (vec)
1524@deffnx {C Function} scm_s8vector_length (vec)
1525@deffnx {C Function} scm_u16vector_length (vec)
1526@deffnx {C Function} scm_s16vector_length (vec)
1527@deffnx {C Function} scm_u32vector_length (vec)
1528@deffnx {C Function} scm_s32vector_length (vec)
1529@deffnx {C Function} scm_u64vector_length (vec)
1530@deffnx {C Function} scm_s64vector_length (vec)
1531@deffnx {C Function} scm_f32vector_length (vec)
1532@deffnx {C Function} scm_f64vector_length (vec)
1533@deffnx {C Function} scm_c32vector_length (vec)
1534@deffnx {C Function} scm_c64vector_length (vec)
1535Return the number of elements in @var{vec}.
1536@end deffn
1537
1538@deffn {Scheme Procedure} u8vector-ref vec i
1539@deffnx {Scheme Procedure} s8vector-ref vec i
1540@deffnx {Scheme Procedure} u16vector-ref vec i
1541@deffnx {Scheme Procedure} s16vector-ref vec i
1542@deffnx {Scheme Procedure} u32vector-ref vec i
1543@deffnx {Scheme Procedure} s32vector-ref vec i
1544@deffnx {Scheme Procedure} u64vector-ref vec i
1545@deffnx {Scheme Procedure} s64vector-ref vec i
1546@deffnx {Scheme Procedure} f32vector-ref vec i
1547@deffnx {Scheme Procedure} f64vector-ref vec i
1548@deffnx {Scheme Procedure} c32vector-ref vec i
1549@deffnx {Scheme Procedure} c64vector-ref vec i
1550@deffnx {C Function} scm_u8vector_ref (vec i)
1551@deffnx {C Function} scm_s8vector_ref (vec i)
1552@deffnx {C Function} scm_u16vector_ref (vec i)
1553@deffnx {C Function} scm_s16vector_ref (vec i)
1554@deffnx {C Function} scm_u32vector_ref (vec i)
1555@deffnx {C Function} scm_s32vector_ref (vec i)
1556@deffnx {C Function} scm_u64vector_ref (vec i)
1557@deffnx {C Function} scm_s64vector_ref (vec i)
1558@deffnx {C Function} scm_f32vector_ref (vec i)
1559@deffnx {C Function} scm_f64vector_ref (vec i)
1560@deffnx {C Function} scm_c32vector_ref (vec i)
1561@deffnx {C Function} scm_c64vector_ref (vec i)
1562Return the element at index @var{i} in @var{vec}. The first element
1563in @var{vec} is index 0.
1564@end deffn
1565
1566@deffn {Scheme Procedure} u8vector-set! vec i value
1567@deffnx {Scheme Procedure} s8vector-set! vec i value
1568@deffnx {Scheme Procedure} u16vector-set! vec i value
1569@deffnx {Scheme Procedure} s16vector-set! vec i value
1570@deffnx {Scheme Procedure} u32vector-set! vec i value
1571@deffnx {Scheme Procedure} s32vector-set! vec i value
1572@deffnx {Scheme Procedure} u64vector-set! vec i value
1573@deffnx {Scheme Procedure} s64vector-set! vec i value
1574@deffnx {Scheme Procedure} f32vector-set! vec i value
1575@deffnx {Scheme Procedure} f64vector-set! vec i value
1576@deffnx {Scheme Procedure} c32vector-set! vec i value
1577@deffnx {Scheme Procedure} c64vector-set! vec i value
1578@deffnx {C Function} scm_u8vector_set_x (vec i value)
1579@deffnx {C Function} scm_s8vector_set_x (vec i value)
1580@deffnx {C Function} scm_u16vector_set_x (vec i value)
1581@deffnx {C Function} scm_s16vector_set_x (vec i value)
1582@deffnx {C Function} scm_u32vector_set_x (vec i value)
1583@deffnx {C Function} scm_s32vector_set_x (vec i value)
1584@deffnx {C Function} scm_u64vector_set_x (vec i value)
1585@deffnx {C Function} scm_s64vector_set_x (vec i value)
1586@deffnx {C Function} scm_f32vector_set_x (vec i value)
1587@deffnx {C Function} scm_f64vector_set_x (vec i value)
1588@deffnx {C Function} scm_c32vector_set_x (vec i value)
1589@deffnx {C Function} scm_c64vector_set_x (vec i value)
1590Set the element at index @var{i} in @var{vec} to @var{value}. The
1591first element in @var{vec} is index 0. The return value is
1592unspecified.
1593@end deffn
1594
1595@deffn {Scheme Procedure} u8vector->list vec
1596@deffnx {Scheme Procedure} s8vector->list vec
1597@deffnx {Scheme Procedure} u16vector->list vec
1598@deffnx {Scheme Procedure} s16vector->list vec
1599@deffnx {Scheme Procedure} u32vector->list vec
1600@deffnx {Scheme Procedure} s32vector->list vec
1601@deffnx {Scheme Procedure} u64vector->list vec
1602@deffnx {Scheme Procedure} s64vector->list vec
1603@deffnx {Scheme Procedure} f32vector->list vec
1604@deffnx {Scheme Procedure} f64vector->list vec
1605@deffnx {Scheme Procedure} c32vector->list vec
1606@deffnx {Scheme Procedure} c64vector->list vec
1607@deffnx {C Function} scm_u8vector_to_list (vec)
1608@deffnx {C Function} scm_s8vector_to_list (vec)
1609@deffnx {C Function} scm_u16vector_to_list (vec)
1610@deffnx {C Function} scm_s16vector_to_list (vec)
1611@deffnx {C Function} scm_u32vector_to_list (vec)
1612@deffnx {C Function} scm_s32vector_to_list (vec)
1613@deffnx {C Function} scm_u64vector_to_list (vec)
1614@deffnx {C Function} scm_s64vector_to_list (vec)
1615@deffnx {C Function} scm_f32vector_to_list (vec)
1616@deffnx {C Function} scm_f64vector_to_list (vec)
1617@deffnx {C Function} scm_c32vector_to_list (vec)
1618@deffnx {C Function} scm_c64vector_to_list (vec)
1619Return a newly allocated list holding all elements of @var{vec}.
1620@end deffn
1621
1622@deffn {Scheme Procedure} list->u8vector lst
1623@deffnx {Scheme Procedure} list->s8vector lst
1624@deffnx {Scheme Procedure} list->u16vector lst
1625@deffnx {Scheme Procedure} list->s16vector lst
1626@deffnx {Scheme Procedure} list->u32vector lst
1627@deffnx {Scheme Procedure} list->s32vector lst
1628@deffnx {Scheme Procedure} list->u64vector lst
1629@deffnx {Scheme Procedure} list->s64vector lst
1630@deffnx {Scheme Procedure} list->f32vector lst
1631@deffnx {Scheme Procedure} list->f64vector lst
1632@deffnx {Scheme Procedure} list->c32vector lst
1633@deffnx {Scheme Procedure} list->c64vector lst
1634@deffnx {C Function} scm_list_to_u8vector (lst)
1635@deffnx {C Function} scm_list_to_s8vector (lst)
1636@deffnx {C Function} scm_list_to_u16vector (lst)
1637@deffnx {C Function} scm_list_to_s16vector (lst)
1638@deffnx {C Function} scm_list_to_u32vector (lst)
1639@deffnx {C Function} scm_list_to_s32vector (lst)
1640@deffnx {C Function} scm_list_to_u64vector (lst)
1641@deffnx {C Function} scm_list_to_s64vector (lst)
1642@deffnx {C Function} scm_list_to_f32vector (lst)
1643@deffnx {C Function} scm_list_to_f64vector (lst)
1644@deffnx {C Function} scm_list_to_c32vector (lst)
1645@deffnx {C Function} scm_list_to_c64vector (lst)
1646Return a newly allocated homogeneous numeric vector of the indicated type,
1647initialized with the elements of the list @var{lst}.
1648@end deffn
1649
1650@deftypefn {C Function} SCM scm_take_u8vector (const scm_t_uint8 *data, size_t len)
1651@deftypefnx {C Function} SCM scm_take_s8vector (const scm_t_int8 *data, size_t len)
1652@deftypefnx {C Function} SCM scm_take_u16vector (const scm_t_uint16 *data, size_t len)
1653@deftypefnx {C Function} SCM scm_take_s16vector (const scm_t_int16 *data, size_t len)
1654@deftypefnx {C Function} SCM scm_take_u32vector (const scm_t_uint32 *data, size_t len)
1655@deftypefnx {C Function} SCM scm_take_s32vector (const scm_t_int32 *data, size_t len)
1656@deftypefnx {C Function} SCM scm_take_u64vector (const scm_t_uint64 *data, size_t len)
1657@deftypefnx {C Function} SCM scm_take_s64vector (const scm_t_int64 *data, size_t len)
1658@deftypefnx {C Function} SCM scm_take_f32vector (const float *data, size_t len)
1659@deftypefnx {C Function} SCM scm_take_f64vector (const double *data, size_t len)
1660@deftypefnx {C Function} SCM scm_take_c32vector (const float *data, size_t len)
1661@deftypefnx {C Function} SCM scm_take_c64vector (const double *data, size_t len)
1662Return a new uniform numeric vector of the indicated type and length
1663that uses the memory pointed to by @var{data} to store its elements.
1664This memory will eventually be freed with @code{free}. The argument
1665@var{len} specifies the number of elements in @var{data}, not its size
1666in bytes.
1667
1668The @code{c32} and @code{c64} variants take a pointer to a C array of
1669@code{float}s or @code{double}s. The real parts of the complex numbers
1670are at even indices in that array, the corresponding imaginary parts are
1671at the following odd index.
1672@end deftypefn
1673
1674@deftypefn {C Function} {const scm_t_uint8 *} scm_u8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1675@deftypefnx {C Function} {const scm_t_int8 *} scm_s8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1676@deftypefnx {C Function} {const scm_t_uint16 *} scm_u16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1677@deftypefnx {C Function} {const scm_t_int16 *} scm_s16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1678@deftypefnx {C Function} {const scm_t_uint32 *} scm_u32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1679@deftypefnx {C Function} {const scm_t_int32 *} scm_s32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1680@deftypefnx {C Function} {const scm_t_uint64 *} scm_u64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1681@deftypefnx {C Function} {const scm_t_int64 *} scm_s64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
ecb87335 1682@deftypefnx {C Function} {const float *} scm_f32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
27219b32
AW
1683@deftypefnx {C Function} {const double *} scm_f64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1684@deftypefnx {C Function} {const float *} scm_c32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1685@deftypefnx {C Function} {const double *} scm_c64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1686Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
1687returns a pointer to the elements of a uniform numeric vector of the
1688indicated kind.
1689@end deftypefn
1690
1691@deftypefn {C Function} {scm_t_uint8 *} scm_u8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1692@deftypefnx {C Function} {scm_t_int8 *} scm_s8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1693@deftypefnx {C Function} {scm_t_uint16 *} scm_u16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1694@deftypefnx {C Function} {scm_t_int16 *} scm_s16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1695@deftypefnx {C Function} {scm_t_uint32 *} scm_u32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1696@deftypefnx {C Function} {scm_t_int32 *} scm_s32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1697@deftypefnx {C Function} {scm_t_uint64 *} scm_u64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1698@deftypefnx {C Function} {scm_t_int64 *} scm_s64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
ecb87335 1699@deftypefnx {C Function} {float *} scm_f32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
27219b32
AW
1700@deftypefnx {C Function} {double *} scm_f64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1701@deftypefnx {C Function} {float *} scm_c32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1702@deftypefnx {C Function} {double *} scm_c64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1703Like @code{scm_vector_writable_elements} (@pxref{Vector Accessing from
1704C}), but returns a pointer to the elements of a uniform numeric vector
1705of the indicated kind.
1706@end deftypefn
1707
1708@node SRFI-4 Generic Operations
1709@subsubsection SRFI-4 - Generic operations
1710
1711Guile also provides procedures that operate on all types of uniform numeric
1712vectors. In what is probably a bug, these procedures are currently available in
1713the default environment as well; however prudent hackers will make sure to
1714import @code{(srfi srfi-4 gnu)} before using these.
1715
1716@deftypefn {C Function} int scm_is_uniform_vector (SCM uvec)
1717Return non-zero when @var{uvec} is a uniform numeric vector, zero
1718otherwise.
1719@end deftypefn
1720
1721@deftypefn {C Function} size_t scm_c_uniform_vector_length (SCM uvec)
1722Return the number of elements of @var{uvec} as a @code{size_t}.
1723@end deftypefn
1724
1725@deffn {Scheme Procedure} uniform-vector? obj
1726@deffnx {C Function} scm_uniform_vector_p (obj)
1727Return @code{#t} if @var{obj} is a homogeneous numeric vector of the
1728indicated type.
1729@end deffn
1730
1731@deffn {Scheme Procedure} uniform-vector-length vec
1732@deffnx {C Function} scm_uniform_vector_length (vec)
1733Return the number of elements in @var{vec}.
1734@end deffn
1735
1736@deffn {Scheme Procedure} uniform-vector-ref vec i
1737@deffnx {C Function} scm_uniform_vector_ref (vec i)
1738Return the element at index @var{i} in @var{vec}. The first element
1739in @var{vec} is index 0.
1740@end deffn
1741
1742@deffn {Scheme Procedure} uniform-vector-set! vec i value
1743@deffnx {C Function} scm_uniform_vector_set_x (vec i value)
1744Set the element at index @var{i} in @var{vec} to @var{value}. The
1745first element in @var{vec} is index 0. The return value is
1746unspecified.
1747@end deffn
1748
1749@deffn {Scheme Procedure} uniform-vector->list vec
1750@deffnx {C Function} scm_uniform_vector_to_list (vec)
1751Return a newly allocated list holding all elements of @var{vec}.
1752@end deffn
1753
1754@deftypefn {C Function} {const void *} scm_uniform_vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1755Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
1756returns a pointer to the elements of a uniform numeric vector.
1757@end deftypefn
1758
1759@deftypefn {C Function} {void *} scm_uniform_vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1760Like @code{scm_vector_writable_elements} (@pxref{Vector Accessing from
1761C}), but returns a pointer to the elements of a uniform numeric vector.
1762@end deftypefn
1763
1764Unless you really need to the limited generality of these functions, it is best
1765to use the type-specific functions, or the generalized vector accessors.
1766
1767@node SRFI-4 and Bytevectors
1768@subsubsection SRFI-4 - Relation to bytevectors
1769
1770Guile implements SRFI-4 vectors using bytevectors (@pxref{Bytevectors}). Often
1771when you have a numeric vector, you end up wanting to write its bytes somewhere,
1772or have access to the underlying bytes, or read in bytes from somewhere else.
1773Bytevectors are very good at this sort of thing. But the SRFI-4 APIs are nicer
1774to use when doing number-crunching, because they are addressed by element and
1775not by byte.
1776
1777So as a compromise, Guile allows all bytevector functions to operate on numeric
1778vectors. They address the underlying bytes in the native endianness, as one
1779would expect.
1780
1781Following the same reasoning, that it's just bytes underneath, Guile also allows
1782uniform vectors of a given type to be accessed as if they were of any type. One
1783can fill a @nicode{u32vector}, and access its elements with
1784@nicode{u8vector-ref}. One can use @nicode{f64vector-ref} on bytevectors. It's
1785all the same to Guile.
1786
1787In this way, uniform numeric vectors may be written to and read from
1788input/output ports using the procedures that operate on bytevectors.
1789
1790@xref{Bytevectors}, for more information.
1791
1792
1793@node SRFI-4 Extensions
1794@subsubsection SRFI-4 - Guile extensions
1795
1796Guile defines some useful extensions to SRFI-4, which are not available in the
1797default Guile environment. They may be imported by loading the extensions
1798module:
1799
1800@example
1801(use-modules (srfi srfi-4 gnu))
1802@end example
1803
1804@deffn {Scheme Procedure} any->u8vector obj
1805@deffnx {Scheme Procedure} any->s8vector obj
1806@deffnx {Scheme Procedure} any->u16vector obj
1807@deffnx {Scheme Procedure} any->s16vector obj
1808@deffnx {Scheme Procedure} any->u32vector obj
1809@deffnx {Scheme Procedure} any->s32vector obj
1810@deffnx {Scheme Procedure} any->u64vector obj
1811@deffnx {Scheme Procedure} any->s64vector obj
1812@deffnx {Scheme Procedure} any->f32vector obj
1813@deffnx {Scheme Procedure} any->f64vector obj
1814@deffnx {Scheme Procedure} any->c32vector obj
1815@deffnx {Scheme Procedure} any->c64vector obj
1816@deffnx {C Function} scm_any_to_u8vector (obj)
1817@deffnx {C Function} scm_any_to_s8vector (obj)
1818@deffnx {C Function} scm_any_to_u16vector (obj)
1819@deffnx {C Function} scm_any_to_s16vector (obj)
1820@deffnx {C Function} scm_any_to_u32vector (obj)
1821@deffnx {C Function} scm_any_to_s32vector (obj)
1822@deffnx {C Function} scm_any_to_u64vector (obj)
1823@deffnx {C Function} scm_any_to_s64vector (obj)
1824@deffnx {C Function} scm_any_to_f32vector (obj)
1825@deffnx {C Function} scm_any_to_f64vector (obj)
1826@deffnx {C Function} scm_any_to_c32vector (obj)
1827@deffnx {C Function} scm_any_to_c64vector (obj)
1828Return a (maybe newly allocated) uniform numeric vector of the indicated
1829type, initialized with the elements of @var{obj}, which must be a list,
1830a vector, or a uniform vector. When @var{obj} is already a suitable
1831uniform numeric vector, it is returned unchanged.
1832@end deffn
1833
a0e07ba4
NJ
1834
1835@node SRFI-6
3229f68b 1836@subsection SRFI-6 - Basic String Ports
8742c48b 1837@cindex SRFI-6
a0e07ba4
NJ
1838
1839SRFI-6 defines the procedures @code{open-input-string},
1840@code{open-output-string} and @code{get-output-string}. These
1841procedures are included in the Guile core, so using this module does not
1842make any difference at the moment. But it is possible that support for
1843SRFI-6 will be factored out of the core library in the future, so using
1844this module does not hurt, after all.
1845
1846@node SRFI-8
3229f68b 1847@subsection SRFI-8 - receive
8742c48b 1848@cindex SRFI-8
a0e07ba4
NJ
1849
1850@code{receive} is a syntax for making the handling of multiple-value
1851procedures easier. It is documented in @xref{Multiple Values}.
1852
1853
1854@node SRFI-9
3229f68b 1855@subsection SRFI-9 - define-record-type
8742c48b 1856@cindex SRFI-9
7c2e18cd 1857@cindex record
a0e07ba4 1858
6afe385d
KR
1859This SRFI is a syntax for defining new record types and creating
1860predicate, constructor, and field getter and setter functions. In
1861Guile this is simply an alternate interface to the core record
1862functionality (@pxref{Records}). It can be used with,
a0e07ba4 1863
6afe385d
KR
1864@example
1865(use-modules (srfi srfi-9))
1866@end example
1867
1868@deffn {library syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
1869@sp 1
1870Create a new record type, and make various @code{define}s for using
1871it. This syntax can only occur at the top-level, not nested within
1872some other form.
1873
1874@var{type} is bound to the record type, which is as per the return
1875from the core @code{make-record-type}. @var{type} also provides the
1876name for the record, as per @code{record-type-name}.
1877
1878@var{constructor} is bound to a function to be called as
1879@code{(@var{constructor} fieldval @dots{})} to create a new record of
1880this type. The arguments are initial values for the fields, one
1881argument for each field, in the order they appear in the
1882@code{define-record-type} form.
1883
1884The @var{fieldname}s provide the names for the record fields, as per
1885the core @code{record-type-fields} etc, and are referred to in the
1886subsequent accessor/modifier forms.
1887
ecb87335 1888@var{predicate} is bound to a function to be called as
6afe385d
KR
1889@code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f}
1890according to whether @var{obj} is a record of this type.
1891
1892Each @var{accessor} is bound to a function to be called
1893@code{(@var{accessor} record)} to retrieve the respective field from a
1894@var{record}. Similarly each @var{modifier} is bound to a function to
1895be called @code{(@var{modifier} record val)} to set the respective
1896field in a @var{record}.
1897@end deffn
1898
1899@noindent
1900An example will illustrate typical usage,
a0e07ba4
NJ
1901
1902@example
6afe385d
KR
1903(define-record-type employee-type
1904 (make-employee name age salary)
1905 employee?
1906 (name get-employee-name)
1907 (age get-employee-age set-employee-age)
1908 (salary get-employee-salary set-employee-salary))
a0e07ba4
NJ
1909@end example
1910
6afe385d
KR
1911This creates a new employee data type, with name, age and salary
1912fields. Accessor functions are created for each field, but no
1913modifier function for the name (the intention in this example being
1914that it's established only when an employee object is created). These
1915can all then be used as for example,
a0e07ba4
NJ
1916
1917@example
6afe385d
KR
1918employee-type @result{} #<record-type employee-type>
1919
1920(define fred (make-employee "Fred" 45 20000.00))
1921
1922(employee? fred) @result{} #t
1923(get-employee-age fred) @result{} 45
1924(set-employee-salary fred 25000.00) ;; pay rise
a0e07ba4
NJ
1925@end example
1926
6afe385d
KR
1927The functions created by @code{define-record-type} are ordinary
1928top-level @code{define}s. They can be redefined or @code{set!} as
1929desired, exported from a module, etc.
1930
531c9f1d
AR
1931@unnumberedsubsubsec Non-toplevel Record Definitions
1932
1933The SRFI-9 specification explicitly disallows record definitions in a
1934non-toplevel context, such as inside @code{lambda} body or inside a
1935@var{let} block. However, Guile's implementation does not enforce that
1936restriction.
1937
167510bc 1938@unnumberedsubsubsec Custom Printers
e525e4e4 1939
45f3d9b6 1940You may use @code{set-record-type-printer!} to customize the default printing
853cb356
NI
1941behavior of records. This is a Guile extension and is not part of SRFI-9. It
1942is located in the @nicode{(srfi srfi-9 gnu)} module.
e525e4e4 1943
45f3d9b6 1944@deffn {Scheme Syntax} set-record-type-printer! name thunk
e525e4e4
NI
1945Where @var{type} corresponds to the first argument of @code{define-record-type},
1946and @var{thunk} is a procedure accepting two arguments, the record to print, and
1947an output port.
e525e4e4
NI
1948@end deffn
1949
1950@noindent
167510bc 1951This example prints the employee's name in brackets, for instance @code{[Fred]}.
e525e4e4
NI
1952
1953@example
167510bc 1954(set-record-type-printer! employee-type
e525e4e4
NI
1955 (lambda (record port)
1956 (write-char #\[ port)
1957 (display (get-employee-name record) port)
1958 (write-char #\] port)))
1959@end example
a0e07ba4
NJ
1960
1961@node SRFI-10
3229f68b 1962@subsection SRFI-10 - Hash-Comma Reader Extension
8742c48b 1963@cindex SRFI-10
a0e07ba4
NJ
1964
1965@cindex hash-comma
1966@cindex #,()
633acbe2
KR
1967This SRFI implements a reader extension @code{#,()} called hash-comma.
1968It allows the reader to give new kinds of objects, for use both in
1969data and as constants or literals in source code. This feature is
1970available with
a0e07ba4 1971
633acbe2
KR
1972@example
1973(use-modules (srfi srfi-10))
1974@end example
1975
1976@noindent
1977The new read syntax is of the form
a0e07ba4
NJ
1978
1979@example
633acbe2 1980#,(@var{tag} @var{arg}@dots{})
a0e07ba4
NJ
1981@end example
1982
633acbe2
KR
1983@noindent
1984where @var{tag} is a symbol and the @var{arg}s are objects taken as
1985parameters. @var{tag}s are registered with the following procedure.
a0e07ba4 1986
633acbe2
KR
1987@deffn {Scheme Procedure} define-reader-ctor tag proc
1988Register @var{proc} as the constructor for a hash-comma read syntax
679cceed 1989starting with symbol @var{tag}, i.e.@: @nicode{#,(@var{tag} arg@dots{})}.
633acbe2
KR
1990@var{proc} is called with the given arguments @code{(@var{proc}
1991arg@dots{})} and the object it returns is the result of the read.
1992@end deffn
a0e07ba4 1993
633acbe2
KR
1994@noindent
1995For example, a syntax giving a list of @var{N} copies of an object.
1996
1997@example
1998(define-reader-ctor 'repeat
1999 (lambda (obj reps)
2000 (make-list reps obj)))
2001
2002(display '#,(repeat 99 3))
2003@print{} (99 99 99)
2004@end example
2005
2006Notice the quote @nicode{'} when the @nicode{#,( )} is used. The
2007@code{repeat} handler returns a list and the program must quote to use
2008it literally, the same as any other list. Ie.
2009
2010@example
2011(display '#,(repeat 99 3))
a0e07ba4 2012@result{}
633acbe2
KR
2013(display '(99 99 99))
2014@end example
a0e07ba4 2015
633acbe2
KR
2016When a handler returns an object which is self-evaluating, like a
2017number or a string, then there's no need for quoting, just as there's
2018no need when giving those directly as literals. For example an
2019addition,
a0e07ba4 2020
633acbe2
KR
2021@example
2022(define-reader-ctor 'sum
2023 (lambda (x y)
2024 (+ x y)))
2025(display #,(sum 123 456)) @print{} 579
2026@end example
2027
2028A typical use for @nicode{#,()} is to get a read syntax for objects
2029which don't otherwise have one. For example, the following allows a
2030hash table to be given literally, with tags and values, ready for fast
2031lookup.
2032
2033@example
2034(define-reader-ctor 'hash
2035 (lambda elems
2036 (let ((table (make-hash-table)))
2037 (for-each (lambda (elem)
01549abb
KR
2038 (apply hash-set! table elem))
2039 elems)
633acbe2
KR
2040 table)))
2041
2042(define (animal->family animal)
2043 (hash-ref '#,(hash ("tiger" "cat")
2044 ("lion" "cat")
2045 ("wolf" "dog"))
2046 animal))
2047
2048(animal->family "lion") @result{} "cat"
2049@end example
2050
2051Or for example the following is a syntax for a compiled regular
2052expression (@pxref{Regular Expressions}).
2053
2054@example
2055(use-modules (ice-9 regex))
2056
2057(define-reader-ctor 'regexp make-regexp)
2058
2059(define (extract-angs str)
2060 (let ((match (regexp-exec '#,(regexp "<([A-Z0-9]+)>") str)))
2061 (and match
2062 (match:substring match 1))))
2063
2064(extract-angs "foo <BAR> quux") @result{} "BAR"
2065@end example
2066
2067@sp 1
2068@nicode{#,()} is somewhat similar to @code{define-macro}
2069(@pxref{Macros}) in that handler code is run to produce a result, but
2070@nicode{#,()} operates at the read stage, so it can appear in data for
2071@code{read} (@pxref{Scheme Read}), not just in code to be executed.
2072
2073Because @nicode{#,()} is handled at read-time it has no direct access
2074to variables etc. A symbol in the arguments is just a symbol, not a
2075variable reference. The arguments are essentially constants, though
2076the handler procedure can use them in any complicated way it might
2077want.
2078
2079Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
2080globally, there's no need to use @code{(srfi srfi-10)} in later
2081modules. Similarly the tags registered are global and can be used
2082anywhere once registered.
2083
2084There's no attempt to record what previous @nicode{#,()} forms have
2085been seen, if two identical forms occur then two calls are made to the
2086handler procedure. The handler might like to maintain a cache or
2087similar to avoid making copies of large objects, depending on expected
2088usage.
2089
2090In code the best uses of @nicode{#,()} are generally when there's a
2091lot of objects of a particular kind as literals or constants. If
2092there's just a few then some local variables and initializers are
2093fine, but that becomes tedious and error prone when there's a lot, and
2094the anonymous and compact syntax of @nicode{#,()} is much better.
a0e07ba4
NJ
2095
2096
2097@node SRFI-11
3229f68b 2098@subsection SRFI-11 - let-values
8742c48b 2099@cindex SRFI-11
a0e07ba4 2100
8742c48b 2101@findex let-values
c010924a 2102@findex let*-values
a0e07ba4 2103This module implements the binding forms for multiple values
c010924a 2104@code{let-values} and @code{let*-values}. These forms are similar to
a0e07ba4
NJ
2105@code{let} and @code{let*} (@pxref{Local Bindings}), but they support
2106binding of the values returned by multiple-valued expressions.
2107
2108Write @code{(use-modules (srfi srfi-11))} to make the bindings
2109available.
2110
2111@lisp
2112(let-values (((x y) (values 1 2))
2113 ((z f) (values 3 4)))
2114 (+ x y z f))
2115@result{}
211610
2117@end lisp
2118
2119@code{let-values} performs all bindings simultaneously, which means that
2120no expression in the binding clauses may refer to variables bound in the
c010924a 2121same clause list. @code{let*-values}, on the other hand, performs the
a0e07ba4
NJ
2122bindings sequentially, just like @code{let*} does for single-valued
2123expressions.
2124
2125
2126@node SRFI-13
3229f68b 2127@subsection SRFI-13 - String Library
8742c48b 2128@cindex SRFI-13
a0e07ba4 2129
5676b4fa 2130The SRFI-13 procedures are always available, @xref{Strings}.
a0e07ba4
NJ
2131
2132@node SRFI-14
3229f68b 2133@subsection SRFI-14 - Character-set Library
8742c48b 2134@cindex SRFI-14
a0e07ba4 2135
050ab45f
MV
2136The SRFI-14 data type and procedures are always available,
2137@xref{Character Sets}.
a0e07ba4
NJ
2138
2139@node SRFI-16
3229f68b 2140@subsection SRFI-16 - case-lambda
8742c48b 2141@cindex SRFI-16
7c2e18cd
KR
2142@cindex variable arity
2143@cindex arity, variable
a0e07ba4 2144
f916cbc4
AW
2145SRFI-16 defines a variable-arity @code{lambda} form,
2146@code{case-lambda}. This form is available in the default Guile
2147environment. @xref{Case-lambda}, for more information.
a0e07ba4
NJ
2148
2149@node SRFI-17
3229f68b 2150@subsection SRFI-17 - Generalized set!
8742c48b 2151@cindex SRFI-17
a0e07ba4 2152
9a18d8d4
KR
2153This SRFI implements a generalized @code{set!}, allowing some
2154``referencing'' functions to be used as the target location of a
2155@code{set!}. This feature is available from
2156
2157@example
2158(use-modules (srfi srfi-17))
2159@end example
2160
2161@noindent
2162For example @code{vector-ref} is extended so that
2163
2164@example
2165(set! (vector-ref vec idx) new-value)
2166@end example
2167
2168@noindent
2169is equivalent to
2170
2171@example
2172(vector-set! vec idx new-value)
2173@end example
2174
2175The idea is that a @code{vector-ref} expression identifies a location,
2176which may be either fetched or stored. The same form is used for the
2177location in both cases, encouraging visual clarity. This is similar
2178to the idea of an ``lvalue'' in C.
2179
2180The mechanism for this kind of @code{set!} is in the Guile core
2181(@pxref{Procedures with Setters}). This module adds definitions of
2182the following functions as procedures with setters, allowing them to
2183be targets of a @code{set!},
2184
2185@quotation
2186@nicode{car}, @nicode{cdr}, @nicode{caar}, @nicode{cadr},
2187@nicode{cdar}, @nicode{cddr}, @nicode{caaar}, @nicode{caadr},
2188@nicode{cadar}, @nicode{caddr}, @nicode{cdaar}, @nicode{cdadr},
2189@nicode{cddar}, @nicode{cdddr}, @nicode{caaaar}, @nicode{caaadr},
2190@nicode{caadar}, @nicode{caaddr}, @nicode{cadaar}, @nicode{cadadr},
2191@nicode{caddar}, @nicode{cadddr}, @nicode{cdaaar}, @nicode{cdaadr},
2192@nicode{cdadar}, @nicode{cdaddr}, @nicode{cddaar}, @nicode{cddadr},
2193@nicode{cdddar}, @nicode{cddddr}
2194
2195@nicode{string-ref}, @nicode{vector-ref}
2196@end quotation
2197
2198The SRFI specifies @code{setter} (@pxref{Procedures with Setters}) as
2199a procedure with setter, allowing the setter for a procedure to be
2200changed, eg.@: @code{(set! (setter foo) my-new-setter-handler)}.
2201Currently Guile does not implement this, a setter can only be
2202specified on creation (@code{getter-with-setter} below).
2203
2204@defun getter-with-setter
2205The same as the Guile core @code{make-procedure-with-setter}
2206(@pxref{Procedures with Setters}).
2207@end defun
a0e07ba4 2208
12991fed 2209
e68f492a
JG
2210@node SRFI-18
2211@subsection SRFI-18 - Multithreading support
2212@cindex SRFI-18
2213
2214This is an implementation of the SRFI-18 threading and synchronization
2215library. The functions and variables described here are provided by
2216
2217@example
2218(use-modules (srfi srfi-18))
2219@end example
2220
2221As a general rule, the data types and functions in this SRFI-18
2222implementation are compatible with the types and functions in Guile's
2223core threading code. For example, mutexes created with the SRFI-18
2224@code{make-mutex} function can be passed to the built-in Guile
2225function @code{lock-mutex} (@pxref{Mutexes and Condition Variables}),
2226and mutexes created with the built-in Guile function @code{make-mutex}
2227can be passed to the SRFI-18 function @code{mutex-lock!}. Cases in
2228which this does not hold true are noted in the following sections.
2229
2230@menu
2231* SRFI-18 Threads:: Executing code
2232* SRFI-18 Mutexes:: Mutual exclusion devices
2233* SRFI-18 Condition variables:: Synchronizing of groups of threads
2234* SRFI-18 Time:: Representation of times and durations
2235* SRFI-18 Exceptions:: Signalling and handling errors
2236@end menu
2237
2238@node SRFI-18 Threads
2239@subsubsection SRFI-18 Threads
2240
2241Threads created by SRFI-18 differ in two ways from threads created by
2242Guile's built-in thread functions. First, a thread created by SRFI-18
2243@code{make-thread} begins in a blocked state and will not start
2244execution until @code{thread-start!} is called on it. Second, SRFI-18
2245threads are constructed with a top-level exception handler that
2246captures any exceptions that are thrown on thread exit. In all other
2247regards, SRFI-18 threads are identical to normal Guile threads.
2248
2249@defun current-thread
2250Returns the thread that called this function. This is the same
2251procedure as the same-named built-in procedure @code{current-thread}
2252(@pxref{Threads}).
2253@end defun
2254
2255@defun thread? obj
2256Returns @code{#t} if @var{obj} is a thread, @code{#f} otherwise. This
2257is the same procedure as the same-named built-in procedure
2258@code{thread?} (@pxref{Threads}).
2259@end defun
2260
2261@defun make-thread thunk [name]
2262Call @code{thunk} in a new thread and with a new dynamic state,
2263returning the new thread and optionally assigning it the object name
2264@var{name}, which may be any Scheme object.
2265
2266Note that the name @code{make-thread} conflicts with the
2267@code{(ice-9 threads)} function @code{make-thread}. Applications
2268wanting to use both of these functions will need to refer to them by
2269different names.
2270@end defun
2271
2272@defun thread-name thread
2273Returns the name assigned to @var{thread} at the time of its creation,
2274or @code{#f} if it was not given a name.
2275@end defun
2276
2277@defun thread-specific thread
2278@defunx thread-specific-set! thread obj
2279Get or set the ``object-specific'' property of @var{thread}. In
2280Guile's implementation of SRFI-18, this value is stored as an object
2281property, and will be @code{#f} if not set.
2282@end defun
2283
2284@defun thread-start! thread
2285Unblocks @var{thread} and allows it to begin execution if it has not
2286done so already.
2287@end defun
2288
2289@defun thread-yield!
2290If one or more threads are waiting to execute, calling
2291@code{thread-yield!} forces an immediate context switch to one of them.
2292Otherwise, @code{thread-yield!} has no effect. @code{thread-yield!}
2293behaves identically to the Guile built-in function @code{yield}.
2294@end defun
2295
2296@defun thread-sleep! timeout
2297The current thread waits until the point specified by the time object
2298@var{timeout} is reached (@pxref{SRFI-18 Time}). This blocks the
2299thread only if @var{timeout} represents a point in the future. it is
2300an error for @var{timeout} to be @code{#f}.
2301@end defun
2302
2303@defun thread-terminate! thread
2304Causes an abnormal termination of @var{thread}. If @var{thread} is
2305not already terminated, all mutexes owned by @var{thread} become
2306unlocked/abandoned. If @var{thread} is the current thread,
2307@code{thread-terminate!} does not return. Otherwise
2308@code{thread-terminate!} returns an unspecified value; the termination
2309of @var{thread} will occur before @code{thread-terminate!} returns.
2310Subsequent attempts to join on @var{thread} will cause a ``terminated
2311thread exception'' to be raised.
2312
2313@code{thread-terminate!} is compatible with the thread cancellation
2314procedures in the core threads API (@pxref{Threads}) in that if a
2315cleanup handler has been installed for the target thread, it will be
2316called before the thread exits and its return value (or exception, if
2317any) will be stored for later retrieval via a call to
2318@code{thread-join!}.
2319@end defun
2320
2321@defun thread-join! thread [timeout [timeout-val]]
2322Wait for @var{thread} to terminate and return its exit value. When a
2323time value @var{timeout} is given, it specifies a point in time where
2324the waiting should be aborted. When the waiting is aborted,
2325@var{timeoutval} is returned if it is specified; otherwise, a
2326@code{join-timeout-exception} exception is raised
2327(@pxref{SRFI-18 Exceptions}). Exceptions may also be raised if the
2328thread was terminated by a call to @code{thread-terminate!}
2329(@code{terminated-thread-exception} will be raised) or if the thread
2330exited by raising an exception that was handled by the top-level
2331exception handler (@code{uncaught-exception} will be raised; the
2332original exception can be retrieved using
2333@code{uncaught-exception-reason}).
2334@end defun
2335
2336
2337@node SRFI-18 Mutexes
2338@subsubsection SRFI-18 Mutexes
2339
2340The behavior of Guile's built-in mutexes is parameterized via a set of
2341flags passed to the @code{make-mutex} procedure in the core
2342(@pxref{Mutexes and Condition Variables}). To satisfy the requirements
2343for mutexes specified by SRFI-18, the @code{make-mutex} procedure
2344described below sets the following flags:
2345@itemize @bullet
2346@item
2347@code{recursive}: the mutex can be locked recursively
2348@item
2349@code{unchecked-unlock}: attempts to unlock a mutex that is already
2350unlocked will not raise an exception
2351@item
2352@code{allow-external-unlock}: the mutex can be unlocked by any thread,
2353not just the thread that locked it originally
2354@end itemize
2355
2356@defun make-mutex [name]
2357Returns a new mutex, optionally assigning it the object name
2358@var{name}, which may be any Scheme object. The returned mutex will be
2359created with the configuration described above. Note that the name
2360@code{make-mutex} conflicts with Guile core function @code{make-mutex}.
2361Applications wanting to use both of these functions will need to refer
2362to them by different names.
2363@end defun
2364
2365@defun mutex-name mutex
2366Returns the name assigned to @var{mutex} at the time of its creation,
2367or @code{#f} if it was not given a name.
2368@end defun
2369
2370@defun mutex-specific mutex
2371@defunx mutex-specific-set! mutex obj
2372Get or set the ``object-specific'' property of @var{mutex}. In Guile's
2373implementation of SRFI-18, this value is stored as an object property,
2374and will be @code{#f} if not set.
2375@end defun
2376
2377@defun mutex-state mutex
2378Returns information about the state of @var{mutex}. Possible values
2379are:
2380@itemize @bullet
2381@item
2382thread @code{T}: the mutex is in the locked/owned state and thread T
2383is the owner of the mutex
2384@item
2385symbol @code{not-owned}: the mutex is in the locked/not-owned state
2386@item
2387symbol @code{abandoned}: the mutex is in the unlocked/abandoned state
2388@item
2389symbol @code{not-abandoned}: the mutex is in the
2390unlocked/not-abandoned state
2391@end itemize
2392@end defun
2393
2394@defun mutex-lock! mutex [timeout [thread]]
2395Lock @var{mutex}, optionally specifying a time object @var{timeout}
2396after which to abort the lock attempt and a thread @var{thread} giving
2397a new owner for @var{mutex} different than the current thread. This
2398procedure has the same behavior as the @code{lock-mutex} procedure in
2399the core library.
2400@end defun
2401
2402@defun mutex-unlock! mutex [condition-variable [timeout]]
2403Unlock @var{mutex}, optionally specifying a condition variable
2404@var{condition-variable} on which to wait, either indefinitely or,
2405optionally, until the time object @var{timeout} has passed, to be
2406signalled. This procedure has the same behavior as the
2407@code{unlock-mutex} procedure in the core library.
2408@end defun
2409
2410
2411@node SRFI-18 Condition variables
2412@subsubsection SRFI-18 Condition variables
2413
2414SRFI-18 does not specify a ``wait'' function for condition variables.
2415Waiting on a condition variable can be simulated using the SRFI-18
2416@code{mutex-unlock!} function described in the previous section, or
2417Guile's built-in @code{wait-condition-variable} procedure can be used.
2418
2419@defun condition-variable? obj
2420Returns @code{#t} if @var{obj} is a condition variable, @code{#f}
2421otherwise. This is the same procedure as the same-named built-in
2422procedure
2423(@pxref{Mutexes and Condition Variables, @code{condition-variable?}}).
2424@end defun
2425
2426@defun make-condition-variable [name]
2427Returns a new condition variable, optionally assigning it the object
2428name @var{name}, which may be any Scheme object. This procedure
2429replaces a procedure of the same name in the core library.
2430@end defun
2431
2432@defun condition-variable-name condition-variable
2433Returns the name assigned to @var{thread} at the time of its creation,
2434or @code{#f} if it was not given a name.
2435@end defun
2436
2437@defun condition-variable-specific condition-variable
2438@defunx condition-variable-specific-set! condition-variable obj
2439Get or set the ``object-specific'' property of
2440@var{condition-variable}. In Guile's implementation of SRFI-18, this
2441value is stored as an object property, and will be @code{#f} if not
2442set.
2443@end defun
2444
2445@defun condition-variable-signal! condition-variable
2446@defunx condition-variable-broadcast! condition-variable
2447Wake up one thread that is waiting for @var{condition-variable}, in
2448the case of @code{condition-variable-signal!}, or all threads waiting
2449for it, in the case of @code{condition-variable-broadcast!}. The
2450behavior of these procedures is equivalent to that of the procedures
2451@code{signal-condition-variable} and
2452@code{broadcast-condition-variable} in the core library.
2453@end defun
2454
2455
2456@node SRFI-18 Time
2457@subsubsection SRFI-18 Time
2458
2459The SRFI-18 time functions manipulate time in two formats: a
2460``time object'' type that represents an absolute point in time in some
2461implementation-specific way; and the number of seconds since some
2462unspecified ``epoch''. In Guile's implementation, the epoch is the
2463Unix epoch, 00:00:00 UTC, January 1, 1970.
2464
2465@defun current-time
2466Return the current time as a time object. This procedure replaces
2467the procedure of the same name in the core library, which returns the
2468current time in seconds since the epoch.
2469@end defun
2470
2471@defun time? obj
2472Returns @code{#t} if @var{obj} is a time object, @code{#f} otherwise.
2473@end defun
2474
2475@defun time->seconds time
2476@defunx seconds->time seconds
2477Convert between time objects and numerical values representing the
2478number of seconds since the epoch. When converting from a time object
2479to seconds, the return value is the number of seconds between
2480@var{time} and the epoch. When converting from seconds to a time
2481object, the return value is a time object that represents a time
2482@var{seconds} seconds after the epoch.
2483@end defun
2484
2485
2486@node SRFI-18 Exceptions
2487@subsubsection SRFI-18 Exceptions
2488
2489SRFI-18 exceptions are identical to the exceptions provided by
2490Guile's implementation of SRFI-34. The behavior of exception
2491handlers invoked to handle exceptions thrown from SRFI-18 functions,
2492however, differs from the conventional behavior of SRFI-34 in that
2493the continuation of the handler is the same as that of the call to
2494the function. Handlers are called in a tail-recursive manner; the
2495exceptions do not ``bubble up''.
2496
2497@defun current-exception-handler
2498Returns the current exception handler.
2499@end defun
2500
2501@defun with-exception-handler handler thunk
2502Installs @var{handler} as the current exception handler and calls the
2503procedure @var{thunk} with no arguments, returning its value as the
2504value of the exception. @var{handler} must be a procedure that accepts
2505a single argument. The current exception handler at the time this
2506procedure is called will be restored after the call returns.
2507@end defun
2508
2509@defun raise obj
2510Raise @var{obj} as an exception. This is the same procedure as the
2511same-named procedure defined in SRFI 34.
2512@end defun
2513
2514@defun join-timeout-exception? obj
2515Returns @code{#t} if @var{obj} is an exception raised as the result of
2516performing a timed join on a thread that does not exit within the
2517specified timeout, @code{#f} otherwise.
2518@end defun
2519
2520@defun abandoned-mutex-exception? obj
2521Returns @code{#t} if @var{obj} is an exception raised as the result of
2522attempting to lock a mutex that has been abandoned by its owner thread,
2523@code{#f} otherwise.
2524@end defun
2525
2526@defun terminated-thread-exception? obj
2527Returns @code{#t} if @var{obj} is an exception raised as the result of
2528joining on a thread that exited as the result of a call to
2529@code{thread-terminate!}.
2530@end defun
2531
2532@defun uncaught-exception? obj
2533@defunx uncaught-exception-reason exc
2534@code{uncaught-exception?} returns @code{#t} if @var{obj} is an
2535exception thrown as the result of joining a thread that exited by
2536raising an exception that was handled by the top-level exception
2537handler installed by @code{make-thread}. When this occurs, the
2538original exception is preserved as part of the exception thrown by
2539@code{thread-join!} and can be accessed by calling
2540@code{uncaught-exception-reason} on that exception. Note that
2541because this exception-preservation mechanism is a side-effect of
2542@code{make-thread}, joining on threads that exited as described above
2543but were created by other means will not raise this
2544@code{uncaught-exception} error.
2545@end defun
2546
2547
12991fed 2548@node SRFI-19
3229f68b 2549@subsection SRFI-19 - Time/Date Library
8742c48b 2550@cindex SRFI-19
7c2e18cd
KR
2551@cindex time
2552@cindex date
12991fed 2553
85600a0f
KR
2554This is an implementation of the SRFI-19 time/date library. The
2555functions and variables described here are provided by
12991fed
TTN
2556
2557@example
85600a0f 2558(use-modules (srfi srfi-19))
12991fed
TTN
2559@end example
2560
7d281fa5
KR
2561@strong{Caution}: The current code in this module incorrectly extends
2562the Gregorian calendar leap year rule back prior to the introduction
2563of those reforms in 1582 (or the appropriate year in various
2564countries). The Julian calendar was used prior to 1582, and there
2565were 10 days skipped for the reform, but the code doesn't implement
2566that.
2567
2568This will be fixed some time. Until then calculations for 1583
2569onwards are correct, but prior to that any day/month/year and day of
2570the week calculations are wrong.
2571
85600a0f
KR
2572@menu
2573* SRFI-19 Introduction::
2574* SRFI-19 Time::
2575* SRFI-19 Date::
2576* SRFI-19 Time/Date conversions::
2577* SRFI-19 Date to string::
2578* SRFI-19 String to date::
2579@end menu
12991fed 2580
85600a0f 2581@node SRFI-19 Introduction
3229f68b 2582@subsubsection SRFI-19 Introduction
85600a0f
KR
2583
2584@cindex universal time
2585@cindex atomic time
2586@cindex UTC
2587@cindex TAI
2588This module implements time and date representations and calculations,
2589in various time systems, including universal time (UTC) and atomic
2590time (TAI).
2591
2592For those not familiar with these time systems, TAI is based on a
2593fixed length second derived from oscillations of certain atoms. UTC
2594differs from TAI by an integral number of seconds, which is increased
2595or decreased at announced times to keep UTC aligned to a mean solar
2596day (the orbit and rotation of the earth are not quite constant).
2597
2598@cindex leap second
2599So far, only increases in the TAI
2600@tex
2601$\leftrightarrow$
2602@end tex
2603@ifnottex
2604<->
2605@end ifnottex
2606UTC difference have been needed. Such an increase is a ``leap
2607second'', an extra second of TAI introduced at the end of a UTC day.
2608When working entirely within UTC this is never seen, every day simply
2609has 86400 seconds. But when converting from TAI to a UTC date, an
2610extra 23:59:60 is present, where normally a day would end at 23:59:59.
2611Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
2612seconds.
2613
2614@cindex system clock
2615In the current implementation, the system clock is assumed to be UTC,
2616and a table of leap seconds in the code converts to TAI. See comments
2617in @file{srfi-19.scm} for how to update this table.
2618
2619@cindex julian day
2620@cindex modified julian day
2621Also, for those not familiar with the terminology, a @dfn{Julian Day}
2622is a real number which is a count of days and fraction of a day, in
2623UTC, starting from -4713-01-01T12:00:00Z, ie.@: midday Monday 1 Jan
7c2e18cd
KR
26244713 B.C. A @dfn{Modified Julian Day} is the same, but starting from
26251858-11-17T00:00:00Z, ie.@: midnight 17 November 1858 UTC. That time
2626is julian day 2400000.5.
85600a0f
KR
2627
2628@c The SRFI-1 spec says -4714-11-24T12:00:00Z (November 24, -4714 at
2629@c noon, UTC), but this is incorrect. It looks like it might have
2630@c arisen from the code incorrectly treating years a multiple of 100
7c2e18cd 2631@c but not 400 prior to 1582 as non-leap years, where instead the Julian
85600a0f
KR
2632@c calendar should be used so all multiples of 4 before 1582 are leap
2633@c years.
2634
2635
2636@node SRFI-19 Time
3229f68b 2637@subsubsection SRFI-19 Time
85600a0f
KR
2638@cindex time
2639
2640A @dfn{time} object has type, seconds and nanoseconds fields
2641representing a point in time starting from some epoch. This is an
2642arbitrary point in time, not just a time of day. Although times are
2643represented in nanoseconds, the actual resolution may be lower.
2644
2645The following variables hold the possible time types. For instance
2646@code{(current-time time-process)} would give the current CPU process
2647time.
2648
2649@defvar time-utc
2650Universal Coordinated Time (UTC).
2651@cindex UTC
2652@end defvar
12991fed 2653
85600a0f
KR
2654@defvar time-tai
2655International Atomic Time (TAI).
2656@cindex TAI
2657@end defvar
12991fed 2658
85600a0f
KR
2659@defvar time-monotonic
2660Monotonic time, meaning a monotonically increasing time starting from
2661an unspecified epoch.
12991fed 2662
85600a0f
KR
2663Note that in the current implementation @code{time-monotonic} is the
2664same as @code{time-tai}, and unfortunately is therefore affected by
2665adjustments to the system clock. Perhaps this will change in the
2666future.
2667@end defvar
12991fed 2668
85600a0f
KR
2669@defvar time-duration
2670A duration, meaning simply a difference between two times.
2671@end defvar
12991fed 2672
85600a0f
KR
2673@defvar time-process
2674CPU time spent in the current process, starting from when the process
2675began.
2676@cindex process time
2677@end defvar
12991fed 2678
85600a0f
KR
2679@defvar time-thread
2680CPU time spent in the current thread. Not currently implemented.
2681@cindex thread time
2682@end defvar
12991fed 2683
85600a0f
KR
2684@sp 1
2685@defun time? obj
2686Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
2687@end defun
2688
2689@defun make-time type nanoseconds seconds
2690Create a time object with the given @var{type}, @var{seconds} and
2691@var{nanoseconds}.
2692@end defun
2693
2694@defun time-type time
2695@defunx time-nanosecond time
2696@defunx time-second time
2697@defunx set-time-type! time type
2698@defunx set-time-nanosecond! time nsec
2699@defunx set-time-second! time sec
2700Get or set the type, seconds or nanoseconds fields of a time object.
2701
2702@code{set-time-type!} merely changes the field, it doesn't convert the
2703time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
2704@end defun
2705
2706@defun copy-time time
2707Return a new time object, which is a copy of the given @var{time}.
2708@end defun
2709
2710@defun current-time [type]
2711Return the current time of the given @var{type}. The default
2712@var{type} is @code{time-utc}.
2713
2714Note that the name @code{current-time} conflicts with the Guile core
e68f492a
JG
2715@code{current-time} function (@pxref{Time}) as well as the SRFI-18
2716@code{current-time} function (@pxref{SRFI-18 Time}). Applications
2717wanting to use more than one of these functions will need to refer to
2718them by different names.
85600a0f
KR
2719@end defun
2720
2721@defun time-resolution [type]
2722Return the resolution, in nanoseconds, of the given time @var{type}.
2723The default @var{type} is @code{time-utc}.
2724@end defun
2725
2726@defun time<=? t1 t2
2727@defunx time<? t1 t2
2728@defunx time=? t1 t2
2729@defunx time>=? t1 t2
2730@defunx time>? t1 t2
2731Return @code{#t} or @code{#f} according to the respective relation
2732between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
2733must be the same time type.
2734@end defun
2735
2736@defun time-difference t1 t2
2737@defunx time-difference! t1 t2
2738Return a time object of type @code{time-duration} representing the
2739period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
2740the same time type.
2741
2742@code{time-difference} returns a new time object,
2743@code{time-difference!} may modify @var{t1} to form its return.
2744@end defun
2745
2746@defun add-duration time duration
2747@defunx add-duration! time duration
2748@defunx subtract-duration time duration
2749@defunx subtract-duration! time duration
2750Return a time object which is @var{time} with the given @var{duration}
2751added or subtracted. @var{duration} must be a time object of type
2752@code{time-duration}.
2753
2754@code{add-duration} and @code{subtract-duration} return a new time
2755object. @code{add-duration!} and @code{subtract-duration!} may modify
2756the given @var{time} to form their return.
2757@end defun
2758
2759
2760@node SRFI-19 Date
3229f68b 2761@subsubsection SRFI-19 Date
85600a0f
KR
2762@cindex date
2763
2764A @dfn{date} object represents a date in the Gregorian calendar and a
2765time of day on that date in some timezone.
2766
2767The fields are year, month, day, hour, minute, second, nanoseconds and
2768timezone. A date object is immutable, its fields can be read but they
2769cannot be modified once the object is created.
2770
2771@defun date? obj
2772Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
2773@end defun
2774
2775@defun make-date nsecs seconds minutes hours date month year zone-offset
2776Create a new date object.
2777@c
2778@c FIXME: What can we say about the ranges of the values. The
2779@c current code looks it doesn't normalize, but expects then in their
2780@c usual range already.
2781@c
2782@end defun
2783
2784@defun date-nanosecond date
2785Nanoseconds, 0 to 999999999.
2786@end defun
2787
2788@defun date-second date
7c2e18cd
KR
2789Seconds, 0 to 59, or 60 for a leap second. 60 is never seen when working
2790entirely within UTC, it's only when converting to or from TAI.
85600a0f
KR
2791@end defun
2792
2793@defun date-minute date
2794Minutes, 0 to 59.
2795@end defun
2796
2797@defun date-hour date
2798Hour, 0 to 23.
2799@end defun
2800
2801@defun date-day date
2802Day of the month, 1 to 31 (or less, according to the month).
2803@end defun
2804
2805@defun date-month date
2806Month, 1 to 12.
2807@end defun
2808
2809@defun date-year date
7c2e18cd
KR
2810Year, eg.@: 2003. Dates B.C.@: are negative, eg.@: @math{-46} is 46
2811B.C. There is no year 0, year @math{-1} is followed by year 1.
85600a0f
KR
2812@end defun
2813
2814@defun date-zone-offset date
2815Time zone, an integer number of seconds east of Greenwich.
2816@end defun
2817
2818@defun date-year-day date
2819Day of the year, starting from 1 for 1st January.
2820@end defun
2821
2822@defun date-week-day date
2823Day of the week, starting from 0 for Sunday.
2824@end defun
2825
2826@defun date-week-number date dstartw
2827Week of the year, ignoring a first partial week. @var{dstartw} is the
2828day of the week which is taken to start a week, 0 for Sunday, 1 for
2829Monday, etc.
2830@c
2831@c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
2832@c The code looks like it's 0, if that's the correct intention.
2833@c
2834@end defun
2835
2836@c The SRFI text doesn't actually give the default for tz-offset, but
2837@c the reference implementation has the local timezone and the
2838@c conversions functions all specify that, so it should be ok to
2839@c document it here.
2840@c
2841@defun current-date [tz-offset]
7c2e18cd
KR
2842Return a date object representing the current date/time, in UTC offset
2843by @var{tz-offset}. @var{tz-offset} is seconds east of Greenwich and
2844defaults to the local timezone.
85600a0f
KR
2845@end defun
2846
2847@defun current-julian-day
2848@cindex julian day
2849Return the current Julian Day.
2850@end defun
2851
2852@defun current-modified-julian-day
2853@cindex modified julian day
2854Return the current Modified Julian Day.
2855@end defun
2856
2857
2858@node SRFI-19 Time/Date conversions
3229f68b 2859@subsubsection SRFI-19 Time/Date conversions
7c2e18cd
KR
2860@cindex time conversion
2861@cindex date conversion
85600a0f
KR
2862
2863@defun date->julian-day date
2864@defunx date->modified-julian-day date
2865@defunx date->time-monotonic date
2866@defunx date->time-tai date
2867@defunx date->time-utc date
2868@end defun
2869@defun julian-day->date jdn [tz-offset]
2870@defunx julian-day->time-monotonic jdn
2871@defunx julian-day->time-tai jdn
2872@defunx julian-day->time-utc jdn
2873@end defun
2874@defun modified-julian-day->date jdn [tz-offset]
2875@defunx modified-julian-day->time-monotonic jdn
2876@defunx modified-julian-day->time-tai jdn
2877@defunx modified-julian-day->time-utc jdn
2878@end defun
2879@defun time-monotonic->date time [tz-offset]
2880@defunx time-monotonic->time-tai time
2881@defunx time-monotonic->time-tai! time
2882@defunx time-monotonic->time-utc time
2883@defunx time-monotonic->time-utc! time
2884@end defun
2885@defun time-tai->date time [tz-offset]
2886@defunx time-tai->julian-day time
2887@defunx time-tai->modified-julian-day time
2888@defunx time-tai->time-monotonic time
2889@defunx time-tai->time-monotonic! time
2890@defunx time-tai->time-utc time
2891@defunx time-tai->time-utc! time
2892@end defun
2893@defun time-utc->date time [tz-offset]
2894@defunx time-utc->julian-day time
2895@defunx time-utc->modified-julian-day time
2896@defunx time-utc->time-monotonic time
2897@defunx time-utc->time-monotonic! time
2898@defunx time-utc->time-tai time
2899@defunx time-utc->time-tai! time
2900@sp 1
2901Convert between dates, times and days of the respective types. For
2902instance @code{time-tai->time-utc} accepts a @var{time} object of type
2903@code{time-tai} and returns an object of type @code{time-utc}.
2904
85600a0f
KR
2905The @code{!} variants may modify their @var{time} argument to form
2906their return. The plain functions create a new object.
702e6e09
KR
2907
2908For conversions to dates, @var{tz-offset} is seconds east of
2909Greenwich. The default is the local timezone, at the given time, as
2910provided by the system, using @code{localtime} (@pxref{Time}).
2911
2912On 32-bit systems, @code{localtime} is limited to a 32-bit
2913@code{time_t}, so a default @var{tz-offset} is only available for
2914times between Dec 1901 and Jan 2038. For prior dates an application
2915might like to use the value in 1902, though some locations have zone
2916changes prior to that. For future dates an application might like to
2917assume today's rules extend indefinitely. But for correct daylight
2918savings transitions it will be necessary to take an offset for the
2919same day and time but a year in range and which has the same starting
2920weekday and same leap/non-leap (to support rules like last Sunday in
2921October).
85600a0f
KR
2922@end defun
2923
2924@node SRFI-19 Date to string
3229f68b 2925@subsubsection SRFI-19 Date to string
85600a0f 2926@cindex date to string
7c2e18cd 2927@cindex string, from date
85600a0f
KR
2928
2929@defun date->string date [format]
2930Convert a date to a string under the control of a format.
2931@var{format} should be a string containing @samp{~} escapes, which
2932will be expanded as per the following conversion table. The default
2933@var{format} is @samp{~c}, a locale-dependent date and time.
2934
2935Many of these conversion characters are the same as POSIX
2936@code{strftime} (@pxref{Time}), but there are some extras and some
2937variations.
2938
2939@multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
2940@item @nicode{~~} @tab literal ~
2941@item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
2942@item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
2943@item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
2944@item @nicode{~B} @tab locale full month, eg.@: @samp{January}
2945@item @nicode{~c} @tab locale date and time, eg.@: @*
2946@samp{Fri Jul 14 20:28:42-0400 2000}
2947@item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
2948
2949@c Spec says d/m/y, reference implementation says m/d/y.
2950@c Apparently the reference code was the intention, but would like to
2951@c see an errata published for the spec before contradicting it here.
2952@c
2953@c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
2954
2955@item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
2956@item @nicode{~f} @tab seconds and fractional seconds,
2957with locale decimal point, eg.@: @samp{5.2}
2958@item @nicode{~h} @tab same as @nicode{~b}
2959@item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
2960@item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
2961@item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
2962@item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
2963@item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
2964@item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
2965@item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
2966@item @nicode{~n} @tab newline
2967@item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
2968@item @nicode{~p} @tab locale AM or PM
2969@item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
2970@item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
2971@item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
2972(usual limit is 59, 60 is a leap second)
2973@item @nicode{~t} @tab horizontal tab character
2974@item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
2975@item @nicode{~U} @tab week of year, Sunday first day of week,
2976@samp{00} to @samp{52}
2977@item @nicode{~V} @tab week of year, Monday first day of week,
2978@samp{01} to @samp{53}
2979@item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
2980@item @nicode{~W} @tab week of year, Monday first day of week,
2981@samp{00} to @samp{52}
2982
2983@c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
2984@c date. The reference code has ~x as the locale date and ~X as a
2985@c locale time. The rule is apparently that the code should be
2986@c believed, but would like to see an errata for the spec before
2987@c contradicting it here.
2988@c
2989@c @item @nicode{~x} @tab week of year, Monday as first day of week,
2990@c @samp{00} to @samp{53}
2991@c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
2992
2993@item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
2994@item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
2995@item @nicode{~z} @tab time zone, RFC-822 style
2996@item @nicode{~Z} @tab time zone symbol (not currently implemented)
2997@item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
2998@item @nicode{~2} @tab ISO-8601 time+zone, @samp{~k:~M:~S~z}
2999@item @nicode{~3} @tab ISO-8601 time, @samp{~k:~M:~S}
3000@item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~k:~M:~S~z}
3001@item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~k:~M:~S}
3002@end multitable
3003@end defun
3004
3005Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
3006described here, since the specification and reference implementation
3007differ.
3008
a2f00b9b
LC
3009Conversion is locale-dependent on systems that support it
3010(@pxref{Accessing Locale Information}). @xref{Locales,
3011@code{setlocale}}, for information on how to change the current
3012locale.
85600a0f
KR
3013
3014
3015@node SRFI-19 String to date
3229f68b 3016@subsubsection SRFI-19 String to date
85600a0f 3017@cindex string to date
7c2e18cd 3018@cindex date, from string
85600a0f
KR
3019
3020@c FIXME: Can we say what happens when an incomplete date is
679cceed 3021@c converted? I.e. fields left as 0, or what? The spec seems to be
85600a0f
KR
3022@c silent on this.
3023
3024@defun string->date input template
3025Convert an @var{input} string to a date under the control of a
3026@var{template} string. Return a newly created date object.
3027
3028Literal characters in @var{template} must match characters in
3029@var{input} and @samp{~} escapes must match the input forms described
3030in the table below. ``Skip to'' means characters up to one of the
3031given type are ignored, or ``no skip'' for no skipping. ``Read'' is
3032what's then read, and ``Set'' is the field affected in the date
3033object.
3034
3035For example @samp{~Y} skips input characters until a digit is reached,
3036at which point it expects a year and stores that to the year field of
3037the date.
3038
3039@multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
3040@item
3041@tab Skip to
3042@tab Read
3043@tab Set
3044
3045@item @nicode{~~}
3046@tab no skip
3047@tab literal ~
3048@tab nothing
3049
3050@item @nicode{~a}
3051@tab @nicode{char-alphabetic?}
3052@tab locale abbreviated weekday name
3053@tab nothing
3054
3055@item @nicode{~A}
3056@tab @nicode{char-alphabetic?}
3057@tab locale full weekday name
3058@tab nothing
3059
3060@c Note that the SRFI spec says that ~b and ~B don't set anything,
3061@c but that looks like a mistake. The reference implementation sets
3062@c the month field, which seems sensible and is what we describe
3063@c here.
3064
3065@item @nicode{~b}
3066@tab @nicode{char-alphabetic?}
3067@tab locale abbreviated month name
3068@tab @nicode{date-month}
3069
3070@item @nicode{~B}
3071@tab @nicode{char-alphabetic?}
3072@tab locale full month name
3073@tab @nicode{date-month}
3074
3075@item @nicode{~d}
3076@tab @nicode{char-numeric?}
3077@tab day of month
3078@tab @nicode{date-day}
3079
3080@item @nicode{~e}
3081@tab no skip
3082@tab day of month, blank padded
3083@tab @nicode{date-day}
3084
3085@item @nicode{~h}
3086@tab same as @samp{~b}
3087
3088@item @nicode{~H}
3089@tab @nicode{char-numeric?}
3090@tab hour
3091@tab @nicode{date-hour}
3092
3093@item @nicode{~k}
3094@tab no skip
3095@tab hour, blank padded
3096@tab @nicode{date-hour}
3097
3098@item @nicode{~m}
3099@tab @nicode{char-numeric?}
3100@tab month
3101@tab @nicode{date-month}
3102
3103@item @nicode{~M}
3104@tab @nicode{char-numeric?}
3105@tab minute
3106@tab @nicode{date-minute}
3107
3108@item @nicode{~S}
3109@tab @nicode{char-numeric?}
3110@tab second
3111@tab @nicode{date-second}
3112
3113@item @nicode{~y}
3114@tab no skip
3115@tab 2-digit year
3116@tab @nicode{date-year} within 50 years
3117
3118@item @nicode{~Y}
3119@tab @nicode{char-numeric?}
3120@tab year
3121@tab @nicode{date-year}
3122
3123@item @nicode{~z}
3124@tab no skip
3125@tab time zone
3126@tab date-zone-offset
3127@end multitable
3128
3129Notice that the weekday matching forms don't affect the date object
3130returned, instead the weekday will be derived from the day, month and
3131year.
3132
a2f00b9b
LC
3133Conversion is locale-dependent on systems that support it
3134(@pxref{Accessing Locale Information}). @xref{Locales,
3135@code{setlocale}}, for information on how to change the current
3136locale.
85600a0f 3137@end defun
12991fed 3138
8e9af854
NL
3139@node SRFI-23
3140@subsection SRFI-23 - Error Reporting
3141@cindex SRFI-23
3142
3143The SRFI-23 @code{error} procedure is always available.
1de8c1ae 3144
b0b55bd6 3145@node SRFI-26
3229f68b 3146@subsection SRFI-26 - specializing parameters
1de8c1ae 3147@cindex SRFI-26
7c2e18cd
KR
3148@cindex parameter specialize
3149@cindex argument specialize
3150@cindex specialize parameter
1de8c1ae
KR
3151
3152This SRFI provides a syntax for conveniently specializing selected
3153parameters of a function. It can be used with,
3154
3155@example
3156(use-modules (srfi srfi-26))
3157@end example
3158
3159@deffn {library syntax} cut slot @dots{}
3160@deffnx {library syntax} cute slot @dots{}
3161Return a new procedure which will make a call (@var{slot} @dots{}) but
3162with selected parameters specialized to given expressions.
3163
3164An example will illustrate the idea. The following is a
3165specialization of @code{write}, sending output to
3166@code{my-output-port},
3167
3168@example
3169(cut write <> my-output-port)
3170@result{}
3171(lambda (obj) (write obj my-output-port))
3172@end example
3173
3174The special symbol @code{<>} indicates a slot to be filled by an
3175argument to the new procedure. @code{my-output-port} on the other
3176hand is an expression to be evaluated and passed, ie.@: it specializes
3177the behaviour of @code{write}.
3178
3179@table @nicode
3180@item <>
3181A slot to be filled by an argument from the created procedure.
3182Arguments are assigned to @code{<>} slots in the order they appear in
3183the @code{cut} form, there's no way to re-arrange arguments.
3184
3185The first argument to @code{cut} is usually a procedure (or expression
3186giving a procedure), but @code{<>} is allowed there too. For example,
3187
3188@example
3189(cut <> 1 2 3)
3190@result{}
3191(lambda (proc) (proc 1 2 3))
3192@end example
3193
3194@item <...>
3195A slot to be filled by all remaining arguments from the new procedure.
3196This can only occur at the end of a @code{cut} form.
3197
3198For example, a procedure taking a variable number of arguments like
3199@code{max} but in addition enforcing a lower bound,
3200
3201@example
3202(define my-lower-bound 123)
3203
3204(cut max my-lower-bound <...>)
3205@result{}
3206(lambda arglist (apply max my-lower-bound arglist))
3207@end example
3208@end table
3209
3210For @code{cut} the specializing expressions are evaluated each time
3211the new procedure is called. For @code{cute} they're evaluated just
3212once, when the new procedure is created. The name @code{cute} stands
3213for ``@code{cut} with evaluated arguments''. In all cases the
3214evaluations take place in an unspecified order.
3215
3216The following illustrates the difference between @code{cut} and
3217@code{cute},
3218
3219@example
3220(cut format <> "the time is ~s" (current-time))
3221@result{}
3222(lambda (port) (format port "the time is ~s" (current-time)))
3223
3224(cute format <> "the time is ~s" (current-time))
3225@result{}
3226(let ((val (current-time)))
3227 (lambda (port) (format port "the time is ~s" val))
3228@end example
3229
3230(There's no provision for a mixture of @code{cut} and @code{cute}
3231where some expressions would be evaluated every time but others
3232evaluated only once.)
3233
3234@code{cut} is really just a shorthand for the sort of @code{lambda}
3235forms shown in the above examples. But notice @code{cut} avoids the
3236need to name unspecialized parameters, and is more compact. Use in
3237functional programming style or just with @code{map}, @code{for-each}
3238or similar is typical.
3239
3240@example
3241(map (cut * 2 <>) '(1 2 3 4))
3242
3243(for-each (cut write <> my-port) my-list)
3244@end example
3245@end deffn
b0b55bd6 3246
56ec46a7
AR
3247@node SRFI-27
3248@subsection SRFI-27 - Sources of Random Bits
3249@cindex SRFI-27
3250
6e3d49a0
AR
3251This subsection is based on the
3252@uref{http://srfi.schemers.org/srfi-27/srfi-27.html, specification of
3253SRFI-27} written by Sebastian Egner.
3254
3255@c The copyright notice and license text of the SRFI-27 specification is
3256@c reproduced below:
56ec46a7
AR
3257
3258@c Copyright (C) Sebastian Egner (2002). All Rights Reserved.
3259
3260@c Permission is hereby granted, free of charge, to any person obtaining a
3261@c copy of this software and associated documentation files (the
3262@c "Software"), to deal in the Software without restriction, including
3263@c without limitation the rights to use, copy, modify, merge, publish,
3264@c distribute, sublicense, and/or sell copies of the Software, and to
3265@c permit persons to whom the Software is furnished to do so, subject to
3266@c the following conditions:
3267
3268@c The above copyright notice and this permission notice shall be included
3269@c in all copies or substantial portions of the Software.
3270
3271@c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
3272@c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3273@c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3274@c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
3275@c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
3276@c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3277@c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3278
56ec46a7
AR
3279This SRFI provides access to a (pseudo) random number generator; for
3280Guile's built-in random number facilities, which SRFI-27 is implemented
3281upon, @xref{Random}. With SRFI-27, random numbers are obtained from a
3282@emph{random source}, which encapsulates a random number generation
3283algorithm and its state.
3284
3285@menu
3286* SRFI-27 Default Random Source:: Obtaining random numbers
3287* SRFI-27 Random Sources:: Creating and manipulating random sources
3288* SRFI-27 Random Number Generators:: Obtaining random number generators
3289@end menu
3290
3291@node SRFI-27 Default Random Source
3292@subsubsection The Default Random Source
3293@cindex SRFI-27
3294
3295@defun random-integer n
3296Return a random number between zero (inclusive) and @var{n} (exclusive),
3297using the default random source. The numbers returned have a uniform
3298distribution.
3299@end defun
3300
3301@defun random-real
3302Return a random number in (0,1), using the default random source. The
3303numbers returned have a uniform distribution.
3304@end defun
3305
3306@defun default-random-source
3307A random source from which @code{random-integer} and @code{random-real}
3308have been derived using @code{random-source-make-integers} and
3309@code{random-source-make-reals} (@pxref{SRFI-27 Random Number Generators}
3310for those procedures). Note that an assignment to
3311@code{default-random-source} does not change @code{random-integer} or
3312@code{random-real}; it is also strongly recommended not to assign a new
3313value.
3314@end defun
3315
3316@node SRFI-27 Random Sources
3317@subsubsection Random Sources
3318@cindex SRFI-27
3319
3320@defun make-random-source
3321Create a new random source. The stream of random numbers obtained from
3322each random source created by this procedure will be identical, unless
3323its state is changed by one of the procedures below.
3324@end defun
3325
3326@defun random-source? object
3327Tests whether @var{object} is a random source. Random sources are a
3328disjoint type.
3329@end defun
3330
3331@defun random-source-randomize! source
3332Attempt to set the state of the random source to a truly random value.
3333The current implementation uses a seed based on the current system time.
3334@end defun
3335
3336@defun random-source-pseudo-randomize! source i j
3337Changes the state of the random source s into the initial state of the
3338(@var{i}, @var{j})-th independent random source, where @var{i} and
3339@var{j} are non-negative integers. This procedure provides a mechanism
3340to obtain a large number of independent random sources (usually all
3341derived from the same backbone generator), indexed by two integers. In
3342contrast to @code{random-source-randomize!}, this procedure is entirely
3343deterministic.
3344@end defun
3345
3346The state associated with a random state can be obtained an reinstated
3347with the following procedures:
3348
3349@defun random-source-state-ref source
3350@defunx random-source-state-set! source state
3351Get and set the state of a random source. No assumptions should be made
3352about the nature of the state object, besides it having an external
679cceed 3353representation (i.e.@: it can be passed to @code{write} and subsequently
56ec46a7
AR
3354@code{read} back).
3355@end defun
3356
3357@node SRFI-27 Random Number Generators
3358@subsubsection Obtaining random number generator procedures
3359@cindex SRFI-27
3360
3361@defun random-source-make-integers source
3362Obtains a procedure to generate random integers using the random source
3363@var{source}. The returned procedure takes a single argument @var{n},
3364which must be a positive integer, and returns the next uniformly
3365distributed random integer from the interval @{0, ..., @var{n}-1@} by
3366advancing the state of @var{source}.
3367
3368If an application obtains and uses several generators for the same
3369random source @var{source}, a call to any of these generators advances
3370the state of @var{source}. Hence, the generators do not produce the
3371same sequence of random integers each but rather share a state. This
3372also holds for all other types of generators derived from a fixed random
3373sources.
3374
3375While the SRFI text specifies that ``Implementations that support
3376concurrency make sure that the state of a generator is properly
3377advanced'', this is currently not the case in Guile's implementation of
3378SRFI-27, as it would cause a severe performance penalty. So in
3379multi-threaded programs, you either must perform locking on random
3380sources shared between threads yourself, or use different random sources
3381for multiple threads.
3382@end defun
3383
3384@defun random-source-make-reals source
3385@defunx random-source-make-reals source unit
3386Obtains a procedure to generate random real numbers @math{0 < x < 1}
3387using the random source @var{source}. The procedure rand is called
3388without arguments.
3389
3390The optional parameter @var{unit} determines the type of numbers being
3391produced by the returned procedure and the quantization of the output.
3392@var{unit} must be a number such that @math{0 < @var{unit} < 1}. The
3393numbers created by the returned procedure are of the same numerical type
3394as @var{unit} and the potential output values are spaced by at most
3395@var{unit}. One can imagine rand to create numbers as @var{x} *
3396@var{unit} where @var{x} is a random integer in @{1, ...,
3397floor(1/unit)-1@}. Note, however, that this need not be the way the
3398values are actually created and that the actual resolution of rand can
3399be much higher than unit. In case @var{unit} is absent it defaults to a
3400reasonably small value (related to the width of the mantissa of an
3401efficient number format).
3402@end defun
3403
620c8965
LC
3404@node SRFI-30
3405@subsection SRFI-30 - Nested Multi-line Comments
3406@cindex SRFI-30
3407
3408Starting from version 2.0, Guile's @code{read} supports SRFI-30/R6RS
3409nested multi-line comments by default, @ref{Block Comments}.
3410
8638c417
RB
3411@node SRFI-31
3412@subsection SRFI-31 - A special form `rec' for recursive evaluation
3413@cindex SRFI-31
7c2e18cd 3414@cindex recursive expression
8638c417
RB
3415@findex rec
3416
3417SRFI-31 defines a special form that can be used to create
3418self-referential expressions more conveniently. The syntax is as
3419follows:
3420
3421@example
3422@group
3423<rec expression> --> (rec <variable> <expression>)
3424<rec expression> --> (rec (<variable>+) <body>)
3425@end group
3426@end example
3427
3428The first syntax can be used to create self-referential expressions,
3429for example:
3430
3431@lisp
3432 guile> (define tmp (rec ones (cons 1 (delay ones))))
3433@end lisp
3434
3435The second syntax can be used to create anonymous recursive functions:
3436
3437@lisp
3438 guile> (define tmp (rec (display-n item n)
3439 (if (positive? n)
3440 (begin (display n) (display-n (- n 1))))))
3441 guile> (tmp 42 3)
3442 424242
3443 guile>
3444@end lisp
12991fed 3445
eeadfda1 3446
f50ca8da
LC
3447@node SRFI-34
3448@subsection SRFI-34 - Exception handling for programs
3449
3450@cindex SRFI-34
3451Guile provides an implementation of
3452@uref{http://srfi.schemers.org/srfi-34/srfi-34.html, SRFI-34's exception
3453handling mechanisms} as an alternative to its own built-in mechanisms
3454(@pxref{Exceptions}). It can be made available as follows:
3455
3456@lisp
3457(use-modules (srfi srfi-34))
3458@end lisp
3459
3460@c FIXME: Document it.
3461
3462
3463@node SRFI-35
3464@subsection SRFI-35 - Conditions
3465
3466@cindex SRFI-35
3467@cindex conditions
3468@cindex exceptions
3469
3470@uref{http://srfi.schemers.org/srfi-35/srfi-35.html, SRFI-35} implements
3471@dfn{conditions}, a data structure akin to records designed to convey
3472information about exceptional conditions between parts of a program. It
3473is normally used in conjunction with SRFI-34's @code{raise}:
3474
3475@lisp
3476(raise (condition (&message
3477 (message "An error occurred"))))
3478@end lisp
3479
3480Users can define @dfn{condition types} containing arbitrary information.
3481Condition types may inherit from one another. This allows the part of
3482the program that handles (or ``catches'') conditions to get accurate
3483information about the exceptional condition that arose.
3484
3485SRFI-35 conditions are made available using:
3486
3487@lisp
3488(use-modules (srfi srfi-35))
3489@end lisp
3490
3491The procedures available to manipulate condition types are the
3492following:
3493
3494@deffn {Scheme Procedure} make-condition-type id parent field-names
3495Return a new condition type named @var{id}, inheriting from
3496@var{parent}, and with the fields whose names are listed in
3497@var{field-names}. @var{field-names} must be a list of symbols and must
3498not contain names already used by @var{parent} or one of its supertypes.
3499@end deffn
3500
3501@deffn {Scheme Procedure} condition-type? obj
3502Return true if @var{obj} is a condition type.
3503@end deffn
3504
3505Conditions can be created and accessed with the following procedures:
3506
3507@deffn {Scheme Procedure} make-condition type . field+value
3508Return a new condition of type @var{type} with fields initialized as
3509specified by @var{field+value}, a sequence of field names (symbols) and
3510values as in the following example:
3511
3512@lisp
1317062f 3513(let ((&ct (make-condition-type 'foo &condition '(a b c))))
f50ca8da
LC
3514 (make-condition &ct 'a 1 'b 2 'c 3))
3515@end lisp
3516
3517Note that all fields of @var{type} and its supertypes must be specified.
3518@end deffn
3519
3520@deffn {Scheme Procedure} make-compound-condition . conditions
3521Return a new compound condition composed of @var{conditions}. The
3522returned condition has the type of each condition of @var{conditions}
3523(per @code{condition-has-type?}).
3524@end deffn
3525
3526@deffn {Scheme Procedure} condition-has-type? c type
3527Return true if condition @var{c} has type @var{type}.
3528@end deffn
3529
3530@deffn {Scheme Procedure} condition-ref c field-name
3531Return the value of the field named @var{field-name} from condition @var{c}.
3532
3533If @var{c} is a compound condition and several underlying condition
3534types contain a field named @var{field-name}, then the value of the
3535first such field is returned, using the order in which conditions were
3536passed to @var{make-compound-condition}.
3537@end deffn
3538
3539@deffn {Scheme Procedure} extract-condition c type
3540Return a condition of condition type @var{type} with the field values
3541specified by @var{c}.
3542
3543If @var{c} is a compound condition, extract the field values from the
3544subcondition belonging to @var{type} that appeared first in the call to
72b3aa56 3545@code{make-compound-condition} that created the condition.
f50ca8da
LC
3546@end deffn
3547
3548Convenience macros are also available to create condition types and
3549conditions.
3550
3551@deffn {library syntax} define-condition-type type supertype predicate field-spec...
3552Define a new condition type named @var{type} that inherits from
3553@var{supertype}. In addition, bind @var{predicate} to a type predicate
3554that returns true when passed a condition of type @var{type} or any of
3555its subtypes. @var{field-spec} must have the form @code{(field
3556accessor)} where @var{field} is the name of field of @var{type} and
3557@var{accessor} is the name of a procedure to access field @var{field} in
3558conditions of type @var{type}.
3559
3560The example below defines condition type @code{&foo}, inheriting from
3561@code{&condition} with fields @code{a}, @code{b} and @code{c}:
3562
3563@lisp
3564(define-condition-type &foo &condition
3565 foo-condition?
3566 (a foo-a)
3567 (b foo-b)
3568 (c foo-c))
3569@end lisp
3570@end deffn
3571
3572@deffn {library syntax} condition type-field-bindings...
3573Return a new condition, or compound condition, initialized according to
3574@var{type-field-bindings}. Each @var{type-field-binding} must have the
3575form @code{(type field-specs...)}, where @var{type} is the name of a
3576variable bound to condition type; each @var{field-spec} must have the
3577form @code{(field-name value)} where @var{field-name} is a symbol
3578denoting the field being initialized to @var{value}. As for
3579@code{make-condition}, all fields must be specified.
3580
3581The following example returns a simple condition:
3582
3583@lisp
3584(condition (&message (message "An error occurred")))
3585@end lisp
3586
3587The one below returns a compound condition:
3588
3589@lisp
3590(condition (&message (message "An error occurred"))
3591 (&serious))
3592@end lisp
3593@end deffn
3594
3595Finally, SRFI-35 defines a several standard condition types.
3596
3597@defvar &condition
3598This condition type is the root of all condition types. It has no
3599fields.
3600@end defvar
3601
3602@defvar &message
3603A condition type that carries a message describing the nature of the
3604condition to humans.
3605@end defvar
3606
3607@deffn {Scheme Procedure} message-condition? c
3608Return true if @var{c} is of type @code{&message} or one of its
3609subtypes.
3610@end deffn
3611
3612@deffn {Scheme Procedure} condition-message c
3613Return the message associated with message condition @var{c}.
3614@end deffn
3615
3616@defvar &serious
3617This type describes conditions serious enough that they cannot safely be
3618ignored. It has no fields.
3619@end defvar
3620
3621@deffn {Scheme Procedure} serious-condition? c
3622Return true if @var{c} is of type @code{&serious} or one of its
3623subtypes.
3624@end deffn
3625
3626@defvar &error
3627This condition describes errors, typically caused by something that has
3628gone wrong in the interaction of the program with the external world or
3629the user.
3630@end defvar
3631
3632@deffn {Scheme Procedure} error? c
3633Return true if @var{c} is of type @code{&error} or one of its subtypes.
3634@end deffn
3635
d4c38221
LC
3636@node SRFI-37
3637@subsection SRFI-37 - args-fold
3638@cindex SRFI-37
3639
3640This is a processor for GNU @code{getopt_long}-style program
3641arguments. It provides an alternative, less declarative interface
3642than @code{getopt-long} in @code{(ice-9 getopt-long)}
3643(@pxref{getopt-long,,The (ice-9 getopt-long) Module}). Unlike
3644@code{getopt-long}, it supports repeated options and any number of
3645short and long names per option. Access it with:
3646
3647@lisp
3648(use-modules (srfi srfi-37))
3649@end lisp
3650
3651@acronym{SRFI}-37 principally provides an @code{option} type and the
3652@code{args-fold} function. To use the library, create a set of
3653options with @code{option} and use it as a specification for invoking
3654@code{args-fold}.
3655
3656Here is an example of a simple argument processor for the typical
3657@samp{--version} and @samp{--help} options, which returns a backwards
3658list of files given on the command line:
3659
3660@lisp
3661(args-fold (cdr (program-arguments))
3662 (let ((display-and-exit-proc
3663 (lambda (msg)
3664 (lambda (opt name arg loads)
3665 (display msg) (quit)))))
3666 (list (option '(#\v "version") #f #f
3667 (display-and-exit-proc "Foo version 42.0\n"))
3668 (option '(#\h "help") #f #f
3669 (display-and-exit-proc
3670 "Usage: foo scheme-file ..."))))
3671 (lambda (opt name arg loads)
3672 (error "Unrecognized option `~A'" name))
3673 (lambda (op loads) (cons op loads))
3674 '())
3675@end lisp
3676
3677@deffn {Scheme Procedure} option names required-arg? optional-arg? processor
3678Return an object that specifies a single kind of program option.
3679
3680@var{names} is a list of command-line option names, and should consist of
3681characters for traditional @code{getopt} short options and strings for
3682@code{getopt_long}-style long options.
3683
3684@var{required-arg?} and @var{optional-arg?} are mutually exclusive;
3685one or both must be @code{#f}. If @var{required-arg?}, the option
3686must be followed by an argument on the command line, such as
3687@samp{--opt=value} for long options, or an error will be signalled.
3688If @var{optional-arg?}, an argument will be taken if available.
3689
3690@var{processor} is a procedure that takes at least 3 arguments, called
3691when @code{args-fold} encounters the option: the containing option
3692object, the name used on the command line, and the argument given for
3693the option (or @code{#f} if none). The rest of the arguments are
3694@code{args-fold} ``seeds'', and the @var{processor} should return
3695seeds as well.
3696@end deffn
3697
3698@deffn {Scheme Procedure} option-names opt
3699@deffnx {Scheme Procedure} option-required-arg? opt
3700@deffnx {Scheme Procedure} option-optional-arg? opt
3701@deffnx {Scheme Procedure} option-processor opt
3702Return the specified field of @var{opt}, an option object, as
3703described above for @code{option}.
3704@end deffn
3705
3706@deffn {Scheme Procedure} args-fold args options unrecognized-option-proc operand-proc seeds @dots{}
3707Process @var{args}, a list of program arguments such as that returned
3708by @code{(cdr (program-arguments))}, in order against @var{options}, a
3709list of option objects as described above. All functions called take
3710the ``seeds'', or the last multiple-values as multiple arguments,
3711starting with @var{seeds}, and must return the new seeds. Return the
3712final seeds.
3713
3714Call @code{unrecognized-option-proc}, which is like an option object's
3715processor, for any options not found in @var{options}.
3716
3717Call @code{operand-proc} with any items on the command line that are
3718not named options. This includes arguments after @samp{--}. It is
3719called with the argument in question, as well as the seeds.
3720@end deffn
3721
12708eeb
AR
3722@node SRFI-38
3723@subsection SRFI-38 - External Representation for Data With Shared Structure
3724@cindex SRFI-38
3725
3726This subsection is based on
3727@uref{http://srfi.schemers.org/srfi-38/srfi-38.html, the specification
3728of SRFI-38} written by Ray Dillinger.
3729
3730@c Copyright (C) Ray Dillinger 2003. All Rights Reserved.
3731
3732@c Permission is hereby granted, free of charge, to any person obtaining a
3733@c copy of this software and associated documentation files (the
3734@c "Software"), to deal in the Software without restriction, including
3735@c without limitation the rights to use, copy, modify, merge, publish,
3736@c distribute, sublicense, and/or sell copies of the Software, and to
3737@c permit persons to whom the Software is furnished to do so, subject to
3738@c the following conditions:
3739
3740@c The above copyright notice and this permission notice shall be included
3741@c in all copies or substantial portions of the Software.
3742
3743@c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
3744@c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3745@c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3746@c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
3747@c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
3748@c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3749@c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3750
3751This SRFI creates an alternative external representation for data
3752written and read using @code{write-with-shared-structure} and
3753@code{read-with-shared-structure}. It is identical to the grammar for
3754external representation for data written and read with @code{write} and
3755@code{read} given in section 7 of R5RS, except that the single
3756production
3757
3758@example
3759<datum> --> <simple datum> | <compound datum>
3760@end example
3761
3762is replaced by the following five productions:
3763
3764@example
3765<datum> --> <defining datum> | <nondefining datum> | <defined datum>
3766<defining datum> --> #<indexnum>=<nondefining datum>
3767<defined datum> --> #<indexnum>#
3768<nondefining datum> --> <simple datum> | <compound datum>
3769<indexnum> --> <digit 10>+
3770@end example
3771
3772@deffn {Scheme procedure} write-with-shared-structure obj
3773@deffnx {Scheme procedure} write-with-shared-structure obj port
3774@deffnx {Scheme procedure} write-with-shared-structure obj port optarg
3775
3776Writes an external representation of @var{obj} to the given port.
3777Strings that appear in the written representation are enclosed in
3778doublequotes, and within those strings backslash and doublequote
3779characters are escaped by backslashes. Character objects are written
3780using the @code{#\} notation.
3781
3782Objects which denote locations rather than values (cons cells, vectors,
3783and non-zero-length strings in R5RS scheme; also Guile's structs,
3784bytevectors and ports and hash-tables), if they appear at more than one
3785point in the data being written, are preceded by @samp{#@var{N}=} the
3786first time they are written and replaced by @samp{#@var{N}#} all
3787subsequent times they are written, where @var{N} is a natural number
3788used to identify that particular object. If objects which denote
3789locations occur only once in the structure, then
3790@code{write-with-shared-structure} must produce the same external
3791representation for those objects as @code{write}.
3792
3793@code{write-with-shared-structure} terminates in finite time and
3794produces a finite representation when writing finite data.
3795
3796@code{write-with-shared-structure} returns an unspecified value. The
3797@var{port} argument may be omitted, in which case it defaults to the
3798value returned by @code{(current-output-port)}. The @var{optarg}
3799argument may also be omitted. If present, its effects on the output and
3800return value are unspecified but @code{write-with-shared-structure} must
3801still write a representation that can be read by
3802@code{read-with-shared-structure}. Some implementations may wish to use
3803@var{optarg} to specify formatting conventions, numeric radixes, or
3804return values. Guile's implementation ignores @var{optarg}.
3805
3806For example, the code
3807
3808@lisp
3809(begin (define a (cons 'val1 'val2))
3810 (set-cdr! a a)
3811 (write-with-shared-structure a))
3812@end lisp
3813
3814should produce the output @code{#1=(val1 . #1#)}. This shows a cons
3815cell whose @code{cdr} contains itself.
3816
3817@end deffn
3818
3819@deffn {Scheme procedure} read-with-shared-structure
3820@deffnx {Scheme procedure} read-with-shared-structure port
3821
3822@code{read-with-shared-structure} converts the external representations
3823of Scheme objects produced by @code{write-with-shared-structure} into
3824Scheme objects. That is, it is a parser for the nonterminal
3825@samp{<datum>} in the augmented external representation grammar defined
3826above. @code{read-with-shared-structure} returns the next object
3827parsable from the given input port, updating @var{port} to point to the
3828first character past the end of the external representation of the
3829object.
3830
3831If an end-of-file is encountered in the input before any characters are
3832found that can begin an object, then an end-of-file object is returned.
3833The port remains open, and further attempts to read it (by
3834@code{read-with-shared-structure} or @code{read} will also return an
3835end-of-file object. If an end of file is encountered after the
3836beginning of an object's external representation, but the external
3837representation is incomplete and therefore not parsable, an error is
3838signalled.
3839
3840The @var{port} argument may be omitted, in which case it defaults to the
3841value returned by @code{(current-input-port)}. It is an error to read
3842from a closed port.
3843
3844@end deffn
d4c38221 3845
eeadfda1
KR
3846@node SRFI-39
3847@subsection SRFI-39 - Parameters
3848@cindex SRFI-39
3849@cindex parameter object
3850@tindex Parameter
3851
3852This SRFI provides parameter objects, which implement dynamically
3853bound locations for values. The functions below are available from
3854
3855@example
3856(use-modules (srfi srfi-39))
3857@end example
3858
3859A parameter object is a procedure. Called with no arguments it
3860returns its value, called with one argument it sets the value.
3861
3862@example
3863(define my-param (make-parameter 123))
3864(my-param) @result{} 123
3865(my-param 456)
3866(my-param) @result{} 456
3867@end example
3868
3869The @code{parameterize} special form establishes new locations for
3870parameters, those new locations having effect within the dynamic scope
3871of the @code{parameterize} body. Leaving restores the previous
3872locations, or re-entering through a saved continuation will again use
3873the new locations.
3874
3875@example
3876(parameterize ((my-param 789))
3877 (my-param) @result{} 789
3878 )
3879(my-param) @result{} 456
3880@end example
3881
72b3aa56 3882Parameters are like dynamically bound variables in other Lisp dialects.
eeadfda1
KR
3883They allow an application to establish parameter settings (as the name
3884suggests) just for the execution of a particular bit of code,
3885restoring when done. Examples of such parameters might be
3886case-sensitivity for a search, or a prompt for user input.
3887
3888Global variables are not as good as parameter objects for this sort of
3889thing. Changes to them are visible to all threads, but in Guile
72b3aa56 3890parameter object locations are per-thread, thereby truly limiting the
eeadfda1
KR
3891effect of @code{parameterize} to just its dynamic execution.
3892
3893Passing arguments to functions is thread-safe, but that soon becomes
3894tedious when there's more than a few or when they need to pass down
3895through several layers of calls before reaching the point they should
3896affect. And introducing a new setting to existing code is often
3897easier with a parameter object than adding arguments.
3898
3899
3900@sp 1
3901@defun make-parameter init [converter]
3902Return a new parameter object, with initial value @var{init}.
3903
3904A parameter object is a procedure. When called @code{(param)} it
3905returns its value, or a call @code{(param val)} sets its value. For
3906example,
3907
3908@example
3909(define my-param (make-parameter 123))
3910(my-param) @result{} 123
3911
3912(my-param 456)
3913(my-param) @result{} 456
3914@end example
3915
3916If a @var{converter} is given, then a call @code{(@var{converter}
3917val)} is made for each value set, its return is the value stored.
3918Such a call is made for the @var{init} initial value too.
3919
3920A @var{converter} allows values to be validated, or put into a
3921canonical form. For example,
3922
3923@example
3924(define my-param (make-parameter 123
3925 (lambda (val)
3926 (if (not (number? val))
3927 (error "must be a number"))
3928 (inexact->exact val))))
3929(my-param 0.75)
3930(my-param) @result{} 3/4
3931@end example
3932@end defun
3933
3934@deffn {library syntax} parameterize ((param value) @dots{}) body @dots{}
3935Establish a new dynamic scope with the given @var{param}s bound to new
3936locations and set to the given @var{value}s. @var{body} is evaluated
3937in that environment, the result is the return from the last form in
3938@var{body}.
3939
3940Each @var{param} is an expression which is evaluated to get the
3941parameter object. Often this will just be the name of a variable
3942holding the object, but it can be anything that evaluates to a
3943parameter.
3944
3945The @var{param} expressions and @var{value} expressions are all
3946evaluated before establishing the new dynamic bindings, and they're
3947evaluated in an unspecified order.
3948
3949For example,
3950
3951@example
3952(define prompt (make-parameter "Type something: "))
3953(define (get-input)
3954 (display (prompt))
3955 ...)
3956
3957(parameterize ((prompt "Type a number: "))
3958 (get-input)
3959 ...)
3960@end example
3961@end deffn
3962
3963@deffn {Parameter object} current-input-port [new-port]
3964@deffnx {Parameter object} current-output-port [new-port]
3965@deffnx {Parameter object} current-error-port [new-port]
3966This SRFI extends the core @code{current-input-port} and
3967@code{current-output-port}, making them parameter objects. The
3968Guile-specific @code{current-error-port} is extended too, for
3969consistency. (@pxref{Default Ports}.)
3970
3971This is an upwardly compatible extension, a plain call like
3972@code{(current-input-port)} still returns the current input port, and
3973@code{set-current-input-port} can still be used. But the port can now
3974also be set with @code{(current-input-port my-port)} and bound
3975dynamically with @code{parameterize}.
3976@end deffn
3977
3978@defun with-parameters* param-list value-list thunk
3979Establish a new dynamic scope, as per @code{parameterize} above,
3980taking parameters from @var{param-list} and corresponding values from
3981@var{values-list}. A call @code{(@var{thunk})} is made in the new
3982scope and the result from that @var{thunk} is the return from
3983@code{with-parameters*}.
3984
3985This function is a Guile-specific addition to the SRFI, it's similar
b4fddbbe 3986to the core @code{with-fluids*} (@pxref{Fluids and Dynamic States}).
eeadfda1
KR
3987@end defun
3988
3989
3990@sp 1
b4fddbbe
MV
3991Parameter objects are implemented using fluids (@pxref{Fluids and
3992Dynamic States}), so each dynamic state has it's own parameter
3993locations. That includes the separate locations when outside any
3994@code{parameterize} form. When a parameter is created it gets a
3995separate initial location in each dynamic state, all initialized to
3996the given @var{init} value.
3997
3998As alluded to above, because each thread usually has a separate
3999dynamic state, each thread has it's own locations behind parameter
4000objects, and changes in one thread are not visible to any other. When
4001a new dynamic state or thread is created, the values of parameters in
4002the originating context are copied, into new locations.
eeadfda1
KR
4003
4004SRFI-39 doesn't specify the interaction between parameter objects and
4005threads, so the threading behaviour described here should be regarded
4006as Guile-specific.
4007
fdc8fd46
AR
4008@node SRFI-42
4009@subsection SRFI-42 - Eager Comprehensions
4010@cindex SRFI-42
4011
4012See @uref{http://srfi.schemers.org/srfi-42/srfi-42.html, the
4013specification of SRFI-42}.
eeadfda1 4014
f16a2007
AR
4015@node SRFI-45
4016@subsection SRFI-45 - Primitives for Expressing Iterative Lazy Algorithms
4017@cindex SRFI-45
4018
4019This subsection is based on @uref{http://srfi.schemers.org/srfi-45/srfi-45.html, the
4020specification of SRFI-45} written by Andr@'e van Tonder.
4021
4022@c Copyright (C) André van Tonder (2003). All Rights Reserved.
4023
4024@c Permission is hereby granted, free of charge, to any person obtaining a
4025@c copy of this software and associated documentation files (the
4026@c "Software"), to deal in the Software without restriction, including
4027@c without limitation the rights to use, copy, modify, merge, publish,
4028@c distribute, sublicense, and/or sell copies of the Software, and to
4029@c permit persons to whom the Software is furnished to do so, subject to
4030@c the following conditions:
4031
4032@c The above copyright notice and this permission notice shall be included
4033@c in all copies or substantial portions of the Software.
4034
4035@c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
4036@c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4037@c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
4038@c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
4039@c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
4040@c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
4041@c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4042
4043Lazy evaluation is traditionally simulated in Scheme using @code{delay}
4044and @code{force}. However, these primitives are not powerful enough to
4045express a large class of lazy algorithms that are iterative. Indeed, it
4046is folklore in the Scheme community that typical iterative lazy
4047algorithms written using delay and force will often require unbounded
4048memory.
4049
4050This SRFI provides set of three operations: @{@code{lazy}, @code{delay},
4051@code{force}@}, which allow the programmer to succinctly express lazy
4052algorithms while retaining bounded space behavior in cases that are
4053properly tail-recursive. A general recipe for using these primitives is
4054provided. An additional procedure @code{eager} is provided for the
4055construction of eager promises in cases where efficiency is a concern.
4056
4057Although this SRFI redefines @code{delay} and @code{force}, the
4058extension is conservative in the sense that the semantics of the subset
4059@{@code{delay}, @code{force}@} in isolation (i.e., as long as the
4060program does not use @code{lazy}) agrees with that in R5RS. In other
4061words, no program that uses the R5RS definitions of delay and force will
4062break if those definition are replaced by the SRFI-45 definitions of
4063delay and force.
4064
4065@deffn {Scheme Syntax} delay expression
4066Takes an expression of arbitrary type @var{a} and returns a promise of
4067type @code{(Promise @var{a})} which at some point in the future may be
4068asked (by the @code{force} procedure) to evaluate the expression and
4069deliver the resulting value.
4070@end deffn
4071
4072@deffn {Scheme Syntax} lazy expression
4073Takes an expression of type @code{(Promise @var{a})} and returns a
4074promise of type @code{(Promise @var{a})} which at some point in the
4075future may be asked (by the @code{force} procedure) to evaluate the
4076expression and deliver the resulting promise.
4077@end deffn
4078
4079@deffn {Scheme Procedure} force expression
4080Takes an argument of type @code{(Promise @var{a})} and returns a value
4081of type @var{a} as follows: If a value of type @var{a} has been computed
4082for the promise, this value is returned. Otherwise, the promise is
4083first evaluated, then overwritten by the obtained promise or value, and
4084then force is again applied (iteratively) to the promise.
4085@end deffn
4086
4087@deffn {Scheme Procedure} eager expression
4088Takes an argument of type @var{a} and returns a value of type
4089@code{(Promise @var{a})}. As opposed to @code{delay}, the argument is
4090evaluated eagerly. Semantically, writing @code{(eager expression)} is
4091equivalent to writing
4092
4093@lisp
4094(let ((value expression)) (delay value)).
4095@end lisp
4096
4097However, the former is more efficient since it does not require
4098unnecessary creation and evaluation of thunks. We also have the
4099equivalence
4100
4101@lisp
4102(delay expression) = (lazy (eager expression))
4103@end lisp
4104@end deffn
4105
4106The following reduction rules may be helpful for reasoning about these
4107primitives. However, they do not express the memoization and memory
4108usage semantics specified above:
4109
4110@lisp
4111(force (delay expression)) -> expression
4112(force (lazy expression)) -> (force expression)
4113(force (eager value)) -> value
4114@end lisp
4115
4116@subsubheading Correct usage
4117
4118We now provide a general recipe for using the primitives @{@code{lazy},
4119@code{delay}, @code{force}@} to express lazy algorithms in Scheme. The
4120transformation is best described by way of an example: Consider the
4121stream-filter algorithm, expressed in a hypothetical lazy language as
4122
4123@lisp
4124(define (stream-filter p? s)
4125 (if (null? s) '()
4126 (let ((h (car s))
4127 (t (cdr s)))
4128 (if (p? h)
4129 (cons h (stream-filter p? t))
4130 (stream-filter p? t)))))
4131@end lisp
4132
ecb87335 4133This algorithm can be expressed as follows in Scheme:
f16a2007
AR
4134
4135@lisp
4136(define (stream-filter p? s)
4137 (lazy
4138 (if (null? (force s)) (delay '())
4139 (let ((h (car (force s)))
4140 (t (cdr (force s))))
4141 (if (p? h)
4142 (delay (cons h (stream-filter p? t)))
4143 (stream-filter p? t))))))
4144@end lisp
4145
4146In other words, we
4147
4148@itemize @bullet
4149@item
4150wrap all constructors (e.g., @code{'()}, @code{cons}) with @code{delay},
4151@item
4152apply @code{force} to arguments of deconstructors (e.g., @code{car},
4153@code{cdr} and @code{null?}),
4154@item
4155wrap procedure bodies with @code{(lazy ...)}.
4156@end itemize
4157
4ea9becb
KR
4158@node SRFI-55
4159@subsection SRFI-55 - Requiring Features
4160@cindex SRFI-55
4161
4162SRFI-55 provides @code{require-extension} which is a portable
4163mechanism to load selected SRFI modules. This is implemented in the
4164Guile core, there's no module needed to get SRFI-55 itself.
4165
4166@deffn {library syntax} require-extension clause@dots{}
4167Require each of the given @var{clause} features, throwing an error if
4168any are unavailable.
4169
4170A @var{clause} is of the form @code{(@var{identifier} arg...)}. The
4171only @var{identifier} currently supported is @code{srfi} and the
4172arguments are SRFI numbers. For example to get SRFI-1 and SRFI-6,
4173
4174@example
4175(require-extension (srfi 1 6))
4176@end example
4177
4178@code{require-extension} can only be used at the top-level.
4179
4180A Guile-specific program can simply @code{use-modules} to load SRFIs
4181not already in the core, @code{require-extension} is for programs
4182designed to be portable to other Scheme implementations.
4183@end deffn
4184
4185
8503beb8
KR
4186@node SRFI-60
4187@subsection SRFI-60 - Integers as Bits
4188@cindex SRFI-60
4189@cindex integers as bits
4190@cindex bitwise logical
4191
4192This SRFI provides various functions for treating integers as bits and
4193for bitwise manipulations. These functions can be obtained with,
4194
4195@example
4196(use-modules (srfi srfi-60))
4197@end example
4198
4199Integers are treated as infinite precision twos-complement, the same
4200as in the core logical functions (@pxref{Bitwise Operations}). And
4201likewise bit indexes start from 0 for the least significant bit. The
4202following functions in this SRFI are already in the Guile core,
4203
4204@quotation
4205@code{logand},
4206@code{logior},
4207@code{logxor},
4208@code{lognot},
4209@code{logtest},
4210@code{logcount},
4211@code{integer-length},
4212@code{logbit?},
4213@code{ash}
4214@end quotation
4215
4216@sp 1
4217@defun bitwise-and n1 ...
4218@defunx bitwise-ior n1 ...
4219@defunx bitwise-xor n1 ...
4220@defunx bitwise-not n
4221@defunx any-bits-set? j k
4222@defunx bit-set? index n
4223@defunx arithmetic-shift n count
4224@defunx bit-field n start end
4225@defunx bit-count n
4226Aliases for @code{logand}, @code{logior}, @code{logxor},
4227@code{lognot}, @code{logtest}, @code{logbit?}, @code{ash},
4228@code{bit-extract} and @code{logcount} respectively.
4229
4230Note that the name @code{bit-count} conflicts with @code{bit-count} in
4231the core (@pxref{Bit Vectors}).
4232@end defun
4233
4234@defun bitwise-if mask n1 n0
4235@defunx bitwise-merge mask n1 n0
4236Return an integer with bits selected from @var{n1} and @var{n0}
4237according to @var{mask}. Those bits where @var{mask} has 1s are taken
4238from @var{n1}, and those where @var{mask} has 0s are taken from
4239@var{n0}.
4240
4241@example
4242(bitwise-if 3 #b0101 #b1010) @result{} 9
4243@end example
4244@end defun
4245
4246@defun log2-binary-factors n
4247@defunx first-set-bit n
4248Return a count of how many factors of 2 are present in @var{n}. This
4249is also the bit index of the lowest 1 bit in @var{n}. If @var{n} is
42500, the return is @math{-1}.
4251
4252@example
4253(log2-binary-factors 6) @result{} 1
4254(log2-binary-factors -8) @result{} 3
4255@end example
4256@end defun
4257
4258@defun copy-bit index n newbit
4259Return @var{n} with the bit at @var{index} set according to
4260@var{newbit}. @var{newbit} should be @code{#t} to set the bit to 1,
4261or @code{#f} to set it to 0. Bits other than at @var{index} are
4262unchanged in the return.
4263
4264@example
4265(copy-bit 1 #b0101 #t) @result{} 7
4266@end example
4267@end defun
4268
4269@defun copy-bit-field n newbits start end
4270Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
4271(exclusive) changed to the value @var{newbits}.
4272
4273The least significant bit in @var{newbits} goes to @var{start}, the
4274next to @math{@var{start}+1}, etc. Anything in @var{newbits} past the
4275@var{end} given is ignored.
4276
4277@example
4278(copy-bit-field #b10000 #b11 1 3) @result{} #b10110
4279@end example
4280@end defun
4281
4282@defun rotate-bit-field n count start end
4283Return @var{n} with the bit field from @var{start} (inclusive) to
4284@var{end} (exclusive) rotated upwards by @var{count} bits.
4285
4286@var{count} can be positive or negative, and it can be more than the
4287field width (it'll be reduced modulo the width).
4288
4289@example
4290(rotate-bit-field #b0110 2 1 4) @result{} #b1010
4291@end example
4292@end defun
4293
4294@defun reverse-bit-field n start end
4295Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
4296(exclusive) reversed.
4297
4298@example
4299(reverse-bit-field #b101001 2 4) @result{} #b100101
4300@end example
4301@end defun
4302
4303@defun integer->list n [len]
4304Return bits from @var{n} in the form of a list of @code{#t} for 1 and
4305@code{#f} for 0. The least significant @var{len} bits are returned,
4306and the first list element is the most significant of those bits. If
4307@var{len} is not given, the default is @code{(integer-length @var{n})}
4308(@pxref{Bitwise Operations}).
4309
4310@example
4311(integer->list 6) @result{} (#t #t #f)
4312(integer->list 1 4) @result{} (#f #f #f #t)
4313@end example
4314@end defun
4315
4316@defun list->integer lst
4317@defunx booleans->integer bool@dots{}
4318Return an integer formed bitwise from the given @var{lst} list of
4319booleans, or for @code{booleans->integer} from the @var{bool}
4320arguments.
4321
4322Each boolean is @code{#t} for a 1 and @code{#f} for a 0. The first
4323element becomes the most significant bit in the return.
4324
4325@example
4326(list->integer '(#t #f #t #f)) @result{} 10
4327@end example
4328@end defun
4329
4330
43ed3b69
MV
4331@node SRFI-61
4332@subsection SRFI-61 - A more general @code{cond} clause
4333
4334This SRFI extends RnRS @code{cond} to support test expressions that
4335return multiple values, as well as arbitrary definitions of test
4336success. SRFI 61 is implemented in the Guile core; there's no module
4337needed to get SRFI-61 itself. Extended @code{cond} is documented in
4338@ref{if cond case,, Simple Conditional Evaluation}.
4339
8175a07e
AR
4340@node SRFI-67
4341@subsection SRFI-67 - Compare procedures
4342@cindex SRFI-67
4343
4344See @uref{http://srfi.schemers.org/srfi-67/srfi-67.html, the
4345specification of SRFI-67}.
43ed3b69 4346
1317062f
LC
4347@node SRFI-69
4348@subsection SRFI-69 - Basic hash tables
4349@cindex SRFI-69
4350
4351This is a portable wrapper around Guile's built-in hash table and weak
4352table support. @xref{Hash Tables}, for information on that built-in
4353support. Above that, this hash-table interface provides association
4354of equality and hash functions with tables at creation time, so
4355variants of each function are not required, as well as a procedure
4356that takes care of most uses for Guile hash table handles, which this
4357SRFI does not provide as such.
4358
4359Access it with:
4360
4361@lisp
4362(use-modules (srfi srfi-69))
4363@end lisp
4364
4365@menu
4366* SRFI-69 Creating hash tables::
4367* SRFI-69 Accessing table items::
4368* SRFI-69 Table properties::
4369* SRFI-69 Hash table algorithms::
4370@end menu
4371
4372@node SRFI-69 Creating hash tables
4373@subsubsection Creating hash tables
4374
4375@deffn {Scheme Procedure} make-hash-table [equal-proc hash-proc #:weak weakness start-size]
4376Create and answer a new hash table with @var{equal-proc} as the
4377equality function and @var{hash-proc} as the hashing function.
4378
4379By default, @var{equal-proc} is @code{equal?}. It can be any
4380two-argument procedure, and should answer whether two keys are the
4381same for this table's purposes.
4382
4383My default @var{hash-proc} assumes that @code{equal-proc} is no
4384coarser than @code{equal?} unless it is literally @code{string-ci=?}.
4385If provided, @var{hash-proc} should be a two-argument procedure that
4386takes a key and the current table size, and answers a reasonably good
4387hash integer between 0 (inclusive) and the size (exclusive).
4388
4389@var{weakness} should be @code{#f} or a symbol indicating how ``weak''
4390the hash table is:
4391
4392@table @code
4393@item #f
4394An ordinary non-weak hash table. This is the default.
4395
4396@item key
4397When the key has no more non-weak references at GC, remove that entry.
4398
4399@item value
4400When the value has no more non-weak references at GC, remove that
4401entry.
4402
4403@item key-or-value
4404When either has no more non-weak references at GC, remove the
4405association.
4406@end table
4407
4408As a legacy of the time when Guile couldn't grow hash tables,
4409@var{start-size} is an optional integer argument that specifies the
dfe8c13b
LC
4410approximate starting size for the hash table, which will be rounded to
4411an algorithmically-sounder number.
1317062f
LC
4412@end deffn
4413
dfe8c13b 4414By @dfn{coarser} than @code{equal?}, we mean that for all @var{x} and
1317062f
LC
4415@var{y} values where @code{(@var{equal-proc} @var{x} @var{y})},
4416@code{(equal? @var{x} @var{y})} as well. If that does not hold for
4417your @var{equal-proc}, you must provide a @var{hash-proc}.
4418
4419In the case of weak tables, remember that @dfn{references} above
4420always refers to @code{eq?}-wise references. Just because you have a
4421reference to some string @code{"foo"} doesn't mean that an association
4422with key @code{"foo"} in a weak-key table @emph{won't} be collected;
4423it only counts as a reference if the two @code{"foo"}s are @code{eq?},
4424regardless of @var{equal-proc}. As such, it is usually only sensible
4425to use @code{eq?} and @code{hashq} as the equivalence and hash
4426functions for a weak table. @xref{Weak References}, for more
4427information on Guile's built-in weak table support.
4428
4429@deffn {Scheme Procedure} alist->hash-table alist [equal-proc hash-proc #:weak weakness start-size]
4430As with @code{make-hash-table}, but initialize it with the
4431associations in @var{alist}. Where keys are repeated in @var{alist},
4432the leftmost association takes precedence.
4433@end deffn
4434
4435@node SRFI-69 Accessing table items
4436@subsubsection Accessing table items
4437
4438@deffn {Scheme Procedure} hash-table-ref table key [default-thunk]
4439@deffnx {Scheme Procedure} hash-table-ref/default table key default
4440Answer the value associated with @var{key} in @var{table}. If
4441@var{key} is not present, answer the result of invoking the thunk
4442@var{default-thunk}, which signals an error instead by default.
4443
4444@code{hash-table-ref/default} is a variant that requires a third
4445argument, @var{default}, and answers @var{default} itself instead of
4446invoking it.
4447@end deffn
4448
4449@deffn {Scheme Procedure} hash-table-set! table key new-value
4450Set @var{key} to @var{new-value} in @var{table}.
4451@end deffn
4452
4453@deffn {Scheme Procedure} hash-table-delete! table key
4454Remove the association of @var{key} in @var{table}, if present. If
4455absent, do nothing.
4456@end deffn
4457
4458@deffn {Scheme Procedure} hash-table-exists? table key
4459Answer whether @var{key} has an association in @var{table}.
4460@end deffn
4461
4462@deffn {Scheme Procedure} hash-table-update! table key modifier [default-thunk]
4463@deffnx {Scheme Procedure} hash-table-update!/default table key modifier default
4464Replace @var{key}'s associated value in @var{table} by invoking
4465@var{modifier} with one argument, the old value.
4466
4467If @var{key} is not present, and @var{default-thunk} is provided,
4468invoke it with no arguments to get the ``old value'' to be passed to
4469@var{modifier} as above. If @var{default-thunk} is not provided in
4470such a case, signal an error.
4471
4472@code{hash-table-update!/default} is a variant that requires the
4473fourth argument, which is used directly as the ``old value'' rather
4474than as a thunk to be invoked to retrieve the ``old value''.
4475@end deffn
4476
4477@node SRFI-69 Table properties
4478@subsubsection Table properties
4479
4480@deffn {Scheme Procedure} hash-table-size table
4481Answer the number of associations in @var{table}. This is guaranteed
4482to run in constant time for non-weak tables.
4483@end deffn
4484
4485@deffn {Scheme Procedure} hash-table-keys table
4486Answer an unordered list of the keys in @var{table}.
4487@end deffn
4488
4489@deffn {Scheme Procedure} hash-table-values table
4490Answer an unordered list of the values in @var{table}.
4491@end deffn
4492
4493@deffn {Scheme Procedure} hash-table-walk table proc
4494Invoke @var{proc} once for each association in @var{table}, passing
4495the key and value as arguments.
4496@end deffn
4497
4498@deffn {Scheme Procedure} hash-table-fold table proc init
4499Invoke @code{(@var{proc} @var{key} @var{value} @var{previous})} for
4500each @var{key} and @var{value} in @var{table}, where @var{previous} is
4501the result of the previous invocation, using @var{init} as the first
4502@var{previous} value. Answer the final @var{proc} result.
4503@end deffn
4504
4505@deffn {Scheme Procedure} hash-table->alist table
4506Answer an alist where each association in @var{table} is an
4507association in the result.
4508@end deffn
4509
4510@node SRFI-69 Hash table algorithms
4511@subsubsection Hash table algorithms
4512
4513Each hash table carries an @dfn{equivalence function} and a @dfn{hash
4514function}, used to implement key lookups. Beginning users should
4515follow the rules for consistency of the default @var{hash-proc}
4516specified above. Advanced users can use these to implement their own
4517equivalence and hash functions for specialized lookup semantics.
4518
4519@deffn {Scheme Procedure} hash-table-equivalence-function hash-table
4520@deffnx {Scheme Procedure} hash-table-hash-function hash-table
4521Answer the equivalence and hash function of @var{hash-table}, respectively.
4522@end deffn
4523
4524@deffn {Scheme Procedure} hash obj [size]
4525@deffnx {Scheme Procedure} string-hash obj [size]
4526@deffnx {Scheme Procedure} string-ci-hash obj [size]
4527@deffnx {Scheme Procedure} hash-by-identity obj [size]
4528Answer a hash value appropriate for equality predicate @code{equal?},
4529@code{string=?}, @code{string-ci=?}, and @code{eq?}, respectively.
4530@end deffn
4531
4532@code{hash} is a backwards-compatible replacement for Guile's built-in
4533@code{hash}.
4534
189681f5
LC
4535@node SRFI-88
4536@subsection SRFI-88 Keyword Objects
4537@cindex SRFI-88
4538@cindex keyword objects
4539
e36280cb 4540@uref{http://srfi.schemers.org/srfi-88/srfi-88.html, SRFI-88} provides
189681f5
LC
4541@dfn{keyword objects}, which are equivalent to Guile's keywords
4542(@pxref{Keywords}). SRFI-88 keywords can be entered using the
4543@dfn{postfix keyword syntax}, which consists of an identifier followed
1518f649 4544by @code{:} (@pxref{Scheme Read, @code{postfix} keyword syntax}).
189681f5
LC
4545SRFI-88 can be made available with:
4546
4547@example
4548(use-modules (srfi srfi-88))
4549@end example
4550
4551Doing so installs the right reader option for keyword syntax, using
4552@code{(read-set! keywords 'postfix)}. It also provides the procedures
4553described below.
4554
4555@deffn {Scheme Procedure} keyword? obj
4556Return @code{#t} if @var{obj} is a keyword. This is the same procedure
4557as the same-named built-in procedure (@pxref{Keyword Procedures,
4558@code{keyword?}}).
4559
4560@example
4561(keyword? foo:) @result{} #t
4562(keyword? 'foo:) @result{} #t
4563(keyword? "foo") @result{} #f
4564@end example
4565@end deffn
4566
4567@deffn {Scheme Procedure} keyword->string kw
4568Return the name of @var{kw} as a string, i.e., without the trailing
4569colon. The returned string may not be modified, e.g., with
4570@code{string-set!}.
4571
4572@example
4573(keyword->string foo:) @result{} "foo"
4574@end example
4575@end deffn
4576
4577@deffn {Scheme Procedure} string->keyword str
4578Return the keyword object whose name is @var{str}.
4579
4580@example
4581(keyword->string (string->keyword "a b c")) @result{} "a b c"
4582@end example
4583@end deffn
4584
922d417b
JG
4585@node SRFI-98
4586@subsection SRFI-98 Accessing environment variables.
4587@cindex SRFI-98
4588@cindex environment variables
4589
4590This is a portable wrapper around Guile's built-in support for
4591interacting with the current environment, @xref{Runtime Environment}.
4592
4593@deffn {Scheme Procedure} get-environment-variable name
4594Returns a string containing the value of the environment variable
4595given by the string @code{name}, or @code{#f} if the named
4596environment variable is not found. This is equivalent to
4597@code{(getenv name)}.
4598@end deffn
4599
4600@deffn {Scheme Procedure} get-environment-variables
4601Returns the names and values of all the environment variables as an
4602association list in which both the keys and the values are strings.
4603@end deffn
1317062f 4604
12991fed 4605@c srfi-modules.texi ends here
193239f1
KR
4606
4607@c Local Variables:
4608@c TeX-master: "guile.texi"
4609@c End: