dotted-tail notation - Scheme programming technique notes

本文探讨了使用带尾参数列表的过程在处理列表时可能遇到的问题,特别是递归调用时过程不会终止的情况,并提出了解决方案。还介绍了如何使用apply过程来正确实现递归。

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

;As we know, the procedure with dotted-tail notation of parameter list can accepts as many as possible parameters. The parameters before the dot will get their values as usual, and the final parameters after the dot will be a list which can accomodate any number of the remaining parameters.

 

;One fact must be clarified that, the procedure with a dotted-tail parameter is not suitable to act as a recursive procedure when dealing with list. This is my experence, and there may be other ways to implement such functions easyly.

 

;A common pattern when dealing with list with recursive procedures is like the following steps (expressed in pseudo codes):

 

;define procedure-with-list-parameter
;if list is not empty
;then process (car list)
;else recursively call procesure with (cdr list)

 

;Whereas, if you recursively call a procedure like above with dotted-tail parameter, the procedure may not terminate. The reason is, even though the list become empty, when you call the procedure with the empty list, the empty list will become a list with the empty list in the procedure. I.E. '(()).

 

;My current solution is, define another helper procedure without dotted-tail parameter so as to avoiding the case. Maybe there're better ways, I do not find them, yet.
 
;Is there a way to expanding the list when call a procedure with dotted-tail parameter?

 

;The codes followed are the answer for SICP 2.20. I put the answer here, because it waste lots of time of me.

 

 

AMMENDMENT(2009-08-25):

"apply" procedure can be used to implement recursive procedure with dotted-tail notation parameters.  

(function 1 2 3) is equivalent to (apply function (list 1 2 3)).

The "apply" procedure is the better way I am seeking.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值