Failing test cases for `letrec` nested inside plain `let`.
Chris Pressey
3 years ago
190 | 190 | let r = fun(x) -> let x = 3 in x in r(10) |
191 | 191 | ???> Already defined: x |
192 | 192 | |
193 | `letrec`. | |
193 | #### `letrec` | |
194 | ||
195 | Basic usage of `letrec`. | |
194 | 196 | |
195 | 197 | letrec |
196 | 198 | oddp = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1)) |
206 | 208 | evenp(5) |
207 | 209 | ===> false |
208 | 210 | |
209 | Nested `letrec`. Nested inside an `if` inside a function definition in an arm. | |
211 | `letrec` nested inside an `if` inside a function definition in an arm of | |
212 | another `letrec`. | |
210 | 213 | |
211 | 214 | letrec |
212 | 215 | facto = fun(n) -> if eq(n, 1) then 1 else |
222 | 225 | facto(8) |
223 | 226 | ===> 105 |
224 | 227 | |
225 | Nested `letrec`, nested in the body. | |
228 | `letrec` nested in the body of another `letrec`. | |
226 | 229 | |
227 | 230 | letrec |
228 | 231 | oddp = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1)) |
257 | 260 | in |
258 | 261 | facto(8) |
259 | 262 | ???> Not in scope: facto |
263 | ||
264 | `letrec` nested inside a function definition inside an arm of a plain `let`. | |
265 | ||
266 | let | |
267 | factoo = fun(f,n) -> | |
268 | letrec | |
269 | oddp = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1)) | |
270 | evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1)) | |
271 | in | |
272 | if eq(n, 1) then 1 else | |
273 | if oddp(n) then | |
274 | mul(n, f(f, sub(n, 1))) | |
275 | else | |
276 | f(f, sub(n, 1)) | |
277 | in | |
278 | factoo(factoo, 7) | |
279 | ===> 105 | |
280 | ||
281 | `letrec` nested inside body of a plain `let`. | |
282 | ||
283 | let | |
284 | factopen = fun(f,n) -> if eq(n, 1) then 1 else f(f, sub(n, 1)) | |
285 | target = 7 | |
286 | in | |
287 | letrec | |
288 | oddp = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1)) | |
289 | evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1)) | |
290 | in | |
291 | if oddp(target) then factopen(factopen, target) else 0 | |
292 | ===> 105 |