rec
for recursive evaluation
rec
. This form is a generalization and
combination of the forms rec
and
named-lambda
of [Clinger1985]. It allows the simple and
non-imperative construction of self-referential expressions. As an
important special case, it extends the A. Church form
lambda
such that it allows the direct definition of
recursive procedures without using further special forms like
let
or 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
let
or letrec
leads to similar
objections as above.
rec
form 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.
rec
special form in
a generalized way that combines the advantages of the
named-lambda
and rec
forms.
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
, syntax-rules
,
letrec
and lambda
might implement
rec
as follows.
(define-syntax rec (syntax-rules () ((rec (NAME . VARIABLES) . BODY) (letrec ( (NAME (lambda VARIABLES . BODY)) ) NAME)) ((rec NAME EXPRESSION) (letrec ( (NAME EXPRESSION) ) NAME))))
rec
allows 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
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.