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