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