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