source: project/wiki/eggref/5/srfi-5 @ 39231

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

Initial revision of SRFI-5 documentation

File size: 7.0 KB
Line 
1== SRFI-5: A compatible let form with signatures and rest arguments
2=== Abstract
3The named-let incarnation of the let form has two slight inconsistencies with the define form. As defined, the let form makes no accommodation for rest arguments, an issue of functionality and consistency. As defined, the let form does not accommodate signature-style syntax, an issue of aesthetics and consistency. Both issues are addressed here in a manner which is compatible with the traditional let form but for minor extensions.
4
5For more information see: [[https://srfi.schemers.org/srfi-5/|SRFI-5: A compatible let form with signatures and rest arguments]]
6=== Rationale
7Signature-style Syntax
8
9Consider the following two equivalent definitions:
10
11<enscript highlight="scheme">
12(define fibonacci
13  (lambda (n i f0 f1)
14    (if (= i n)
15      f0
16      (fibonacci n (+ i 1) f1 (+ f0 f1)))))
17
18(define (fibonacci n i f0 f1)
19  (if (= i n)
20    f0
21    (fibonacci n (+ i 1) f1 (+ f0 f1))))
22</enscript>
23
24Although there is a named-let analog for the former form, there is none for the latter. To wit, suppose one wished to compute the 10th element of the Fibonacci sequence using a named let:
25
26<enscript highlight="scheme">
27(let fibonacci ((n 10) (i 0) (f0 0) (f1 1))
28  (if (= i n)
29    f0
30    (fibonacci n (+ i 1) f1 (+ f0 f1))))
31</enscript>
32Values: 55
33
34As it stands, one cannot equivalently write
35
36<enscript highlight="scheme">
37(let (fibonacci (n 10) (i 0) (f0 0) (f1 1))
38  (if (= i n)
39    f0
40    (fibonacci n (+ i 1) f1 (+ f0 f1))))
41</enscript>
42
43which is consistent with define's signature-style form.
44
45Those that favor the signature style may prefer this extension. In any case, it may be more appropriate to include all bound names within the binding section. As presented, this straightforward extension introduces no ambiguity or incompatibility with the existing definition of let.
46=== Rest Arguments
47As it stands, one cannot write a named let with rest arguments, as in
48
49<enscript highlight="scheme">
50(let (blast (port (current-output-port)) . (x (+ 1 2) 4 5))
51  (if (null? x)
52    'just-a-silly-contrived-example
53    (begin
54      (write (car x) port)
55      (apply blast port (cdr x)))))
56</enscript>
57
58otherwise equivalent to
59
60<enscript highlight="scheme">
61(letrec ((blast (lambda (port . x)
62          (if (null? x)
63              'just-a-silly-contrived-example
64              (begin
65            (write (car x) port)
66            (apply blast port (cdr x)))))))
67  (blast (current-output-port) (+ 1 2) 4 5))
68</enscript>
69
70While this example is rather contrived, the functionality is not. There are several times when the author has used this construct in practice. Regardless, there is little reason to deny the let form access to all the features of lambda functionality.
71=== Symbols in Binding Sections
72Both the features above rely upon the placement of symbols in let binding lists (this statement is intentially simplistic). The only other apparent use of such symbol placement is to tersely bind variables to unspecified values. For example, one might desire to use {{(let (foo bar baz) ...)}} to bind foo, bar, and baz to unspecified values.
73
74This usage is considered less important in light of the rationales presented above, and an alternate syntax is immediately apparent, as in {{(let ((foo) (bar) (baz)) ...)}} This may even be preferable, consistently parenthesizing normal binding clauses.
75=== Specification
76==== Syntax
77A formal specification of the syntax follows. Below, body, expression, and
78identifier are free. Each instantiation of binding-name must be unique.
79
80             let = "(" "let" let-bindings body ")"
81     expressions = nothing | expression expressions
82    let-bindings = let-name bindings
83                 | "(" let-name "." bindings ")"
84        let-name = identifier
85        bindings = "(" ")"
86                 | rest-binding
87                 | "(" normal-bindings ["." rest-binding] ")"
88 normal-bindings = nothing
89                 | normal-binding normal-bindings
90  normal-binding = "(" binding-name expression ")"
91    binding-name = identifier
92    rest-binding = "(" binding-name expressions ")"
93
94For clarity and convenience, an informal specification follows.
95===== 1. Unnamed
96<enscript highlight="scheme">
97(let ((<parameter> <argument>)...)
98  <body>...)
99</enscript>
100===== 2. Named, non-signature-style, no rest argument
101<enscript highlight="scheme">
102(let <name> ((<parameter> <argument>)...)
103  <body>...)
104</enscript>
105===== 3. Named, signature-style, no rest argument
106<enscript highlight="scheme">
107(let (<name> (<parameter> <argument>)...)
108  <body>...)
109</enscript>
110===== 4. Named, non-signature-style, rest argument
111<enscript highlight="scheme">
112(let <name> ((<parameter> <argument>)...
113
114. (<rest-parameter> <rest-argument>...))
115  <body>...)
116</enscript>
117===== 5. Named, signature-style, rest argument
118<enscript highlight="scheme">
119(let (<name> (<parameter> <argument>)...
120
121. (<rest-parameter> <rest-argument>...))
122  <body>...)
123</enscript>
124==== Semantics
125Let {{$lambda}} and {{$letrec}} be hygienic bindings for the {{lambda}} and {{letrec}} forms, respectively.
126===== For informal syntax 1:
127<enscript highlight="scheme">
128(($lambda (<parameter>...) <body>...) <argument>...)
129</enscript>
130===== For informal syntaxes 2 and 3:
131<enscript highlight="scheme">
132($letrec ((<name> ($lambda (<parameter>...) <body>...)))
133  (<name> <argument>...))
134</enscript>
135===== For informal syntaxes 4 and 5:
136<enscript highlight="scheme">
137($letrec ((<name> ($lambda (<parameter>...
138
139. <rest-parameter>) <body>...)))
140  (<name> <argument>... <rest-argument>...))
141</enscript>
142=== Author
143* Andy Gaynor
144* Ported to Chicken Scheme 5 by [[https://wiki.call-cc.org/users/sergey-goldgaber|Sergey Goldgaber]]
145=== Copyright
146Copyright (C) Andy Gaynor (1999). All Rights Reserved.
147
148This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Scheme Request For Implementation process or editors, except as needed for the purpose of developing SRFIs in which case the procedures for copyrights defined in the SRFI process must be followed, or as required to translate it into languages other than English.
149
150The limited permissions granted above are perpetual and will not be revoked by the authors or their successors or assigns.
151
152This document and the information contained herein is provided on an "AS IS" basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
153=== Source code
154* The source code for this egg can be found: [[https://github.com/diamond-lizard/srfi-5|here]]
155=== Version history
156* 0.1 - Ported to Chicken Scheme 5
Note: See TracBrowser for help on using the repository browser.