Previous: , Up: srfi general-cond   [Index]


2.26.4 Specification

The <cond clause> production in the formal syntax of Scheme as written by R5RS Section 7.1.3 is extended with a new option:

<cond clause> --->
    ...
  | (<generator> <guard> => <receiver>)

where <generator>, <guard> and <receiver> are all <expression>.

Clauses of this form have the following semantics: <generator> is evaluated and it may return arbitrarily many values. <guard> is applied to an argument list containing the values in order that <generator> returned. If <guard> returns a true value for that argument list, <receiver> is applied with an equivalent argument list. If <guard> returns a false value, however, the clause is abandoned and the next one is tried.

Examples

This port->char-list procedure accepts an input port and returns a list of all the characters it produces until the end.

(define (port->char-list port)
  (cond [(read-char port)
            char?
         => (lambda (c)
              (cons c (port->char-list port)))]
        [else '()]))

Consider now a hypothetical table-entry procedure that accepts two arguments, a table (perhaps a hash table) and a key to an entry that may be in the table; it returns two values: a boolean that denotes whether or not an entry with the given key was in the table and, if it was, the value associated with the key. Also, a hypothetical proj0 combinator (projection of argument 0) returns its 0th argument and ignores all others. One might conditionally branch to a certain body of code if the table contains the desired entry like so with the new type of cond clause:

(cond ...
      [(table-entry <table> <key>) proj0
       => (lambda (present? value)
            ...[VALUE is bound to the value of the entry]...)]
      ...)

Previous: , Up: srfi general-cond   [Index]