(define (sum sum-so-far lis) (if (null? lis) sum-so-far (sum (+ (car lis) sum-so-far) (cdr lis)))) (sum 0 '(1 2 3)) => (sum 1 '(2 3)) => (sum 3 '(3)) => (sum 6 '()) => 6
As you can see, the size does not vary (except by the length of the argument list). We gave arecursive procedure the efficiency advantage of an iterative procedure. Actually, it can be proven that every iteration is easily expressed using tail calls!
Therefore, we call the
process
created by
a procedure that builds up data on the call stack a
recursive process
, and a
process
created by
a procedure that requires constant space an
iterative process
. An
iterative procedure
can create only
iterative processes
, while a
recursive procedure
can create both
recursive
as well as
iterative
processes.
Consequently, Scheme does not provide iterative procedures (such as
while
) at all. They can be easily defined in terms of tail calls, though. Hence, Scheme can
emulate
an
iterative
procedure using the appropriate syntax, but internally, it is translated into a
recursive
procedure. You can see examples of such emulations on
simple-iterators
.
两个例子把用递归来实现迭代讲的很透彻了。
Some sources explaining the Y-combinator:
The Why of Y by Richard Gabriel
(Y Y) Works! by Matthias Felleisen and Daniel P. Friedman.
There is a section explaining the Y combinator in Sketchy-LISP.
See also the article Wikipedia:Y-combinator.
实用Common Lisp编程
The Scheme
Programming Language
Programming Language
本文深入探讨了在Scheme编程语言中使用递归来实现迭代过程的方法,包括如何利用尾调用来定义迭代过程,以及递归过程与迭代过程的区别。通过具体的例子和解释,阐述了递归和迭代在不同场景下的应用与转换,强调了Scheme语言内部将迭代过程转化为递归过程的机制。

被折叠的 条评论
为什么被折叠?



