* guile.texi: Commented out menu entry and inclusion of Tcl/Tk
[bpt/guile.git] / doc / srfi-modules.texi
1 @page
2 @node SRFI Support
3 @chapter SRFI Support Modules
4
5 SRFI is an acronym for Scheme Request For Implementation. The SRFI
6 documents define a lot of syntactic and procedure extensions to standard
7 Scheme as defined in R5RS.
8
9 Guile has support for a number of SRFIs. This chapter gives an overview
10 over the available SRFIs and some usage hints. For complete
11 documentation, design rationales and further examples, we advise you to
12 get the relevant SRFI documents from the SRFI home page
13 @url{http://srfi.schemers.org}.
14
15 @menu
16 * About SRFI Usage:: What to know about Guile's SRFI support.
17 * SRFI-0:: cond-expand
18 * SRFI-2:: and-let*.
19 * SRFI-6:: Basic String Ports.
20 * SRFI-8:: receive.
21 * SRFI-9:: define-record-type.
22 * SRFI-10:: Hash-Comma Reader Extension.
23 * SRFI-11:: let-values and let-values*.
24 * SRFI-13:: String library.
25 * SRFI-14:: Character-set library.
26 * SRFI-16:: case-lambda
27 * SRFI-17:: Generalized set!
28 @end menu
29
30
31 @node About SRFI Usage
32 @section About SRFI Usage
33
34 @c FIXME::martin: Review me!
35
36 SRFI support in Guile is currently implemented partly in the core
37 library, and partly as add-on modules. That means that some SRFIs are
38 automatically available when the interpreter is started, whereas the
39 other SRFIs require you to use the appropriate support module
40 explicitly.
41
42 There are several reasons for this inconsistency. First, the feature
43 checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be
44 available immediately, because it must be there when the user wants to
45 check for the Scheme implementation, that is, before she can know that
46 it is safe to use @code{use-modules} to load SRFI support modules. The
47 second reason is that some features defined in SRFIs had been
48 implemented in Guile before the developers started to add SRFI
49 implementations as modules (for example SRFI-6 (@pxref{SRFI-6})). In
50 the future, it is possible that SRFIs in the core library might be
51 factored out into separate modules, requiring explicit module loading
52 when they are needed. So you should be prepared to have to use
53 @code{use-modules} someday in the future to access SRFI-6 bindings. If
54 you want, you can do that already. We have included the module
55 @code{(srfi srfi-6)} in the distribution, which currently does nothing,
56 but ensures that you can write future-safe code.
57
58 Generally, support for a specific SRFI is made available by using
59 modules named @code{(srfi srfi-@var{number})}, where @var{number} is the
60 number of the SRFI needed. Another possibility is to use the command
61 line option @code{--use-srfi}, which will load the necessary modules
62 automatically (@pxref{Invoking Guile}).
63
64
65 @node SRFI-0
66 @section SRFI-0 - cond-expand
67
68 @c FIXME::martin: Review me!
69
70 SRFI-0 defines a means for checking whether a Scheme implementation has
71 support for a specified feature. The syntactic form @code{cond-expand},
72 which implements this means, has the following syntax.
73
74 @example
75 <cond-expand>
76 --> (cond-expand <cond-expand-clause>+)
77 | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
78 <cond-expand-clause>
79 --> (<feature-requirement> <command-or-definition>*)
80 <feature-requirement>
81 --> <feature-identifier>
82 | (and <feature-requirement>*)
83 | (or <feature-requirement>*)
84 | (not <feature-requirement>)
85 <feature-identifier>
86 --> <a symbol which is the name or alias of a SRFI>
87 @end example
88
89 When evaluated, this form checks all clauses in order, until it finds
90 one whose feature requirement is satisfied. Then the form expands into
91 the commands or definitions in the clause. A requirement is tested as
92 follows:
93
94 @itemize @bullet
95 @item
96 If it is a symbol, it is satisfied if the feature identifier is
97 supported.
98
99 @item
100 If it is an @code{and} form, all requirements must be satisfied. If no
101 requirements are given, it is satisfied, too.
102
103 @item
104 If it is an @code{or} form, at least one of the requirements must be
105 satisfied. If no requirements are given, it is not satisfied.
106
107 @item
108 If it is a @code{not} form, the feature requirement must @emph{not} be
109 satisfied.
110
111 @item
112 If the feature requirement is the keyword @code{else} and it is the last
113 clause, it is satisfied if no prior clause matched.
114 @end itemize
115
116 If no clause is satisfied, an error is signalled.
117
118 Since @code{cond-expand} is needed to tell what a Scheme implementation
119 provides, it must be accessible without using any
120 implementation-dependant operations, such as @code{use-modules} in
121 Guile. Thus, it is not necessary to use any module to get access to
122 this form.
123
124 Currently, the feature identifiers @code{guile}, @code{r5rs} and
125 @code{srfi-0} are supported. The other SRFIs are not in that list by
126 default, because the SRFI modules must be explicitly used before their
127 exported bindings can be used.
128
129 So if a Scheme program wishes to use SRFI-8, it has two possibilities:
130 First, it can check whether the running Scheme implementation is Guile,
131 and if it is, it can use the appropriate module:
132
133 @lisp
134 (cond-expand
135 (guile
136 (use-modules (srfi srfi-8)))
137 (srfi-8
138 #t))
139 ;; otherwise fail.
140 @end lisp
141
142 The other possibility is to use the @code{--use-srfi} command line
143 option when invoking Guile (@pxref{Invoking Guile}). When you do that,
144 the specified SRFI support modules will be loaded and add their feature
145 identifier to the list of symbols checked by @code{cond-expand}.
146
147 So, if you invoke Guile like this:
148
149 @example
150 $ guile --use-srfi=8
151 @end example
152
153 the following snippet will expand to @code{'hooray}.
154
155 @lisp
156 (cond-expand (srfi-8 'hooray))
157 @end lisp
158
159
160 @node SRFI-2
161 @section SRFI-2 - and-let*
162
163 @c FIXME::martin: Review me!
164
165 The syntactic form @code{and-let*} combines the conditional evaluation
166 form @code{and} with the binding form @var{let*}. Each argument
167 expression will be evaluated sequentially, bound to a variable (if a
168 variable name is given), but only as long as no expression returns
169 the false value @code{#f}.
170
171 Use @code{(use-modules (srfi srfi-2)} to access this syntax form.
172
173 A short example will demonstrate how it works. In the first expression,
174 @var{x} will get bound to 1, but the next expression (@code{#f}) is
175 false, so evaluation of the form is stopped, and @code{#f} is returned.
176 In the next expression, @var{x} is bound to 1, @var{y} is bound to
177 @code{#t} and since no expression in the binding section was false, the
178 body of the @code{and-let*} expression is evaluated, which in this case
179 returns the value of @var{x}.
180
181 @lisp
182 (and-let* ((x 1) (y #f)) 42)
183 @result{}
184 #f
185 (and-let* ((x 1) (y #t)) x)
186 @result{}
187 1
188 @end lisp
189
190
191 @node SRFI-6
192 @section SRFI-6 - Basic String Ports
193
194 SRFI-6 defines the procedures @code{open-input-string},
195 @code{open-output-string} and @code{get-output-string}. These
196 procedures are included in the Guile core, so using this module does not
197 make any difference at the moment. But it is possible that support for
198 SRFI-6 will be factored out of the core library in the future, so using
199 this module does not hurt, after all.
200
201 @node SRFI-8
202 @section SRFI-8 - receive
203
204 @code{receive} is a syntax for making the handling of multiple-value
205 procedures easier. It is documented in @xref{Multiple Values}.
206
207
208 @node SRFI-9
209 @section SRFI-9 - define-record-type
210
211 This is the SRFI way for defining record types. The Guile
212 implementation is a layer above Guile's normal record construction
213 procedures (@pxref{Records}). The nice thing about this kind of record
214 definition method is that no new names are implicitly created, all
215 constructor, accessor and predicates are explicitly given. This reduces
216 the risk of variable capture.
217
218 The syntax of a record type definition is:
219
220 @example
221 <record type definition>
222 -> (define-record-type <type name>
223 (<constructor name> <field tag> ...)
224 <predicate name>
225 <field spec> ...)
226 <field spec> -> (<field tag> <accessor name>)
227 -> (<field tag> <accessor name> <modifier name>)
228 <field tag> -> <identifier>
229 <... name> -> <identifier>
230 @end example
231
232 Usage example:
233
234 @example
235 guile> (use-modules (srfi srfi-9))
236 guile> (define-record-type :foo (make-foo x) foo?
237 (x get-x) (y get-y set-y!))
238 guile> (define f (make-foo 1))
239 guile> f
240 #<:foo x: 1 y: #f>
241 guile> (get-x f)
242 1
243 guile> (set-y! f 2)
244 2
245 guile> (get-y f)
246 2
247 guile> f
248 #<:foo x: 1 y: 2>
249 guile> (foo? f)
250 #t
251 guile> (foo? 1)
252 #f
253 @end example
254
255
256 @node SRFI-10
257 @section SRFI-10 - Hash-Comma Reader Extension
258
259 @cindex hash-comma
260 @cindex #,()
261 The module @code{(srfi srfi-10)} implements the syntax extension
262 @code{#,()}, also called hash-comma, which is defined in SRFI-10.
263
264 The support for SRFI-10 consists of the procedure
265 @code{define-reader-ctor} for defining new reader constructors and the
266 read syntax form
267
268 @example
269 #,(@var{ctor} @var{datum} ...)
270 @end example
271
272 where @var{ctor} must be a symbol for which a read constructor was
273 defined previouly, using @code{define-reader-ctor}.
274
275 Example:
276
277 @lisp
278 (define-reader-ctor 'file open-input-file)
279 (define f '#,(file "/etc/passwd"))
280 (read-line f)
281 @result{}
282 "root:x:0:0:root:/root:/bin/bash"
283 @end lisp
284
285 Please note the quote before the @code{#,(file ...)} expression. This
286 is necessary because ports are not self-evaluating in Guile.
287
288 @deffn procedure define-reader-ctor symbol proc
289 Define @var{proc} as the reader constructor for hash-comma forms with a
290 tag @var{symbol}. @var{proc} will be applied to the datum(s) following
291 the tag in the hash-comma expression after the complete form has been
292 read in. The result of @var{proc} is returned by the Scheme reader.
293 @end deffn
294
295
296 @node SRFI-11
297 @section SRFI-11 - let-values
298
299 This module implements the binding forms for multiple values
300 @code{let-values} and @code{let-values*}. These forms are similar to
301 @code{let} and @code{let*} (@pxref{Local Bindings}), but they support
302 binding of the values returned by multiple-valued expressions.
303
304 Write @code{(use-modules (srfi srfi-11))} to make the bindings
305 available.
306
307 @lisp
308 (let-values (((x y) (values 1 2))
309 ((z f) (values 3 4)))
310 (+ x y z f))
311 @result{}
312 10
313 @end lisp
314
315 @code{let-values} performs all bindings simultaneously, which means that
316 no expression in the binding clauses may refer to variables bound in the
317 same clause list. @code{let-values*}, on the other hand, performs the
318 bindings sequentially, just like @code{let*} does for single-valued
319 expressions.
320
321
322 @node SRFI-13
323 @section SRFI-13 - String Library
324
325 In this section, we will describe all procedures defined in SRFI-13
326 (string library) and implemented by the module @code{(srfi srfi-13)}.
327
328 Note that only the procedures from SRFI-13 are documented here which are
329 not already contained in Guile. For procedures not documented here
330 please refer to the relevant chapters in the Guile Reference Manual, for
331 example the documentation of strings and string procedures
332 (@pxref{Strings}).
333
334 All of the procedures defined in SRFI-13, which are not already included
335 in the Guile core library, are implemented in the module @code{(srfi
336 srfi-13)}. The procedures which are both in Guile and in SRFI-13, but
337 which are slightly extended, have been implemented in this module, and
338 the bindings overwrite those in the Guile core.
339
340 The procedures which are defined in the section @emph{Low-level
341 procedures} of SRFI-13 for parsing optional string indices, substring
342 specification checking and Knuth-Morris-Pratt-Searching are not
343 implemented.
344
345 The procedures @code{string-contains} and @code{string-contains-ci} are
346 not implemented very efficiently at the moment. This will be changed as
347 soon as possible.
348
349 @menu
350 * Loading SRFI-13:: How to load SRFI-13 support.
351 * SRFI-13 Predicates:: String predicates.
352 * SRFI-13 Constructors:: String constructing procedures.
353 * SRFI-13 List/String Conversion:: Conversion from/to lists.
354 * SRFI-13 Selection:: Selection portions of strings.
355 * SRFI-13 Modification:: Modfify strings in-place.
356 * SRFI-13 Comparison:: Compare strings.
357 * SRFI-13 Prefixes/Suffixes:: Detect common pre-/suffixes.
358 * SRFI-13 Searching:: Searching for substrings.
359 * SRFI-13 Case Mapping:: Mapping to lower-/upper-case.
360 * SRFI-13 Reverse/Append:: Reverse and append strings.
361 * SRFI-13 Fold/Unfold/Map:: Construct/deconstruct strings.
362 * SRFI-13 Replicate/Rotate:: Replacate and rotate portions of strings.
363 * SRFI-13 Miscellaneous:: Left-over string procedures.
364 * SRFI-13 Filtering/Deleting:: Filter and delete characters from strings.
365 @end menu
366
367
368 @node Loading SRFI-13
369 @subsection Loading SRFI-13
370
371 When Guile is properly installed, SRFI-13 support can be loaded into a
372 running Guile by using the @code{(srfi srfi-13)} module.
373
374 @example
375 $ guile
376 guile> (use-modules (srfi srfi-13))
377 guile>
378 @end example
379
380 When this step causes any errors, Guile is not properly installed.
381
382 One possible reason is that Guile cannot find either the Scheme module
383 file @file{srfi-13.scm}, or it cannot find the shared object file
384 @file{libguile-srfi-srfi-13-14.so}. Make sure that the former is in the
385 Guile load path and that the latter is either installed in some default
386 location like @file{/usr/local/lib} or that the directory it was
387 installed to is in your @code{LTDL_LIBRARY_PATH}. The same applies to
388 @file{srfi-14.scm}.
389
390 Now you can test whether the SRFI-13 procedures are working by calling
391 the @code{string-concatenate} procedure.
392
393 @example
394 guile> (string-concatenate '("Hello" " " "World!"))
395 "Hello World!"
396 @end example
397
398 @node SRFI-13 Predicates
399 @subsection Predicates
400
401 In addition to the primitives @code{string?} and @code{string-null?},
402 which are already in the Guile core, the string predicates
403 @code{string-any} and @code{string-every} are defined by SRFI-13.
404
405 @deffn primitive string-any pred s [start end]
406 Check if the predicate @var{pred} is true for any character in
407 the string @var{s}, proceeding from left (index @var{start}) to
408 right (index @var{end}). If @code{string-any} returns true,
409 the returned true value is the one produced by the first
410 successful application of @var{pred}.
411 @end deffn
412
413 @deffn primitive string-every pred s [start end]
414 Check if the predicate @var{pred} is true for every character
415 in the string @var{s}, proceeding from left (index @var{start})
416 to right (index @var{end}). If @code{string-every} returns
417 true, the returned true value is the one produced by the final
418 application of @var{pred} to the last character of @var{s}.
419 @end deffn
420
421
422 @c ===================================================================
423
424 @node SRFI-13 Constructors
425 @subsection Constructors
426
427 SRFI-13 defines several procedures for constructing new strings. In
428 addition to @code{make-string} and @code{string} (available in the Guile
429 core library), the procedure @code{string-tabulate} does exist.
430
431 @deffn primitive string-tabulate proc len
432 @var{proc} is an integer->char procedure. Construct a string
433 of size @var{len} by applying @var{proc} to each index to
434 produce the corresponding string element. The order in which
435 @var{proc} is applied to the indices is not specified.
436 @end deffn
437
438
439 @c ===================================================================
440
441 @node SRFI-13 List/String Conversion
442 @subsection List/String Conversion
443
444 The procedure @code{string->list} is extended by SRFI-13, that is why it
445 is included in @code{(srfi srfi-13)}. The other procedures are new.
446 The Guile core already contains the procedure @code{list->string} for
447 converting a list of characters into a string (@pxref{List/String
448 Conversion}).
449
450 @deffn primitive string->list str [start end]
451 Convert the string @var{str} into a list of characters.
452 @end deffn
453
454 @deffn primitive reverse-list->string chrs
455 An efficient implementation of @code{(compose string->list
456 reverse)}:
457
458 @smalllisp
459 (reverse-list->string '(#\a #\B #\c)) @result{} "cBa"
460 @end smalllisp
461 @end deffn
462
463 @deffn primitive string-join ls [delimiter grammar]
464 Append the string in the string list @var{ls}, using the string
465 @var{delim} as a delimiter between the elements of @var{ls}.
466 @var{grammar} is a symbol which specifies how the delimiter is
467 placed between the strings, and defaults to the symbol
468 @code{infix}.
469
470 @table @code
471 @item infix
472 Insert the separator between list elements. An empty string
473 will produce an empty list.
474
475 @item string-infix
476 Like @code{infix}, but will raise an error if given the empty
477 list.
478
479 @item suffix
480 Insert the separator after every list element.
481
482 @item prefix
483 Insert the separator before each list element.
484 @end table
485 @end deffn
486
487
488 @c ===================================================================
489
490 @node SRFI-13 Selection
491 @subsection Selection
492
493 These procedures are called @dfn{selectors}, because they access
494 information about the string or select pieces of a given string.
495
496 Additional selector procedures are documented in the Strings section
497 (@pxref{String Selection}), like @code{string-length} or
498 @code{string-ref}.
499
500 @code{string-copy} is also available in core Guile, but this version
501 accepts additional start/end indices.
502
503 @deffn primitive string-copy str [start end]
504 Return a freshly allocated copy of the string @var{str}. If
505 given, @var{start} and @var{end} delimit the portion of
506 @var{str} which is copied.
507 @end deffn
508
509 @deffn primitive substring/shared str start [end]
510 Like @code{substring}, but the result may share memory with the
511 argument @var{str}.
512 @end deffn
513
514 @deffn primitive string-copy! target tstart s [start end]
515 Copy the sequence of characters from index range [@var{start},
516 @var{end}) in string @var{s} to string @var{target}, beginning
517 at index @var{tstart}. The characters are copied left-to-right
518 or right-to-left as needed - the copy is guaranteed to work,
519 even if @var{target} and @var{s} are the same string. It is an
520 error if the copy operation runs off the end of the target
521 string.
522 @end deffn
523
524 @deffn primitive string-take s n
525 @deffnx primitive string-take-right s n
526 Return the @var{n} first/last characters of @var{s}.
527 @end deffn
528
529 @deffn primitive string-drop s n
530 @deffnx primitive string-drop-right s n
531 Return all but the first/last @var{n} characters of @var{s}.
532 @end deffn
533
534 @deffn primitive string-pad s len [chr start end]
535 @deffnx primitive string-pad-right s len [chr start end]
536 Take that characters from @var{start} to @var{end} from the
537 string @var{s} and return a new string, right(left)-padded by the
538 character @var{chr} to length @var{len}. If the resulting
539 string is longer than @var{len}, it is truncated on the right (left).
540 @end deffn
541
542 @deffn primitive string-trim s [char_pred start end]
543 @deffnx primitive string-trim-right s [char_pred start end]
544 @deffnx primitive string-trim-both s [char_pred start end]
545 Trim @var{s} by skipping over all characters on the left/right/both
546 sides of the string that satisfy the parameter @var{char_pred}:
547
548 @itemize @bullet
549 @item
550 if it is the character @var{ch}, characters equal to
551 @var{ch} are trimmed,
552
553 @item
554 if it is a procedure @var{pred} characters that
555 satisfy @var{pred} are trimmed,
556
557 @item
558 if it is a character set, characters in that set are trimmed.
559 @end itemize
560
561 If called without a @var{char_pred} argument, all whitespace is
562 trimmed.
563 @end deffn
564
565
566 @c ===================================================================
567
568 @node SRFI-13 Modification
569 @subsection Modification
570
571 The procedure @code{string-fill!} is extended from R5RS because it
572 accepts optional start/end indices. This bindings shadows the procedure
573 of the same name in the Guile core. The second modification procedure
574 @code{string-set!} is documented in the Strings section (@pxref{String
575 Modification}).
576
577 @deffn primitive string-fill! str chr [start end]
578 Stores @var{chr} in every element of the given @var{str} and
579 returns an unspecified value.
580 @end deffn
581
582
583 @c ===================================================================
584
585 @node SRFI-13 Comparison
586 @subsection Comparison
587
588 The procedures in this section are used for comparing strings in
589 different ways. The comparison predicates differ from those in R5RS in
590 that they do not only return @code{#t} or @code{#f}, but the mismatch
591 index in the case of a true return value.
592
593 @code{string-hash} and @code{string-hash-ci} are for calculating hash
594 values for strings, useful for implementing fast lookup mechanisms.
595
596 @deffn primitive string-compare s1 s2 proc_lt proc_eq proc_gt [start1 end1 start2 end2]
597 @deffnx primitive string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 end1 start2 end2]
598 Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
599 mismatch index, depending upon whether @var{s1} is less than,
600 equal to, or greater than @var{s2}. The mismatch index is the
601 largest index @var{i} such that for every 0 <= @var{j} <
602 @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] - that is,
603 @var{i} is the first position that does not match. The
604 character comparison is done case-insensitively.
605 @end deffn
606
607 @deffn primitive string= s1 s2 [start1 end1 start2 end2]
608 @deffnx primitive string<> s1 s2 [start1 end1 start2 end2]
609 @deffnx primitive string< s1 s2 [start1 end1 start2 end2]
610 @deffnx primitive string> s1 s2 [start1 end1 start2 end2]
611 @deffnx primitive string<= s1 s2 [start1 end1 start2 end2]
612 @deffnx primitive string>= s1 s2 [start1 end1 start2 end2]
613 Compare @var{s1} and @var{s2} and return @code{#f} if the predicate
614 fails. Otherwise, the mismatch index is returned (or @var{end1} in the
615 case of @code{string=}.
616 @end deffn
617
618 @deffn primitive string-ci= s1 s2 [start1 end1 start2 end2]
619 @deffnx primitive string-ci<> s1 s2 [start1 end1 start2 end2]
620 @deffnx primitive string-ci< s1 s2 [start1 end1 start2 end2]
621 @deffnx primitive string-ci> s1 s2 [start1 end1 start2 end2]
622 @deffnx primitive string-ci<= s1 s2 [start1 end1 start2 end2]
623 @deffnx primitive string-ci>= s1 s2 [start1 end1 start2 end2]
624 Compare @var{s1} and @var{s2} and return @code{#f} if the predicate
625 fails. Otherwise, the mismatch index is returned (or @var{end1} in the
626 case of @code{string=}. These are the case-insensitive variants.
627 @end deffn
628
629 @deffn primitive string-hash s [bound start end]
630 @deffnx primitive string-hash-ci s [bound start end]
631 Return a hash value of the string @var{s} in the range 0 @dots{}
632 @var{bound} - 1. @code{string-hash-ci} is the case-insensitive variant.
633 @end deffn
634
635
636 @c ===================================================================
637
638 @node SRFI-13 Prefixes/Suffixes
639 @subsection Prefixes/Suffixes
640
641 Using these procedures you can determine whether a given string is a
642 prefix or suffix of another string or how long a common prefix/suffix
643 is.
644
645 @deffn primitive string-prefix-length s1 s2 [start1 end1 start2 end2]
646 @deffnx primitive string-prefix-length-ci s1 s2 [start1 end1 start2 end2]
647 @deffnx primitive string-suffix-length s1 s2 [start1 end1 start2 end2]
648 @deffnx primitive string-suffix-length-ci s1 s2 [start1 end1 start2 end2]
649 Return the length of the longest common prefix/suffix of the two
650 strings. @code{string-prefix-length-ci} and
651 @code{string-suffix-length-ci} are the case-insensitive variants.
652 @end deffn
653
654 @deffn primitive string-prefix? s1 s2 [start1 end1 start2 end2]
655 @deffnx primitive string-prefix-ci? s1 s2 [start1 end1 start2 end2]
656 @deffnx primitive string-suffix? s1 s2 [start1 end1 start2 end2]
657 @deffnx primitive string-suffix-ci? s1 s2 [start1 end1 start2 end2]
658 Is @var{s1} a prefix/suffix of @var{s2}. @code{string-prefix-ci?} and
659 @code{string-suffix-ci?} are the case-insensitive variants.
660 @end deffn
661
662
663 @c ===================================================================
664
665 @node SRFI-13 Searching
666 @subsection Searching
667
668 Use these procedures to find out whether a string contains a given
669 character or a given substring, or a character from a set of characters.
670
671 @deffn primitive string-index s char_pred [start end]
672 @deffnx primitive string-index-right s char_pred [start end]
673 Search through the string @var{s} from left to right (right to left),
674 returning the index of the first (last) occurence of a character which
675
676 @itemize @bullet
677 @item
678 equals @var{char_pred}, if it is character,
679
680 @item
681 satisifies the predicate @var{char_pred}, if it is a
682 procedure,
683
684 @item
685 is in the set @var{char_pred}, if it is a character set.
686 @end itemize
687 @end deffn
688
689 @deffn primitive string-skip s char_pred [start end]
690 @deffnx primitive string-skip-right s char_pred [start end]
691 Search through the string @var{s} from left to right (right to left),
692 returning the index of the first (last) occurence of a character which
693
694 @itemize @bullet
695 @item
696 does not equal @var{char_pred}, if it is character,
697
698 @item
699 does not satisify the predicate @var{char_pred}, if it is
700 a procedure.
701
702 @item
703 is not in the set if @var{char_pred} is a character set.
704 @end itemize
705 @end deffn
706
707 @deffn primitive string-count s char_pred [start end]
708 Return the count of the number of characters in the string
709 @var{s} which
710
711 @itemize @bullet
712 @item
713 equals @var{char_pred}, if it is character,
714
715 @item
716 satisifies the predicate @var{char_pred}, if it is a procedure.
717
718 @item
719 is in the set @var{char_pred}, if it is a character set.
720 @end itemize
721 @end deffn
722
723 @deffn primitive string-contains s1 s2 [start1 end1 start2 end2]
724 @deffnx primitive string-contains-ci s1 s2 [start1 end1 start2 end2]
725 Does string @var{s1} contain string @var{s2}? Return the index
726 in @var{s1} where @var{s2} occurs as a substring, or false.
727 The optional start/end indices restrict the operation to the
728 indicated substrings.
729
730 @code{string-contains-ci} is the case-insensitive variant.
731 @end deffn
732
733
734 @c ===================================================================
735
736 @node SRFI-13 Case Mapping
737 @subsection Alphabetic Case Mapping
738
739 These procedures convert the alphabetic case of strings. They are
740 similar to the procedures in the Guile core, but are extended to handle
741 optional start/end indices.
742
743 @deffn primitive string-upcase s [start end]
744 @deffnx primitive string-upcase! s [start end]
745 Upcase every character in @var{s}. @code{string-upcase!} is the
746 side-effecting variant.
747 @end deffn
748
749 @deffn primitive string-downcase s [start end]
750 @deffnx primitive string-downcase! s [start end]
751 Downcase every character in @var{s}. @code{string-downcase!} is the
752 side-effecting variant.
753 @end deffn
754
755 @deffn primitive string-titlecase s [start end]
756 @deffnx primitive string-titlecase! s [start end]
757 Upcase every first character in every word in @var{s}, downcase the
758 other characters. @code{string-titlecase!} is the side-effecting
759 variant.
760 @end deffn
761
762
763 @c ===================================================================
764
765 @node SRFI-13 Reverse/Append
766 @subsection Reverse/Append
767
768 One appending procedure, @code{string-append} is the same in R5RS and in
769 SRFI-13, so it is not redefined.
770
771 @deffn primitive string-reverse str [start end]
772 @deffnx primitive string-reverse! str [start end]
773 Reverse the string @var{str}. The optional arguments
774 @var{start} and @var{end} delimit the region of @var{str} to
775 operate on.
776
777 @code{string-reverse!} modifies the argument string and returns an
778 unspecified value.
779 @end deffn
780
781 @deffn primitive string-append/shared ls @dots{}
782 Like @code{string-append}, but the result may share memory
783 with the argument strings.
784 @end deffn
785
786 @deffn primitive string-concatenate ls
787 Append the elements of @var{ls} (which must be strings)
788 together into a single string. Guaranteed to return a freshly
789 allocated string.
790 @end deffn
791
792 @deffn primitive string-concatenate/shared ls
793 Like @code{string-concatenate}, but the result may share memory
794 with the strings in the list @var{ls}.
795 @end deffn
796
797 @deffn primitive string-concatenate-reverse ls final_string end
798 Without optional arguments, this procedure is equivalent to
799
800 @smalllisp
801 (string-concatenate (reverse ls))
802 @end smalllisp
803
804 If the optional argument @var{final_string} is specified, it is
805 consed onto the beginning to @var{ls} before performing the
806 list-reverse and string-concatenate operations. If @var{end}
807 is given, only the characters of @var{final_string} up to index
808 @var{end} are used.
809
810 Guaranteed to return a freshly allocated string.
811 @end deffn
812
813 @deffn primitive string-concatenate-reverse/shared ls final_string end
814 Like @code{string-concatenate-reverse}, but the result may
815 share memory with the the strings in the @var{ls} arguments.
816 @end deffn
817
818
819 @c ===================================================================
820
821 @node SRFI-13 Fold/Unfold/Map
822 @subsection Fold/Unfold/Map
823
824 @code{string-map}, @code{string-for-each} etc. are for iterating over
825 the characters a string is composed of. The fold and unfold procedures
826 are list iterators and constructors.
827
828 @deffn primitive string-map proc s [start end]
829 @var{proc} is a char->char procedure, it is mapped over
830 @var{s}. The order in which the procedure is applied to the
831 string elements is not specified.
832 @end deffn
833
834 @deffn primitive string-map! proc s [start end]
835 @var{proc} is a char->char procedure, it is mapped over
836 @var{s}. The order in which the procedure is applied to the
837 string elements is not specified. The string @var{s} is
838 modified in-place, the return value is not specified.
839 @end deffn
840
841 @deffn primitive string-fold kons knil s [start end]
842 @deffnx primitive string-fold-right kons knil s [start end]
843 Fold @var{kons} over the characters of @var{s}, with @var{knil} as the
844 terminating element, from left to right (or right to left, for
845 @code{string-fold-right}). @var{kons} must expect two arguments: The
846 actual character and the last result of @var{kons}' application.
847 @end deffn
848
849 @deffn primitive string-unfold p f g seed [base make_final]
850 @deffnx primitive string-unfold-right p f g seed [base make_final]
851 These are the fundamental string constructors.
852 @itemize @bullet
853 @item @var{g} is used to generate a series of @emph{seed}
854 values from the initial @var{seed}: @var{seed}, (@var{g}
855 @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
856 @dots{}
857 @item @var{p} tells us when to stop - when it returns true
858 when applied to one of these seed values.
859 @item @var{f} maps each seed value to the corresponding
860 character in the result string. These chars are assembled into the
861 string in a left-to-right (right-to-left) order.
862 @item @var{base} is the optional initial/leftmost (rightmost)
863 portion of the constructed string; it default to the empty string.
864 @item @var{make_final} is applied to the terminal seed
865 value (on which @var{p} returns true) to produce the final/rightmost
866 (leftmost) portion of the constructed string. It defaults to
867 @code{(lambda (x) "")}.
868 @end itemize
869 @end deffn
870
871 @deffn primitive string-for-each proc s [start end]
872 @var{proc} is mapped over @var{s} in left-to-right order. The
873 return value is not specified.
874 @end deffn
875
876
877 @c ===================================================================
878
879 @node SRFI-13 Replicate/Rotate
880 @subsection Replicate/Rotate
881
882 These procedures are special substring procedures, which can also be
883 used for replicating strings. They are a bit tricky to use, but
884 consider this code fragment, which replicates the input string
885 @code{"foo"} so often that the resulting string has a length of six.
886
887 @lisp
888 (xsubstring "foo" 0 6)
889 @result{}
890 "foofoo"
891 @end lisp
892
893 @deffn primitive xsubstring s from [to start end]
894 This is the @emph{extended substring} procedure that implements
895 replicated copying of a substring of some string.
896
897 @var{s} is a string, @var{start} and @var{end} are optional
898 arguments that demarcate a substring of @var{s}, defaulting to
899 0 and the length of @var{s}. Replicate this substring up and
900 down index space, in both the positive and negative directions.
901 @code{xsubstring} returns the substring of this string
902 beginning at index @var{from}, and ending at @var{to}, which
903 defaults to @var{from} + (@var{end} - @var{start}).
904 @end deffn
905
906 @deffn primitive string-xcopy! target tstart s sfrom [sto start end]
907 Exactly the same as @code{xsubstring}, but the extracted text
908 is written into the string @var{target} starting at index
909 @var{tstart}. The operation is not defined if @code{(eq?
910 @var{target} @var{s})} or these arguments share storage - you
911 cannot copy a string on top of itself.
912 @end deffn
913
914
915 @c ===================================================================
916
917 @node SRFI-13 Miscellaneous
918 @subsection Miscellaneous
919
920 @code{string-replace} is for replacing a portion of a string with
921 another string and @code{string-tokenize} splits a string into a list of
922 strings, breaking it up at a specified character.
923
924 @deffn primitive string-replace s1 s2 [start1 end1 start2 end2]
925 Return the string @var{s1}, but with the characters
926 @var{start1} @dots{} @var{end1} replaced by the characters
927 @var{start2} @dots{} @var{end2} from @var{s2}.
928 @end deffn
929
930 @deffn primitive string-tokenize s [token_char start end]
931 Split the string @var{s} into a list of substrings, where each
932 substring is a maximal non-empty contiguous sequence of
933 characters equal to the character @var{token_char}, or
934 whitespace, if @var{token_char} is not given. If
935 @var{token_char} is a character set, it is used for finding the
936 token borders.
937 @end deffn
938
939
940 @c ===================================================================
941
942 @node SRFI-13 Filtering/Deleting
943 @subsection Filtering/Deleting
944
945 @dfn{Filtering} means to remove all characters from a string which do
946 not match a given criteria, @dfn{deleting} means the opposite.
947
948 @deffn primitive string-filter s char_pred [start end]
949 Filter the string @var{s}, retaining only those characters that
950 satisfy the @var{char_pred} argument. If the argument is a
951 procedure, it is applied to each character as a predicate, if
952 it is a character, it is tested for equality and if it is a
953 character set, it is tested for membership.
954 @end deffn
955
956 @deffn primitive string-delete s char_pred [start end]
957 Filter the string @var{s}, retaining only those characters that
958 do not satisfy the @var{char_pred} argument. If the argument
959 is a procedure, it is applied to each character as a predicate,
960 if it is a character, it is tested for equality and if it is a
961 character set, it is tested for membership.
962 @end deffn
963
964
965 @node SRFI-14
966 @section SRFI-14 - Character-set Library
967
968 SRFI-14 defines the data type @dfn{character set}, and also defines a
969 lot of procedures for handling this character type, and a few standard
970 character sets like whitespace, alphabetic characters and others.
971
972 All procedures from SRFI-14 (character-set library) are implemented in
973 the module @code{(srfi srfi-14)}, as well as the standard variables
974 @code{char-set:letter}, @code{char-set:digit} etc.
975
976 @menu
977 * Loading SRFI-14:: How to make charsets available.
978 * SRFI-14 Character Set Data Type:: Underlying data type for charsets.
979 * SRFI-14 Predicates/Comparison:: Charset predicates.
980 * SRFI-14 Iterating Over Character Sets:: Enumerate charset elements.
981 * SRFI-14 Creating Character Sets:: Makeing new charsets.
982 * SRFI-14 Querying Character Sets:: Test charsets for membership etc.
983 * SRFI-14 Character-Set Algebra:: Calculating new charsets.
984 * SRFI-14 Standard Character Sets:: Variables containing predefined charsets.
985 @end menu
986
987
988 @node Loading SRFI-14
989 @subsection Loading SRFI-14
990
991 When Guile is properly installed, SRFI-14 support can be loaded into a
992 running Guile by using the @code{(srfi srfi-14)} module.
993
994 @example
995 $ guile
996 guile> (use-modules (srfi srfi-14))
997 guile> (char-set-union (char-set #\f #\o #\o) (string->char-set "bar"))
998 #<charset @{#\a #\b #\f #\o #\r@}>
999 guile>
1000 @end example
1001
1002
1003 @node SRFI-14 Character Set Data Type
1004 @subsection Character Set Data Type
1005
1006 The data type @dfn{charset} implements sets of characters
1007 (@pxref{Characters}). Because the internal representation of character
1008 sets is not visible to the user, a lot of procedures for handling them
1009 are provided.
1010
1011 Character sets can be created, extended, tested for the membership of a
1012 characters and be compared to other character sets.
1013
1014 The Guile implementation of character sets deals with 8-bit characters.
1015 In the standard variables, only the ASCII part of the character range is
1016 really used, so that for example @dfn{Umlaute} and other accented
1017 characters are not considered to be letters. In the future, as Guile
1018 may get support for international character sets, this will change, so
1019 don't rely on these ``features''.
1020
1021
1022 @c ===================================================================
1023
1024 @node SRFI-14 Predicates/Comparison
1025 @subsection Predicates/Comparison
1026
1027 Use these procedures for testing whether an object is a character set,
1028 or whether several character sets are equal or subsets of each other.
1029 @code{char-set-hash} can be used for calculating a hash value, maybe for
1030 usage in fast lookup procedures.
1031
1032 @deffn primitive char-set? obj
1033 Return @code{#t} if @var{obj} is a character set, @code{#f}
1034 otherwise.
1035 @end deffn
1036
1037 @deffn primitive char-set= cs1 @dots{}
1038 Return @code{#t} if all given character sets are equal.
1039 @end deffn
1040
1041 @deffn primitive char-set<= cs1 @dots{}
1042 Return @code{#t} if every character set @var{cs}i is a subset
1043 of character set @var{cs}i+1.
1044 @end deffn
1045
1046 @deffn primitive char-set-hash cs [bound]
1047 Compute a hash value for the character set @var{cs}. If
1048 @var{bound} is given and not @code{#f}, it restricts the
1049 returned value to the range 0 @dots{} @var{bound - 1}.
1050 @end deffn
1051
1052
1053 @c ===================================================================
1054
1055 @node SRFI-14 Iterating Over Character Sets
1056 @subsection Iterating Over Character Sets
1057
1058 Character set cursors are a means for iterating over the members of a
1059 character sets. After creating a character set cursor with
1060 @code{char-set-cursor}, a cursor can be dereferenced with
1061 @code{char-set-ref}, advanced to the next member with
1062 @code{char-set-cursor-next}. Whether a cursor has passed past the last
1063 element of the set can be checked with @code{end-of-char-set?}.
1064
1065 Additionally, mapping and (un-)folding procedures for character sets are
1066 provided.
1067
1068 @deffn primitive char-set-cursor cs
1069 Return a cursor into the character set @var{cs}.
1070 @end deffn
1071
1072 @deffn primitive char-set-ref cs cursor
1073 Return the character at the current cursor position
1074 @var{cursor} in the character set @var{cs}. It is an error to
1075 pass a cursor for which @code{end-of-char-set?} returns true.
1076 @end deffn
1077
1078 @deffn primitive char-set-cursor-next cs cursor
1079 Advance the character set cursor @var{cursor} to the next
1080 character in the character set @var{cs}. It is an error if the
1081 cursor given satisfies @code{end-of-char-set?}.
1082 @end deffn
1083
1084 @deffn primitive end-of-char-set? cursor
1085 Return @code{#t} if @var{cursor} has reached the end of a
1086 character set, @code{#f} otherwise.
1087 @end deffn
1088
1089 @deffn primitive char-set-fold kons knil cs
1090 Fold the procedure @var{kons} over the character set @var{cs},
1091 initializing it with @var{knil}.
1092 @end deffn
1093
1094 @deffn primitive char-set-unfold p f g seed [base_cs]
1095 @deffnx primitive char-set-unfold! p f g seed base_cs
1096 This is a fundamental constructor for character sets.
1097 @itemize @bullet
1098 @item @var{g} is used to generate a series of ``seed'' values
1099 from the initial seed: @var{seed}, (@var{g} @var{seed}),
1100 (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
1101 @item @var{p} tells us when to stop -- when it returns true
1102 when applied to one of the seed values.
1103 @item @var{f} maps each seed value to a character. These
1104 characters are added to the base character set @var{base_cs} to
1105 form the result; @var{base_cs} defaults to the empty set.
1106 @end itemize
1107
1108 @code{char-set-unfold!} is the side-effecting variant.
1109 @end deffn
1110
1111 @deffn primitive char-set-for-each proc cs
1112 Apply @var{proc} to every character in the character set
1113 @var{cs}. The return value is not specified.
1114 @end deffn
1115
1116 @deffn primitive char-set-map proc cs
1117 Map the procedure @var{proc} over every character in @var{cs}.
1118 @var{proc} must be a character -> character procedure.
1119 @end deffn
1120
1121
1122 @c ===================================================================
1123
1124 @node SRFI-14 Creating Character Sets
1125 @subsection Creating Character Sets
1126
1127 New character sets are produced with these procedures.
1128
1129 @deffn primitive char-set-copy cs
1130 Return a newly allocated character set containing all
1131 characters in @var{cs}.
1132 @end deffn
1133
1134 @deffn primitive char-set char1 @dots{}
1135 Return a character set containing all given characters.
1136 @end deffn
1137
1138 @deffn primitive list->char-set char_list [base_cs]
1139 @deffnx primitive list->char-set! char_list base_cs
1140 Convert the character list @var{list} to a character set. If
1141 the character set @var{base_cs} is given, the character in this
1142 set are also included in the result.
1143
1144 @code{list->char-set!} is the side-effecting variant.
1145 @end deffn
1146
1147 @deffn primitive string->char-set s [base_cs]
1148 @deffnx primitive string->char-set! s base_cs
1149 Convert the string @var{str} to a character set. If the
1150 character set @var{base_cs} is given, the characters in this
1151 set are also included in the result.
1152
1153 @code{string->char-set!} is the side-effecting variant.
1154 @end deffn
1155
1156 @deffn primitive char-set-filter pred cs [base_cs]
1157 @deffnx primitive char-set-filter! pred cs base_cs
1158 Return a character set containing every character from @var{cs}
1159 so that it satisfies @var{pred}. If provided, the characters
1160 from @var{base_cs} are added to the result.
1161
1162 @code{char-set-filter!} is the side-effecting variant.
1163 @end deffn
1164
1165 @deffn primitive ucs-range->char-set lower upper [error? base_cs]
1166 @deffnx primitive uce-range->char-set! lower upper error? base_cs
1167 Return a character set containing all characters whose
1168 character codes lie in the half-open range
1169 [@var{lower},@var{upper}).
1170
1171 If @var{error} is a true value, an error is signalled if the
1172 specified range contains characters which are not contained in
1173 the implemented character range. If @var{error} is @code{#f},
1174 these characters are silently left out of the resultung
1175 character set.
1176
1177 The characters in @var{base_cs} are added to the result, if
1178 given.
1179
1180 @code{ucs-range->char-set!} is the side-effecting variant.
1181 @end deffn
1182
1183 @deffn procedure ->char-set x
1184 Coerce @var{x} into a character set. @var{x} may be a string, a
1185 character or a character set.
1186 @end deffn
1187
1188
1189 @c ===================================================================
1190
1191 @node SRFI-14 Querying Character Sets
1192 @subsection Querying Character Sets
1193
1194 Access the elements and other information of a character set with these
1195 procedures.
1196
1197 @deffn primitive char-set-size cs
1198 Return the number of elements in character set @var{cs}.
1199 @end deffn
1200
1201 @deffn primitive char-set-count pred cs
1202 Return the number of the elements int the character set
1203 @var{cs} which satisfy the predicate @var{pred}.
1204 @end deffn
1205
1206 @deffn primitive char-set->list cs
1207 Return a list containing the elements of the character set
1208 @var{cs}.
1209 @end deffn
1210
1211 @deffn primitive char-set->string cs
1212 Return a string containing the elements of the character set
1213 @var{cs}. The order in which the characters are placed in the
1214 string is not defined.
1215 @end deffn
1216
1217 @deffn primitive char-set-contains? cs char
1218 Return @code{#t} iff the character @var{ch} is contained in the
1219 character set @var{cs}.
1220 @end deffn
1221
1222 @deffn primitive char-set-every pred cs
1223 Return a true value if every character in the character set
1224 @var{cs} satisfies the predicate @var{pred}.
1225 @end deffn
1226
1227 @deffn primitive char-set-any pred cs
1228 Return a true value if any character in the character set
1229 @var{cs} satisfies the predicate @var{pred}.
1230 @end deffn
1231
1232
1233 @c ===================================================================
1234
1235 @node SRFI-14 Character-Set Algebra
1236 @subsection Character-Set Algebra
1237
1238 Character sets can be manipulated with the common set algebra operation,
1239 such as union, complement, intersection etc. All of these procedures
1240 provide side-effecting variants, which modify their character set
1241 argument(s).
1242
1243 @deffn primitive char-set-adjoin cs char1 @dots{}
1244 @deffnx primitive char-set-adjoin! cs char1 @dots{}
1245 Add all character arguments to the first argument, which must
1246 be a character set.
1247 @end deffn
1248
1249 @deffn primitive char-set-delete cs char1 @dots{}
1250 @deffnx primitive char-set-delete! cs char1 @dots{}
1251 Delete all character arguments from the first argument, which
1252 must be a character set.
1253 @end deffn
1254
1255 @deffn primitive char-set-complement cs
1256 @deffnx primitive char-set-complement! cs
1257 Return the complement of the character set @var{cs}.
1258 @end deffn
1259
1260 @deffn primitive char-set-union cs1 @dots{}
1261 @deffnx primitive char-set-union! cs1 @dots{}
1262 Return the union of all argument character sets.
1263 @end deffn
1264
1265 @deffn primitive char-set-intersection cs1 @dots{}
1266 @deffnx primitive char-set-intersection! cs1 @dots{}
1267 Return the intersection of all argument character sets.
1268 @end deffn
1269
1270 @deffn primitive char-set-difference cs1 @dots{}
1271 @deffnx primitive char-set-difference! cs1 @dots{}
1272 Return the difference of all argument character sets.
1273 @end deffn
1274
1275 @deffn primitive char-set-xor cs1 @dots{}
1276 @deffnx primitive char-set-xor! cs1 @dots{}
1277 Return the exclusive-or of all argument character sets.
1278 @end deffn
1279
1280 @deffn primitive char-set-diff+intersection cs1 @dots{}
1281 @deffnx primitive char-set-diff+intersection! cs1 @dots{}
1282 Return the difference and the intersection of all argument
1283 character sets.
1284 @end deffn
1285
1286
1287 @c ===================================================================
1288
1289 @node SRFI-14 Standard Character Sets
1290 @subsection Standard Character Sets
1291
1292 In order to make the use of the character set data type and procedures
1293 useful, several predefined character set variables exist.
1294
1295 @defvar char-set:lower-case
1296 All lower-case characters.
1297 @end defvar
1298
1299 @defvar char-set:upper-case
1300 All upper-case characters.
1301 @end defvar
1302
1303 @defvar char-set:title-case
1304 This is empty, because ASCII has no titlecase characters.
1305 @end defvar
1306
1307 @defvar char-set:letter
1308 All letters, e.g. the union of @code{char-set:lower-case} and
1309 @code{char-set:upper-case}.
1310 @end defvar
1311
1312 @defvar char-set:digit
1313 All digits.
1314 @end defvar
1315
1316 @defvar char-set:letter+digit
1317 The union of @code{char-set:letter} and @code{char-set:digit}.
1318 @end defvar
1319
1320 @defvar char-set:graphic
1321 All characters which would put ink on the paper.
1322 @end defvar
1323
1324 @defvar char-set:printing
1325 The union of @code{char-set:graphic} and @code{char-set:whitespace}.
1326 @end defvar
1327
1328 @defvar char-set:whitespace
1329 All whitespace characters.
1330 @end defvar
1331
1332 @defvar char-set:blank
1333 All horizontal whitespace characters, that is @code{#\space} and
1334 @code{#\tab}.
1335 @end defvar
1336
1337 @defvar char-set:iso-control
1338 The ISO control characters with the codes 0--31 and 127.
1339 @end defvar
1340
1341 @defvar char-set:punctuation
1342 The characters @code{!"#%&'()*,-./:;?@@[\\]_@{@}}
1343 @end defvar
1344
1345 @defvar char-set:symbol
1346 The characters @code{$+<=>^`|~}.
1347 @end defvar
1348
1349 @defvar char-set:hex-digit
1350 The hexadecimal digits @code{0123456789abcdefABCDEF}.
1351 @end defvar
1352
1353 @defvar char-set:ascii
1354 All ASCII characters.
1355 @end defvar
1356
1357 @defvar char-set:empty
1358 The empty character set.
1359 @end defvar
1360
1361 @defvar char-set:full
1362 This character set contains all possible characters.
1363 @end defvar
1364
1365 @node SRFI-16
1366 @section SRFI-16 - case-lambda
1367
1368 @c FIXME::martin: Review me!
1369
1370 The syntactic form @code{case-lambda} creates procedures, just like
1371 @code{lambda}, but has syntactic extensions for writing procedures of
1372 varying arity easier.
1373
1374 The syntax of the @code{case-lambda} form is defined in the following
1375 EBNF grammar.
1376
1377 @example
1378 <case-lambda>
1379 --> (case-lambda <case-lambda-clause>)
1380 <case-lambda-clause>
1381 --> (<formals> <definition-or-command>*)
1382 <formals>
1383 --> (<identifier>*)
1384 | (<identifier>* . <identifier>)
1385 | <identifier>
1386 @end example
1387
1388 The value returned by a @code{case-lambda} form is a procedure which
1389 matches the number of actual arguments against the formals in the
1390 various clauses, in order. @dfn{Formals} means a formal argument list
1391 just like with @code{lambda} (@pxref{Lambda}). The first matching clause
1392 is selected, the corresponding values from the actual parameter list are
1393 bound to the variable names in the clauses and the body of the clause is
1394 evaluated. If no clause matches, an error is signalled.
1395
1396 The following (silly) definition creates a procedure @var{foo} which
1397 acts differently, depending on the number of actual arguments. If one
1398 argument is given, the constant @code{#t} is returned, two arguments are
1399 added and if more arguments are passed, their product is calculated.
1400
1401 @lisp
1402 (define foo (case-lambda
1403 ((x) #t)
1404 ((x y) (+ x y))
1405 (z
1406 (apply * z))))
1407 (foo 'bar)
1408 @result{}
1409 #t
1410 (foo 2 4)
1411 @result{}
1412 6
1413 (foo 3 3 3)
1414 @result{}
1415 27
1416 (foo)
1417 @result{}
1418 1
1419 @end lisp
1420
1421 The last expression evaluates to 1 because the last clause is matched,
1422 @var{z} is bound to the empty list and the following multiplication,
1423 applied to zero arguments, yields 1.
1424
1425
1426 @node SRFI-17
1427 @section SRFI-17 - Generalized set!
1428
1429 This is an implementation of SRFI-17: Generalized set!
1430
1431 It exports the Guile procedure @code{make-procedure-with-setter} under
1432 the SRFI name @code{getter-with-setter} and exports the standard
1433 procedures @code{car}, @code{cdr}, @dots{}, @code{cdddr},
1434 @code{string-ref} and @code{vector-ref} as procedures with setters, as
1435 required by the SRFI.
1436
1437 SRFI-17 was heavily criticized during its discussion period but it was
1438 finalized anyway. One issue was its concept of globally associating
1439 setter @dfn{properties} with (procedure) values, which is non-Schemy.
1440 For this reason, this implementation chooses not to provide a way to set
1441 the setter of a procedure. In fact, @code{(set! (setter @var{proc})
1442 @var{setter})} signals an error. The only way to attach a setter to a
1443 procedure is to create a new object (a @dfn{procedure with setter}) via
1444 the @code{getter-with-setter} procedure. This procedure is also
1445 specified in the SRFI. Using it avoids the described problems.
1446