chickadee » srfi-87

SRFI-87: => in case clauses

Abstract

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

For more information see: SRFI-87: => in case clauses

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
Syntax

<Key> may be any expression. Each <clause> should have the form

((<datum1> ...) <expression1> <expression2> ...)syntax

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> ...)syntax

Alternatively, a <clause> may be of the form

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

and the last <clause> may be of the form

(else => <expression>)syntax
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.

(Based on R5RS section 3.5 Proper tail recursion)

If a cond or case expression is in a tail context, and has a clause of the form (<expression[1]> => <expression[2]>) then the (implied) call to the procedure that results from the evaluation of <expression[2]> is in a tail context. <expression[2]> itself is not in a tail context.

Author

Copyright

Copyright (C) 2006 Chongkai Zhu. 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.

Version history

Contents »