binding-constructs.texinfo (9000B)
1 @node Binding constructs 2 @subsection Binding constructs 3 4 @cindex region 5 6 The binding constructs @code{let}, @code{let*}, @code{letrec}, 7 @code{letrec*}, @code{let-values}, and @code{let*-values} give Scheme a 8 block structure, like Algol 60. The syntax of the first four constructs is 9 identical, but they differ in the regions they establish for their variable 10 bindings. In a @code{let} expression, the initial values are computed before 11 any of the variables become bound; in a @code{let*} expression, the bindings 12 and evaluations are performed sequentially; while in @code{letrec} and 13 @code{letrec*} expressions, all the bindings are in effect while their 14 initial values are being computed, thus allowing mutually recursive 15 definitions. The @code{let-values} and @code{let*-values} constructs are 16 analogous to @code{let} and @code{let*} respectively, but are designed to 17 handle multiple-valued expressions, binding different identifiers to the 18 returned values. 19 20 @deffn syntax let @svar{bindings} @svar{body} 21 22 Syntax: @svar{Bindings} has the form 23 24 @display 25 @code{((}@svar{variable@sub{1}} @svar{init@sub{1}}@code{)} @dots{}@code{)}, 26 @end display 27 28 where each @svar{init} is an expression, and @svar{body} is a sequence of 29 zero or more definitions followed by a sequence of one or more expressions 30 as described in @ref{Expressions}. It is an error for a @svar{variable} to 31 appear more than once in the list of variables being bound. 32 33 @cindex region 34 35 Semantics: The @svar{init}s are evaluated in the current environment (in 36 some unspecified order), the @svar{variable}s are bound to fresh locations 37 holding the results, the @svar{body} is evaluated in the extended 38 environment, and the values of the last expression of @svar{body} are 39 returned. Each binding of a @svar{variable} has @svar{body} as its region. 40 41 @lisp 42 (let ((x 2) (y 3)) 43 (* x y)) @result{} 6 44 45 (let ((x 2) (y 3)) 46 (let ((x 7) 47 (z (+ x y))) 48 (* z x))) @result{} 35 49 @end lisp 50 51 See also ``named @code{let},'' @ref{Iteration}. 52 @end deffn 53 54 @deffn syntax let* @svar{bindings} @svar{body} 55 56 Syntax: @svar{Bindings} has the form 57 58 @display 59 @code{((}@svar{variable1} @svar{init@sub{1}}) @dots{}@code{)}, 60 @end display 61 62 and @svar{body} is a sequence of zero or more definitions followed by 63 one or more expressions as described in @ref{Expressions}. 64 65 @cindex region 66 67 Semantics: The @code{let*} binding construct is similar to @code{let}, but 68 the bindings are performed sequentially from left to right, and the region 69 of a binding indicated by @code{(}@svar{variable} @svar{init}@code{)} is 70 that part of the @code{let*} expression to the right of the binding. Thus 71 the second binding is done in an environment in which the first binding is 72 visible, and so on. The @svar{variable}s need not be distinct. 73 74 @lisp 75 (let ((x 2) (y 3)) 76 (let* ((x 7) 77 (z (+ x y))) 78 (* z x))) @result{} 70 79 @end lisp 80 @end deffn 81 82 @deffn syntax letrec @svar{bindings} @svar{body} 83 84 Syntax: @svar{Bindings} has the form 85 86 @display 87 @code{((}@svar{variable@sub{1}} @svar{init@sub{1}}@code{)} @dots{}@code{)}, 88 @end display 89 90 and @svar{body} is a sequence of zero or more definitions followed by one or 91 more expressions as described in @ref{Expressions}. It is an error for a 92 @svar{variable} to appear more than once in the list of variables being 93 bound. 94 95 @cindex region 96 97 Semantics: The @svar{variable}s are bound to fresh locations holding 98 unspecified values, the @svar{init}s are evaluated in the resulting 99 environment (in some unspecified order), each @svar{variable} is assigned to 100 the result of the corresponding @svar{init}, the @svar{body} is evaluated in 101 the resulting environment, and the values of the last expression in 102 @svar{body} are returned. Each binding of a @svar{variable} has the entire 103 letrec expression as its region, making it possible to define mutually 104 recursive procedures. 105 106 @lisp 107 (letrec ((even? 108 (lambda (n) 109 (if (zero? n) 110 #t 111 (odd? (- n 1))))) 112 (odd? 113 (lambda (n) 114 (if (zero? n) 115 #f 116 (even? (- n 1)))))) 117 (even? 88)) 118 @result{} #t 119 @end lisp 120 121 One restriction on @code{letrec} is very important: if it is not possible to 122 evaluate each @svar{init} without assigning or referring to the value of any 123 @svar{variable}, it is an error. The restriction is necessary because 124 @code{letrec} is defined in terms of a procedure call where a @code{lambda} 125 expression binds the @svar{variable}s to the values of the @svar{init}s. In 126 the most common uses of @code{letrec}, all the @svar{init}s are 127 @code{lambda} expressions and the restriction is satisfied automatically. 128 @end deffn 129 130 @deffn syntax letrec* @svar{bindings} @svar{body} 131 132 Syntax: @svar{Bindings} has the form 133 134 @display 135 @code{((}@svar{variable@sub{1}} @svar{init@sub{1}}@code{)} @dots{}@code{)}, 136 @end display 137 138 and @svar{body}is a sequence of zero or more definitions followed by one or 139 more expressions as described in @ref{Expressions}. It is an error for a 140 @svar{variable} to appear more than once in the list of variables being 141 bound. 142 143 @cindex region 144 145 Semantics: The @svar{variable}s are bound to fresh locations, each 146 @svar{variable} is assigned in left-to-right order to the result of 147 evaluating the corresponding @svar{init} (interleaving evaluations and 148 assignments), the @svar{body} is evaluated in the resulting environment, and 149 the values of the last expression in @svar{body} are returned. Despite the 150 left-to-right evaluation and assignment order, each binding of a 151 @svar{variable} has the entire @code{letrec*} expression as its region, 152 making it possible to define mutually recursive procedures. 153 154 If it is not possible to evaluate each @svar{init} without assigning or 155 referring to the value of the corresponding @svar{variable} or the 156 @svar{variable} of any of the bindings that follow it in @svar{bindings}, it 157 is an error. Another restriction is that it is an error to invoke the 158 continuation of an @svar{init} more than once. 159 160 @lisp 161 ;; Returns the arithmetic, geometric, and 162 ;; harmonic means of a nested list of numbers 163 (define (means ton) 164 (letrec* 165 ((mean 166 (lambda (f g) 167 (f (/ (sum g ton) n)))) 168 (sum 169 (lambda (g ton) 170 (if (null? ton) 171 (+) 172 (if (number? ton) 173 (g ton) 174 (+ (sum g (car ton)) 175 (sum g (cdr ton))))))) 176 (n (sum (lambda (x) 1) ton))) 177 (values (mean values values) 178 (mean exp log) 179 (mean / /)))) 180 @end lisp 181 182 Evaluating @code{(means '(3 (1 4)))} returns three values: 8/3, 183 2.28942848510666 (approximately), and 36/19. 184 @end deffn 185 186 @deffn syntax let-values @svar{mv binding spec} @svar{body} 187 188 Syntax: @svar{Mv binding spec} has the form 189 190 @display 191 @code{((}@svar{formals@sub{1}} @svar{init@sub{1}}@code{)} @dots{}@code{)}, 192 @end display 193 194 where each @svar{init} is an expression, and @svar{body} is zero or more 195 definitions followed by a sequence of one or more expressions as described in @ref{Expressions}. 196 It is an error for a variable to appear more than once in the set of @svar{formals}. 197 198 @cindex region 199 200 Semantics: 201 The @svar{init}s are evaluated in the current environment (in some 202 unspecified order) as if by invoking @code{call-with-values}, and the 203 variables occurring in the @svar{formals} are bound to fresh locations 204 holding the values returned by the @svar{init}s, where the 205 @svar{formals} are matched to the return values in the same way that 206 the @svar{formals} in a @code{lambda} expression are matched to the 207 arguments in a procedure call. Then, the @svar{body} is evaluated in 208 the extended environment, and the values of the last expression of 209 @svar{body} are returned. Each binding of a @svar{variable} has 210 @svar{body} as its region. 211 212 It is an error if the @svar{formals} do not match the number of values 213 returned by the corresponding @svar{init}. 214 215 @lisp 216 (let-values (((root rem) (exact-integer-sqrt 32))) 217 (* root rem)) @result{} 35 218 @end lisp 219 @end deffn 220 221 @deffn syntax let*-values @svar{mv binding spec} @svar{body} 222 223 Syntax: @svar{Mv binding spec} has the form 224 225 @display 226 @code{((}@svar{formals} @svar{init}@code{)} @dots{}@code{)}, 227 @end display 228 229 and @svar{body} is a sequence of zero or more definitions followed by one or 230 more expressions as described in @ref{Expressions}. In each @svar{formals}, 231 it is an error if any variable appears more than once. 232 233 @cindex region 234 235 Semantics: 236 The @code{let*-values} construct is similar to @code{let-values}, but the 237 @svar{init}s are evaluated and bindings created sequentially from left to 238 right, with the region of the bindings of each @svar{formals} including the 239 @svar{init}s to its right as well as @svar{body}. Thus the second 240 @svar{init} is evaluated in an environment in which the first set of 241 bindings is visible and initialized, and so on. 242 243 @lisp 244 (let ((a 'a) (b 'b) (x 'x) (y 'y)) 245 (let*-values (((a b) (values x y)) 246 ((x y) (values a b))) 247 (list a b x y))) @result{} (x y x y) 248 @end lisp 249 @end deffn