现在的位置: 首页 > 综合 > 正文

dotted-tail notation – Scheme programming technique notes

2018年03月15日 ⁄ 综合 ⁄ 共 1770字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.