scheme(Lisp)资源

本文深入探讨了在Scheme编程语言中使用递归来实现迭代过程的方法,包括如何利用尾调用来定义迭代过程,以及递归过程与迭代过程的区别。通过具体的例子和解释,阐述了递归和迭代在不同场景下的应用与转换,强调了Scheme语言内部将迭代过程转化为递归过程的机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 (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.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值