SRFI is an acronym for Scheme Request For Implementation. The SRFI documents define a lot of syntactic and procedure extensions to standard Scheme as defined in R5RS.
Guile has support for a number of SRFIs. This chapter gives an overview over the available SRFIs and some usage hints. For complete documentation, design rationales and further examples, we advise you to get the relevant SRFI documents from the SRFI home page http://srfi.schemers.org.
SRFI support in Guile is currently implemented partly in the core library, and partly as add-on modules. That means that some SRFIs are automatically available when the interpreter is started, whereas the other SRFIs require you to use the appropriate support module explicitly.
There are several reasons for this inconsistency. First, the feature
checking syntactic form cond-expand (see section SRFI-0 - cond-expand) must be
available immediately, because it must be there when the user wants to
check for the Scheme implementation, that is, before she can know that
it is safe to use use-modules to load SRFI support modules. The
second reason is that some features defined in SRFIs had been
implemented in Guile before the developers started to add SRFI
implementations as modules (for example SRFI-6 (see section SRFI-6 - Basic String Ports)). In
the future, it is possible that SRFIs in the core library might be
factored out into separate modules, requiring explicit module loading
when they are needed. So you should be prepared to have to use
use-modules someday in the future to access SRFI-6 bindings. If
you want, you can do that already. We have included the module
(srfi srfi-6) in the distribution, which currently does nothing,
but ensures that you can write future-safe code.
Generally, support for a specific SRFI is made available by using
modules named (srfi srfi-number), where number is the
number of the SRFI needed. Another possibility is to use the command
line option --use-srfi, which will load the necessary modules
automatically (see section Invoking Guile).
SRFI-0 defines a means for checking whether a Scheme implementation has
support for a specified feature. The syntactic form cond-expand,
which implements this means, has the following syntax.
<cond-expand>
--> (cond-expand <cond-expand-clause>+)
| (cond-expand <cond-expand-clause>* (else <command-or-definition>))
<cond-expand-clause>
--> (<feature-requirement> <command-or-definition>*)
<feature-requirement>
--> <feature-identifier>
| (and <feature-requirement>*)
| (or <feature-requirement>*)
| (not <feature-requirement>)
<feature-identifier>
--> <a symbol which is the name or alias of a SRFI>
When evaluated, this form checks all clauses in order, until it finds one whose feature requirement is satisfied. Then the form expands into the commands or definitions in the clause. A requirement is tested as follows:
and form, all requirements must be satisfied. If no
requirements are given, it is satisfied, too.
or form, at least one of the requirements must be
satisfied. If no requirements are given, it is not satisfied.
not form, the feature requirement must not be
satisfied.
else and it is the last
clause, it is satisfied if no prior clause matched.
If no clause is satisfied, an error is signalled.
Since cond-expand is needed to tell what a Scheme implementation
provides, it must be accessible without using any
implementation-dependent operations, such as use-modules in
Guile. Thus, it is not necessary to use any module to get access to
this form.
Currently, the feature identifiers guile, r5rs and
srfi-0 are supported. The other SRFIs are not in that list by
default, because the SRFI modules must be explicitly used before their
exported bindings can be used.
So if a Scheme program wishes to use SRFI-8, it has two possibilities: First, it can check whether the running Scheme implementation is Guile, and if it is, it can use the appropriate module:
(cond-expand
(guile
(use-modules (srfi srfi-8)))
(srfi-8
#t))
;; otherwise fail.
The other possibility is to use the --use-srfi command line
option when invoking Guile (see section Invoking Guile). When you do that,
the specified SRFI support modules will be loaded and add their feature
identifier to the list of symbols checked by cond-expand.
So, if you invoke Guile like this:
$ guile --use-srfi=8
the following snippet will expand to 'hooray.
(cond-expand (srfi-8 'hooray))
The list library defined in SRFI-1 contains a lot of useful list processing procedures for construction, examining, destructuring and manipulating lists and pairs.
Since SRFI-1 also defines some procedures which are already contained in R5RS and thus are supported by the Guile core library, some list and pair procedures which appear in the SRFI-1 document may not appear in this section. So when looking for a particular list/pair processing procedure, you should also have a look at the sections section Lists and section Pairs.
New lists can be constructed by calling one of the following procedures.
cons, but with interchanged arguments. Useful mostly when
passed to higher-order procedures.
start + (count - 1) * step
start defaults to 0 and step defaults to 1.
The procedures in this section test specific properties of lists.
#t if obj is a proper list, that is a finite list,
terminated with the empty list. Otherwise, return #f.
#t if obj is a circular list, otherwise return
#f.
#t if obj is a dotted list, return #f
otherwise. A dotted list is a finite list which is not terminated by
the empty list, but some other value.
#t if lst is the empty list (), #f
otherwise. If something else than a proper or circular list is passed
as lst, an error is signalled. This procedure is recommended
for checking for the end of a list in contexts where dotted lists are
not allowed.
#t is obj is not a pair, #f otherwise.
This is shorthand notation (not (pair? obj)) and is
supposed to be used for end-of-list checking in contexts where dotted
lists are allowed.
#t if all argument lists are equal, #f otherwise.
List equality is determined by testing whether all lists have the same
length and the corresponding elements are equal in the sense of the
equality predicate elt=. If no or only one list is given,
#t is returned.
car, cadr, caddr, ....
take! may modify the structure of the argument list lst
in order to produce the result.
drop-right! may modify the structure of the argument list
lst in order to produce the result.
split-at! may modify the structure of the argument list
lst in order to produce the result.
#f is returned.
concatenate! may modify the structure of the given lists in
order to produce the result.
(append (reverse rev-head) tail),
but more efficient.
append-reverse! may modify rev-head in order to produce
the result.
unzip1 takes a list of lists, and returns a list containing the
first elements of each list, unzip2 returns two lists, the
first containing the first elements of each lists and the second
containing the second elements of each lists, and so on.
(kons en1 en2 ... (kons e21
e22 (kons e11 e12 knil))),
if enm are the elements of the lists lst1, lst2, ....
fold, but applies kons in right-to-left order
to the list elements, that is:
(kons e11 e12(kons e21
e22 ... (kons en1 en2 knil))),
fold, but apply kons to the pairs of the list
instead of the list elements.
fold-right, but apply kons to the pairs of the list
instead of the list elements.
reduce is a variant of reduce. If lst is
(), ridentity is returned. Otherwise, (fold (car
lst) (cdr lst)) is returned.
fold-right variant of reduce.
unfold is defined as follows:
(unfold p f g seed) =
(if (p seed) (tail-gen seed)
(cons (f seed)
(unfold p f g (g seed))))
(lambda (x) '()).
g produces a series of seed values, which are mapped to list elements by f. These elements are put into a list in left-to-right order, and p tells when to stop unfolding.
(let lp ((seed seed) (lis tail))
(if (p seed) lis
(lp (g seed)
(cons (f seed) lis))))
(lambda (x) '()).
(apply append (map f clist1 clist2 ...))
and
(apply append! (map f clist1 clist2 ...))
Map f over the elements of the lists, just as in the map
function. However, the results of the applications are appended
together to make the final result. append-map uses
append to append the results together; append-map! uses
append!.
The dynamic order in which the various applications of f are made is not specified.
map -- map! is allowed, but not
required, to alter the cons cells of lst1 to construct the
result list.
The dynamic order in which the various applications of f are made is not specified. In the n-ary case, lst2, lst3, ... must have at least as many elements as lst1.
for-each, but applies the procedure f to the pairs
from which the argument lists are constructed, instead of the list
elements. The return value is not specified.
map, but only results from the applications of f
which are true are saved in the result list.
Filtering means to collect all elements from a list which satisfy a specific condition. Partitioning a list means to make two groups of list elements, one which contains the elements satisfying a condition, and the other for the elements which don't.
filter! is allowed, but not required to modify the structure of
partition! is allowed, but not required to modify the structure of
the input list.
remove! is allowed, but not required to modify the structure of
the input list.
The procedures for searching elements in lists either accept a predicate or a comparison object for determining which elements are to be searched.
#f if no such element is found.
#f if no such element is found.
take-while! is allowed, but not required to modify the input
list while producing the result.
span splits the list lst into the longest initial prefix
whose elements all satisfy the predicate pred, and the remaining
tail. break inverts the sense of the predicate.
span! and break! are allowed, but not required to modify
the structure of the input list lst in order to produce the
result.
#f otherwise. The true value returned is always the result of
the first successful application of pred.
#f otherwise. The true value returned is always the result of
the final successful application of pred.
#f.
Equality is determined by the equality predicate =, or
equal? if = is not given.
The procedures for deleting elements from a list either accept a predicate or a comparison object for determining which elements are to be removed.
equal? if not given.
delete! is allowed, but not required to modify the structure of
the argument list in order to produce the result.
equal? if not
given.
delete-duplicates! is allowed, but not required to modify the
structure of the argument list in order to produce the result.
Association lists are described in detail in section section Association Lists. The present section only documents the additional procedures for dealing with association lists defined by SRFI-1.
equal? if not given.
alist must be an association lists--a list of pairs.
(cons (cons key datum) alist)
This procedure is used to coons a new pair onto an existing association list.
equal? if not given.
alist-delete! is allowed, but not required to modify the
structure of the list alist in order to produce the result.
Lists can be used for representing sets of objects. The procedures documented in this section can be used for such set representations. Man combining several sets or adding elements, they make sure that no object is contained more than once in a given list. Please note that lists are not a too efficient implementation method for sets, so if you need high performance, you should think about implementing a custom data structure for representing sets, such as trees, bitsets, hash tables or something similar.
All these procedures accept an equality predicate as the first argument. This predicate is used for testing the objects in the list sets for sameness.
#t if every listi is a subset of listi+1,
otherwise return #f. Returns #t if called with less
than two arguments. = is used for testing element equality.
#t if all argument lists are equal. = is used for
testing element equality.
lset-adjoin! is allowed, but not
required to modify its first argument. = is used for testing
element equality.
lset-union! is allowed, but not required to modify its first
argument. = is used for testing element equality.
lset-intersection! is allowed, but not required to modify its
first argument. = is used for testing element equality.
lset-difference! is allowed, but
not required to modify its first argument. = is used for testing
element equality.
lset-xor! is allowed, but
not required to modify its first argument. = is used for testing
element equality.
lset-difference and
lset-intersection, but is more efficient.
lset-diff+intersection! is allowed, but not required to modify
its first argument. = is used for testing element equality. You
have to use some means to deal with the multiple values these
procedures return (see section Returning and Accepting Multiple Values).
The syntactic form and-let* combines the conditional evaluation
form and with the binding form let*. Each argument
expression will be evaluated sequentially, bound to a variable (if a
variable name is given), but only as long as no expression returns
the false value #f.
Use (use-modules (srfi srfi-2) to access this syntax form.
A short example will demonstrate how it works. In the first expression,
x will get bound to 1, but the next expression (#f) is
false, so evaluation of the form is stopped, and #f is returned.
In the next expression, x is bound to 1, y is bound to
#t and since no expression in the binding section was false, the
body of the and-let* expression is evaluated, which in this case
returns the value of x.
(and-let* ((x 1) (y #f)) 42) => #f (and-let* ((x 1) (y #t)) x) => 1
SRFI-4 defines a set of datatypes for vectors whose elements are all of the same numeric type. Vectors for signed and unsigned exact integer or inexact real numbers in several precisions are available.
Procedures similar to the vector procedures (see section Vectors) are provided for handling these homogeneous vectors, but they are distinct datatypes.
The reason for providing this set of datatypes is that with the limitation (all elements must have the same type), it is possible to implement them much more memory-efficient than normal, heterogenous vectors.
If you want to use these datatypes and the corresponding procedures,
you have to use the module (srfi srfi-4).
Ten vector data types are provided: Unsigned and signed integer values
with 8, 16, 32 and 64 bits and floating point values with 32 and 64
bits. In the following descriptions, the tags u8, s8,
u16, s16, u32, s32, u64,
s64, f32, f64, respectively, are used for
denoting the various types.
Homogeneous numeric vectors have an external representation (read syntax) similar to normal Scheme vectors, but with an additional tag telling the vector's type.
#u16(1 2 3)
denotes a homogeneous numeric vector of three elements, which are the values 1, 2 and 3, represented as 16-bit unsigned integers. Correspondingly,
#f64(3.1415 2.71)
denotes a vector of two elements, which are the values 3.1415 and 2.71, represented as floating-point values of 64 bit precision.
Please note that the read syntax for floating-point vectors conflicts
with Standard Scheme, because there #f is defined to be the
literal false value. That means, that with the loaded SRFI-4 module,
it is not possible to enter some list like
'(1 #f3)
and hope that it will be parsed as a three-element list with the
elements 1, #f and 3. In normal use, this should be no
problem, because people tend to terminate tokens sensibly when writing
Scheme expressions.
The procedures listed in this section are provided for all homogeneous
numeric vector datatypes. For brevity, they are not all documented,
but a summary of the procedures is given. In the following
descriptions, you can replace TAG by any of the datatype
indicators u8, s8, u16, s16, u32,
s32, u64, s64, f32 and f64.
For example, you can use the procedures u8vector?,
make-s8vector, u16vector, u32vector-length,
s64vector-ref, f32vector-set! or f64vector->list.
#t if obj is a homogeneous numeric vector of type
TAG.
TAG, which can hold n elements. If value is given,
the vector is initialized with the value, otherwise, the contents of
the returned vector is not specified.
TAG. The returned vector is as long as the number of arguments
given, and is initialized with the argument values.
TAG,
initialized with the elements of the list lst.
SRFI-6 defines the procedures open-input-string,
open-output-string and get-output-string. These
procedures are included in the Guile core, so using this module does not
make any difference at the moment. But it is possible that support for
SRFI-6 will be factored out of the core library in the future, so using
this module does not hurt, after all.
receive is a syntax for making the handling of multiple-value
procedures easier. It is documented in See section Returning and Accepting Multiple Values.
This is the SRFI way for defining record types. The Guile implementation is a layer above Guile's normal record construction procedures (see section Records). The nice thing about this kind of record definition method is that no new names are implicitly created, all constructor, accessor and predicates are explicitly given. This reduces the risk of variable capture.
The syntax of a record type definition is:
<record type definition>
-> (define-record-type <type name>
(<constructor name> <field tag> ...)
<predicate name>
<field spec> ...)
<field spec> -> (<field tag> <accessor name>)
-> (<field tag> <accessor name> <modifier name>)
<field tag> -> <identifier>
<... name> -> <identifier>
Usage example:
guile> (use-modules (srfi srfi-9))
guile> (define-record-type :foo (make-foo x) foo?
(x get-x) (y get-y set-y!))
guile> (define f (make-foo 1))
guile> f
#<:foo x: 1 y: #f>
guile> (get-x f)
1
guile> (set-y! f 2)
2
guile> (get-y f)
2
guile> f
#<:foo x: 1 y: 2>
guile> (foo? f)
#t
guile> (foo? 1)
#f
The module (srfi srfi-10) implements the syntax extension
#,(), also called hash-comma, which is defined in SRFI-10.
The support for SRFI-10 consists of the procedure
define-reader-ctor for defining new reader constructors and the
read syntax form
#,(ctor datum ...)
where ctor must be a symbol for which a read constructor was
defined previously, using define-reader-ctor.
Example:
(define-reader-ctor 'file open-input-file) (define f '#,(file "/etc/passwd")) (read-line f) => "root:x:0:0:root:/root:/bin/bash"
Please note the quote before the #,(file ...) expression. This
is necessary because ports are not self-evaluating in Guile.
This module implements the binding forms for multiple values
let-values and let-values*. These forms are similar to
let and let* (see section Local Variable Bindings), but they support
binding of the values returned by multiple-valued expressions.
Write (use-modules (srfi srfi-11)) to make the bindings
available.
(let-values (((x y) (values 1 2))
((z f) (values 3 4)))
(+ x y z f))
=>
10
let-values performs all bindings simultaneously, which means that
no expression in the binding clauses may refer to variables bound in the
same clause list. let-values*, on the other hand, performs the
bindings sequentially, just like let* does for single-valued
expressions.
In this section, we will describe all procedures defined in SRFI-13
(string library) and implemented by the module (srfi srfi-13).
Note that only the procedures from SRFI-13 are documented here which are not already contained in Guile. For procedures not documented here please refer to the relevant chapters in the Guile Reference Manual, for example the documentation of strings and string procedures (see section Strings).
All of the procedures defined in SRFI-13, which are not already
included in the Guile core library, are implemented in the module
(srfi srfi-13). The procedures which are both in Guile and in
SRFI-13 are slightly extended in this module. Their bindings
overwrite those in the Guile core.
The procedures which are defined in the section Low-level procedures of SRFI-13 for parsing optional string indices, substring specification checking and Knuth-Morris-Pratt-Searching are not implemented.
The procedures string-contains and string-contains-ci are
not implemented very efficiently at the moment. This will be changed as
soon as possible.
When Guile is properly installed, SRFI-13 support can be loaded into a
running Guile by using the (srfi srfi-13) module.
$ guile guile> (use-modules (srfi srfi-13)) guile>
When this step causes any errors, Guile is not properly installed.
One possible reason is that Guile cannot find either the Scheme module
file `srfi-13.scm', or it cannot find the shared object file
`libguile-srfi-srfi-13-14.so'. Make sure that the former is in the
Guile load path and that the latter is either installed in some default
location like `/usr/local/lib' or that the directory it was
installed to is in your LTDL_LIBRARY_PATH. The same applies to
`srfi-14.scm'.
Now you can test whether the SRFI-13 procedures are working by calling
the string-concatenate procedure.
guile> (string-concatenate '("Hello" " " "World!"))
"Hello World!"
In addition to the primitives string? and string-null?,
which are already in the Guile core, the string predicates
string-any and string-every are defined by SRFI-13.
string-any returns true,
the returned true value is the one produced by the first
successful application of pred.
string-every returns
true, the returned true value is the one produced by the final
application of pred to the last character of s.
SRFI-13 defines several procedures for constructing new strings. In
addition to make-string and string (available in the Guile
core library), the procedure string-tabulate does exist.
The procedure string->list is extended by SRFI-13, that is why it
is included in (srfi srfi-13). The other procedures are new.
The Guile core already contains the procedure list->string for
converting a list of characters into a string (see section List/String conversion).
(compose string->list
reverse):
(reverse-list->string '(#\a #\B #\c)) => "cBa"
infix.
infix
string-infix
infix, but will raise an error if given the empty
list.
suffix
prefix
These procedures are called selectors, because they access information about the string or select pieces of a given string.
Additional selector procedures are documented in the Strings section
(see section String Selection), like string-length or
string-ref.
string-copy is also available in core Guile, but this version
accepts additional start/end indices.
substring, but the result may share memory with the
argument str.
If called without a char_pred argument, all whitespace is trimmed.
The procedure string-fill! is extended from R5RS because it
accepts optional start/end indices. This bindings shadows the procedure
of the same name in the Guile core. The second modification procedure
string-set! is documented in the Strings section (see section String Modification).
The procedures in this section are used for comparing strings in
different ways. The comparison predicates differ from those in R5RS in
that they do not only return #t or #f, but the mismatch
index in the case of a true return value.
string-hash and string-hash-ci are for calculating hash
values for strings, useful for implementing fast lookup mechanisms.
#f if the predicate
fails. Otherwise, the mismatch index is returned (or end1 in the
case of string=.
#f if the predicate
fails. Otherwise, the mismatch index is returned (or end1 in the
case of string=. These are the case-insensitive variants.
string-hash-ci is the case-insensitive variant.
Using these procedures you can determine whether a given string is a prefix or suffix of another string or how long a common prefix/suffix is.
string-prefix-length-ci and
string-suffix-length-ci are the case-insensitive variants.
string-prefix-ci? and
string-suffix-ci? are the case-insensitive variants.
Use these procedures to find out whether a string contains a given character or a given substring, or a character from a set of characters.
string-contains-ci is the case-insensitive variant.
These procedures convert the alphabetic case of strings. They are similar to the procedures in the Guile core, but are extended to handle optional start/end indices.
string-upcase! is the
side-effecting variant.
string-downcase! is the
side-effecting variant.
string-titlecase! is the side-effecting
variant.
One appending procedure, string-append is the same in R5RS and in
SRFI-13, so it is not redefined.
string-reverse! modifies the argument string and returns an
unspecified value.
string-append, but the result may share memory
with the argument strings.
string-concatenate, but the result may share memory
with the strings in the list ls.
(string-concatenate (reverse ls))
If the optional argument final_string is specified, it is consed onto the beginning to ls before performing the list-reverse and string-concatenate operations. If end is given, only the characters of final_string up to index end are used.
Guaranteed to return a freshly allocated string.
string-concatenate-reverse, but the result may
share memory with the the strings in the ls arguments.
string-map, string-for-each etc. are for iterating over
the characters a string is composed of. The fold and unfold procedures
are list iterators and constructors.
string-fold-right). kons must expect two arguments: The
actual character and the last result of kons' application.
(lambda (x) "").
These procedures are special substring procedures, which can also be
used for replicating strings. They are a bit tricky to use, but
consider this code fragment, which replicates the input string
"foo" so often that the resulting string has a length of six.
(xsubstring "foo" 0 6) => "foofoo"
s is a string, start and end are optional
arguments that demarcate a substring of s, defaulting to
0 and the length of s. Replicate this substring up and
down index space, in both the positive and negative directions.
xsubstring returns the substring of this string
beginning at index from, and ending at to, which
defaults to from + (end - start).
xsubstring, but the extracted text
is written into the string target starting at index
tstart. The operation is not defined if (eq?
target s) or these arguments share storage - you
cannot copy a string on top of itself.
string-replace is for replacing a portion of a string with
another string and string-tokenize splits a string into a list of
strings, breaking it up at a specified character.
Filtering means to remove all characters from a string which do not match a given criteria, deleting means the opposite.
SRFI-14 defines the data type character set, and also defines a lot of procedures for handling this character type, and a few standard character sets like whitespace, alphabetic characters and others.
All procedures from SRFI-14 (character-set library) are implemented in
the module (srfi srfi-14), as well as the standard variables
char-set:letter, char-set:digit etc.
When Guile is properly installed, SRFI-14 support can be loaded into a
running Guile by using the (srfi srfi-14) module.
$ guile
guile> (use-modules (srfi srfi-14))
guile> (char-set-union (char-set #\f #\o #\o) (string->char-set "bar"))
#<charset {#\a #\b #\f #\o #\r}>
guile>
The data type charset implements sets of characters (see section Characters). Because the internal representation of character sets is not visible to the user, a lot of procedures for handling them are provided.
Character sets can be created, extended, tested for the membership of a characters and be compared to other character sets.
The Guile implementation of character sets deals with 8-bit characters. In the standard variables, only the ASCII part of the character range is really used, so that for example Umlaute and other accented characters are not considered to be letters. In the future, as Guile may get support for international character sets, this will change, so don't rely on these "features".
Use these procedures for testing whether an object is a character set,
or whether several character sets are equal or subsets of each other.
char-set-hash can be used for calculating a hash value, maybe for
usage in fast lookup procedures.
#t if obj is a character set, #f
otherwise.
#t if all given character sets are equal.
#t if every character set csi is a subset
of character set csi+1.
#f, it restricts the
returned value to the range 0 ... bound - 1.
Character set cursors are a means for iterating over the members of a
character sets. After creating a character set cursor with
char-set-cursor, a cursor can be dereferenced with
char-set-ref, advanced to the next member with
char-set-cursor-next. Whether a cursor has passed past the last
element of the set can be checked with end-of-char-set?.
Additionally, mapping and (un-)folding procedures for character sets are provided.
end-of-char-set? returns true.
end-of-char-set?.
#t if cursor has reached the end of a
character set, #f otherwise.
char-set-unfold! is the side-effecting variant.
New character sets are produced with these procedures.
list->char-set! is the side-effecting variant.
string->char-set! is the side-effecting variant.
char-set-filter! is the side-effecting variant.
If error is a true value, an error is signalled if the
specified range contains characters which are not contained in
the implemented character range. If error is #f,
these characters are silently left out of the resulting
character set.
The characters in base_cs are added to the result, if given.
ucs-range->char-set! is the side-effecting variant.
Access the elements and other information of a character set with these procedures.
#t iff the character ch is contained in the
character set cs.
Character sets can be manipulated with the common set algebra operation, such as union, complement, intersection etc. All of these procedures provide side-effecting variants, which modify their character set argument(s).
In order to make the use of the character set data type and procedures useful, several predefined character set variables exist.
char-set:lower-case and
char-set:upper-case.
char-set:letter and char-set:digit.
char-set:graphic and char-set:whitespace.
#\space and
#\tab.
!"#%&'()*,-./:;?@[\\]_{}
$+<=>^`|~.
0123456789abcdefABCDEF.
The syntactic form case-lambda creates procedures, just like
lambda, but has syntactic extensions for writing procedures of
varying arity easier.
The syntax of the case-lambda form is defined in the following
EBNF grammar.
<case-lambda>
--> (case-lambda <case-lambda-clause>)
<case-lambda-clause>
--> (<formals> <definition-or-command>*)
<formals>
--> (<identifier>*)
| (<identifier>* . <identifier>)
| <identifier>
The value returned by a case-lambda form is a procedure which
matches the number of actual arguments against the formals in the
various clauses, in order. Formals means a formal argument list
just like with lambda (see section Lambda: Basic Procedure Creation). The first matching clause
is selected, the corresponding values from the actual parameter list are
bound to the variable names in the clauses and the body of the clause is
evaluated. If no clause matches, an error is signalled.
The following (silly) definition creates a procedure foo which
acts differently, depending on the number of actual arguments. If one
argument is given, the constant #t is returned, two arguments are
added and if more arguments are passed, their product is calculated.
(define foo (case-lambda
((x) #t)
((x y) (+ x y))
(z
(apply * z))))
(foo 'bar)
=>
#t
(foo 2 4)
=>
6
(foo 3 3 3)
=>
27
(foo)
=>
1
The last expression evaluates to 1 because the last clause is matched, z is bound to the empty list and the following multiplication, applied to zero arguments, yields 1.
This is an implementation of SRFI-17: Generalized set!
It exports the Guile procedure make-procedure-with-setter under
the SRFI name getter-with-setter and exports the standard
procedures car, cdr, ..., cdddr,
string-ref and vector-ref as procedures with setters, as
required by the SRFI.
SRFI-17 was heavily criticized during its discussion period but it was
finalized anyway. One issue was its concept of globally associating
setter properties with (procedure) values, which is non-Schemy.
For this reason, this implementation chooses not to provide a way to set
the setter of a procedure. In fact, (set! (setter proc)
setter) signals an error. The only way to attach a setter to a
procedure is to create a new object (a procedure with setter) via
the getter-with-setter procedure. This procedure is also
specified in the SRFI. Using it avoids the described problems.
This is an implementation of SRFI-19: Time/Date Library
It depends on SRFIs: 6 (see section SRFI-6 - Basic String Ports), 8 (see section SRFI-8 - receive), 9 (see section SRFI-9 - define-record-type).
This section documents constants and procedure signatures.
All these are bound to their symbol names:
time-duration
time-monotonic
time-process
time-tai
time-thread
time-utc
(current-date . tz-offset)
(current-julian-day)
(current-modified-julian-day)
(current-time . clock-type)
(time-resolution . clock-type)
(make-time type nanosecond second)
(time? obj)
(time-type time)
(time-nanosecond time)
(time-second time)
(set-time-type! time type)
(set-time-nanosecond! time nsec)
(set-time-second! time sec)
(copy-time time)
Args are all time values.
(time<=? t1 t2)
(time<? t1 t2)
(time=? t1 t2)
(time>=? t1 t2)
(time>? t1 t2)
The foo! variants modify in place. Time difference
is expressed in time-duration values.
(time-difference t1 t2)
(time-difference! t1 t2)
(add-duration time duration)
(add-duration! time duration)
(subtract-duration time duration)
(subtract-duration! time duration)
(make-date nsecs seconds minutes hours
date month year offset)
(date? obj)
(date-nanosecond date)
(date-second date)
(date-minute date)
(date-hour date)
(date-day date)
(date-month date)
(date-year date)
(date-zone-offset date)
(date-year-day date)
(date-week-day date)
(date-week-number date day-of-week-starting-week)
(date->julian-day date)
(date->modified-julian-day date)
(date->time-monotonic date)
(date->time-tai date)
(date->time-utc date)
(julian-day->date jdn . tz-offset)
(julian-day->time-monotonic jdn)
(julian-day->time-tai jdn)
(julian-day->time-utc jdn)
(modified-julian-day->date jdn . tz-offset)
(modified-julian-day->time-monotonic jdn)
(modified-julian-day->time-tai jdn)
(modified-julian-day->time-utc jdn)
(time-monotonic->date time . tz-offset)
(time-monotonic->time-tai time-in)
(time-monotonic->time-tai! time-in)
(time-monotonic->time-utc time-in)
(time-monotonic->time-utc! time-in)
(time-tai->date time . tz-offset)
(time-tai->julian-day time)
(time-tai->modified-julian-day time)
(time-tai->time-monotonic time-in)
(time-tai->time-monotonic! time-in)
(time-tai->time-utc time-in)
(time-tai->time-utc! time-in)
(time-utc->date time . tz-offset)
(time-utc->julian-day time)
(time-utc->modified-julian-day time)
(time-utc->time-monotonic time-in)
(time-utc->time-monotonic! time-in)
(time-utc->time-tai time-in)
(time-utc->time-tai! time-in)
(date->string date . format-string)
(string->date input-string template-string)
Go to the first, previous, next, last section, table of contents.