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