forked from diamond-lizard/srfi-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.svnwiki
157 lines (134 loc) · 7.17 KB
/
README.svnwiki
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
== SRFI-5: A compatible let form with signatures and rest arguments
=== Abstract
The 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.
For more information see: [[https://srfi.schemers.org/srfi-5/|SRFI-5: A compatible let form with signatures and rest arguments]]
=== Rationale
Signature-style Syntax
Consider the following two equivalent definitions:
<enscript highlight="scheme">
(define fibonacci
(lambda (n i f0 f1)
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1)))))
(define (fibonacci n i f0 f1)
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1))))
</enscript>
Although 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:
<enscript highlight="scheme">
(let fibonacci ((n 10) (i 0) (f0 0) (f1 1))
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1))))
</enscript>
Values: 55
As it stands, one cannot equivalently write
<enscript highlight="scheme">
(let (fibonacci (n 10) (i 0) (f0 0) (f1 1))
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1))))
</enscript>
which is consistent with define's signature-style form.
Those 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.
=== Rest Arguments
As it stands, one cannot write a named let with rest arguments, as in
<enscript highlight="scheme">
(let (blast (port (current-output-port)) . (x (+ 1 2) 4 5))
(if (null? x)
'just-a-silly-contrived-example
(begin
(write (car x) port)
(apply blast port (cdr x)))))
</enscript>
otherwise equivalent to
<enscript highlight="scheme">
(letrec ((blast (lambda (port . x)
(if (null? x)
'just-a-silly-contrived-example
(begin
(write (car x) port)
(apply blast port (cdr x)))))))
(blast (current-output-port) (+ 1 2) 4 5))
</enscript>
While 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.
=== Symbols in Binding Sections
Both 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.
This 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.
=== Specification
==== Syntax
A formal specification of the syntax follows. Below, body, expression, and
identifier are free. Each instantiation of binding-name must be unique.
let = "(" "let" let-bindings body ")"
expressions = nothing | expression expressions
let-bindings = let-name bindings
| "(" let-name "." bindings ")"
let-name = identifier
bindings = "(" ")"
| rest-binding
| "(" normal-bindings ["." rest-binding] ")"
normal-bindings = nothing
| normal-binding normal-bindings
normal-binding = "(" binding-name expression ")"
binding-name = identifier
rest-binding = "(" binding-name expressions ")"
For clarity and convenience, an informal specification follows.
===== 1. Unnamed
<enscript highlight="scheme">
(let ((<parameter> <argument>)...)
<body>...)
</enscript>
===== 2. Named, non-signature-style, no rest argument
<enscript highlight="scheme">
(let <name> ((<parameter> <argument>)...)
<body>...)
</enscript>
===== 3. Named, signature-style, no rest argument
<enscript highlight="scheme">
(let (<name> (<parameter> <argument>)...)
<body>...)
</enscript>
===== 4. Named, non-signature-style, rest argument
<enscript highlight="scheme">
(let <name> ((<parameter> <argument>)...
. (<rest-parameter> <rest-argument>...))
<body>...)
</enscript>
===== 5. Named, signature-style, rest argument
<enscript highlight="scheme">
(let (<name> (<parameter> <argument>)...
. (<rest-parameter> <rest-argument>...))
<body>...)
</enscript>
==== Semantics
Let {{$lambda}} and {{$letrec}} be hygienic bindings for the {{lambda}} and {{letrec}} forms, respectively.
===== For informal syntax 1:
<enscript highlight="scheme">
(($lambda (<parameter>...) <body>...) <argument>...)
</enscript>
===== For informal syntaxes 2 and 3:
<enscript highlight="scheme">
($letrec ((<name> ($lambda (<parameter>...) <body>...)))
(<name> <argument>...))
</enscript>
===== For informal syntaxes 4 and 5:
<enscript highlight="scheme">
($letrec ((<name> ($lambda (<parameter>...
. <rest-parameter>) <body>...)))
(<name> <argument>... <rest-argument>...))
</enscript>
=== Author
* Andy Gaynor
* Ported to Chicken Scheme 5 by [[https://wiki.call-cc.org/users/sergey-goldgaber|Sergey Goldgaber]]
=== Copyright
Copyright (C) Andy Gaynor (1999). All Rights Reserved.
This 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.
The limited permissions granted above are perpetual and will not be revoked by the authors or their successors or assigns.
This 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.
=== Source code
* The source code for this egg can be found: [[https://github.com/diamond-lizard/srfi-5|here]]
=== Version history
* [[https://github.com/diamond-lizard/srfi-5/releases/tag/0.2|0.2]] - Fixed broken .egg file name
* [[https://github.com/diamond-lizard/srfi-5/releases/tag/0.1|0.1]] - Ported to Chicken Scheme 5