recfor recursive evaluation
This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to
email@example.com. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.
rec. This form is a generalization and combination of the forms
named-lambdaof [Clinger1985]. It allows the simple and non-imperative construction of self-referential expressions. As an important special case, it extends the A. Church form
lambdasuch that it allows the direct definition of recursive procedures without using further special forms like
letrec, without using advanced constructions like the H. B. Curry combinator and, unlike
define, without introducing variable bindings into the external environment.
(F : N |--> 1, if N = 0; N * F(N - 1), otherwise).This expression is a term and not a definition or proposition.
We investigate some approaches to express the factorial function in Scheme.
(define (F N) (if (zero? N) 1 (* N (F (- N 1)))))But this expression is not a term. It binds the factorial function to the variable
F. The expression itself may not occur in a syntactical context where a name of the factorial is required.
(let () (define (F N) (if (zero? N) 1 (* N (F (- N 1))))) F)
(lambda (N) (let F ( (N N) ) (if (zero? N) 1 (* N (F (- N 1))))))
(letrec ( (F (lambda (N) (if (zero? N) 1 (* N (F (- N 1)))))) ) F)
((lambda (F) (F F)) (lambda (G) (lambda (N) (if (zero? N) 1 (* N ((G G) (- N 1)))))))
named-lambda. An even earlier solution with a slightly different syntax was implemented in Kent Dybvig's Chez Scheme system. Using this special form, we can denote the factorial simply by
(named-lambda (F N) (if (zero? N) 1 (* N (F (- N 1)))))This expression is a function term that denotes the factorial in the appropriate brevity.
However, the form
named-lambda has been dropped from
later versions of the Scheme Report. Also it is missing in
state-of-the-art implementations such as Chez Scheme (6.0a) and MIT
Scheme (7.7.0). (The latter actually knows a form
named-lambda with different semantics).
(define S (cons 1 (delay S)))As in the case of the factorial, we are able to define the recursive object at the price of spending an externally bound name. Remedying this with
letrecleads to similar objections as above.
recform in [Clinger1985]. This form allows writing
(rec S (cons 1 (delay S)))This expression is non-imperative and does not introduce an external variable binding.
Also this form has been dropped from later versions of the Scheme Report. Moreover, from our point of view this form alone is not capable of solving Problem 1. The respective definition would look like
(rec F (lambda (N) (if (zero? N) 1 (* N (F (- N 1))))))This again does not seem quite as simple and intuitive as the mathematical notation.
recspecial form in a generalized way that combines the advantages of the
recforms. The factorial function could be written
(rec (F N) (if (zero? N) 1 (* N (F (- N 1)))))
<derived expression> --> <rec expression>
<rec expression> --> (rec <variable> <expression>)
<rec expression> --> (rec (<variable>+) <body>)
(define-syntax rec (syntax-rules () ((rec (NAME . VARIABLES) . BODY) (letrec ( (NAME (lambda VARIABLES . BODY)) ) NAME)) ((rec NAME EXPRESSION) (letrec ( (NAME EXPRESSION) ) NAME))))
recallows a tail-recursive implementation of the factorial function.
> (define F (rec (F N) ((rec (G K L) (if (zero? K) L (G (- K 1) (* K L)))) N 1))) > F #<procedure> > (F 0) 1 > (F 10) 3628800
Copyright (C) Dr. Mirko Luedde (2002). All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.