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