source: project/wiki/eggref/5/srfi-87 @ 39329

Last change on this file since 39329 was 39329, checked in by gnosis, 2 months ago

Initial revision of SRFI-87 documentation

File size: 4.3 KB
Line 
1== SRFI-87: => in case clauses
2=== Abstract
3This SRFI proposes an extension to the case syntax to allow the => clauses as in cond.
4
5For more information see: [[https://srfi.schemers.org/srfi-87/|SRFI-87: => in case clauses]]
6=== Rationale
7{{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:
8
9<enscript highlight="scheme">
10(case (get-symbol)
11  ((true) #t)
12  ((false) #f)
13  (else => (lambda (x) x)))
14</enscript>
15
16Without the {{=>}} clause in case, we have to write:
17
18<enscript highlight="scheme">
19(let ((key (get-symbol)))
20  (cond ((eq? key 'true) #t)
21        ((eq? key 'false) #f)
22        (else key)))
23</enscript>
24=== Specification
25==== (Based on R5RS section 4.2.1 Conditionals)
26library syntax: case <key> <clause1> <clause2> ...
27===== Syntax
28{{<Key>}} may be any expression. Each {{<clause>}} should have the form
29
30<enscript highlight="scheme">
31((<datum1> ...) <expression1> <expression2> ...),
32</enscript>
33
34where 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
35
36<enscript highlight="scheme">
37(else <expression1> <expression2> ...).
38</enscript>
39
40Alternatively, a {{<clause>}} may be of the form
41
42<enscript highlight="scheme">
43((<datum1> ...) => <expression>)
44</enscript>
45
46and the last {{<clause>}} may be of the form
47
48<enscript highlight="scheme">
49(else => <expression>)
50</enscript>
51===== Semantics
52A {{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 [[http://www.swiss.ai.mit.edu/~jaffer/r5rs_8.html#SEC49|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.
53==== (Based on R5RS section 3.5 Proper tail recursion)
54If 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.
55=== Author
56* Chongkai Zhu
57* Packaged for Chicken Scheme 5 by Sergey Goldgaber
58=== Copyright
59Copyright (C) 2006 Chongkai Zhu. All Rights Reserved.
60
61Permission 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:
62
63The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
64
65THE 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.
66=== Version history
67* [[https://github.com/diamond-lizard/srfi-87/releases/tag/0.1|0.1]] - Ported to Chicken Scheme 5
Note: See TracBrowser for help on using the repository browser.