Title

=> in case clauses

Author

Chongkai Zhu

Status

This SRFI is currently in ``draft'' status. To see an explanation of each status that a SRFI can hold, see here. It will remain in draft status until 2006/06/08, or as amended. To provide input on this SRFI, please mailto:srfi-87@srfi.schemers.org. See instructions here to subscribe to the list. You can access previous messages via the archive of the mailing list.

Abstract

This SRFI proposes an extension to the case syntax to allow the => clauses as in cond.

Rationale

case is introduced as a syntax sugar based on cond, which helps to save a explicit calling to let. But without the => clause, if the result expression needs the value of key, the let can't be saved. For an easy example, suppose we want the following:
(case (get-symbol)
  ((true) #t)
  ((false) #f)
  (else => (lambda (x) x)))

Without the => clause in case, we have to write:

(let ((key (get-symbol)))
  (cond ((eq? key 'true) #t)
        ((eq? key 'false) #f)
        (else key)))

Specification

(Based on R5RS section 4.2.1 Conditionals)

library syntax: case <key> <clause1> <clause2> ...
Syntax: <Key> may be any expression. Each <clause> should have the form
((<datum1> ...) <expression1> <expression2> ...),

where each <datum> is an external representation of some object. All the <datum>s must be distinct. The last <clause> may be an "else clause," which has the form

(else <expression1> <expression2> ...).

Alternatively, a <clause> may be of the form

((<datum1> ...) => <expression>)

and the last <clause> may be of the form

(else => <expression>)

Semantics: A `case' expression is evaluated as follows. <Key> is evaluated and its result is compared against each <datum>. If the result of evaluating <key> is equivalent (in the sense of `eqv?'; see section see section 6.1 Equivalence predicates) to a <datum>, then the expressions in the corresponding <clause> are evaluated from left to right and the result(s) of the last expression in the <clause> is(are) returned as the result(s) of the `case' expression. If the result of evaluating <key> is different from every <datum>, then if there is an else clause its expressions are evaluated and the result(s) of the last is(are) the result(s) of the `case' expression; otherwise the result of the `case' expression is unspecified. If the selected <clause> uses the => alternate form, then the <expression> is evaluated. Its value must be a procedure that accepts one argument; this procedure is then called on the value of <Key> and the value(s) returned by this procedure is(are) returned by the `case' expression.

Implementation

(define-syntax case
  (syntax-rules (else =>)
    ((case (key ...)
       clauses ...)
     (let ((atom-key (key ...)))
       (case atom-key clauses ...)))
    ((case key
       (else => result))
     (result key))
    ((case key
       ((atoms ...) => result))
     (if (memv key '(atoms ...))
         (result key)))
    ((case key
       ((atoms ...) => result)
       clause clauses ...)
     (if (memv key '(atoms ...))
         (result key)
         (case key clause clauses ...)))
    ((case key
       (else result1 result2 ...))
     (begin result1 result2 ...))
    ((case key
       ((atoms ...) result1 result2 ...))
     (if (memv key '(atoms ...))
         (begin result1 result2 ...)))
    ((case key
       ((atoms ...) result1 result2 ...)
       clause clauses ...)
     (if (memv key '(atoms ...))
         (begin result1 result2 ...)
         (case key clause clauses ...)))))

Copyright

Copyright (C) 2006. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Editor: Mike Sperber
Last modified: Mon Apr 10 21:20:25 CEST 2006