letrec -- r7rs Definition syntax;
Syntax keywords:
variable: identifier;initializer: identifier;binding: pattern with variants:
(variable initializer);bindings: pattern with variants:
();(binding |...|);expression: expression;Syntax variants:
(_ bindings)(_ bindings expression |...|)scheme:base -- (scheme base);scheme -- (scheme);(letrec <bindings> <body>)Syntax:
<Bindings>has the form((<variable_1> <init_1>) ...)and
<body>is a sequence of zero or more definitions followed by one or more expressions as described in section onlambda. It is an error for a<variable>to appear more than once in the list of variables being bound.Semantics: The
<variable>s are bound to fresh locations holding unspecified values, the<init>s are evaluated in the resulting environment (in some unspecified order), each<variable>is assigned to the result of the corresponding<init>, the<body>is evaluated in the resulting environment, and the values of the last expression in<body>are returned. Each binding of a<variable>has the entireletrecexpression as its region, making it possible to define mutually recursive procedures.(letrec ((even? (lambda (n) (if (zero? n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (even? 88)) ===> #tOne restriction on
letrecis very important: if it is not possible to evaluate each<init>without assigning or referring to the value of any<variable>, it is an error. The restriction is necessary becauseletrecis defined in terms of a procedure call where alambdaexpression binds the<variable>s to the values of the<init>s. In the most common uses ofletrec, all the<init>s arelambdaexpressions and the restriction is satisfied automatically.
The text herein was sourced and adapted as described in the "R7RS attribution of various text snippets" appendix.