Description

Versions of cond and case that implement SRFI-61 and SRFI-87, and generalized case* and switch*

Version

Requires

Download

extended-cond.egg

Documentation

This extension provides several variations on the standard cond and case syntax and the chicken-specific switch, and some useful functions. It implements SRFI-61 (cond with guards) and SRFI-87 (case with cond-like => syntax).

The extension relies on syntax-case macros.

Functions

procedure: (member* EQUAL?? VALUE LIST)

Generalized member that returns the first tail of LIST with a car such that (EQUAL?? foo VALUE) is true.

Syntax

macro: (case VALUE ((VALUE-LIST) => PROCEDURE) | ((VALUE-LIST) EXPRS) [(else => PROC) | (else EXPRS)])

case implemented using SRFI-87 extensions, which add an optional => keyword that acts like cond's: The matching object is pased to PROCEDURE, which is a one-argument function. In all other cases, it behaves just like the traditional case.

macro: (case* EQUAL? VALUE ((VALUE-LIST) => PROCEDURE) | ((VALUE-LIST) EXPRESSIONS) ... [(else => PROC) | (else EXPRS)])

A generalised version of SRFI-87 case that takes a user-defined equality predicate that takes two arguments.

macro: (cond (TEST GUARD => PROC) | (TEST EXPRS) (TEST => PROC) ... [(else EXPRS)])

cond implemented with the SRFI-61 syntax extension. (TEST GUARD => PROC) first evaluates TEST. If true, its return value is passed to GUARD. If that also returns true, the original return values of TEST are passed to PROC. In all other cases, it behaves just like the traditional cond.

macro: (switch VALUE (VAL => PROC) | (VAL EXPRS) ... [(else => PROC) | (else EXPRS)])

switch implemented with the same SRFI-87 => syntax as case.

macro: (switch* EQUAL? VALUE (VAL => PROC) | (VAL EXPRS) ... [(else => PROC) | (else EXPRS)])

A generalized version of switch that takes a user-defined equality predicate and implements SRFI-87 style => syntax.

Examples

(require-extension extended-cond)

(case* string=? "foo"
       (("foo" "bar" "baz") => (lambda (what)
                                 (format "Matched ~A!~N" what)))
       (else (print "Didn't match!")))

(define foo '((do-this . #t)
              (do-that . #f)))
(cond
 ((assq 'do-that foo) cdr => (lambda (pair)
                               (format "Will ~A~N" (car pair))))
 (else (format "Won't do anything.~N")))

License

Public domain.