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 | |
9 | @chapter 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 |
a0e07ba4 NJ |
39 | @end menu |
40 | ||
41 | ||
42 | @node About SRFI Usage | |
43 | @section About SRFI Usage | |
44 | ||
45 | @c FIXME::martin: Review me! | |
46 | ||
47 | SRFI support in Guile is currently implemented partly in the core | |
48 | library, and partly as add-on modules. That means that some SRFIs are | |
49 | automatically available when the interpreter is started, whereas the | |
50 | other SRFIs require you to use the appropriate support module | |
12991fed | 51 | explicitly. |
a0e07ba4 NJ |
52 | |
53 | There are several reasons for this inconsistency. First, the feature | |
54 | checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be | |
55 | available immediately, because it must be there when the user wants to | |
56 | check for the Scheme implementation, that is, before she can know that | |
57 | it is safe to use @code{use-modules} to load SRFI support modules. The | |
58 | second reason is that some features defined in SRFIs had been | |
59 | implemented in Guile before the developers started to add SRFI | |
60 | implementations as modules (for example SRFI-6 (@pxref{SRFI-6})). In | |
61 | the future, it is possible that SRFIs in the core library might be | |
62 | factored out into separate modules, requiring explicit module loading | |
63 | when they are needed. So you should be prepared to have to use | |
64 | @code{use-modules} someday in the future to access SRFI-6 bindings. If | |
65 | you want, you can do that already. We have included the module | |
66 | @code{(srfi srfi-6)} in the distribution, which currently does nothing, | |
67 | but ensures that you can write future-safe code. | |
68 | ||
69 | Generally, support for a specific SRFI is made available by using | |
70 | modules named @code{(srfi srfi-@var{number})}, where @var{number} is the | |
71 | number of the SRFI needed. Another possibility is to use the command | |
72 | line option @code{--use-srfi}, which will load the necessary modules | |
73 | automatically (@pxref{Invoking Guile}). | |
74 | ||
75 | ||
76 | @node SRFI-0 | |
77 | @section SRFI-0 - cond-expand | |
8742c48b | 78 | @cindex SRFI-0 |
7ea6af07 | 79 | @findex cond-expand |
a0e07ba4 NJ |
80 | |
81 | @c FIXME::martin: Review me! | |
82 | ||
83 | SRFI-0 defines a means for checking whether a Scheme implementation has | |
84 | support for a specified feature. The syntactic form @code{cond-expand}, | |
85 | which implements this means, has the following syntax. | |
86 | ||
87 | @example | |
88 | @group | |
89 | <cond-expand> | |
90 | --> (cond-expand <cond-expand-clause>+) | |
91 | | (cond-expand <cond-expand-clause>* (else <command-or-definition>)) | |
92 | <cond-expand-clause> | |
93 | --> (<feature-requirement> <command-or-definition>*) | |
94 | <feature-requirement> | |
95 | --> <feature-identifier> | |
96 | | (and <feature-requirement>*) | |
97 | | (or <feature-requirement>*) | |
98 | | (not <feature-requirement>) | |
99 | <feature-identifier> | |
100 | --> <a symbol which is the name or alias of a SRFI> | |
101 | @end group | |
102 | @end example | |
103 | ||
104 | When evaluated, this form checks all clauses in order, until it finds | |
105 | one whose feature requirement is satisfied. Then the form expands into | |
106 | the commands or definitions in the clause. A requirement is tested as | |
107 | follows: | |
108 | ||
109 | @itemize @bullet | |
110 | @item | |
111 | If it is a symbol, it is satisfied if the feature identifier is | |
112 | supported. | |
113 | ||
114 | @item | |
115 | If it is an @code{and} form, all requirements must be satisfied. If no | |
116 | requirements are given, it is satisfied, too. | |
117 | ||
118 | @item | |
119 | If it is an @code{or} form, at least one of the requirements must be | |
120 | satisfied. If no requirements are given, it is not satisfied. | |
121 | ||
122 | @item | |
123 | If it is a @code{not} form, the feature requirement must @emph{not} be | |
124 | satisfied. | |
125 | ||
126 | @item | |
127 | If the feature requirement is the keyword @code{else} and it is the last | |
128 | clause, it is satisfied if no prior clause matched. | |
129 | @end itemize | |
130 | ||
131 | If no clause is satisfied, an error is signalled. | |
132 | ||
133 | Since @code{cond-expand} is needed to tell what a Scheme implementation | |
134 | provides, it must be accessible without using any | |
85a9b4ed | 135 | implementation-dependent operations, such as @code{use-modules} in |
a0e07ba4 NJ |
136 | Guile. Thus, it is not necessary to use any module to get access to |
137 | this form. | |
138 | ||
139 | Currently, the feature identifiers @code{guile}, @code{r5rs} and | |
140 | @code{srfi-0} are supported. The other SRFIs are not in that list by | |
141 | default, because the SRFI modules must be explicitly used before their | |
12991fed | 142 | exported bindings can be used. |
a0e07ba4 NJ |
143 | |
144 | So if a Scheme program wishes to use SRFI-8, it has two possibilities: | |
145 | First, it can check whether the running Scheme implementation is Guile, | |
146 | and if it is, it can use the appropriate module: | |
147 | ||
148 | @lisp | |
149 | (cond-expand | |
150 | (guile | |
151 | (use-modules (srfi srfi-8))) | |
152 | (srfi-8 | |
153 | #t)) | |
154 | ;; otherwise fail. | |
155 | @end lisp | |
156 | ||
157 | The other possibility is to use the @code{--use-srfi} command line | |
158 | option when invoking Guile (@pxref{Invoking Guile}). When you do that, | |
159 | the specified SRFI support modules will be loaded and add their feature | |
160 | identifier to the list of symbols checked by @code{cond-expand}. | |
161 | ||
162 | So, if you invoke Guile like this: | |
163 | ||
164 | @example | |
165 | $ guile --use-srfi=8 | |
166 | @end example | |
167 | ||
168 | the following snippet will expand to @code{'hooray}. | |
169 | ||
170 | @lisp | |
171 | (cond-expand (srfi-8 'hooray)) | |
172 | @end lisp | |
173 | ||
174 | ||
175 | @node SRFI-1 | |
176 | @section SRFI-1 - List library | |
8742c48b | 177 | @cindex SRFI-1 |
a0e07ba4 NJ |
178 | |
179 | @c FIXME::martin: Review me! | |
180 | ||
181 | The list library defined in SRFI-1 contains a lot of useful list | |
182 | processing procedures for construction, examining, destructuring and | |
183 | manipulating lists and pairs. | |
184 | ||
185 | Since SRFI-1 also defines some procedures which are already contained | |
186 | in R5RS and thus are supported by the Guile core library, some list | |
187 | and pair procedures which appear in the SRFI-1 document may not appear | |
188 | in this section. So when looking for a particular list/pair | |
189 | processing procedure, you should also have a look at the sections | |
190 | @ref{Lists} and @ref{Pairs}. | |
191 | ||
192 | @menu | |
193 | * SRFI-1 Constructors:: Constructing new lists. | |
194 | * SRFI-1 Predicates:: Testing list for specific properties. | |
195 | * SRFI-1 Selectors:: Selecting elements from lists. | |
196 | * SRFI-1 Length Append etc:: Length calculation and list appending. | |
197 | * SRFI-1 Fold and Map:: Higher-order list processing. | |
198 | * SRFI-1 Filtering and Partitioning:: Filter lists based on predicates. | |
85a9b4ed | 199 | * SRFI-1 Searching:: Search for elements. |
a0e07ba4 NJ |
200 | * SRFI-1 Deleting:: Delete elements from lists. |
201 | * SRFI-1 Association Lists:: Handle association lists. | |
202 | * SRFI-1 Set Operations:: Use lists for representing sets. | |
203 | @end menu | |
204 | ||
205 | @node SRFI-1 Constructors | |
206 | @subsection Constructors | |
207 | ||
208 | @c FIXME::martin: Review me! | |
209 | ||
210 | New lists can be constructed by calling one of the following | |
211 | procedures. | |
212 | ||
8f85c0c6 | 213 | @deffn {Scheme Procedure} xcons d a |
a0e07ba4 NJ |
214 | Like @code{cons}, but with interchanged arguments. Useful mostly when |
215 | passed to higher-order procedures. | |
216 | @end deffn | |
217 | ||
8f85c0c6 | 218 | @deffn {Scheme Procedure} list-tabulate n init-proc |
a0e07ba4 NJ |
219 | Return an @var{n}-element list, where each list element is produced by |
220 | applying the procedure @var{init-proc} to the corresponding list | |
221 | index. The order in which @var{init-proc} is applied to the indices | |
222 | is not specified. | |
223 | @end deffn | |
224 | ||
57066448 KR |
225 | @deffn {Scheme Procedure} list-copy lst |
226 | Return a new list containing the elements of the list @var{lst}. | |
227 | ||
228 | This function differs from the core @code{list-copy} (@pxref{List | |
229 | Constructors}) in accepting improper lists too. And if @var{lst} is | |
230 | not a pair at all then it's treated as the final tail of an improper | |
231 | list and simply returned. | |
232 | @end deffn | |
233 | ||
8f85c0c6 | 234 | @deffn {Scheme Procedure} circular-list elt1 elt2 @dots{} |
a0e07ba4 NJ |
235 | Return a circular list containing the given arguments @var{elt1} |
236 | @var{elt2} @dots{}. | |
237 | @end deffn | |
238 | ||
8f85c0c6 | 239 | @deffn {Scheme Procedure} iota count [start step] |
256853db KR |
240 | Return a list containing @var{count} numbers, starting from |
241 | @var{start} and adding @var{step} each time. The default @var{start} | |
242 | is 0, the default @var{step} is 1. For example, | |
a0e07ba4 | 243 | |
256853db KR |
244 | @example |
245 | (iota 6) @result{} (0 1 2 3 4 5) | |
246 | (iota 4 2.5 -2) @result{} (2.5 0.5 -1.5 -3.5) | |
247 | @end example | |
a0e07ba4 | 248 | |
256853db KR |
249 | This function takes its name from the corresponding primitive in the |
250 | APL language. | |
a0e07ba4 NJ |
251 | @end deffn |
252 | ||
253 | ||
254 | @node SRFI-1 Predicates | |
255 | @subsection Predicates | |
256 | ||
257 | @c FIXME::martin: Review me! | |
258 | ||
259 | The procedures in this section test specific properties of lists. | |
260 | ||
8f85c0c6 | 261 | @deffn {Scheme Procedure} proper-list? obj |
a0e07ba4 NJ |
262 | Return @code{#t} if @var{obj} is a proper list, that is a finite list, |
263 | terminated with the empty list. Otherwise, return @code{#f}. | |
264 | @end deffn | |
265 | ||
8f85c0c6 | 266 | @deffn {Scheme Procedure} circular-list? obj |
a0e07ba4 NJ |
267 | Return @code{#t} if @var{obj} is a circular list, otherwise return |
268 | @code{#f}. | |
269 | @end deffn | |
270 | ||
8f85c0c6 | 271 | @deffn {Scheme Procedure} dotted-list? obj |
a0e07ba4 NJ |
272 | Return @code{#t} if @var{obj} is a dotted list, return @code{#f} |
273 | otherwise. A dotted list is a finite list which is not terminated by | |
274 | the empty list, but some other value. | |
275 | @end deffn | |
276 | ||
8f85c0c6 | 277 | @deffn {Scheme Procedure} null-list? lst |
a0e07ba4 NJ |
278 | Return @code{#t} if @var{lst} is the empty list @code{()}, @code{#f} |
279 | otherwise. If something else than a proper or circular list is passed | |
85a9b4ed | 280 | as @var{lst}, an error is signalled. This procedure is recommended |
a0e07ba4 NJ |
281 | for checking for the end of a list in contexts where dotted lists are |
282 | not allowed. | |
283 | @end deffn | |
284 | ||
8f85c0c6 | 285 | @deffn {Scheme Procedure} not-pair? obj |
a0e07ba4 NJ |
286 | Return @code{#t} is @var{obj} is not a pair, @code{#f} otherwise. |
287 | This is shorthand notation @code{(not (pair? @var{obj}))} and is | |
288 | supposed to be used for end-of-list checking in contexts where dotted | |
289 | lists are allowed. | |
290 | @end deffn | |
291 | ||
8f85c0c6 | 292 | @deffn {Scheme Procedure} list= elt= list1 @dots{} |
a0e07ba4 NJ |
293 | Return @code{#t} if all argument lists are equal, @code{#f} otherwise. |
294 | List equality is determined by testing whether all lists have the same | |
295 | length and the corresponding elements are equal in the sense of the | |
296 | equality predicate @var{elt=}. If no or only one list is given, | |
297 | @code{#t} is returned. | |
298 | @end deffn | |
299 | ||
300 | ||
301 | @node SRFI-1 Selectors | |
302 | @subsection Selectors | |
303 | ||
304 | @c FIXME::martin: Review me! | |
305 | ||
8f85c0c6 NJ |
306 | @deffn {Scheme Procedure} first pair |
307 | @deffnx {Scheme Procedure} second pair | |
308 | @deffnx {Scheme Procedure} third pair | |
309 | @deffnx {Scheme Procedure} fourth pair | |
310 | @deffnx {Scheme Procedure} fifth pair | |
311 | @deffnx {Scheme Procedure} sixth pair | |
312 | @deffnx {Scheme Procedure} seventh pair | |
313 | @deffnx {Scheme Procedure} eighth pair | |
314 | @deffnx {Scheme Procedure} ninth pair | |
315 | @deffnx {Scheme Procedure} tenth pair | |
a0e07ba4 NJ |
316 | These are synonyms for @code{car}, @code{cadr}, @code{caddr}, @dots{}. |
317 | @end deffn | |
318 | ||
8f85c0c6 | 319 | @deffn {Scheme Procedure} car+cdr pair |
a0e07ba4 NJ |
320 | Return two values, the @sc{car} and the @sc{cdr} of @var{pair}. |
321 | @end deffn | |
322 | ||
8f85c0c6 NJ |
323 | @deffn {Scheme Procedure} take lst i |
324 | @deffnx {Scheme Procedure} take! lst i | |
a0e07ba4 NJ |
325 | Return a list containing the first @var{i} elements of @var{lst}. |
326 | ||
327 | @code{take!} may modify the structure of the argument list @var{lst} | |
328 | in order to produce the result. | |
329 | @end deffn | |
330 | ||
8f85c0c6 | 331 | @deffn {Scheme Procedure} drop lst i |
a0e07ba4 NJ |
332 | Return a list containing all but the first @var{i} elements of |
333 | @var{lst}. | |
334 | @end deffn | |
335 | ||
8f85c0c6 | 336 | @deffn {Scheme Procedure} take-right lst i |
a0e07ba4 NJ |
337 | Return the a list containing the @var{i} last elements of @var{lst}. |
338 | @end deffn | |
339 | ||
8f85c0c6 NJ |
340 | @deffn {Scheme Procedure} drop-right lst i |
341 | @deffnx {Scheme Procedure} drop-right! lst i | |
a0e07ba4 NJ |
342 | Return the a list containing all but the @var{i} last elements of |
343 | @var{lst}. | |
344 | ||
345 | @code{drop-right!} may modify the structure of the argument list | |
346 | @var{lst} in order to produce the result. | |
347 | @end deffn | |
348 | ||
8f85c0c6 NJ |
349 | @deffn {Scheme Procedure} split-at lst i |
350 | @deffnx {Scheme Procedure} split-at! lst i | |
a0e07ba4 NJ |
351 | Return two values, a list containing the first @var{i} elements of the |
352 | list @var{lst} and a list containing the remaining elements. | |
353 | ||
354 | @code{split-at!} may modify the structure of the argument list | |
355 | @var{lst} in order to produce the result. | |
356 | @end deffn | |
357 | ||
8f85c0c6 | 358 | @deffn {Scheme Procedure} last lst |
a0e07ba4 NJ |
359 | Return the last element of the non-empty, finite list @var{lst}. |
360 | @end deffn | |
361 | ||
362 | ||
363 | @node SRFI-1 Length Append etc | |
364 | @subsection Length, Append, Concatenate, etc. | |
365 | ||
366 | @c FIXME::martin: Review me! | |
367 | ||
8f85c0c6 | 368 | @deffn {Scheme Procedure} length+ lst |
a0e07ba4 NJ |
369 | Return the length of the argument list @var{lst}. When @var{lst} is a |
370 | circular list, @code{#f} is returned. | |
371 | @end deffn | |
372 | ||
8f85c0c6 NJ |
373 | @deffn {Scheme Procedure} concatenate list-of-lists |
374 | @deffnx {Scheme Procedure} concatenate! list-of-lists | |
a0e07ba4 NJ |
375 | Construct a list by appending all lists in @var{list-of-lists}. |
376 | ||
377 | @code{concatenate!} may modify the structure of the given lists in | |
378 | order to produce the result. | |
379 | @end deffn | |
380 | ||
8f85c0c6 NJ |
381 | @deffn {Scheme Procedure} append-reverse rev-head tail |
382 | @deffnx {Scheme Procedure} append-reverse! rev-head tail | |
a0e07ba4 NJ |
383 | Reverse @var{rev-head}, append @var{tail} and return the result. This |
384 | is equivalent to @code{(append (reverse @var{rev-head}) @var{tail})}, | |
385 | but more efficient. | |
386 | ||
387 | @code{append-reverse!} may modify @var{rev-head} in order to produce | |
388 | the result. | |
389 | @end deffn | |
390 | ||
8f85c0c6 | 391 | @deffn {Scheme Procedure} zip lst1 lst2 @dots{} |
a0e07ba4 NJ |
392 | Return a list as long as the shortest of the argument lists, where |
393 | each element is a list. The first list contains the first elements of | |
394 | the argument lists, the second list contains the second elements, and | |
395 | so on. | |
396 | @end deffn | |
397 | ||
8f85c0c6 NJ |
398 | @deffn {Scheme Procedure} unzip1 lst |
399 | @deffnx {Scheme Procedure} unzip2 lst | |
400 | @deffnx {Scheme Procedure} unzip3 lst | |
401 | @deffnx {Scheme Procedure} unzip4 lst | |
402 | @deffnx {Scheme Procedure} unzip5 lst | |
a0e07ba4 NJ |
403 | @code{unzip1} takes a list of lists, and returns a list containing the |
404 | first elements of each list, @code{unzip2} returns two lists, the | |
405 | first containing the first elements of each lists and the second | |
406 | containing the second elements of each lists, and so on. | |
407 | @end deffn | |
408 | ||
e508c863 KR |
409 | @deffn {Scheme Procedure} count pred lst1 @dots{} lstN |
410 | Return a count of the number of times @var{pred} returns true when | |
411 | called on elements from the given lists. | |
412 | ||
413 | @var{pred} is called with @var{N} parameters @code{(@var{pred} | |
414 | @var{elem1} @dots{} @var{elemN})}, each element being from the | |
415 | corresponding @var{lst1} @dots{} @var{lstN}. The first call is with | |
416 | the first element of each list, the second with the second element | |
417 | from each, and so on. | |
418 | ||
419 | Counting stops when the end of the shortest list is reached. At least | |
420 | one list must be non-circular. | |
421 | @end deffn | |
422 | ||
a0e07ba4 NJ |
423 | |
424 | @node SRFI-1 Fold and Map | |
425 | @subsection Fold, Unfold & Map | |
426 | ||
427 | @c FIXME::martin: Review me! | |
428 | ||
8f85c0c6 | 429 | @deffn {Scheme Procedure} fold kons knil lst1 lst2 @dots{} |
a0e07ba4 NJ |
430 | Fold the procedure @var{kons} across all elements of @var{lst1}, |
431 | @var{lst2}, @dots{}. Produce the result of | |
432 | ||
433 | @code{(@var{kons} @var{en1} @var{en2} @dots{} (@var{kons} @var{e21} | |
434 | @var{e22} (@var{kons} @var{e11} @var{e12} @var{knil})))}, | |
435 | ||
436 | if @var{enm} are the elements of the lists @var{lst1}, @var{lst2}, | |
437 | @dots{}. | |
438 | @end deffn | |
439 | ||
8f85c0c6 | 440 | @deffn {Scheme Procedure} fold-right kons knil lst1 lst2 @dots{} |
a0e07ba4 NJ |
441 | Similar to @code{fold}, but applies @var{kons} in right-to-left order |
442 | to the list elements, that is: | |
443 | ||
444 | @code{(@var{kons} @var{e11} @var{e12}(@var{kons} @var{e21} | |
445 | @var{e22} @dots{} (@var{kons} @var{en1} @var{en2} @var{knil})))}, | |
446 | @end deffn | |
447 | ||
8f85c0c6 | 448 | @deffn {Scheme Procedure} pair-fold kons knil lst1 lst2 @dots{} |
a0e07ba4 NJ |
449 | Like @code{fold}, but apply @var{kons} to the pairs of the list |
450 | instead of the list elements. | |
451 | @end deffn | |
452 | ||
8f85c0c6 | 453 | @deffn {Scheme Procedure} pair-fold-right kons knil lst1 lst2 @dots{} |
a0e07ba4 NJ |
454 | Like @code{fold-right}, but apply @var{kons} to the pairs of the list |
455 | instead of the list elements. | |
456 | @end deffn | |
457 | ||
8f85c0c6 | 458 | @deffn {Scheme Procedure} reduce f ridentity lst |
1ae7b878 KR |
459 | @code{reduce} is a variant of @code{fold}. If @var{lst} is |
460 | @code{()}, @var{ridentity} is returned. Otherwise, @code{(fold f (car | |
a0e07ba4 NJ |
461 | @var{lst}) (cdr @var{lst}))} is returned. |
462 | @end deffn | |
463 | ||
8f85c0c6 | 464 | @deffn {Scheme Procedure} reduce-right f ridentity lst |
b5aa0215 | 465 | This is the @code{fold-right} variant of @code{reduce}. |
a0e07ba4 NJ |
466 | @end deffn |
467 | ||
8f85c0c6 | 468 | @deffn {Scheme Procedure} unfold p f g seed [tail-gen] |
a0e07ba4 NJ |
469 | @code{unfold} is defined as follows: |
470 | ||
471 | @lisp | |
472 | (unfold p f g seed) = | |
473 | (if (p seed) (tail-gen seed) | |
474 | (cons (f seed) | |
475 | (unfold p f g (g seed)))) | |
476 | @end lisp | |
477 | ||
478 | @table @var | |
479 | @item p | |
480 | Determines when to stop unfolding. | |
481 | ||
482 | @item f | |
483 | Maps each seed value to the corresponding list element. | |
484 | ||
485 | @item g | |
486 | Maps each seed value to next seed valu. | |
487 | ||
488 | @item seed | |
489 | The state value for the unfold. | |
490 | ||
491 | @item tail-gen | |
492 | Creates the tail of the list; defaults to @code{(lambda (x) '())}. | |
493 | @end table | |
494 | ||
495 | @var{g} produces a series of seed values, which are mapped to list | |
496 | elements by @var{f}. These elements are put into a list in | |
497 | left-to-right order, and @var{p} tells when to stop unfolding. | |
498 | @end deffn | |
499 | ||
8f85c0c6 | 500 | @deffn {Scheme Procedure} unfold-right p f g seed [tail] |
a0e07ba4 NJ |
501 | Construct a list with the following loop. |
502 | ||
503 | @lisp | |
504 | (let lp ((seed seed) (lis tail)) | |
505 | (if (p seed) lis | |
506 | (lp (g seed) | |
507 | (cons (f seed) lis)))) | |
508 | @end lisp | |
509 | ||
510 | @table @var | |
511 | @item p | |
512 | Determines when to stop unfolding. | |
513 | ||
514 | @item f | |
515 | Maps each seed value to the corresponding list element. | |
516 | ||
517 | @item g | |
518 | Maps each seed value to next seed valu. | |
519 | ||
520 | @item seed | |
521 | The state value for the unfold. | |
522 | ||
523 | @item tail-gen | |
524 | Creates the tail of the list; defaults to @code{(lambda (x) '())}. | |
525 | @end table | |
526 | ||
527 | @end deffn | |
528 | ||
8f85c0c6 | 529 | @deffn {Scheme Procedure} map f lst1 lst2 @dots{} |
a0e07ba4 NJ |
530 | Map the procedure over the list(s) @var{lst1}, @var{lst2}, @dots{} and |
531 | return a list containing the results of the procedure applications. | |
532 | This procedure is extended with respect to R5RS, because the argument | |
533 | lists may have different lengths. The result list will have the same | |
534 | length as the shortest argument lists. The order in which @var{f} | |
535 | will be applied to the list element(s) is not specified. | |
536 | @end deffn | |
537 | ||
8f85c0c6 | 538 | @deffn {Scheme Procedure} for-each f lst1 lst2 @dots{} |
a0e07ba4 NJ |
539 | Apply the procedure @var{f} to each pair of corresponding elements of |
540 | the list(s) @var{lst1}, @var{lst2}, @dots{}. The return value is not | |
541 | specified. This procedure is extended with respect to R5RS, because | |
542 | the argument lists may have different lengths. The shortest argument | |
543 | list determines the number of times @var{f} is called. @var{f} will | |
85a9b4ed | 544 | be applied to the list elements in left-to-right order. |
a0e07ba4 NJ |
545 | |
546 | @end deffn | |
547 | ||
8f85c0c6 NJ |
548 | @deffn {Scheme Procedure} append-map f lst1 lst2 @dots{} |
549 | @deffnx {Scheme Procedure} append-map! f lst1 lst2 @dots{} | |
12991fed | 550 | Equivalent to |
a0e07ba4 NJ |
551 | |
552 | @lisp | |
12991fed | 553 | (apply append (map f clist1 clist2 ...)) |
a0e07ba4 NJ |
554 | @end lisp |
555 | ||
12991fed | 556 | and |
a0e07ba4 NJ |
557 | |
558 | @lisp | |
12991fed | 559 | (apply append! (map f clist1 clist2 ...)) |
a0e07ba4 NJ |
560 | @end lisp |
561 | ||
562 | Map @var{f} over the elements of the lists, just as in the @code{map} | |
563 | function. However, the results of the applications are appended | |
564 | together to make the final result. @code{append-map} uses | |
565 | @code{append} to append the results together; @code{append-map!} uses | |
566 | @code{append!}. | |
567 | ||
568 | The dynamic order in which the various applications of @var{f} are | |
569 | made is not specified. | |
570 | @end deffn | |
571 | ||
8f85c0c6 | 572 | @deffn {Scheme Procedure} map! f lst1 lst2 @dots{} |
a0e07ba4 NJ |
573 | Linear-update variant of @code{map} -- @code{map!} is allowed, but not |
574 | required, to alter the cons cells of @var{lst1} to construct the | |
575 | result list. | |
576 | ||
577 | The dynamic order in which the various applications of @var{f} are | |
578 | made is not specified. In the n-ary case, @var{lst2}, @var{lst3}, | |
579 | @dots{} must have at least as many elements as @var{lst1}. | |
580 | @end deffn | |
581 | ||
8f85c0c6 | 582 | @deffn {Scheme Procedure} pair-for-each f lst1 lst2 @dots{} |
a0e07ba4 NJ |
583 | Like @code{for-each}, but applies the procedure @var{f} to the pairs |
584 | from which the argument lists are constructed, instead of the list | |
585 | elements. The return value is not specified. | |
586 | @end deffn | |
587 | ||
8f85c0c6 | 588 | @deffn {Scheme Procedure} filter-map f lst1 lst2 @dots{} |
a0e07ba4 NJ |
589 | Like @code{map}, but only results from the applications of @var{f} |
590 | which are true are saved in the result list. | |
591 | @end deffn | |
592 | ||
593 | ||
594 | @node SRFI-1 Filtering and Partitioning | |
595 | @subsection Filtering and Partitioning | |
596 | ||
597 | @c FIXME::martin: Review me! | |
598 | ||
599 | Filtering means to collect all elements from a list which satisfy a | |
600 | specific condition. Partitioning a list means to make two groups of | |
601 | list elements, one which contains the elements satisfying a condition, | |
602 | and the other for the elements which don't. | |
603 | ||
60e25dc4 KR |
604 | The @code{filter} and @code{filter!} functions are implemented in the |
605 | Guile core, @xref{List Modification}. | |
a0e07ba4 | 606 | |
8f85c0c6 NJ |
607 | @deffn {Scheme Procedure} partition pred lst |
608 | @deffnx {Scheme Procedure} partition! pred lst | |
193239f1 KR |
609 | Split @var{lst} into those elements which do and don't satisfy the |
610 | predicate @var{pred}. | |
a0e07ba4 | 611 | |
193239f1 KR |
612 | The return is two values (@pxref{Multiple Values}), the first being a |
613 | list of all elements from @var{lst} which satisfy @var{pred}, the | |
614 | second a list of those which do not. | |
615 | ||
616 | The elements in the result lists are in the same order as in @var{lst} | |
617 | but the order in which the calls @code{(@var{pred} elem)} are made on | |
618 | the list elements is unspecified. | |
619 | ||
620 | @code{partition} does not change @var{lst}, but one of the returned | |
621 | lists may share a tail with it. @code{partition!} may modify | |
622 | @var{lst} to construct its return. | |
a0e07ba4 NJ |
623 | @end deffn |
624 | ||
8f85c0c6 NJ |
625 | @deffn {Scheme Procedure} remove pred lst |
626 | @deffnx {Scheme Procedure} remove! pred lst | |
a0e07ba4 NJ |
627 | Return a list containing all elements from @var{lst} which do not |
628 | satisfy the predicate @var{pred}. The elements in the result list | |
629 | have the same order as in @var{lst}. The order in which @var{pred} is | |
630 | applied to the list elements is not specified. | |
631 | ||
632 | @code{remove!} is allowed, but not required to modify the structure of | |
633 | the input list. | |
634 | @end deffn | |
635 | ||
636 | ||
637 | @node SRFI-1 Searching | |
638 | @subsection Searching | |
639 | ||
640 | @c FIXME::martin: Review me! | |
641 | ||
642 | The procedures for searching elements in lists either accept a | |
643 | predicate or a comparison object for determining which elements are to | |
644 | be searched. | |
645 | ||
8f85c0c6 | 646 | @deffn {Scheme Procedure} find pred lst |
a0e07ba4 NJ |
647 | Return the first element of @var{lst} which satisfies the predicate |
648 | @var{pred} and @code{#f} if no such element is found. | |
649 | @end deffn | |
650 | ||
8f85c0c6 | 651 | @deffn {Scheme Procedure} find-tail pred lst |
a0e07ba4 NJ |
652 | Return the first pair of @var{lst} whose @sc{car} satisfies the |
653 | predicate @var{pred} and @code{#f} if no such element is found. | |
654 | @end deffn | |
655 | ||
8f85c0c6 NJ |
656 | @deffn {Scheme Procedure} take-while pred lst |
657 | @deffnx {Scheme Procedure} take-while! pred lst | |
a0e07ba4 NJ |
658 | Return the longest initial prefix of @var{lst} whose elements all |
659 | satisfy the predicate @var{pred}. | |
660 | ||
661 | @code{take-while!} is allowed, but not required to modify the input | |
662 | list while producing the result. | |
663 | @end deffn | |
664 | ||
8f85c0c6 | 665 | @deffn {Scheme Procedure} drop-while pred lst |
a0e07ba4 NJ |
666 | Drop the longest initial prefix of @var{lst} whose elements all |
667 | satisfy the predicate @var{pred}. | |
668 | @end deffn | |
669 | ||
8f85c0c6 NJ |
670 | @deffn {Scheme Procedure} span pred lst |
671 | @deffnx {Scheme Procedure} span! pred lst | |
672 | @deffnx {Scheme Procedure} break pred lst | |
673 | @deffnx {Scheme Procedure} break! pred lst | |
a0e07ba4 NJ |
674 | @code{span} splits the list @var{lst} into the longest initial prefix |
675 | whose elements all satisfy the predicate @var{pred}, and the remaining | |
676 | tail. @code{break} inverts the sense of the predicate. | |
677 | ||
678 | @code{span!} and @code{break!} are allowed, but not required to modify | |
679 | the structure of the input list @var{lst} in order to produce the | |
680 | result. | |
3e73b6f9 KR |
681 | |
682 | Note that the name @code{break} conflicts with the @code{break} | |
683 | binding established by @code{while} (@pxref{while do}). Applications | |
684 | wanting to use @code{break} from within a @code{while} loop will need | |
685 | to make a new define under a different name. | |
a0e07ba4 NJ |
686 | @end deffn |
687 | ||
8f85c0c6 | 688 | @deffn {Scheme Procedure} any pred lst1 lst2 @dots{} |
a0e07ba4 NJ |
689 | Apply @var{pred} across the lists and return a true value if the |
690 | predicate returns true for any of the list elements(s); return | |
691 | @code{#f} otherwise. The true value returned is always the result of | |
85a9b4ed | 692 | the first successful application of @var{pred}. |
a0e07ba4 NJ |
693 | @end deffn |
694 | ||
8f85c0c6 | 695 | @deffn {Scheme Procedure} every pred lst1 lst2 @dots{} |
a0e07ba4 NJ |
696 | Apply @var{pred} across the lists and return a true value if the |
697 | predicate returns true for every of the list elements(s); return | |
698 | @code{#f} otherwise. The true value returned is always the result of | |
85a9b4ed | 699 | the final successful application of @var{pred}. |
a0e07ba4 NJ |
700 | @end deffn |
701 | ||
8f85c0c6 | 702 | @deffn {Scheme Procedure} list-index pred lst1 lst2 @dots{} |
a0e07ba4 NJ |
703 | Return the index of the leftmost element that satisfies @var{pred}. |
704 | @end deffn | |
705 | ||
8f85c0c6 | 706 | @deffn {Scheme Procedure} member x lst [=] |
a0e07ba4 NJ |
707 | Return the first sublist of @var{lst} whose @sc{car} is equal to |
708 | @var{x}. If @var{x} does no appear in @var{lst}, return @code{#f}. | |
709 | Equality is determined by the equality predicate @var{=}, or | |
710 | @code{equal?} if @var{=} is not given. | |
ea6ea01b KR |
711 | |
712 | This function extends the core @code{member} by accepting an equality | |
713 | predicate. (@pxref{List Searching}) | |
a0e07ba4 NJ |
714 | @end deffn |
715 | ||
716 | ||
717 | @node SRFI-1 Deleting | |
718 | @subsection Deleting | |
719 | ||
720 | @c FIXME::martin: Review me! | |
721 | ||
8f85c0c6 NJ |
722 | @deffn {Scheme Procedure} delete x lst [=] |
723 | @deffnx {Scheme Procedure} delete! x lst [=] | |
b6b9376a KR |
724 | Return a list containing the elements of @var{lst} but with those |
725 | equal to @var{x} deleted. The returned elements will be in the same | |
726 | order as they were in @var{lst}. | |
727 | ||
728 | Equality is determined by the @var{=} predicate, or @code{equal?} if | |
729 | not given. An equality call is made just once for each element, but | |
730 | the order in which the calls are made on the elements is unspecified. | |
a0e07ba4 | 731 | |
243bdb63 | 732 | The equality calls are always @code{(= x elem)}, ie.@: the given @var{x} |
b6b9376a KR |
733 | is first. This means for instance elements greater than 5 can be |
734 | deleted with @code{(delete 5 lst <)}. | |
735 | ||
736 | @code{delete} does not modify @var{lst}, but the return might share a | |
737 | common tail with @var{lst}. @code{delete!} may modify the structure | |
738 | of @var{lst} to construct its return. | |
ea6ea01b KR |
739 | |
740 | These functions extend the core @code{delete} and @code{delete!} in | |
741 | accepting an equality predicate. (@pxref{List Modification}) | |
a0e07ba4 NJ |
742 | @end deffn |
743 | ||
8f85c0c6 NJ |
744 | @deffn {Scheme Procedure} delete-duplicates lst [=] |
745 | @deffnx {Scheme Procedure} delete-duplicates! lst [=] | |
b6b9376a KR |
746 | Return a list containing the elements of @var{lst} but without |
747 | duplicates. | |
748 | ||
749 | When elements are equal, only the first in @var{lst} is retained. | |
750 | Equal elements can be anywhere in @var{lst}, they don't have to be | |
751 | adjacent. The returned list will have the retained elements in the | |
752 | same order as they were in @var{lst}. | |
753 | ||
754 | Equality is determined by the @var{=} predicate, or @code{equal?} if | |
755 | not given. Calls @code{(= x y)} are made with element @var{x} being | |
756 | before @var{y} in @var{lst}. A call is made at most once for each | |
757 | combination, but the sequence of the calls across the elements is | |
758 | unspecified. | |
759 | ||
760 | @code{delete-duplicates} does not modify @var{lst}, but the return | |
761 | might share a common tail with @var{lst}. @code{delete-duplicates!} | |
762 | may modify the structure of @var{lst} to construct its return. | |
763 | ||
764 | In the worst case, this is an @math{O(N^2)} algorithm because it must | |
765 | check each element against all those preceding it. For long lists it | |
766 | is more efficient to sort and then compare only adjacent elements. | |
a0e07ba4 NJ |
767 | @end deffn |
768 | ||
769 | ||
770 | @node SRFI-1 Association Lists | |
771 | @subsection Association Lists | |
772 | ||
773 | @c FIXME::martin: Review me! | |
774 | ||
775 | Association lists are described in detail in section @ref{Association | |
776 | Lists}. The present section only documents the additional procedures | |
777 | for dealing with association lists defined by SRFI-1. | |
778 | ||
8f85c0c6 | 779 | @deffn {Scheme Procedure} assoc key alist [=] |
a0e07ba4 NJ |
780 | Return the pair from @var{alist} which matches @var{key}. Equality is |
781 | determined by @var{=}, which defaults to @code{equal?} if not given. | |
782 | @var{alist} must be an association lists---a list of pairs. | |
ea6ea01b KR |
783 | |
784 | This function extends the core @code{assoc} by accepting an equality | |
785 | predicate. (@pxref{Association Lists}) | |
a0e07ba4 NJ |
786 | @end deffn |
787 | ||
8f85c0c6 | 788 | @deffn {Scheme Procedure} alist-cons key datum alist |
a0e07ba4 NJ |
789 | Equivalent to |
790 | ||
791 | @lisp | |
792 | (cons (cons @var{key} @var{datum}) @var{alist}) | |
793 | @end lisp | |
794 | ||
795 | This procedure is used to coons a new pair onto an existing | |
796 | association list. | |
797 | @end deffn | |
798 | ||
8f85c0c6 | 799 | @deffn {Scheme Procedure} alist-copy alist |
a0e07ba4 NJ |
800 | Return a newly allocated copy of @var{alist}, that means that the |
801 | spine of the list as well as the pairs are copied. | |
802 | @end deffn | |
803 | ||
8f85c0c6 NJ |
804 | @deffn {Scheme Procedure} alist-delete key alist [=] |
805 | @deffnx {Scheme Procedure} alist-delete! key alist [=] | |
bd35f1f0 KR |
806 | Return a list containing the elements of @var{alist} but with those |
807 | elements whose keys are equal to @var{key} deleted. The returned | |
808 | elements will be in the same order as they were in @var{alist}. | |
a0e07ba4 | 809 | |
bd35f1f0 KR |
810 | Equality is determined by the @var{=} predicate, or @code{equal?} if |
811 | not given. The order in which elements are tested is unspecified, but | |
812 | each equality call is made @code{(= key alistkey)}, ie. the given | |
813 | @var{key} parameter is first and the key from @var{alist} second. | |
814 | This means for instance all associations with a key greater than 5 can | |
815 | be removed with @code{(alist-delete 5 alist <)}. | |
816 | ||
817 | @code{alist-delete} does not modify @var{alist}, but the return might | |
818 | share a common tail with @var{alist}. @code{alist-delete!} may modify | |
819 | the list structure of @var{alist} to construct its return. | |
a0e07ba4 NJ |
820 | @end deffn |
821 | ||
822 | ||
823 | @node SRFI-1 Set Operations | |
824 | @subsection Set Operations on Lists | |
825 | ||
826 | @c FIXME::martin: Review me! | |
827 | ||
828 | Lists can be used for representing sets of objects. The procedures | |
829 | documented in this section can be used for such set representations. | |
85a9b4ed | 830 | Man combining several sets or adding elements, they make sure that no |
a0e07ba4 NJ |
831 | object is contained more than once in a given list. Please note that |
832 | lists are not a too efficient implementation method for sets, so if | |
833 | you need high performance, you should think about implementing a | |
834 | custom data structure for representing sets, such as trees, bitsets, | |
835 | hash tables or something similar. | |
836 | ||
837 | All these procedures accept an equality predicate as the first | |
838 | argument. This predicate is used for testing the objects in the list | |
839 | sets for sameness. | |
840 | ||
8f85c0c6 | 841 | @deffn {Scheme Procedure} lset<= = list1 @dots{} |
a0e07ba4 NJ |
842 | Return @code{#t} if every @var{listi} is a subset of @var{listi+1}, |
843 | otherwise return @code{#f}. Returns @code{#t} if called with less | |
844 | than two arguments. @var{=} is used for testing element equality. | |
845 | @end deffn | |
846 | ||
8f85c0c6 | 847 | @deffn {Scheme Procedure} lset= = list1 list2 @dots{} |
a0e07ba4 NJ |
848 | Return @code{#t} if all argument lists are equal. @var{=} is used for |
849 | testing element equality. | |
850 | @end deffn | |
851 | ||
8f85c0c6 NJ |
852 | @deffn {Scheme Procedure} lset-adjoin = list elt1 @dots{} |
853 | @deffnx {Scheme Procedure} lset-adjoin! = list elt1 @dots{} | |
a0e07ba4 NJ |
854 | Add all @var{elts} to the list @var{list}, suppressing duplicates and |
855 | return the resulting list. @code{lset-adjoin!} is allowed, but not | |
856 | required to modify its first argument. @var{=} is used for testing | |
857 | element equality. | |
858 | @end deffn | |
859 | ||
8f85c0c6 NJ |
860 | @deffn {Scheme Procedure} lset-union = list1 @dots{} |
861 | @deffnx {Scheme Procedure} lset-union! = list1 @dots{} | |
a0e07ba4 NJ |
862 | Return the union of all argument list sets. The union is the set of |
863 | all elements which appear in any of the argument sets. | |
864 | @code{lset-union!} is allowed, but not required to modify its first | |
865 | argument. @var{=} is used for testing element equality. | |
866 | @end deffn | |
867 | ||
8f85c0c6 NJ |
868 | @deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{} |
869 | @deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{} | |
a0e07ba4 NJ |
870 | Return the intersection of all argument list sets. The intersection |
871 | is the set containing all elements which appear in all argument sets. | |
872 | @code{lset-intersection!} is allowed, but not required to modify its | |
873 | first argument. @var{=} is used for testing element equality. | |
874 | @end deffn | |
875 | ||
8f85c0c6 NJ |
876 | @deffn {Scheme Procedure} lset-difference = list1 list2 @dots{} |
877 | @deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{} | |
a0e07ba4 NJ |
878 | Return the difference of all argument list sets. The difference is |
879 | the the set containing all elements of the first list which do not | |
880 | appear in the other lists. @code{lset-difference!} is allowed, but | |
881 | not required to modify its first argument. @var{=} is used for testing | |
882 | element equality. | |
883 | @end deffn | |
884 | ||
8f85c0c6 NJ |
885 | @deffn {Scheme Procedure} lset-xor = list1 @dots{} |
886 | @deffnx {Scheme Procedure} lset-xor! = list1 @dots{} | |
a0e07ba4 NJ |
887 | Return the set containing all elements which appear in the first |
888 | argument list set, but not in the second; or, more generally: which | |
889 | appear in an odd number of sets. @code{lset-xor!} is allowed, but | |
890 | not required to modify its first argument. @var{=} is used for testing | |
891 | element equality. | |
892 | @end deffn | |
893 | ||
8f85c0c6 NJ |
894 | @deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{} |
895 | @deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{} | |
a0e07ba4 NJ |
896 | Return two values, the difference and the intersection of the argument |
897 | list sets. This works like a combination of @code{lset-difference} and | |
898 | @code{lset-intersection}, but is more efficient. | |
899 | @code{lset-diff+intersection!} is allowed, but not required to modify | |
900 | its first argument. @var{=} is used for testing element equality. You | |
901 | have to use some means to deal with the multiple values these | |
902 | procedures return (@pxref{Multiple Values}). | |
903 | @end deffn | |
904 | ||
905 | ||
906 | @node SRFI-2 | |
907 | @section SRFI-2 - and-let* | |
8742c48b | 908 | @cindex SRFI-2 |
a0e07ba4 | 909 | |
4fd0db14 KR |
910 | @noindent |
911 | The following syntax can be obtained with | |
a0e07ba4 | 912 | |
4fd0db14 KR |
913 | @lisp |
914 | (use-modules (srfi srfi-2)) | |
915 | @end lisp | |
a0e07ba4 | 916 | |
4fd0db14 KR |
917 | @deffn {library syntax} and-let* (clause @dots{}) body @dots{} |
918 | A combination of @code{and} and @code{let*}. | |
919 | ||
920 | Each @var{clause} is evaluated in turn, and if @code{#f} is obtained | |
921 | then evaluation stops and @code{#f} is returned. If all are | |
922 | non-@code{#f} then @var{body} is evaluated and the last form gives the | |
923 | return value. Each @var{clause} should be one of the following, | |
924 | ||
925 | @table @code | |
926 | @item (symbol expr) | |
927 | Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}. | |
928 | Like @code{let*}, that binding is available to subsequent clauses. | |
929 | @item (expr) | |
930 | Evaluate @var{expr} and check for @code{#f}. | |
931 | @item symbol | |
932 | Get the value bound to @var{symbol} and check for @code{#f}. | |
933 | @end table | |
a0e07ba4 | 934 | |
4fd0db14 KR |
935 | Notice that @code{(expr)} has an ``extra'' pair of parentheses, for |
936 | instance @code{((eq? x y))}. One way to remember this is to imagine | |
937 | the @code{symbol} in @code{(symbol expr)} is omitted. | |
a0e07ba4 | 938 | |
4fd0db14 KR |
939 | @code{and-let*} is good for calculations where a @code{#f} value means |
940 | termination, but where a non-@code{#f} value is going to be needed in | |
941 | subsequent expressions. | |
942 | ||
943 | The following illustrates this, it returns text between brackets | |
944 | @samp{[...]} in a string, or @code{#f} if there are no such brackets | |
945 | (ie.@: either @code{string-index} gives @code{#f}). | |
946 | ||
947 | @example | |
948 | (define (extract-brackets str) | |
949 | (and-let* ((start (string-index str #\[)) | |
950 | (end (string-index str #\] start))) | |
951 | (substring str (1+ start) end))) | |
952 | @end example | |
953 | ||
954 | The following shows plain variables and expressions tested too. | |
955 | @code{diagnostic-levels} is taken to be an alist associating a | |
956 | diagnostic type with a level. @code{str} is printed only if the type | |
957 | is known and its level is high enough. | |
958 | ||
959 | @example | |
960 | (define (show-diagnostic type str) | |
961 | (and-let* (want-diagnostics | |
962 | (level (assq-ref diagnostic-levels type)) | |
963 | ((>= level current-diagnostic-level))) | |
964 | (display str))) | |
965 | @end example | |
966 | ||
967 | The advantage of @code{and-let*} is that an extended sequence of | |
968 | expressions and tests doesn't require lots of nesting as would arise | |
969 | from separate @code{and} and @code{let*}, or from @code{cond} with | |
970 | @code{=>}. | |
971 | ||
972 | @end deffn | |
a0e07ba4 NJ |
973 | |
974 | ||
975 | @node SRFI-4 | |
f85f9591 | 976 | @section SRFI-4 - Homogeneous numeric vector datatypes |
8742c48b | 977 | @cindex SRFI-4 |
a0e07ba4 NJ |
978 | |
979 | @c FIXME::martin: Review me! | |
980 | ||
f85f9591 KR |
981 | SRFI-4 defines a set of datatypes and functions for vectors whose |
982 | elements are numbers, all of the same numeric type. Vectors for | |
983 | signed and unsigned exact integers and inexact reals in several | |
984 | precisions are available. Being homogeneous means they require less | |
985 | memory than normal vectors. | |
a0e07ba4 | 986 | |
f85f9591 KR |
987 | The functions and the read syntax in this section are made available |
988 | with | |
a0e07ba4 | 989 | |
f85f9591 KR |
990 | @lisp |
991 | (use-modules (srfi srfi-4)) | |
992 | @end lisp | |
a0e07ba4 | 993 | |
f85f9591 KR |
994 | Procedures similar to the vector procedures (@pxref{Vectors}) are |
995 | provided for handling these homogeneous vectors, but they are distinct | |
996 | datatypes and the two cannot be inter-mixed. | |
a0e07ba4 NJ |
997 | |
998 | Ten vector data types are provided: Unsigned and signed integer values | |
999 | with 8, 16, 32 and 64 bits and floating point values with 32 and 64 | |
f85f9591 KR |
1000 | bits. The type is indicated by a tag in the function names, |
1001 | @code{u8}, @code{s8}, @code{u16}, @code{s16}, @code{u32}, @code{s32}, | |
1002 | @code{u64}, @code{s64}, @code{f32}, @code{f64}. | |
a0e07ba4 | 1003 | |
f85f9591 KR |
1004 | The external representation (ie.@: read syntax) for these vectors is |
1005 | similar to normal Scheme vectors, but with an additional tag | |
1006 | indiciating the vector's type. For example, | |
a0e07ba4 NJ |
1007 | |
1008 | @lisp | |
1009 | #u16(1 2 3) | |
a0e07ba4 NJ |
1010 | #f64(3.1415 2.71) |
1011 | @end lisp | |
1012 | ||
f85f9591 KR |
1013 | Note that the read syntax for floating-point here conflicts with |
1014 | @code{#f} for false. In Standard Scheme one can write @code{(1 | |
1015 | #f3)} for a three element list @code{(1 #f 3)}, but with the SRFI-4 | |
1016 | module @code{(1 #f3)} is invalid. @code{(1 #f 3)} is almost certainly | |
1017 | what one should write anyway to make the intention clear, so this is | |
1018 | rarely a problem. | |
1019 | ||
1020 | @deffn {Scheme Procedure} u8vector? obj | |
1021 | @deffnx {Scheme Procedure} s8vector? obj | |
1022 | @deffnx {Scheme Procedure} u16vector? obj | |
1023 | @deffnx {Scheme Procedure} s16vector? obj | |
1024 | @deffnx {Scheme Procedure} u32vector? obj | |
1025 | @deffnx {Scheme Procedure} s32vector? obj | |
1026 | @deffnx {Scheme Procedure} u64vector? obj | |
1027 | @deffnx {Scheme Procedure} s64vector? obj | |
1028 | @deffnx {Scheme Procedure} f32vector? obj | |
1029 | @deffnx {Scheme Procedure} f64vector? obj | |
1030 | Return @code{#t} if @var{obj} is a homogeneous numeric vector of the | |
1031 | indicated type. | |
1032 | @end deffn | |
1033 | ||
1034 | @deffn {Scheme Procedure} make-u8vector n [value] | |
1035 | @deffnx {Scheme Procedure} make-s8vector n [value] | |
1036 | @deffnx {Scheme Procedure} make-u16vector n [value] | |
1037 | @deffnx {Scheme Procedure} make-s16vector n [value] | |
1038 | @deffnx {Scheme Procedure} make-u32vector n [value] | |
1039 | @deffnx {Scheme Procedure} make-s32vector n [value] | |
1040 | @deffnx {Scheme Procedure} make-u64vector n [value] | |
1041 | @deffnx {Scheme Procedure} make-s64vector n [value] | |
1042 | @deffnx {Scheme Procedure} make-f32vector n [value] | |
1043 | @deffnx {Scheme Procedure} make-f64vector n [value] | |
1044 | Return a newly allocated homogeneous numeric vector holding @var{n} | |
1045 | elements of the indicated type. If @var{value} is given, the vector | |
1046 | is initialized with that value, otherwise the contents are | |
1047 | unspecified. | |
a0e07ba4 NJ |
1048 | @end deffn |
1049 | ||
f85f9591 KR |
1050 | @deffn {Scheme Procedure} u8vector value @dots{} |
1051 | @deffnx {Scheme Procedure} s8vector value @dots{} | |
1052 | @deffnx {Scheme Procedure} u16vector value @dots{} | |
1053 | @deffnx {Scheme Procedure} s16vector value @dots{} | |
1054 | @deffnx {Scheme Procedure} u32vector value @dots{} | |
1055 | @deffnx {Scheme Procedure} s32vector value @dots{} | |
1056 | @deffnx {Scheme Procedure} u64vector value @dots{} | |
1057 | @deffnx {Scheme Procedure} s64vector value @dots{} | |
1058 | @deffnx {Scheme Procedure} f32vector value @dots{} | |
1059 | @deffnx {Scheme Procedure} f64vector value @dots{} | |
1060 | Return a newly allocated homogeneous numeric vector of the indicated | |
1061 | type, holding the given parameter @var{value}s. The vector length is | |
1062 | the number of parameters given. | |
1063 | @end deffn | |
1064 | ||
1065 | @deffn {Scheme Procedure} u8vector-length vec | |
1066 | @deffnx {Scheme Procedure} s8vector-length vec | |
1067 | @deffnx {Scheme Procedure} u16vector-length vec | |
1068 | @deffnx {Scheme Procedure} s16vector-length vec | |
1069 | @deffnx {Scheme Procedure} u32vector-length vec | |
1070 | @deffnx {Scheme Procedure} s32vector-length vec | |
1071 | @deffnx {Scheme Procedure} u64vector-length vec | |
1072 | @deffnx {Scheme Procedure} s64vector-length vec | |
1073 | @deffnx {Scheme Procedure} f32vector-length vec | |
1074 | @deffnx {Scheme Procedure} f64vector-length vec | |
1075 | Return the number of elements in @var{vec}. | |
1076 | @end deffn | |
1077 | ||
1078 | @deffn {Scheme Procedure} u8vector-ref vec i | |
1079 | @deffnx {Scheme Procedure} s8vector-ref vec i | |
1080 | @deffnx {Scheme Procedure} u16vector-ref vec i | |
1081 | @deffnx {Scheme Procedure} s16vector-ref vec i | |
1082 | @deffnx {Scheme Procedure} u32vector-ref vec i | |
1083 | @deffnx {Scheme Procedure} s32vector-ref vec i | |
1084 | @deffnx {Scheme Procedure} u64vector-ref vec i | |
1085 | @deffnx {Scheme Procedure} s64vector-ref vec i | |
1086 | @deffnx {Scheme Procedure} f32vector-ref vec i | |
1087 | @deffnx {Scheme Procedure} f64vector-ref vec i | |
1088 | Return the element at index @var{i} in @var{vec}. The first element | |
1089 | in @var{vec} is index 0. | |
1090 | @end deffn | |
1091 | ||
1092 | @deffn {Scheme Procedure} u8vector-ref vec i value | |
1093 | @deffnx {Scheme Procedure} s8vector-ref vec i value | |
1094 | @deffnx {Scheme Procedure} u16vector-ref vec i value | |
1095 | @deffnx {Scheme Procedure} s16vector-ref vec i value | |
1096 | @deffnx {Scheme Procedure} u32vector-ref vec i value | |
1097 | @deffnx {Scheme Procedure} s32vector-ref vec i value | |
1098 | @deffnx {Scheme Procedure} u64vector-ref vec i value | |
1099 | @deffnx {Scheme Procedure} s64vector-ref vec i value | |
1100 | @deffnx {Scheme Procedure} f32vector-ref vec i value | |
1101 | @deffnx {Scheme Procedure} f64vector-ref vec i value | |
1102 | Set the element at index @var{i} in @var{vec} to @var{value}. The | |
1103 | first element in @var{vec} is index 0. The return value is | |
1104 | unspecified. | |
a0e07ba4 NJ |
1105 | @end deffn |
1106 | ||
f85f9591 KR |
1107 | @deffn {Scheme Procedure} u8vector->list vec |
1108 | @deffnx {Scheme Procedure} s8vector->list vec | |
1109 | @deffnx {Scheme Procedure} u16vector->list vec | |
1110 | @deffnx {Scheme Procedure} s16vector->list vec | |
1111 | @deffnx {Scheme Procedure} u32vector->list vec | |
1112 | @deffnx {Scheme Procedure} s32vector->list vec | |
1113 | @deffnx {Scheme Procedure} u64vector->list vec | |
1114 | @deffnx {Scheme Procedure} s64vector->list vec | |
1115 | @deffnx {Scheme Procedure} f32vector->list vec | |
1116 | @deffnx {Scheme Procedure} f64vector->list vec | |
1117 | Return a newly allocated list holding all elements of @var{vec}. | |
1118 | @end deffn | |
1119 | ||
1120 | @deffn {Scheme Procedure} list->u8vector lst | |
1121 | @deffnx {Scheme Procedure} list->s8vector lst | |
1122 | @deffnx {Scheme Procedure} list->u16vector lst | |
1123 | @deffnx {Scheme Procedure} list->s16vector lst | |
1124 | @deffnx {Scheme Procedure} list->u32vector lst | |
1125 | @deffnx {Scheme Procedure} list->s32vector lst | |
1126 | @deffnx {Scheme Procedure} list->u64vector lst | |
1127 | @deffnx {Scheme Procedure} list->s64vector lst | |
1128 | @deffnx {Scheme Procedure} list->f32vector lst | |
1129 | @deffnx {Scheme Procedure} list->f64vector lst | |
1130 | Return a newly allocated homogeneous numeric vector of the indicated type, | |
a0e07ba4 NJ |
1131 | initialized with the elements of the list @var{lst}. |
1132 | @end deffn | |
1133 | ||
1134 | ||
1135 | @node SRFI-6 | |
1136 | @section SRFI-6 - Basic String Ports | |
8742c48b | 1137 | @cindex SRFI-6 |
a0e07ba4 NJ |
1138 | |
1139 | SRFI-6 defines the procedures @code{open-input-string}, | |
1140 | @code{open-output-string} and @code{get-output-string}. These | |
1141 | procedures are included in the Guile core, so using this module does not | |
1142 | make any difference at the moment. But it is possible that support for | |
1143 | SRFI-6 will be factored out of the core library in the future, so using | |
1144 | this module does not hurt, after all. | |
1145 | ||
1146 | @node SRFI-8 | |
1147 | @section SRFI-8 - receive | |
8742c48b | 1148 | @cindex SRFI-8 |
a0e07ba4 NJ |
1149 | |
1150 | @code{receive} is a syntax for making the handling of multiple-value | |
1151 | procedures easier. It is documented in @xref{Multiple Values}. | |
1152 | ||
1153 | ||
1154 | @node SRFI-9 | |
1155 | @section SRFI-9 - define-record-type | |
8742c48b | 1156 | @cindex SRFI-9 |
a0e07ba4 | 1157 | |
6afe385d KR |
1158 | This SRFI is a syntax for defining new record types and creating |
1159 | predicate, constructor, and field getter and setter functions. In | |
1160 | Guile this is simply an alternate interface to the core record | |
1161 | functionality (@pxref{Records}). It can be used with, | |
a0e07ba4 | 1162 | |
6afe385d KR |
1163 | @example |
1164 | (use-modules (srfi srfi-9)) | |
1165 | @end example | |
1166 | ||
1167 | @deffn {library syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{} | |
1168 | @sp 1 | |
1169 | Create a new record type, and make various @code{define}s for using | |
1170 | it. This syntax can only occur at the top-level, not nested within | |
1171 | some other form. | |
1172 | ||
1173 | @var{type} is bound to the record type, which is as per the return | |
1174 | from the core @code{make-record-type}. @var{type} also provides the | |
1175 | name for the record, as per @code{record-type-name}. | |
1176 | ||
1177 | @var{constructor} is bound to a function to be called as | |
1178 | @code{(@var{constructor} fieldval @dots{})} to create a new record of | |
1179 | this type. The arguments are initial values for the fields, one | |
1180 | argument for each field, in the order they appear in the | |
1181 | @code{define-record-type} form. | |
1182 | ||
1183 | The @var{fieldname}s provide the names for the record fields, as per | |
1184 | the core @code{record-type-fields} etc, and are referred to in the | |
1185 | subsequent accessor/modifier forms. | |
1186 | ||
1187 | @var{predictate} is bound to a function to be called as | |
1188 | @code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f} | |
1189 | according to whether @var{obj} is a record of this type. | |
1190 | ||
1191 | Each @var{accessor} is bound to a function to be called | |
1192 | @code{(@var{accessor} record)} to retrieve the respective field from a | |
1193 | @var{record}. Similarly each @var{modifier} is bound to a function to | |
1194 | be called @code{(@var{modifier} record val)} to set the respective | |
1195 | field in a @var{record}. | |
1196 | @end deffn | |
1197 | ||
1198 | @noindent | |
1199 | An example will illustrate typical usage, | |
a0e07ba4 NJ |
1200 | |
1201 | @example | |
6afe385d KR |
1202 | (define-record-type employee-type |
1203 | (make-employee name age salary) | |
1204 | employee? | |
1205 | (name get-employee-name) | |
1206 | (age get-employee-age set-employee-age) | |
1207 | (salary get-employee-salary set-employee-salary)) | |
a0e07ba4 NJ |
1208 | @end example |
1209 | ||
6afe385d KR |
1210 | This creates a new employee data type, with name, age and salary |
1211 | fields. Accessor functions are created for each field, but no | |
1212 | modifier function for the name (the intention in this example being | |
1213 | that it's established only when an employee object is created). These | |
1214 | can all then be used as for example, | |
a0e07ba4 NJ |
1215 | |
1216 | @example | |
6afe385d KR |
1217 | employee-type @result{} #<record-type employee-type> |
1218 | ||
1219 | (define fred (make-employee "Fred" 45 20000.00)) | |
1220 | ||
1221 | (employee? fred) @result{} #t | |
1222 | (get-employee-age fred) @result{} 45 | |
1223 | (set-employee-salary fred 25000.00) ;; pay rise | |
a0e07ba4 NJ |
1224 | @end example |
1225 | ||
6afe385d KR |
1226 | The functions created by @code{define-record-type} are ordinary |
1227 | top-level @code{define}s. They can be redefined or @code{set!} as | |
1228 | desired, exported from a module, etc. | |
1229 | ||
a0e07ba4 NJ |
1230 | |
1231 | @node SRFI-10 | |
1232 | @section SRFI-10 - Hash-Comma Reader Extension | |
8742c48b | 1233 | @cindex SRFI-10 |
a0e07ba4 NJ |
1234 | |
1235 | @cindex hash-comma | |
1236 | @cindex #,() | |
1237 | The module @code{(srfi srfi-10)} implements the syntax extension | |
1238 | @code{#,()}, also called hash-comma, which is defined in SRFI-10. | |
1239 | ||
1240 | The support for SRFI-10 consists of the procedure | |
1241 | @code{define-reader-ctor} for defining new reader constructors and the | |
1242 | read syntax form | |
1243 | ||
1244 | @example | |
1245 | #,(@var{ctor} @var{datum} ...) | |
1246 | @end example | |
1247 | ||
1248 | where @var{ctor} must be a symbol for which a read constructor was | |
85a9b4ed | 1249 | defined previously, using @code{define-reader-ctor}. |
a0e07ba4 NJ |
1250 | |
1251 | Example: | |
1252 | ||
1253 | @lisp | |
4310df36 | 1254 | (use-modules (ice-9 rdelim)) ; for read-line |
a0e07ba4 NJ |
1255 | (define-reader-ctor 'file open-input-file) |
1256 | (define f '#,(file "/etc/passwd")) | |
1257 | (read-line f) | |
1258 | @result{} | |
1259 | "root:x:0:0:root:/root:/bin/bash" | |
1260 | @end lisp | |
1261 | ||
1262 | Please note the quote before the @code{#,(file ...)} expression. This | |
1263 | is necessary because ports are not self-evaluating in Guile. | |
1264 | ||
8f85c0c6 | 1265 | @deffn {Scheme Procedure} define-reader-ctor symbol proc |
a0e07ba4 NJ |
1266 | Define @var{proc} as the reader constructor for hash-comma forms with a |
1267 | tag @var{symbol}. @var{proc} will be applied to the datum(s) following | |
1268 | the tag in the hash-comma expression after the complete form has been | |
1269 | read in. The result of @var{proc} is returned by the Scheme reader. | |
1270 | @end deffn | |
1271 | ||
1272 | ||
1273 | @node SRFI-11 | |
1274 | @section SRFI-11 - let-values | |
8742c48b | 1275 | @cindex SRFI-11 |
a0e07ba4 | 1276 | |
8742c48b KR |
1277 | @findex let-values |
1278 | @findex let-values* | |
a0e07ba4 NJ |
1279 | This module implements the binding forms for multiple values |
1280 | @code{let-values} and @code{let-values*}. These forms are similar to | |
1281 | @code{let} and @code{let*} (@pxref{Local Bindings}), but they support | |
1282 | binding of the values returned by multiple-valued expressions. | |
1283 | ||
1284 | Write @code{(use-modules (srfi srfi-11))} to make the bindings | |
1285 | available. | |
1286 | ||
1287 | @lisp | |
1288 | (let-values (((x y) (values 1 2)) | |
1289 | ((z f) (values 3 4))) | |
1290 | (+ x y z f)) | |
1291 | @result{} | |
1292 | 10 | |
1293 | @end lisp | |
1294 | ||
1295 | @code{let-values} performs all bindings simultaneously, which means that | |
1296 | no expression in the binding clauses may refer to variables bound in the | |
1297 | same clause list. @code{let-values*}, on the other hand, performs the | |
1298 | bindings sequentially, just like @code{let*} does for single-valued | |
1299 | expressions. | |
1300 | ||
1301 | ||
1302 | @node SRFI-13 | |
1303 | @section SRFI-13 - String Library | |
8742c48b | 1304 | @cindex SRFI-13 |
a0e07ba4 NJ |
1305 | |
1306 | In this section, we will describe all procedures defined in SRFI-13 | |
1307 | (string library) and implemented by the module @code{(srfi srfi-13)}. | |
1308 | ||
1309 | Note that only the procedures from SRFI-13 are documented here which are | |
1310 | not already contained in Guile. For procedures not documented here | |
1311 | please refer to the relevant chapters in the Guile Reference Manual, for | |
1312 | example the documentation of strings and string procedures | |
1313 | (@pxref{Strings}). | |
1314 | ||
40f316d0 MG |
1315 | All of the procedures defined in SRFI-13, which are not already |
1316 | included in the Guile core library, are implemented in the module | |
1317 | @code{(srfi srfi-13)}. The procedures which are both in Guile and in | |
1318 | SRFI-13 are slightly extended in this module. Their bindings | |
1319 | overwrite those in the Guile core. | |
a0e07ba4 NJ |
1320 | |
1321 | The procedures which are defined in the section @emph{Low-level | |
1322 | procedures} of SRFI-13 for parsing optional string indices, substring | |
1323 | specification checking and Knuth-Morris-Pratt-Searching are not | |
1324 | implemented. | |
1325 | ||
1326 | The procedures @code{string-contains} and @code{string-contains-ci} are | |
1327 | not implemented very efficiently at the moment. This will be changed as | |
1328 | soon as possible. | |
1329 | ||
1330 | @menu | |
1331 | * Loading SRFI-13:: How to load SRFI-13 support. | |
1332 | * SRFI-13 Predicates:: String predicates. | |
1333 | * SRFI-13 Constructors:: String constructing procedures. | |
1334 | * SRFI-13 List/String Conversion:: Conversion from/to lists. | |
1335 | * SRFI-13 Selection:: Selection portions of strings. | |
85a9b4ed | 1336 | * SRFI-13 Modification:: Modify strings in-place. |
a0e07ba4 NJ |
1337 | * SRFI-13 Comparison:: Compare strings. |
1338 | * SRFI-13 Prefixes/Suffixes:: Detect common pre-/suffixes. | |
1339 | * SRFI-13 Searching:: Searching for substrings. | |
1340 | * SRFI-13 Case Mapping:: Mapping to lower-/upper-case. | |
1341 | * SRFI-13 Reverse/Append:: Reverse and append strings. | |
1342 | * SRFI-13 Fold/Unfold/Map:: Construct/deconstruct strings. | |
40f316d0 | 1343 | * SRFI-13 Replicate/Rotate:: Replicate and rotate portions of strings. |
a0e07ba4 NJ |
1344 | * SRFI-13 Miscellaneous:: Left-over string procedures. |
1345 | * SRFI-13 Filtering/Deleting:: Filter and delete characters from strings. | |
1346 | @end menu | |
1347 | ||
1348 | ||
1349 | @node Loading SRFI-13 | |
1350 | @subsection Loading SRFI-13 | |
1351 | ||
1352 | When Guile is properly installed, SRFI-13 support can be loaded into a | |
1353 | running Guile by using the @code{(srfi srfi-13)} module. | |
1354 | ||
1355 | @example | |
1356 | $ guile | |
1357 | guile> (use-modules (srfi srfi-13)) | |
1358 | guile> | |
1359 | @end example | |
1360 | ||
1361 | When this step causes any errors, Guile is not properly installed. | |
1362 | ||
1363 | One possible reason is that Guile cannot find either the Scheme module | |
1364 | file @file{srfi-13.scm}, or it cannot find the shared object file | |
1365 | @file{libguile-srfi-srfi-13-14.so}. Make sure that the former is in the | |
1366 | Guile load path and that the latter is either installed in some default | |
1367 | location like @file{/usr/local/lib} or that the directory it was | |
1368 | installed to is in your @code{LTDL_LIBRARY_PATH}. The same applies to | |
1369 | @file{srfi-14.scm}. | |
1370 | ||
1371 | Now you can test whether the SRFI-13 procedures are working by calling | |
1372 | the @code{string-concatenate} procedure. | |
1373 | ||
1374 | @example | |
1375 | guile> (string-concatenate '("Hello" " " "World!")) | |
1376 | "Hello World!" | |
1377 | @end example | |
1378 | ||
1379 | @node SRFI-13 Predicates | |
12991fed | 1380 | @subsection Predicates |
a0e07ba4 NJ |
1381 | |
1382 | In addition to the primitives @code{string?} and @code{string-null?}, | |
1383 | which are already in the Guile core, the string predicates | |
1384 | @code{string-any} and @code{string-every} are defined by SRFI-13. | |
1385 | ||
8f85c0c6 | 1386 | @deffn {Scheme Procedure} string-any pred s [start end] |
a0e07ba4 NJ |
1387 | Check if the predicate @var{pred} is true for any character in |
1388 | the string @var{s}, proceeding from left (index @var{start}) to | |
1389 | right (index @var{end}). If @code{string-any} returns true, | |
1390 | the returned true value is the one produced by the first | |
1391 | successful application of @var{pred}. | |
1392 | @end deffn | |
1393 | ||
8f85c0c6 | 1394 | @deffn {Scheme Procedure} string-every pred s [start end] |
a0e07ba4 NJ |
1395 | Check if the predicate @var{pred} is true for every character |
1396 | in the string @var{s}, proceeding from left (index @var{start}) | |
1397 | to right (index @var{end}). If @code{string-every} returns | |
1398 | true, the returned true value is the one produced by the final | |
1399 | application of @var{pred} to the last character of @var{s}. | |
1400 | @end deffn | |
1401 | ||
1402 | ||
1403 | @c =================================================================== | |
1404 | ||
1405 | @node SRFI-13 Constructors | |
1406 | @subsection Constructors | |
1407 | ||
1408 | SRFI-13 defines several procedures for constructing new strings. In | |
1409 | addition to @code{make-string} and @code{string} (available in the Guile | |
1410 | core library), the procedure @code{string-tabulate} does exist. | |
1411 | ||
8f85c0c6 | 1412 | @deffn {Scheme Procedure} string-tabulate proc len |
a0e07ba4 NJ |
1413 | @var{proc} is an integer->char procedure. Construct a string |
1414 | of size @var{len} by applying @var{proc} to each index to | |
1415 | produce the corresponding string element. The order in which | |
1416 | @var{proc} is applied to the indices is not specified. | |
1417 | @end deffn | |
1418 | ||
1419 | ||
1420 | @c =================================================================== | |
1421 | ||
1422 | @node SRFI-13 List/String Conversion | |
1423 | @subsection List/String Conversion | |
1424 | ||
1425 | The procedure @code{string->list} is extended by SRFI-13, that is why it | |
1426 | is included in @code{(srfi srfi-13)}. The other procedures are new. | |
1427 | The Guile core already contains the procedure @code{list->string} for | |
1428 | converting a list of characters into a string (@pxref{List/String | |
1429 | Conversion}). | |
1430 | ||
8f85c0c6 | 1431 | @deffn {Scheme Procedure} string->list str [start end] |
a0e07ba4 NJ |
1432 | Convert the string @var{str} into a list of characters. |
1433 | @end deffn | |
1434 | ||
8f85c0c6 | 1435 | @deffn {Scheme Procedure} reverse-list->string chrs |
a0e07ba4 NJ |
1436 | An efficient implementation of @code{(compose string->list |
1437 | reverse)}: | |
1438 | ||
1439 | @smalllisp | |
1440 | (reverse-list->string '(#\a #\B #\c)) @result{} "cBa" | |
1441 | @end smalllisp | |
1442 | @end deffn | |
1443 | ||
8f85c0c6 | 1444 | @deffn {Scheme Procedure} string-join ls [delimiter grammar] |
a0e07ba4 NJ |
1445 | Append the string in the string list @var{ls}, using the string |
1446 | @var{delim} as a delimiter between the elements of @var{ls}. | |
1447 | @var{grammar} is a symbol which specifies how the delimiter is | |
1448 | placed between the strings, and defaults to the symbol | |
1449 | @code{infix}. | |
1450 | ||
1451 | @table @code | |
1452 | @item infix | |
1453 | Insert the separator between list elements. An empty string | |
1454 | will produce an empty list. | |
1455 | ||
1456 | @item string-infix | |
1457 | Like @code{infix}, but will raise an error if given the empty | |
1458 | list. | |
1459 | ||
1460 | @item suffix | |
1461 | Insert the separator after every list element. | |
1462 | ||
1463 | @item prefix | |
1464 | Insert the separator before each list element. | |
1465 | @end table | |
1466 | @end deffn | |
1467 | ||
1468 | ||
1469 | @c =================================================================== | |
1470 | ||
1471 | @node SRFI-13 Selection | |
1472 | @subsection Selection | |
1473 | ||
1474 | These procedures are called @dfn{selectors}, because they access | |
1475 | information about the string or select pieces of a given string. | |
1476 | ||
1477 | Additional selector procedures are documented in the Strings section | |
1478 | (@pxref{String Selection}), like @code{string-length} or | |
1479 | @code{string-ref}. | |
1480 | ||
1481 | @code{string-copy} is also available in core Guile, but this version | |
1482 | accepts additional start/end indices. | |
1483 | ||
8f85c0c6 | 1484 | @deffn {Scheme Procedure} string-copy str [start end] |
a0e07ba4 NJ |
1485 | Return a freshly allocated copy of the string @var{str}. If |
1486 | given, @var{start} and @var{end} delimit the portion of | |
1487 | @var{str} which is copied. | |
1488 | @end deffn | |
1489 | ||
8f85c0c6 | 1490 | @deffn {Scheme Procedure} substring/shared str start [end] |
a0e07ba4 NJ |
1491 | Like @code{substring}, but the result may share memory with the |
1492 | argument @var{str}. | |
1493 | @end deffn | |
1494 | ||
8f85c0c6 | 1495 | @deffn {Scheme Procedure} string-copy! target tstart s [start end] |
a0e07ba4 NJ |
1496 | Copy the sequence of characters from index range [@var{start}, |
1497 | @var{end}) in string @var{s} to string @var{target}, beginning | |
1498 | at index @var{tstart}. The characters are copied left-to-right | |
1499 | or right-to-left as needed - the copy is guaranteed to work, | |
1500 | even if @var{target} and @var{s} are the same string. It is an | |
1501 | error if the copy operation runs off the end of the target | |
1502 | string. | |
1503 | @end deffn | |
1504 | ||
8f85c0c6 NJ |
1505 | @deffn {Scheme Procedure} string-take s n |
1506 | @deffnx {Scheme Procedure} string-take-right s n | |
a0e07ba4 NJ |
1507 | Return the @var{n} first/last characters of @var{s}. |
1508 | @end deffn | |
1509 | ||
8f85c0c6 NJ |
1510 | @deffn {Scheme Procedure} string-drop s n |
1511 | @deffnx {Scheme Procedure} string-drop-right s n | |
a0e07ba4 NJ |
1512 | Return all but the first/last @var{n} characters of @var{s}. |
1513 | @end deffn | |
1514 | ||
8f85c0c6 NJ |
1515 | @deffn {Scheme Procedure} string-pad s len [chr start end] |
1516 | @deffnx {Scheme Procedure} string-pad-right s len [chr start end] | |
a0e07ba4 NJ |
1517 | Take that characters from @var{start} to @var{end} from the |
1518 | string @var{s} and return a new string, right(left)-padded by the | |
1519 | character @var{chr} to length @var{len}. If the resulting | |
1520 | string is longer than @var{len}, it is truncated on the right (left). | |
1521 | @end deffn | |
1522 | ||
8f85c0c6 NJ |
1523 | @deffn {Scheme Procedure} string-trim s [char_pred start end] |
1524 | @deffnx {Scheme Procedure} string-trim-right s [char_pred start end] | |
1525 | @deffnx {Scheme Procedure} string-trim-both s [char_pred start end] | |
a0e07ba4 NJ |
1526 | Trim @var{s} by skipping over all characters on the left/right/both |
1527 | sides of the string that satisfy the parameter @var{char_pred}: | |
1528 | ||
1529 | @itemize @bullet | |
1530 | @item | |
1531 | if it is the character @var{ch}, characters equal to | |
1532 | @var{ch} are trimmed, | |
1533 | ||
1534 | @item | |
1535 | if it is a procedure @var{pred} characters that | |
1536 | satisfy @var{pred} are trimmed, | |
1537 | ||
1538 | @item | |
1539 | if it is a character set, characters in that set are trimmed. | |
1540 | @end itemize | |
1541 | ||
1542 | If called without a @var{char_pred} argument, all whitespace is | |
1543 | trimmed. | |
1544 | @end deffn | |
1545 | ||
1546 | ||
1547 | @c =================================================================== | |
1548 | ||
1549 | @node SRFI-13 Modification | |
1550 | @subsection Modification | |
1551 | ||
1552 | The procedure @code{string-fill!} is extended from R5RS because it | |
1553 | accepts optional start/end indices. This bindings shadows the procedure | |
1554 | of the same name in the Guile core. The second modification procedure | |
1555 | @code{string-set!} is documented in the Strings section (@pxref{String | |
1556 | Modification}). | |
1557 | ||
8f85c0c6 | 1558 | @deffn {Scheme Procedure} string-fill! str chr [start end] |
a0e07ba4 NJ |
1559 | Stores @var{chr} in every element of the given @var{str} and |
1560 | returns an unspecified value. | |
1561 | @end deffn | |
1562 | ||
1563 | ||
1564 | @c =================================================================== | |
1565 | ||
1566 | @node SRFI-13 Comparison | |
1567 | @subsection Comparison | |
1568 | ||
1569 | The procedures in this section are used for comparing strings in | |
1570 | different ways. The comparison predicates differ from those in R5RS in | |
1571 | that they do not only return @code{#t} or @code{#f}, but the mismatch | |
1572 | index in the case of a true return value. | |
1573 | ||
1574 | @code{string-hash} and @code{string-hash-ci} are for calculating hash | |
1575 | values for strings, useful for implementing fast lookup mechanisms. | |
1576 | ||
8f85c0c6 NJ |
1577 | @deffn {Scheme Procedure} string-compare s1 s2 proc_lt proc_eq proc_gt [start1 end1 start2 end2] |
1578 | @deffnx {Scheme Procedure} string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 end1 start2 end2] | |
a0e07ba4 NJ |
1579 | Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the |
1580 | mismatch index, depending upon whether @var{s1} is less than, | |
1581 | equal to, or greater than @var{s2}. The mismatch index is the | |
1582 | largest index @var{i} such that for every 0 <= @var{j} < | |
1583 | @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] - that is, | |
1584 | @var{i} is the first position that does not match. The | |
1585 | character comparison is done case-insensitively. | |
1586 | @end deffn | |
1587 | ||
8f85c0c6 NJ |
1588 | @deffn {Scheme Procedure} string= s1 s2 [start1 end1 start2 end2] |
1589 | @deffnx {Scheme Procedure} string<> s1 s2 [start1 end1 start2 end2] | |
1590 | @deffnx {Scheme Procedure} string< s1 s2 [start1 end1 start2 end2] | |
1591 | @deffnx {Scheme Procedure} string> s1 s2 [start1 end1 start2 end2] | |
1592 | @deffnx {Scheme Procedure} string<= s1 s2 [start1 end1 start2 end2] | |
1593 | @deffnx {Scheme Procedure} string>= s1 s2 [start1 end1 start2 end2] | |
a0e07ba4 NJ |
1594 | Compare @var{s1} and @var{s2} and return @code{#f} if the predicate |
1595 | fails. Otherwise, the mismatch index is returned (or @var{end1} in the | |
1596 | case of @code{string=}. | |
1597 | @end deffn | |
1598 | ||
8f85c0c6 NJ |
1599 | @deffn {Scheme Procedure} string-ci= s1 s2 [start1 end1 start2 end2] |
1600 | @deffnx {Scheme Procedure} string-ci<> s1 s2 [start1 end1 start2 end2] | |
1601 | @deffnx {Scheme Procedure} string-ci< s1 s2 [start1 end1 start2 end2] | |
1602 | @deffnx {Scheme Procedure} string-ci> s1 s2 [start1 end1 start2 end2] | |
1603 | @deffnx {Scheme Procedure} string-ci<= s1 s2 [start1 end1 start2 end2] | |
1604 | @deffnx {Scheme Procedure} string-ci>= s1 s2 [start1 end1 start2 end2] | |
a0e07ba4 NJ |
1605 | Compare @var{s1} and @var{s2} and return @code{#f} if the predicate |
1606 | fails. Otherwise, the mismatch index is returned (or @var{end1} in the | |
1607 | case of @code{string=}. These are the case-insensitive variants. | |
1608 | @end deffn | |
1609 | ||
8f85c0c6 NJ |
1610 | @deffn {Scheme Procedure} string-hash s [bound start end] |
1611 | @deffnx {Scheme Procedure} string-hash-ci s [bound start end] | |
a0e07ba4 NJ |
1612 | Return a hash value of the string @var{s} in the range 0 @dots{} |
1613 | @var{bound} - 1. @code{string-hash-ci} is the case-insensitive variant. | |
1614 | @end deffn | |
1615 | ||
1616 | ||
1617 | @c =================================================================== | |
1618 | ||
1619 | @node SRFI-13 Prefixes/Suffixes | |
1620 | @subsection Prefixes/Suffixes | |
1621 | ||
1622 | Using these procedures you can determine whether a given string is a | |
1623 | prefix or suffix of another string or how long a common prefix/suffix | |
1624 | is. | |
1625 | ||
8f85c0c6 NJ |
1626 | @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 end1 start2 end2] |
1627 | @deffnx {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 end1 start2 end2] | |
1628 | @deffnx {Scheme Procedure} string-suffix-length s1 s2 [start1 end1 start2 end2] | |
1629 | @deffnx {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 end1 start2 end2] | |
a0e07ba4 NJ |
1630 | Return the length of the longest common prefix/suffix of the two |
1631 | strings. @code{string-prefix-length-ci} and | |
1632 | @code{string-suffix-length-ci} are the case-insensitive variants. | |
1633 | @end deffn | |
1634 | ||
8f85c0c6 NJ |
1635 | @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 end1 start2 end2] |
1636 | @deffnx {Scheme Procedure} string-prefix-ci? s1 s2 [start1 end1 start2 end2] | |
1637 | @deffnx {Scheme Procedure} string-suffix? s1 s2 [start1 end1 start2 end2] | |
1638 | @deffnx {Scheme Procedure} string-suffix-ci? s1 s2 [start1 end1 start2 end2] | |
a0e07ba4 NJ |
1639 | Is @var{s1} a prefix/suffix of @var{s2}. @code{string-prefix-ci?} and |
1640 | @code{string-suffix-ci?} are the case-insensitive variants. | |
1641 | @end deffn | |
1642 | ||
1643 | ||
1644 | @c =================================================================== | |
1645 | ||
1646 | @node SRFI-13 Searching | |
1647 | @subsection Searching | |
1648 | ||
1649 | Use these procedures to find out whether a string contains a given | |
1650 | character or a given substring, or a character from a set of characters. | |
1651 | ||
8f85c0c6 NJ |
1652 | @deffn {Scheme Procedure} string-index s char_pred [start end] |
1653 | @deffnx {Scheme Procedure} string-index-right s char_pred [start end] | |
a0e07ba4 | 1654 | Search through the string @var{s} from left to right (right to left), |
85a9b4ed | 1655 | returning the index of the first (last) occurrence of a character which |
a0e07ba4 NJ |
1656 | |
1657 | @itemize @bullet | |
1658 | @item | |
1659 | equals @var{char_pred}, if it is character, | |
1660 | ||
1661 | @item | |
85a9b4ed | 1662 | satisfies the predicate @var{char_pred}, if it is a |
a0e07ba4 NJ |
1663 | procedure, |
1664 | ||
1665 | @item | |
1666 | is in the set @var{char_pred}, if it is a character set. | |
1667 | @end itemize | |
1668 | @end deffn | |
1669 | ||
8f85c0c6 NJ |
1670 | @deffn {Scheme Procedure} string-skip s char_pred [start end] |
1671 | @deffnx {Scheme Procedure} string-skip-right s char_pred [start end] | |
a0e07ba4 | 1672 | Search through the string @var{s} from left to right (right to left), |
85a9b4ed | 1673 | returning the index of the first (last) occurrence of a character which |
a0e07ba4 NJ |
1674 | |
1675 | @itemize @bullet | |
1676 | @item | |
1677 | does not equal @var{char_pred}, if it is character, | |
1678 | ||
1679 | @item | |
85a9b4ed | 1680 | does not satisfy the predicate @var{char_pred}, if it is |
a0e07ba4 NJ |
1681 | a procedure. |
1682 | ||
1683 | @item | |
1684 | is not in the set if @var{char_pred} is a character set. | |
1685 | @end itemize | |
1686 | @end deffn | |
1687 | ||
8f85c0c6 | 1688 | @deffn {Scheme Procedure} string-count s char_pred [start end] |
a0e07ba4 NJ |
1689 | Return the count of the number of characters in the string |
1690 | @var{s} which | |
1691 | ||
1692 | @itemize @bullet | |
1693 | @item | |
1694 | equals @var{char_pred}, if it is character, | |
1695 | ||
1696 | @item | |
85a9b4ed | 1697 | satisfies the predicate @var{char_pred}, if it is a procedure. |
a0e07ba4 NJ |
1698 | |
1699 | @item | |
1700 | is in the set @var{char_pred}, if it is a character set. | |
1701 | @end itemize | |
1702 | @end deffn | |
1703 | ||
8f85c0c6 NJ |
1704 | @deffn {Scheme Procedure} string-contains s1 s2 [start1 end1 start2 end2] |
1705 | @deffnx {Scheme Procedure} string-contains-ci s1 s2 [start1 end1 start2 end2] | |
a0e07ba4 NJ |
1706 | Does string @var{s1} contain string @var{s2}? Return the index |
1707 | in @var{s1} where @var{s2} occurs as a substring, or false. | |
1708 | The optional start/end indices restrict the operation to the | |
1709 | indicated substrings. | |
1710 | ||
1711 | @code{string-contains-ci} is the case-insensitive variant. | |
1712 | @end deffn | |
1713 | ||
1714 | ||
1715 | @c =================================================================== | |
1716 | ||
1717 | @node SRFI-13 Case Mapping | |
1718 | @subsection Alphabetic Case Mapping | |
1719 | ||
1720 | These procedures convert the alphabetic case of strings. They are | |
1721 | similar to the procedures in the Guile core, but are extended to handle | |
1722 | optional start/end indices. | |
1723 | ||
8f85c0c6 NJ |
1724 | @deffn {Scheme Procedure} string-upcase s [start end] |
1725 | @deffnx {Scheme Procedure} string-upcase! s [start end] | |
a0e07ba4 NJ |
1726 | Upcase every character in @var{s}. @code{string-upcase!} is the |
1727 | side-effecting variant. | |
1728 | @end deffn | |
1729 | ||
8f85c0c6 NJ |
1730 | @deffn {Scheme Procedure} string-downcase s [start end] |
1731 | @deffnx {Scheme Procedure} string-downcase! s [start end] | |
a0e07ba4 NJ |
1732 | Downcase every character in @var{s}. @code{string-downcase!} is the |
1733 | side-effecting variant. | |
1734 | @end deffn | |
1735 | ||
8f85c0c6 NJ |
1736 | @deffn {Scheme Procedure} string-titlecase s [start end] |
1737 | @deffnx {Scheme Procedure} string-titlecase! s [start end] | |
a0e07ba4 NJ |
1738 | Upcase every first character in every word in @var{s}, downcase the |
1739 | other characters. @code{string-titlecase!} is the side-effecting | |
1740 | variant. | |
1741 | @end deffn | |
1742 | ||
1743 | ||
1744 | @c =================================================================== | |
1745 | ||
1746 | @node SRFI-13 Reverse/Append | |
1747 | @subsection Reverse/Append | |
1748 | ||
1749 | One appending procedure, @code{string-append} is the same in R5RS and in | |
1750 | SRFI-13, so it is not redefined. | |
1751 | ||
8f85c0c6 NJ |
1752 | @deffn {Scheme Procedure} string-reverse str [start end] |
1753 | @deffnx {Scheme Procedure} string-reverse! str [start end] | |
a0e07ba4 NJ |
1754 | Reverse the string @var{str}. The optional arguments |
1755 | @var{start} and @var{end} delimit the region of @var{str} to | |
1756 | operate on. | |
1757 | ||
1758 | @code{string-reverse!} modifies the argument string and returns an | |
1759 | unspecified value. | |
1760 | @end deffn | |
1761 | ||
8f85c0c6 | 1762 | @deffn {Scheme Procedure} string-append/shared ls @dots{} |
a0e07ba4 NJ |
1763 | Like @code{string-append}, but the result may share memory |
1764 | with the argument strings. | |
1765 | @end deffn | |
1766 | ||
8f85c0c6 | 1767 | @deffn {Scheme Procedure} string-concatenate ls |
a0e07ba4 NJ |
1768 | Append the elements of @var{ls} (which must be strings) |
1769 | together into a single string. Guaranteed to return a freshly | |
1770 | allocated string. | |
1771 | @end deffn | |
1772 | ||
8f85c0c6 | 1773 | @deffn {Scheme Procedure} string-concatenate/shared ls |
a0e07ba4 NJ |
1774 | Like @code{string-concatenate}, but the result may share memory |
1775 | with the strings in the list @var{ls}. | |
1776 | @end deffn | |
1777 | ||
8f85c0c6 | 1778 | @deffn {Scheme Procedure} string-concatenate-reverse ls final_string end |
a0e07ba4 NJ |
1779 | Without optional arguments, this procedure is equivalent to |
1780 | ||
1781 | @smalllisp | |
1782 | (string-concatenate (reverse ls)) | |
1783 | @end smalllisp | |
1784 | ||
1785 | If the optional argument @var{final_string} is specified, it is | |
1786 | consed onto the beginning to @var{ls} before performing the | |
1787 | list-reverse and string-concatenate operations. If @var{end} | |
1788 | is given, only the characters of @var{final_string} up to index | |
1789 | @var{end} are used. | |
1790 | ||
1791 | Guaranteed to return a freshly allocated string. | |
1792 | @end deffn | |
1793 | ||
8f85c0c6 | 1794 | @deffn {Scheme Procedure} string-concatenate-reverse/shared ls final_string end |
a0e07ba4 NJ |
1795 | Like @code{string-concatenate-reverse}, but the result may |
1796 | share memory with the the strings in the @var{ls} arguments. | |
1797 | @end deffn | |
1798 | ||
1799 | ||
1800 | @c =================================================================== | |
1801 | ||
1802 | @node SRFI-13 Fold/Unfold/Map | |
1803 | @subsection Fold/Unfold/Map | |
1804 | ||
1805 | @code{string-map}, @code{string-for-each} etc. are for iterating over | |
1806 | the characters a string is composed of. The fold and unfold procedures | |
1807 | are list iterators and constructors. | |
1808 | ||
8f85c0c6 | 1809 | @deffn {Scheme Procedure} string-map proc s [start end] |
a0e07ba4 NJ |
1810 | @var{proc} is a char->char procedure, it is mapped over |
1811 | @var{s}. The order in which the procedure is applied to the | |
1812 | string elements is not specified. | |
1813 | @end deffn | |
1814 | ||
8f85c0c6 | 1815 | @deffn {Scheme Procedure} string-map! proc s [start end] |
a0e07ba4 NJ |
1816 | @var{proc} is a char->char procedure, it is mapped over |
1817 | @var{s}. The order in which the procedure is applied to the | |
1818 | string elements is not specified. The string @var{s} is | |
1819 | modified in-place, the return value is not specified. | |
1820 | @end deffn | |
1821 | ||
8f85c0c6 NJ |
1822 | @deffn {Scheme Procedure} string-fold kons knil s [start end] |
1823 | @deffnx {Scheme Procedure} string-fold-right kons knil s [start end] | |
a0e07ba4 NJ |
1824 | Fold @var{kons} over the characters of @var{s}, with @var{knil} as the |
1825 | terminating element, from left to right (or right to left, for | |
1826 | @code{string-fold-right}). @var{kons} must expect two arguments: The | |
1827 | actual character and the last result of @var{kons}' application. | |
1828 | @end deffn | |
1829 | ||
8f85c0c6 NJ |
1830 | @deffn {Scheme Procedure} string-unfold p f g seed [base make_final] |
1831 | @deffnx {Scheme Procedure} string-unfold-right p f g seed [base make_final] | |
a0e07ba4 NJ |
1832 | These are the fundamental string constructors. |
1833 | @itemize @bullet | |
1834 | @item @var{g} is used to generate a series of @emph{seed} | |
1835 | values from the initial @var{seed}: @var{seed}, (@var{g} | |
1836 | @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), | |
1837 | @dots{} | |
1838 | @item @var{p} tells us when to stop - when it returns true | |
1839 | when applied to one of these seed values. | |
12991fed | 1840 | @item @var{f} maps each seed value to the corresponding |
a0e07ba4 NJ |
1841 | character in the result string. These chars are assembled into the |
1842 | string in a left-to-right (right-to-left) order. | |
1843 | @item @var{base} is the optional initial/leftmost (rightmost) | |
1844 | portion of the constructed string; it default to the empty string. | |
1845 | @item @var{make_final} is applied to the terminal seed | |
1846 | value (on which @var{p} returns true) to produce the final/rightmost | |
1847 | (leftmost) portion of the constructed string. It defaults to | |
1848 | @code{(lambda (x) "")}. | |
1849 | @end itemize | |
1850 | @end deffn | |
1851 | ||
8f85c0c6 | 1852 | @deffn {Scheme Procedure} string-for-each proc s [start end] |
a0e07ba4 NJ |
1853 | @var{proc} is mapped over @var{s} in left-to-right order. The |
1854 | return value is not specified. | |
1855 | @end deffn | |
1856 | ||
1857 | ||
1858 | @c =================================================================== | |
1859 | ||
1860 | @node SRFI-13 Replicate/Rotate | |
1861 | @subsection Replicate/Rotate | |
1862 | ||
1863 | These procedures are special substring procedures, which can also be | |
1864 | used for replicating strings. They are a bit tricky to use, but | |
1865 | consider this code fragment, which replicates the input string | |
1866 | @code{"foo"} so often that the resulting string has a length of six. | |
1867 | ||
1868 | @lisp | |
1869 | (xsubstring "foo" 0 6) | |
1870 | @result{} | |
1871 | "foofoo" | |
1872 | @end lisp | |
1873 | ||
8f85c0c6 | 1874 | @deffn {Scheme Procedure} xsubstring s from [to start end] |
a0e07ba4 NJ |
1875 | This is the @emph{extended substring} procedure that implements |
1876 | replicated copying of a substring of some string. | |
1877 | ||
1878 | @var{s} is a string, @var{start} and @var{end} are optional | |
1879 | arguments that demarcate a substring of @var{s}, defaulting to | |
1880 | 0 and the length of @var{s}. Replicate this substring up and | |
1881 | down index space, in both the positive and negative directions. | |
1882 | @code{xsubstring} returns the substring of this string | |
1883 | beginning at index @var{from}, and ending at @var{to}, which | |
1884 | defaults to @var{from} + (@var{end} - @var{start}). | |
1885 | @end deffn | |
1886 | ||
8f85c0c6 | 1887 | @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto start end] |
a0e07ba4 NJ |
1888 | Exactly the same as @code{xsubstring}, but the extracted text |
1889 | is written into the string @var{target} starting at index | |
1890 | @var{tstart}. The operation is not defined if @code{(eq? | |
1891 | @var{target} @var{s})} or these arguments share storage - you | |
1892 | cannot copy a string on top of itself. | |
1893 | @end deffn | |
1894 | ||
1895 | ||
1896 | @c =================================================================== | |
1897 | ||
1898 | @node SRFI-13 Miscellaneous | |
1899 | @subsection Miscellaneous | |
1900 | ||
1901 | @code{string-replace} is for replacing a portion of a string with | |
1902 | another string and @code{string-tokenize} splits a string into a list of | |
1903 | strings, breaking it up at a specified character. | |
1904 | ||
8c24f46e | 1905 | @deffn {Scheme Procedure} string-replace s1 s2 [start1 end1 start2 end2] |
a0e07ba4 NJ |
1906 | Return the string @var{s1}, but with the characters |
1907 | @var{start1} @dots{} @var{end1} replaced by the characters | |
1908 | @var{start2} @dots{} @var{end2} from @var{s2}. | |
5519096e KR |
1909 | |
1910 | For reference, note that SRFI-13 specifies @var{start1} and @var{end1} | |
1911 | as mandatory, but in Guile they are optional. | |
a0e07ba4 NJ |
1912 | @end deffn |
1913 | ||
c0ab7f13 | 1914 | @deffn {Scheme Procedure} string-tokenize s [token-set start end] |
a0e07ba4 | 1915 | Split the string @var{s} into a list of substrings, where each |
c0ab7f13 MV |
1916 | substring is a maximal non-empty contiguous sequence of characters |
1917 | from the character set @var{token_set}, which defaults to an | |
1918 | equivalent of @code{char-set:graphic}. If @var{start} or @var{end} | |
1919 | indices are provided, they restrict @code{string-tokenize} to | |
1920 | operating on the indicated substring of @var{s}. | |
a0e07ba4 NJ |
1921 | @end deffn |
1922 | ||
1923 | ||
1924 | @c =================================================================== | |
1925 | ||
1926 | @node SRFI-13 Filtering/Deleting | |
1927 | @subsection Filtering/Deleting | |
1928 | ||
1929 | @dfn{Filtering} means to remove all characters from a string which do | |
1930 | not match a given criteria, @dfn{deleting} means the opposite. | |
1931 | ||
8f85c0c6 | 1932 | @deffn {Scheme Procedure} string-filter s char_pred [start end] |
a0e07ba4 NJ |
1933 | Filter the string @var{s}, retaining only those characters that |
1934 | satisfy the @var{char_pred} argument. If the argument is a | |
1935 | procedure, it is applied to each character as a predicate, if | |
1936 | it is a character, it is tested for equality and if it is a | |
1937 | character set, it is tested for membership. | |
1938 | @end deffn | |
1939 | ||
8f85c0c6 | 1940 | @deffn {Scheme Procedure} string-delete s char_pred [start end] |
a0e07ba4 NJ |
1941 | Filter the string @var{s}, retaining only those characters that |
1942 | do not satisfy the @var{char_pred} argument. If the argument | |
1943 | is a procedure, it is applied to each character as a predicate, | |
1944 | if it is a character, it is tested for equality and if it is a | |
1945 | character set, it is tested for membership. | |
1946 | @end deffn | |
1947 | ||
1948 | ||
1949 | @node SRFI-14 | |
1950 | @section SRFI-14 - Character-set Library | |
8742c48b | 1951 | @cindex SRFI-14 |
a0e07ba4 NJ |
1952 | |
1953 | SRFI-14 defines the data type @dfn{character set}, and also defines a | |
1954 | lot of procedures for handling this character type, and a few standard | |
1955 | character sets like whitespace, alphabetic characters and others. | |
1956 | ||
1957 | All procedures from SRFI-14 (character-set library) are implemented in | |
1958 | the module @code{(srfi srfi-14)}, as well as the standard variables | |
1959 | @code{char-set:letter}, @code{char-set:digit} etc. | |
1960 | ||
1961 | @menu | |
1962 | * Loading SRFI-14:: How to make charsets available. | |
1963 | * SRFI-14 Character Set Data Type:: Underlying data type for charsets. | |
1964 | * SRFI-14 Predicates/Comparison:: Charset predicates. | |
1965 | * SRFI-14 Iterating Over Character Sets:: Enumerate charset elements. | |
85a9b4ed | 1966 | * SRFI-14 Creating Character Sets:: Making new charsets. |
a0e07ba4 NJ |
1967 | * SRFI-14 Querying Character Sets:: Test charsets for membership etc. |
1968 | * SRFI-14 Character-Set Algebra:: Calculating new charsets. | |
1969 | * SRFI-14 Standard Character Sets:: Variables containing predefined charsets. | |
1970 | @end menu | |
1971 | ||
1972 | ||
1973 | @node Loading SRFI-14 | |
1974 | @subsection Loading SRFI-14 | |
1975 | ||
1976 | When Guile is properly installed, SRFI-14 support can be loaded into a | |
1977 | running Guile by using the @code{(srfi srfi-14)} module. | |
1978 | ||
1979 | @example | |
1980 | $ guile | |
1981 | guile> (use-modules (srfi srfi-14)) | |
1982 | guile> (char-set-union (char-set #\f #\o #\o) (string->char-set "bar")) | |
1983 | #<charset @{#\a #\b #\f #\o #\r@}> | |
1984 | guile> | |
1985 | @end example | |
1986 | ||
1987 | ||
1988 | @node SRFI-14 Character Set Data Type | |
1989 | @subsection Character Set Data Type | |
1990 | ||
1991 | The data type @dfn{charset} implements sets of characters | |
1992 | (@pxref{Characters}). Because the internal representation of character | |
1993 | sets is not visible to the user, a lot of procedures for handling them | |
1994 | are provided. | |
1995 | ||
1996 | Character sets can be created, extended, tested for the membership of a | |
1997 | characters and be compared to other character sets. | |
1998 | ||
1999 | The Guile implementation of character sets deals with 8-bit characters. | |
2000 | In the standard variables, only the ASCII part of the character range is | |
2001 | really used, so that for example @dfn{Umlaute} and other accented | |
2002 | characters are not considered to be letters. In the future, as Guile | |
2003 | may get support for international character sets, this will change, so | |
2004 | don't rely on these ``features''. | |
2005 | ||
2006 | ||
2007 | @c =================================================================== | |
2008 | ||
2009 | @node SRFI-14 Predicates/Comparison | |
2010 | @subsection Predicates/Comparison | |
2011 | ||
2012 | Use these procedures for testing whether an object is a character set, | |
2013 | or whether several character sets are equal or subsets of each other. | |
2014 | @code{char-set-hash} can be used for calculating a hash value, maybe for | |
2015 | usage in fast lookup procedures. | |
2016 | ||
8f85c0c6 | 2017 | @deffn {Scheme Procedure} char-set? obj |
a0e07ba4 NJ |
2018 | Return @code{#t} if @var{obj} is a character set, @code{#f} |
2019 | otherwise. | |
2020 | @end deffn | |
2021 | ||
8f85c0c6 | 2022 | @deffn {Scheme Procedure} char-set= cs1 @dots{} |
a0e07ba4 NJ |
2023 | Return @code{#t} if all given character sets are equal. |
2024 | @end deffn | |
2025 | ||
8f85c0c6 | 2026 | @deffn {Scheme Procedure} char-set<= cs1 @dots{} |
a0e07ba4 NJ |
2027 | Return @code{#t} if every character set @var{cs}i is a subset |
2028 | of character set @var{cs}i+1. | |
2029 | @end deffn | |
2030 | ||
8f85c0c6 | 2031 | @deffn {Scheme Procedure} char-set-hash cs [bound] |
a0e07ba4 NJ |
2032 | Compute a hash value for the character set @var{cs}. If |
2033 | @var{bound} is given and not @code{#f}, it restricts the | |
2034 | returned value to the range 0 @dots{} @var{bound - 1}. | |
2035 | @end deffn | |
2036 | ||
2037 | ||
2038 | @c =================================================================== | |
2039 | ||
2040 | @node SRFI-14 Iterating Over Character Sets | |
2041 | @subsection Iterating Over Character Sets | |
2042 | ||
2043 | Character set cursors are a means for iterating over the members of a | |
2044 | character sets. After creating a character set cursor with | |
2045 | @code{char-set-cursor}, a cursor can be dereferenced with | |
2046 | @code{char-set-ref}, advanced to the next member with | |
2047 | @code{char-set-cursor-next}. Whether a cursor has passed past the last | |
2048 | element of the set can be checked with @code{end-of-char-set?}. | |
2049 | ||
2050 | Additionally, mapping and (un-)folding procedures for character sets are | |
2051 | provided. | |
2052 | ||
8f85c0c6 | 2053 | @deffn {Scheme Procedure} char-set-cursor cs |
a0e07ba4 NJ |
2054 | Return a cursor into the character set @var{cs}. |
2055 | @end deffn | |
2056 | ||
8f85c0c6 | 2057 | @deffn {Scheme Procedure} char-set-ref cs cursor |
a0e07ba4 NJ |
2058 | Return the character at the current cursor position |
2059 | @var{cursor} in the character set @var{cs}. It is an error to | |
2060 | pass a cursor for which @code{end-of-char-set?} returns true. | |
2061 | @end deffn | |
2062 | ||
8f85c0c6 | 2063 | @deffn {Scheme Procedure} char-set-cursor-next cs cursor |
a0e07ba4 NJ |
2064 | Advance the character set cursor @var{cursor} to the next |
2065 | character in the character set @var{cs}. It is an error if the | |
2066 | cursor given satisfies @code{end-of-char-set?}. | |
2067 | @end deffn | |
2068 | ||
8f85c0c6 | 2069 | @deffn {Scheme Procedure} end-of-char-set? cursor |
a0e07ba4 NJ |
2070 | Return @code{#t} if @var{cursor} has reached the end of a |
2071 | character set, @code{#f} otherwise. | |
2072 | @end deffn | |
2073 | ||
8f85c0c6 | 2074 | @deffn {Scheme Procedure} char-set-fold kons knil cs |
a0e07ba4 NJ |
2075 | Fold the procedure @var{kons} over the character set @var{cs}, |
2076 | initializing it with @var{knil}. | |
2077 | @end deffn | |
2078 | ||
8f85c0c6 NJ |
2079 | @deffn {Scheme Procedure} char-set-unfold p f g seed [base_cs] |
2080 | @deffnx {Scheme Procedure} char-set-unfold! p f g seed base_cs | |
a0e07ba4 NJ |
2081 | This is a fundamental constructor for character sets. |
2082 | @itemize @bullet | |
12991fed | 2083 | @item @var{g} is used to generate a series of ``seed'' values |
a0e07ba4 NJ |
2084 | from the initial seed: @var{seed}, (@var{g} @var{seed}), |
2085 | (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{} | |
2086 | @item @var{p} tells us when to stop -- when it returns true | |
12991fed | 2087 | when applied to one of the seed values. |
a0e07ba4 NJ |
2088 | @item @var{f} maps each seed value to a character. These |
2089 | characters are added to the base character set @var{base_cs} to | |
2090 | form the result; @var{base_cs} defaults to the empty set. | |
2091 | @end itemize | |
2092 | ||
2093 | @code{char-set-unfold!} is the side-effecting variant. | |
2094 | @end deffn | |
2095 | ||
8f85c0c6 | 2096 | @deffn {Scheme Procedure} char-set-for-each proc cs |
a0e07ba4 NJ |
2097 | Apply @var{proc} to every character in the character set |
2098 | @var{cs}. The return value is not specified. | |
2099 | @end deffn | |
2100 | ||
8f85c0c6 | 2101 | @deffn {Scheme Procedure} char-set-map proc cs |
a0e07ba4 NJ |
2102 | Map the procedure @var{proc} over every character in @var{cs}. |
2103 | @var{proc} must be a character -> character procedure. | |
2104 | @end deffn | |
2105 | ||
2106 | ||
2107 | @c =================================================================== | |
2108 | ||
2109 | @node SRFI-14 Creating Character Sets | |
2110 | @subsection Creating Character Sets | |
2111 | ||
2112 | New character sets are produced with these procedures. | |
2113 | ||
8f85c0c6 | 2114 | @deffn {Scheme Procedure} char-set-copy cs |
a0e07ba4 NJ |
2115 | Return a newly allocated character set containing all |
2116 | characters in @var{cs}. | |
2117 | @end deffn | |
2118 | ||
8f85c0c6 | 2119 | @deffn {Scheme Procedure} char-set char1 @dots{} |
a0e07ba4 NJ |
2120 | Return a character set containing all given characters. |
2121 | @end deffn | |
2122 | ||
8f85c0c6 NJ |
2123 | @deffn {Scheme Procedure} list->char-set char_list [base_cs] |
2124 | @deffnx {Scheme Procedure} list->char-set! char_list base_cs | |
a0e07ba4 NJ |
2125 | Convert the character list @var{list} to a character set. If |
2126 | the character set @var{base_cs} is given, the character in this | |
2127 | set are also included in the result. | |
2128 | ||
2129 | @code{list->char-set!} is the side-effecting variant. | |
2130 | @end deffn | |
2131 | ||
8f85c0c6 NJ |
2132 | @deffn {Scheme Procedure} string->char-set s [base_cs] |
2133 | @deffnx {Scheme Procedure} string->char-set! s base_cs | |
a0e07ba4 NJ |
2134 | Convert the string @var{str} to a character set. If the |
2135 | character set @var{base_cs} is given, the characters in this | |
2136 | set are also included in the result. | |
2137 | ||
2138 | @code{string->char-set!} is the side-effecting variant. | |
2139 | @end deffn | |
2140 | ||
8f85c0c6 NJ |
2141 | @deffn {Scheme Procedure} char-set-filter pred cs [base_cs] |
2142 | @deffnx {Scheme Procedure} char-set-filter! pred cs base_cs | |
a0e07ba4 NJ |
2143 | Return a character set containing every character from @var{cs} |
2144 | so that it satisfies @var{pred}. If provided, the characters | |
2145 | from @var{base_cs} are added to the result. | |
2146 | ||
2147 | @code{char-set-filter!} is the side-effecting variant. | |
2148 | @end deffn | |
2149 | ||
8f85c0c6 NJ |
2150 | @deffn {Scheme Procedure} ucs-range->char-set lower upper [error? base_cs] |
2151 | @deffnx {Scheme Procedure} uce-range->char-set! lower upper error? base_cs | |
a0e07ba4 NJ |
2152 | Return a character set containing all characters whose |
2153 | character codes lie in the half-open range | |
2154 | [@var{lower},@var{upper}). | |
2155 | ||
2156 | If @var{error} is a true value, an error is signalled if the | |
2157 | specified range contains characters which are not contained in | |
2158 | the implemented character range. If @var{error} is @code{#f}, | |
85a9b4ed | 2159 | these characters are silently left out of the resulting |
a0e07ba4 NJ |
2160 | character set. |
2161 | ||
2162 | The characters in @var{base_cs} are added to the result, if | |
2163 | given. | |
2164 | ||
2165 | @code{ucs-range->char-set!} is the side-effecting variant. | |
2166 | @end deffn | |
2167 | ||
8f85c0c6 | 2168 | @deffn {Scheme Procedure} ->char-set x |
a0e07ba4 NJ |
2169 | Coerce @var{x} into a character set. @var{x} may be a string, a |
2170 | character or a character set. | |
2171 | @end deffn | |
2172 | ||
2173 | ||
2174 | @c =================================================================== | |
2175 | ||
2176 | @node SRFI-14 Querying Character Sets | |
2177 | @subsection Querying Character Sets | |
2178 | ||
2179 | Access the elements and other information of a character set with these | |
2180 | procedures. | |
2181 | ||
8f85c0c6 | 2182 | @deffn {Scheme Procedure} char-set-size cs |
a0e07ba4 NJ |
2183 | Return the number of elements in character set @var{cs}. |
2184 | @end deffn | |
2185 | ||
8f85c0c6 | 2186 | @deffn {Scheme Procedure} char-set-count pred cs |
a0e07ba4 NJ |
2187 | Return the number of the elements int the character set |
2188 | @var{cs} which satisfy the predicate @var{pred}. | |
2189 | @end deffn | |
2190 | ||
8f85c0c6 | 2191 | @deffn {Scheme Procedure} char-set->list cs |
a0e07ba4 NJ |
2192 | Return a list containing the elements of the character set |
2193 | @var{cs}. | |
2194 | @end deffn | |
2195 | ||
8f85c0c6 | 2196 | @deffn {Scheme Procedure} char-set->string cs |
a0e07ba4 NJ |
2197 | Return a string containing the elements of the character set |
2198 | @var{cs}. The order in which the characters are placed in the | |
2199 | string is not defined. | |
2200 | @end deffn | |
2201 | ||
8f85c0c6 | 2202 | @deffn {Scheme Procedure} char-set-contains? cs char |
a0e07ba4 NJ |
2203 | Return @code{#t} iff the character @var{ch} is contained in the |
2204 | character set @var{cs}. | |
2205 | @end deffn | |
2206 | ||
8f85c0c6 | 2207 | @deffn {Scheme Procedure} char-set-every pred cs |
a0e07ba4 NJ |
2208 | Return a true value if every character in the character set |
2209 | @var{cs} satisfies the predicate @var{pred}. | |
2210 | @end deffn | |
2211 | ||
8f85c0c6 | 2212 | @deffn {Scheme Procedure} char-set-any pred cs |
a0e07ba4 NJ |
2213 | Return a true value if any character in the character set |
2214 | @var{cs} satisfies the predicate @var{pred}. | |
2215 | @end deffn | |
2216 | ||
2217 | ||
2218 | @c =================================================================== | |
2219 | ||
2220 | @node SRFI-14 Character-Set Algebra | |
2221 | @subsection Character-Set Algebra | |
2222 | ||
2223 | Character sets can be manipulated with the common set algebra operation, | |
2224 | such as union, complement, intersection etc. All of these procedures | |
2225 | provide side-effecting variants, which modify their character set | |
2226 | argument(s). | |
2227 | ||
8f85c0c6 NJ |
2228 | @deffn {Scheme Procedure} char-set-adjoin cs char1 @dots{} |
2229 | @deffnx {Scheme Procedure} char-set-adjoin! cs char1 @dots{} | |
a0e07ba4 NJ |
2230 | Add all character arguments to the first argument, which must |
2231 | be a character set. | |
2232 | @end deffn | |
2233 | ||
8f85c0c6 NJ |
2234 | @deffn {Scheme Procedure} char-set-delete cs char1 @dots{} |
2235 | @deffnx {Scheme Procedure} char-set-delete! cs char1 @dots{} | |
a0e07ba4 NJ |
2236 | Delete all character arguments from the first argument, which |
2237 | must be a character set. | |
2238 | @end deffn | |
2239 | ||
8f85c0c6 NJ |
2240 | @deffn {Scheme Procedure} char-set-complement cs |
2241 | @deffnx {Scheme Procedure} char-set-complement! cs | |
a0e07ba4 NJ |
2242 | Return the complement of the character set @var{cs}. |
2243 | @end deffn | |
2244 | ||
8f85c0c6 NJ |
2245 | @deffn {Scheme Procedure} char-set-union cs1 @dots{} |
2246 | @deffnx {Scheme Procedure} char-set-union! cs1 @dots{} | |
a0e07ba4 NJ |
2247 | Return the union of all argument character sets. |
2248 | @end deffn | |
2249 | ||
8f85c0c6 NJ |
2250 | @deffn {Scheme Procedure} char-set-intersection cs1 @dots{} |
2251 | @deffnx {Scheme Procedure} char-set-intersection! cs1 @dots{} | |
a0e07ba4 NJ |
2252 | Return the intersection of all argument character sets. |
2253 | @end deffn | |
2254 | ||
8f85c0c6 NJ |
2255 | @deffn {Scheme Procedure} char-set-difference cs1 @dots{} |
2256 | @deffnx {Scheme Procedure} char-set-difference! cs1 @dots{} | |
a0e07ba4 NJ |
2257 | Return the difference of all argument character sets. |
2258 | @end deffn | |
2259 | ||
8f85c0c6 NJ |
2260 | @deffn {Scheme Procedure} char-set-xor cs1 @dots{} |
2261 | @deffnx {Scheme Procedure} char-set-xor! cs1 @dots{} | |
a0e07ba4 NJ |
2262 | Return the exclusive-or of all argument character sets. |
2263 | @end deffn | |
2264 | ||
8f85c0c6 NJ |
2265 | @deffn {Scheme Procedure} char-set-diff+intersection cs1 @dots{} |
2266 | @deffnx {Scheme Procedure} char-set-diff+intersection! cs1 @dots{} | |
a0e07ba4 NJ |
2267 | Return the difference and the intersection of all argument |
2268 | character sets. | |
2269 | @end deffn | |
2270 | ||
2271 | ||
2272 | @c =================================================================== | |
2273 | ||
2274 | @node SRFI-14 Standard Character Sets | |
2275 | @subsection Standard Character Sets | |
2276 | ||
2277 | In order to make the use of the character set data type and procedures | |
2278 | useful, several predefined character set variables exist. | |
2279 | ||
2280 | @defvar char-set:lower-case | |
2281 | All lower-case characters. | |
2282 | @end defvar | |
2283 | ||
2284 | @defvar char-set:upper-case | |
2285 | All upper-case characters. | |
2286 | @end defvar | |
2287 | ||
2288 | @defvar char-set:title-case | |
2289 | This is empty, because ASCII has no titlecase characters. | |
2290 | @end defvar | |
2291 | ||
2292 | @defvar char-set:letter | |
2293 | All letters, e.g. the union of @code{char-set:lower-case} and | |
2294 | @code{char-set:upper-case}. | |
2295 | @end defvar | |
2296 | ||
2297 | @defvar char-set:digit | |
2298 | All digits. | |
2299 | @end defvar | |
2300 | ||
2301 | @defvar char-set:letter+digit | |
2302 | The union of @code{char-set:letter} and @code{char-set:digit}. | |
2303 | @end defvar | |
2304 | ||
2305 | @defvar char-set:graphic | |
2306 | All characters which would put ink on the paper. | |
2307 | @end defvar | |
2308 | ||
2309 | @defvar char-set:printing | |
2310 | The union of @code{char-set:graphic} and @code{char-set:whitespace}. | |
2311 | @end defvar | |
2312 | ||
2313 | @defvar char-set:whitespace | |
2314 | All whitespace characters. | |
2315 | @end defvar | |
2316 | ||
2317 | @defvar char-set:blank | |
2318 | All horizontal whitespace characters, that is @code{#\space} and | |
2319 | @code{#\tab}. | |
2320 | @end defvar | |
2321 | ||
2322 | @defvar char-set:iso-control | |
2323 | The ISO control characters with the codes 0--31 and 127. | |
2324 | @end defvar | |
2325 | ||
2326 | @defvar char-set:punctuation | |
2327 | The characters @code{!"#%&'()*,-./:;?@@[\\]_@{@}} | |
2328 | @end defvar | |
2329 | ||
2330 | @defvar char-set:symbol | |
2331 | The characters @code{$+<=>^`|~}. | |
2332 | @end defvar | |
2333 | ||
2334 | @defvar char-set:hex-digit | |
2335 | The hexadecimal digits @code{0123456789abcdefABCDEF}. | |
2336 | @end defvar | |
2337 | ||
2338 | @defvar char-set:ascii | |
2339 | All ASCII characters. | |
2340 | @end defvar | |
2341 | ||
2342 | @defvar char-set:empty | |
2343 | The empty character set. | |
2344 | @end defvar | |
2345 | ||
2346 | @defvar char-set:full | |
2347 | This character set contains all possible characters. | |
2348 | @end defvar | |
2349 | ||
2350 | @node SRFI-16 | |
2351 | @section SRFI-16 - case-lambda | |
8742c48b | 2352 | @cindex SRFI-16 |
a0e07ba4 NJ |
2353 | |
2354 | @c FIXME::martin: Review me! | |
2355 | ||
8742c48b | 2356 | @findex case-lambda |
a0e07ba4 NJ |
2357 | The syntactic form @code{case-lambda} creates procedures, just like |
2358 | @code{lambda}, but has syntactic extensions for writing procedures of | |
2359 | varying arity easier. | |
2360 | ||
2361 | The syntax of the @code{case-lambda} form is defined in the following | |
2362 | EBNF grammar. | |
2363 | ||
2364 | @example | |
2365 | @group | |
2366 | <case-lambda> | |
2367 | --> (case-lambda <case-lambda-clause>) | |
2368 | <case-lambda-clause> | |
2369 | --> (<formals> <definition-or-command>*) | |
2370 | <formals> | |
2371 | --> (<identifier>*) | |
2372 | | (<identifier>* . <identifier>) | |
2373 | | <identifier> | |
2374 | @end group | |
2375 | @end example | |
2376 | ||
2377 | The value returned by a @code{case-lambda} form is a procedure which | |
2378 | matches the number of actual arguments against the formals in the | |
2379 | various clauses, in order. @dfn{Formals} means a formal argument list | |
2380 | just like with @code{lambda} (@pxref{Lambda}). The first matching clause | |
2381 | is selected, the corresponding values from the actual parameter list are | |
2382 | bound to the variable names in the clauses and the body of the clause is | |
2383 | evaluated. If no clause matches, an error is signalled. | |
2384 | ||
2385 | The following (silly) definition creates a procedure @var{foo} which | |
2386 | acts differently, depending on the number of actual arguments. If one | |
2387 | argument is given, the constant @code{#t} is returned, two arguments are | |
2388 | added and if more arguments are passed, their product is calculated. | |
2389 | ||
2390 | @lisp | |
2391 | (define foo (case-lambda | |
2392 | ((x) #t) | |
2393 | ((x y) (+ x y)) | |
2394 | (z | |
2395 | (apply * z)))) | |
2396 | (foo 'bar) | |
2397 | @result{} | |
2398 | #t | |
2399 | (foo 2 4) | |
2400 | @result{} | |
2401 | 6 | |
2402 | (foo 3 3 3) | |
2403 | @result{} | |
2404 | 27 | |
2405 | (foo) | |
2406 | @result{} | |
2407 | 1 | |
2408 | @end lisp | |
2409 | ||
2410 | The last expression evaluates to 1 because the last clause is matched, | |
2411 | @var{z} is bound to the empty list and the following multiplication, | |
2412 | applied to zero arguments, yields 1. | |
2413 | ||
2414 | ||
2415 | @node SRFI-17 | |
2416 | @section SRFI-17 - Generalized set! | |
8742c48b | 2417 | @cindex SRFI-17 |
a0e07ba4 NJ |
2418 | |
2419 | This is an implementation of SRFI-17: Generalized set! | |
2420 | ||
8742c48b | 2421 | @findex getter-with-setter |
a0e07ba4 NJ |
2422 | It exports the Guile procedure @code{make-procedure-with-setter} under |
2423 | the SRFI name @code{getter-with-setter} and exports the standard | |
2424 | procedures @code{car}, @code{cdr}, @dots{}, @code{cdddr}, | |
2425 | @code{string-ref} and @code{vector-ref} as procedures with setters, as | |
2426 | required by the SRFI. | |
2427 | ||
2428 | SRFI-17 was heavily criticized during its discussion period but it was | |
2429 | finalized anyway. One issue was its concept of globally associating | |
2430 | setter @dfn{properties} with (procedure) values, which is non-Schemy. | |
2431 | For this reason, this implementation chooses not to provide a way to set | |
2432 | the setter of a procedure. In fact, @code{(set! (setter @var{proc}) | |
2433 | @var{setter})} signals an error. The only way to attach a setter to a | |
2434 | procedure is to create a new object (a @dfn{procedure with setter}) via | |
2435 | the @code{getter-with-setter} procedure. This procedure is also | |
2436 | specified in the SRFI. Using it avoids the described problems. | |
2437 | ||
12991fed TTN |
2438 | |
2439 | @node SRFI-19 | |
2440 | @section SRFI-19 - Time/Date Library | |
8742c48b | 2441 | @cindex SRFI-19 |
12991fed | 2442 | |
85600a0f KR |
2443 | This is an implementation of the SRFI-19 time/date library. The |
2444 | functions and variables described here are provided by | |
12991fed TTN |
2445 | |
2446 | @example | |
85600a0f | 2447 | (use-modules (srfi srfi-19)) |
12991fed TTN |
2448 | @end example |
2449 | ||
85600a0f KR |
2450 | @menu |
2451 | * SRFI-19 Introduction:: | |
2452 | * SRFI-19 Time:: | |
2453 | * SRFI-19 Date:: | |
2454 | * SRFI-19 Time/Date conversions:: | |
2455 | * SRFI-19 Date to string:: | |
2456 | * SRFI-19 String to date:: | |
2457 | @end menu | |
12991fed | 2458 | |
85600a0f KR |
2459 | @node SRFI-19 Introduction |
2460 | @subsection SRFI-19 Introduction | |
2461 | ||
2462 | @cindex universal time | |
2463 | @cindex atomic time | |
2464 | @cindex UTC | |
2465 | @cindex TAI | |
2466 | This module implements time and date representations and calculations, | |
2467 | in various time systems, including universal time (UTC) and atomic | |
2468 | time (TAI). | |
2469 | ||
2470 | For those not familiar with these time systems, TAI is based on a | |
2471 | fixed length second derived from oscillations of certain atoms. UTC | |
2472 | differs from TAI by an integral number of seconds, which is increased | |
2473 | or decreased at announced times to keep UTC aligned to a mean solar | |
2474 | day (the orbit and rotation of the earth are not quite constant). | |
2475 | ||
2476 | @cindex leap second | |
2477 | So far, only increases in the TAI | |
2478 | @tex | |
2479 | $\leftrightarrow$ | |
2480 | @end tex | |
2481 | @ifnottex | |
2482 | <-> | |
2483 | @end ifnottex | |
2484 | UTC difference have been needed. Such an increase is a ``leap | |
2485 | second'', an extra second of TAI introduced at the end of a UTC day. | |
2486 | When working entirely within UTC this is never seen, every day simply | |
2487 | has 86400 seconds. But when converting from TAI to a UTC date, an | |
2488 | extra 23:59:60 is present, where normally a day would end at 23:59:59. | |
2489 | Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI | |
2490 | seconds. | |
2491 | ||
2492 | @cindex system clock | |
2493 | In the current implementation, the system clock is assumed to be UTC, | |
2494 | and a table of leap seconds in the code converts to TAI. See comments | |
2495 | in @file{srfi-19.scm} for how to update this table. | |
2496 | ||
2497 | @cindex julian day | |
2498 | @cindex modified julian day | |
2499 | Also, for those not familiar with the terminology, a @dfn{Julian Day} | |
2500 | is a real number which is a count of days and fraction of a day, in | |
2501 | UTC, starting from -4713-01-01T12:00:00Z, ie.@: midday Monday 1 Jan | |
2502 | 4713 B.C. And a @dfn{Modified Julian Day} is the same, but starting | |
2503 | from 1858-11-17T00:00:00Z, ie.@: midnight 17 November 1858 UTC. | |
2504 | ||
2505 | @c The SRFI-1 spec says -4714-11-24T12:00:00Z (November 24, -4714 at | |
2506 | @c noon, UTC), but this is incorrect. It looks like it might have | |
2507 | @c arisen from the code incorrectly treating years a multiple of 100 | |
2508 | @c but not 400 prior to 1582 as leap years, where instead the Julian | |
2509 | @c calendar should be used so all multiples of 4 before 1582 are leap | |
2510 | @c years. | |
2511 | ||
2512 | ||
2513 | @node SRFI-19 Time | |
2514 | @subsection SRFI-19 Time | |
2515 | @cindex time | |
2516 | ||
2517 | A @dfn{time} object has type, seconds and nanoseconds fields | |
2518 | representing a point in time starting from some epoch. This is an | |
2519 | arbitrary point in time, not just a time of day. Although times are | |
2520 | represented in nanoseconds, the actual resolution may be lower. | |
2521 | ||
2522 | The following variables hold the possible time types. For instance | |
2523 | @code{(current-time time-process)} would give the current CPU process | |
2524 | time. | |
2525 | ||
2526 | @defvar time-utc | |
2527 | Universal Coordinated Time (UTC). | |
2528 | @cindex UTC | |
2529 | @end defvar | |
12991fed | 2530 | |
85600a0f KR |
2531 | @defvar time-tai |
2532 | International Atomic Time (TAI). | |
2533 | @cindex TAI | |
2534 | @end defvar | |
12991fed | 2535 | |
85600a0f KR |
2536 | @defvar time-monotonic |
2537 | Monotonic time, meaning a monotonically increasing time starting from | |
2538 | an unspecified epoch. | |
12991fed | 2539 | |
85600a0f KR |
2540 | Note that in the current implementation @code{time-monotonic} is the |
2541 | same as @code{time-tai}, and unfortunately is therefore affected by | |
2542 | adjustments to the system clock. Perhaps this will change in the | |
2543 | future. | |
2544 | @end defvar | |
12991fed | 2545 | |
85600a0f KR |
2546 | @defvar time-duration |
2547 | A duration, meaning simply a difference between two times. | |
2548 | @end defvar | |
12991fed | 2549 | |
85600a0f KR |
2550 | @defvar time-process |
2551 | CPU time spent in the current process, starting from when the process | |
2552 | began. | |
2553 | @cindex process time | |
2554 | @end defvar | |
12991fed | 2555 | |
85600a0f KR |
2556 | @defvar time-thread |
2557 | CPU time spent in the current thread. Not currently implemented. | |
2558 | @cindex thread time | |
2559 | @end defvar | |
12991fed | 2560 | |
85600a0f KR |
2561 | @sp 1 |
2562 | @defun time? obj | |
2563 | Return @code{#t} if @var{obj} is a time object, or @code{#f} if not. | |
2564 | @end defun | |
2565 | ||
2566 | @defun make-time type nanoseconds seconds | |
2567 | Create a time object with the given @var{type}, @var{seconds} and | |
2568 | @var{nanoseconds}. | |
2569 | @end defun | |
2570 | ||
2571 | @defun time-type time | |
2572 | @defunx time-nanosecond time | |
2573 | @defunx time-second time | |
2574 | @defunx set-time-type! time type | |
2575 | @defunx set-time-nanosecond! time nsec | |
2576 | @defunx set-time-second! time sec | |
2577 | Get or set the type, seconds or nanoseconds fields of a time object. | |
2578 | ||
2579 | @code{set-time-type!} merely changes the field, it doesn't convert the | |
2580 | time value. For conversions, see @ref{SRFI-19 Time/Date conversions}. | |
2581 | @end defun | |
2582 | ||
2583 | @defun copy-time time | |
2584 | Return a new time object, which is a copy of the given @var{time}. | |
2585 | @end defun | |
2586 | ||
2587 | @defun current-time [type] | |
2588 | Return the current time of the given @var{type}. The default | |
2589 | @var{type} is @code{time-utc}. | |
2590 | ||
2591 | Note that the name @code{current-time} conflicts with the Guile core | |
2592 | @code{current-time} function (@pxref{Time}). Applications wanting to | |
2593 | use both will need to use a different name for one of them. | |
2594 | @end defun | |
2595 | ||
2596 | @defun time-resolution [type] | |
2597 | Return the resolution, in nanoseconds, of the given time @var{type}. | |
2598 | The default @var{type} is @code{time-utc}. | |
2599 | @end defun | |
2600 | ||
2601 | @defun time<=? t1 t2 | |
2602 | @defunx time<? t1 t2 | |
2603 | @defunx time=? t1 t2 | |
2604 | @defunx time>=? t1 t2 | |
2605 | @defunx time>? t1 t2 | |
2606 | Return @code{#t} or @code{#f} according to the respective relation | |
2607 | between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2} | |
2608 | must be the same time type. | |
2609 | @end defun | |
2610 | ||
2611 | @defun time-difference t1 t2 | |
2612 | @defunx time-difference! t1 t2 | |
2613 | Return a time object of type @code{time-duration} representing the | |
2614 | period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be | |
2615 | the same time type. | |
2616 | ||
2617 | @code{time-difference} returns a new time object, | |
2618 | @code{time-difference!} may modify @var{t1} to form its return. | |
2619 | @end defun | |
2620 | ||
2621 | @defun add-duration time duration | |
2622 | @defunx add-duration! time duration | |
2623 | @defunx subtract-duration time duration | |
2624 | @defunx subtract-duration! time duration | |
2625 | Return a time object which is @var{time} with the given @var{duration} | |
2626 | added or subtracted. @var{duration} must be a time object of type | |
2627 | @code{time-duration}. | |
2628 | ||
2629 | @code{add-duration} and @code{subtract-duration} return a new time | |
2630 | object. @code{add-duration!} and @code{subtract-duration!} may modify | |
2631 | the given @var{time} to form their return. | |
2632 | @end defun | |
2633 | ||
2634 | ||
2635 | @node SRFI-19 Date | |
2636 | @subsection SRFI-19 Date | |
2637 | @cindex date | |
2638 | ||
2639 | A @dfn{date} object represents a date in the Gregorian calendar and a | |
2640 | time of day on that date in some timezone. | |
2641 | ||
2642 | The fields are year, month, day, hour, minute, second, nanoseconds and | |
2643 | timezone. A date object is immutable, its fields can be read but they | |
2644 | cannot be modified once the object is created. | |
2645 | ||
2646 | @defun date? obj | |
2647 | Return @code{#t} if @var{obj} is a date object, or @code{#f} if not. | |
2648 | @end defun | |
2649 | ||
2650 | @defun make-date nsecs seconds minutes hours date month year zone-offset | |
2651 | Create a new date object. | |
2652 | @c | |
2653 | @c FIXME: What can we say about the ranges of the values. The | |
2654 | @c current code looks it doesn't normalize, but expects then in their | |
2655 | @c usual range already. | |
2656 | @c | |
2657 | @end defun | |
2658 | ||
2659 | @defun date-nanosecond date | |
2660 | Nanoseconds, 0 to 999999999. | |
2661 | @end defun | |
2662 | ||
2663 | @defun date-second date | |
2664 | Seconds, 0 to 60. 0 to 59 is the usual range, 60 is for a leap second. | |
2665 | @end defun | |
2666 | ||
2667 | @defun date-minute date | |
2668 | Minutes, 0 to 59. | |
2669 | @end defun | |
2670 | ||
2671 | @defun date-hour date | |
2672 | Hour, 0 to 23. | |
2673 | @end defun | |
2674 | ||
2675 | @defun date-day date | |
2676 | Day of the month, 1 to 31 (or less, according to the month). | |
2677 | @end defun | |
2678 | ||
2679 | @defun date-month date | |
2680 | Month, 1 to 12. | |
2681 | @end defun | |
2682 | ||
2683 | @defun date-year date | |
2684 | Year, eg.@: 2003. | |
2685 | @end defun | |
2686 | ||
2687 | @defun date-zone-offset date | |
2688 | Time zone, an integer number of seconds east of Greenwich. | |
2689 | @end defun | |
2690 | ||
2691 | @defun date-year-day date | |
2692 | Day of the year, starting from 1 for 1st January. | |
2693 | @end defun | |
2694 | ||
2695 | @defun date-week-day date | |
2696 | Day of the week, starting from 0 for Sunday. | |
2697 | @end defun | |
2698 | ||
2699 | @defun date-week-number date dstartw | |
2700 | Week of the year, ignoring a first partial week. @var{dstartw} is the | |
2701 | day of the week which is taken to start a week, 0 for Sunday, 1 for | |
2702 | Monday, etc. | |
2703 | @c | |
2704 | @c FIXME: The spec doesn't say whether numbering starts at 0 or 1. | |
2705 | @c The code looks like it's 0, if that's the correct intention. | |
2706 | @c | |
2707 | @end defun | |
2708 | ||
2709 | @c The SRFI text doesn't actually give the default for tz-offset, but | |
2710 | @c the reference implementation has the local timezone and the | |
2711 | @c conversions functions all specify that, so it should be ok to | |
2712 | @c document it here. | |
2713 | @c | |
2714 | @defun current-date [tz-offset] | |
2715 | Return a date object representing the current date/time UTC. | |
2716 | @var{tz-offset} is seconds east of Greenwich, and defaults to the | |
2717 | local timezone. | |
2718 | @end defun | |
2719 | ||
2720 | @defun current-julian-day | |
2721 | @cindex julian day | |
2722 | Return the current Julian Day. | |
2723 | @end defun | |
2724 | ||
2725 | @defun current-modified-julian-day | |
2726 | @cindex modified julian day | |
2727 | Return the current Modified Julian Day. | |
2728 | @end defun | |
2729 | ||
2730 | ||
2731 | @node SRFI-19 Time/Date conversions | |
2732 | @subsection SRFI-19 Time/Date conversions | |
2733 | ||
2734 | @defun date->julian-day date | |
2735 | @defunx date->modified-julian-day date | |
2736 | @defunx date->time-monotonic date | |
2737 | @defunx date->time-tai date | |
2738 | @defunx date->time-utc date | |
2739 | @end defun | |
2740 | @defun julian-day->date jdn [tz-offset] | |
2741 | @defunx julian-day->time-monotonic jdn | |
2742 | @defunx julian-day->time-tai jdn | |
2743 | @defunx julian-day->time-utc jdn | |
2744 | @end defun | |
2745 | @defun modified-julian-day->date jdn [tz-offset] | |
2746 | @defunx modified-julian-day->time-monotonic jdn | |
2747 | @defunx modified-julian-day->time-tai jdn | |
2748 | @defunx modified-julian-day->time-utc jdn | |
2749 | @end defun | |
2750 | @defun time-monotonic->date time [tz-offset] | |
2751 | @defunx time-monotonic->time-tai time | |
2752 | @defunx time-monotonic->time-tai! time | |
2753 | @defunx time-monotonic->time-utc time | |
2754 | @defunx time-monotonic->time-utc! time | |
2755 | @end defun | |
2756 | @defun time-tai->date time [tz-offset] | |
2757 | @defunx time-tai->julian-day time | |
2758 | @defunx time-tai->modified-julian-day time | |
2759 | @defunx time-tai->time-monotonic time | |
2760 | @defunx time-tai->time-monotonic! time | |
2761 | @defunx time-tai->time-utc time | |
2762 | @defunx time-tai->time-utc! time | |
2763 | @end defun | |
2764 | @defun time-utc->date time [tz-offset] | |
2765 | @defunx time-utc->julian-day time | |
2766 | @defunx time-utc->modified-julian-day time | |
2767 | @defunx time-utc->time-monotonic time | |
2768 | @defunx time-utc->time-monotonic! time | |
2769 | @defunx time-utc->time-tai time | |
2770 | @defunx time-utc->time-tai! time | |
2771 | @sp 1 | |
2772 | Convert between dates, times and days of the respective types. For | |
2773 | instance @code{time-tai->time-utc} accepts a @var{time} object of type | |
2774 | @code{time-tai} and returns an object of type @code{time-utc}. | |
2775 | ||
2776 | For conversions to dates, @var{tz-offset} is seconds east of | |
2777 | Greenwich. The default is the local timezone. | |
2778 | ||
2779 | The @code{!} variants may modify their @var{time} argument to form | |
2780 | their return. The plain functions create a new object. | |
2781 | @end defun | |
2782 | ||
2783 | @node SRFI-19 Date to string | |
2784 | @subsection SRFI-19 Date to string | |
2785 | @cindex date to string | |
2786 | ||
2787 | @defun date->string date [format] | |
2788 | Convert a date to a string under the control of a format. | |
2789 | @var{format} should be a string containing @samp{~} escapes, which | |
2790 | will be expanded as per the following conversion table. The default | |
2791 | @var{format} is @samp{~c}, a locale-dependent date and time. | |
2792 | ||
2793 | Many of these conversion characters are the same as POSIX | |
2794 | @code{strftime} (@pxref{Time}), but there are some extras and some | |
2795 | variations. | |
2796 | ||
2797 | @multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM} | |
2798 | @item @nicode{~~} @tab literal ~ | |
2799 | @item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun} | |
2800 | @item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday} | |
2801 | @item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan} | |
2802 | @item @nicode{~B} @tab locale full month, eg.@: @samp{January} | |
2803 | @item @nicode{~c} @tab locale date and time, eg.@: @* | |
2804 | @samp{Fri Jul 14 20:28:42-0400 2000} | |
2805 | @item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31} | |
2806 | ||
2807 | @c Spec says d/m/y, reference implementation says m/d/y. | |
2808 | @c Apparently the reference code was the intention, but would like to | |
2809 | @c see an errata published for the spec before contradicting it here. | |
2810 | @c | |
2811 | @c @item @nicode{~D} @tab date @nicode{~d/~m/~y} | |
2812 | ||
2813 | @item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31} | |
2814 | @item @nicode{~f} @tab seconds and fractional seconds, | |
2815 | with locale decimal point, eg.@: @samp{5.2} | |
2816 | @item @nicode{~h} @tab same as @nicode{~b} | |
2817 | @item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23} | |
2818 | @item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12} | |
2819 | @item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366} | |
2820 | @item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23} | |
2821 | @item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12} | |
2822 | @item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12} | |
2823 | @item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59} | |
2824 | @item @nicode{~n} @tab newline | |
2825 | @item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999} | |
2826 | @item @nicode{~p} @tab locale AM or PM | |
2827 | @item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p} | |
2828 | @item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC | |
2829 | @item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @* | |
2830 | (usual limit is 59, 60 is a leap second) | |
2831 | @item @nicode{~t} @tab horizontal tab character | |
2832 | @item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S} | |
2833 | @item @nicode{~U} @tab week of year, Sunday first day of week, | |
2834 | @samp{00} to @samp{52} | |
2835 | @item @nicode{~V} @tab week of year, Monday first day of week, | |
2836 | @samp{01} to @samp{53} | |
2837 | @item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6} | |
2838 | @item @nicode{~W} @tab week of year, Monday first day of week, | |
2839 | @samp{00} to @samp{52} | |
2840 | ||
2841 | @c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale | |
2842 | @c date. The reference code has ~x as the locale date and ~X as a | |
2843 | @c locale time. The rule is apparently that the code should be | |
2844 | @c believed, but would like to see an errata for the spec before | |
2845 | @c contradicting it here. | |
2846 | @c | |
2847 | @c @item @nicode{~x} @tab week of year, Monday as first day of week, | |
2848 | @c @samp{00} to @samp{53} | |
2849 | @c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00} | |
2850 | ||
2851 | @item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99} | |
2852 | @item @nicode{~Y} @tab year, full, eg.@: @samp{2003} | |
2853 | @item @nicode{~z} @tab time zone, RFC-822 style | |
2854 | @item @nicode{~Z} @tab time zone symbol (not currently implemented) | |
2855 | @item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d} | |
2856 | @item @nicode{~2} @tab ISO-8601 time+zone, @samp{~k:~M:~S~z} | |
2857 | @item @nicode{~3} @tab ISO-8601 time, @samp{~k:~M:~S} | |
2858 | @item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~k:~M:~S~z} | |
2859 | @item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~k:~M:~S} | |
2860 | @end multitable | |
2861 | @end defun | |
2862 | ||
2863 | Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently | |
2864 | described here, since the specification and reference implementation | |
2865 | differ. | |
2866 | ||
2867 | Currently Guile doesn't implement any localizations for the above, all | |
2868 | outputs are in English, and the @samp{~c} conversion is POSIX | |
2869 | @code{ctime} style @samp{~a ~b ~d ~H:~M:~S~z ~Y}. This may change in | |
2870 | the future. | |
2871 | ||
2872 | ||
2873 | @node SRFI-19 String to date | |
2874 | @subsection SRFI-19 String to date | |
2875 | @cindex string to date | |
2876 | ||
2877 | @c FIXME: Can we say what happens when an incomplete date is | |
2878 | @c converted? Ie. fields left as 0, or what? The spec seems to be | |
2879 | @c silent on this. | |
2880 | ||
2881 | @defun string->date input template | |
2882 | Convert an @var{input} string to a date under the control of a | |
2883 | @var{template} string. Return a newly created date object. | |
2884 | ||
2885 | Literal characters in @var{template} must match characters in | |
2886 | @var{input} and @samp{~} escapes must match the input forms described | |
2887 | in the table below. ``Skip to'' means characters up to one of the | |
2888 | given type are ignored, or ``no skip'' for no skipping. ``Read'' is | |
2889 | what's then read, and ``Set'' is the field affected in the date | |
2890 | object. | |
2891 | ||
2892 | For example @samp{~Y} skips input characters until a digit is reached, | |
2893 | at which point it expects a year and stores that to the year field of | |
2894 | the date. | |
2895 | ||
2896 | @multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}} | |
2897 | @item | |
2898 | @tab Skip to | |
2899 | @tab Read | |
2900 | @tab Set | |
2901 | ||
2902 | @item @nicode{~~} | |
2903 | @tab no skip | |
2904 | @tab literal ~ | |
2905 | @tab nothing | |
2906 | ||
2907 | @item @nicode{~a} | |
2908 | @tab @nicode{char-alphabetic?} | |
2909 | @tab locale abbreviated weekday name | |
2910 | @tab nothing | |
2911 | ||
2912 | @item @nicode{~A} | |
2913 | @tab @nicode{char-alphabetic?} | |
2914 | @tab locale full weekday name | |
2915 | @tab nothing | |
2916 | ||
2917 | @c Note that the SRFI spec says that ~b and ~B don't set anything, | |
2918 | @c but that looks like a mistake. The reference implementation sets | |
2919 | @c the month field, which seems sensible and is what we describe | |
2920 | @c here. | |
2921 | ||
2922 | @item @nicode{~b} | |
2923 | @tab @nicode{char-alphabetic?} | |
2924 | @tab locale abbreviated month name | |
2925 | @tab @nicode{date-month} | |
2926 | ||
2927 | @item @nicode{~B} | |
2928 | @tab @nicode{char-alphabetic?} | |
2929 | @tab locale full month name | |
2930 | @tab @nicode{date-month} | |
2931 | ||
2932 | @item @nicode{~d} | |
2933 | @tab @nicode{char-numeric?} | |
2934 | @tab day of month | |
2935 | @tab @nicode{date-day} | |
2936 | ||
2937 | @item @nicode{~e} | |
2938 | @tab no skip | |
2939 | @tab day of month, blank padded | |
2940 | @tab @nicode{date-day} | |
2941 | ||
2942 | @item @nicode{~h} | |
2943 | @tab same as @samp{~b} | |
2944 | ||
2945 | @item @nicode{~H} | |
2946 | @tab @nicode{char-numeric?} | |
2947 | @tab hour | |
2948 | @tab @nicode{date-hour} | |
2949 | ||
2950 | @item @nicode{~k} | |
2951 | @tab no skip | |
2952 | @tab hour, blank padded | |
2953 | @tab @nicode{date-hour} | |
2954 | ||
2955 | @item @nicode{~m} | |
2956 | @tab @nicode{char-numeric?} | |
2957 | @tab month | |
2958 | @tab @nicode{date-month} | |
2959 | ||
2960 | @item @nicode{~M} | |
2961 | @tab @nicode{char-numeric?} | |
2962 | @tab minute | |
2963 | @tab @nicode{date-minute} | |
2964 | ||
2965 | @item @nicode{~S} | |
2966 | @tab @nicode{char-numeric?} | |
2967 | @tab second | |
2968 | @tab @nicode{date-second} | |
2969 | ||
2970 | @item @nicode{~y} | |
2971 | @tab no skip | |
2972 | @tab 2-digit year | |
2973 | @tab @nicode{date-year} within 50 years | |
2974 | ||
2975 | @item @nicode{~Y} | |
2976 | @tab @nicode{char-numeric?} | |
2977 | @tab year | |
2978 | @tab @nicode{date-year} | |
2979 | ||
2980 | @item @nicode{~z} | |
2981 | @tab no skip | |
2982 | @tab time zone | |
2983 | @tab date-zone-offset | |
2984 | @end multitable | |
2985 | ||
2986 | Notice that the weekday matching forms don't affect the date object | |
2987 | returned, instead the weekday will be derived from the day, month and | |
2988 | year. | |
2989 | ||
2990 | Currently Guile doesn't implement any localizations for the above, | |
2991 | month and weekday names are always expected in English. This may | |
2992 | change in the future. | |
2993 | @end defun | |
12991fed | 2994 | |
1de8c1ae | 2995 | |
b0b55bd6 | 2996 | @node SRFI-26 |
1de8c1ae KR |
2997 | @section SRFI-26 - specializing parameters |
2998 | @cindex SRFI-26 | |
2999 | ||
3000 | This SRFI provides a syntax for conveniently specializing selected | |
3001 | parameters of a function. It can be used with, | |
3002 | ||
3003 | @example | |
3004 | (use-modules (srfi srfi-26)) | |
3005 | @end example | |
3006 | ||
3007 | @deffn {library syntax} cut slot @dots{} | |
3008 | @deffnx {library syntax} cute slot @dots{} | |
3009 | Return a new procedure which will make a call (@var{slot} @dots{}) but | |
3010 | with selected parameters specialized to given expressions. | |
3011 | ||
3012 | An example will illustrate the idea. The following is a | |
3013 | specialization of @code{write}, sending output to | |
3014 | @code{my-output-port}, | |
3015 | ||
3016 | @example | |
3017 | (cut write <> my-output-port) | |
3018 | @result{} | |
3019 | (lambda (obj) (write obj my-output-port)) | |
3020 | @end example | |
3021 | ||
3022 | The special symbol @code{<>} indicates a slot to be filled by an | |
3023 | argument to the new procedure. @code{my-output-port} on the other | |
3024 | hand is an expression to be evaluated and passed, ie.@: it specializes | |
3025 | the behaviour of @code{write}. | |
3026 | ||
3027 | @table @nicode | |
3028 | @item <> | |
3029 | A slot to be filled by an argument from the created procedure. | |
3030 | Arguments are assigned to @code{<>} slots in the order they appear in | |
3031 | the @code{cut} form, there's no way to re-arrange arguments. | |
3032 | ||
3033 | The first argument to @code{cut} is usually a procedure (or expression | |
3034 | giving a procedure), but @code{<>} is allowed there too. For example, | |
3035 | ||
3036 | @example | |
3037 | (cut <> 1 2 3) | |
3038 | @result{} | |
3039 | (lambda (proc) (proc 1 2 3)) | |
3040 | @end example | |
3041 | ||
3042 | @item <...> | |
3043 | A slot to be filled by all remaining arguments from the new procedure. | |
3044 | This can only occur at the end of a @code{cut} form. | |
3045 | ||
3046 | For example, a procedure taking a variable number of arguments like | |
3047 | @code{max} but in addition enforcing a lower bound, | |
3048 | ||
3049 | @example | |
3050 | (define my-lower-bound 123) | |
3051 | ||
3052 | (cut max my-lower-bound <...>) | |
3053 | @result{} | |
3054 | (lambda arglist (apply max my-lower-bound arglist)) | |
3055 | @end example | |
3056 | @end table | |
3057 | ||
3058 | For @code{cut} the specializing expressions are evaluated each time | |
3059 | the new procedure is called. For @code{cute} they're evaluated just | |
3060 | once, when the new procedure is created. The name @code{cute} stands | |
3061 | for ``@code{cut} with evaluated arguments''. In all cases the | |
3062 | evaluations take place in an unspecified order. | |
3063 | ||
3064 | The following illustrates the difference between @code{cut} and | |
3065 | @code{cute}, | |
3066 | ||
3067 | @example | |
3068 | (cut format <> "the time is ~s" (current-time)) | |
3069 | @result{} | |
3070 | (lambda (port) (format port "the time is ~s" (current-time))) | |
3071 | ||
3072 | (cute format <> "the time is ~s" (current-time)) | |
3073 | @result{} | |
3074 | (let ((val (current-time))) | |
3075 | (lambda (port) (format port "the time is ~s" val)) | |
3076 | @end example | |
3077 | ||
3078 | (There's no provision for a mixture of @code{cut} and @code{cute} | |
3079 | where some expressions would be evaluated every time but others | |
3080 | evaluated only once.) | |
3081 | ||
3082 | @code{cut} is really just a shorthand for the sort of @code{lambda} | |
3083 | forms shown in the above examples. But notice @code{cut} avoids the | |
3084 | need to name unspecialized parameters, and is more compact. Use in | |
3085 | functional programming style or just with @code{map}, @code{for-each} | |
3086 | or similar is typical. | |
3087 | ||
3088 | @example | |
3089 | (map (cut * 2 <>) '(1 2 3 4)) | |
3090 | ||
3091 | (for-each (cut write <> my-port) my-list) | |
3092 | @end example | |
3093 | @end deffn | |
b0b55bd6 | 3094 | |
12991fed TTN |
3095 | |
3096 | @c srfi-modules.texi ends here | |
193239f1 KR |
3097 | |
3098 | @c Local Variables: | |
3099 | @c TeX-master: "guile.texi" | |
3100 | @c End: |