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

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

tagged syntax in SRFI-87 documentation

File size: 4.2 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:
27
28<syntax>case <key> <clause1> <clause2> ...</syntax>
29===== Syntax
30{{<Key>}} may be any expression. Each {{<clause>}} should have the form
31
32<syntax>((<datum1> ...) <expression1> <expression2> ...)</syntax>
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<syntax>(else <expression1> <expression2> ...)</syntax>
37
38Alternatively, a {{<clause>}} may be of the form
39
40<syntax>((<datum1> ...) => <expression>)</syntax>
41
42and the last {{<clause>}} may be of the form
43
44<syntax>(else => <expression>)</syntax>
45===== Semantics
46A {{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.
47==== (Based on R5RS section 3.5 Proper tail recursion)
48If 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.
49=== Author
50* Chongkai Zhu
51* Packaged for Chicken Scheme 5 by Sergey Goldgaber
52=== Copyright
53Copyright (C) 2006 Chongkai Zhu. All Rights Reserved.
54
55Permission 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:
56
57The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
58
59THE 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.
60=== Version history
61* [[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.