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