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