考慮下面的實施的功能的計算階乘:[1]爲什麼'let`不能用於命名內部遞歸過程?
(define fac-tail
(lambda (n)
(define fac-tail-helper
(lambda (n ac)
(if (= 0 n)
ac
(fac-tail-helper (- n 1) (* n ac)))))
(fac-tail-helper n 1)))
我試圖使用let
用於內重寫定義:
(define fac-tail-2
(lambda (n)
(let ((fac-tail-helper-2
(lambda (n ac)
(if (= 0 n)
ac
(fac-tail-helper-2 (- n 1) (* n ac))))))
(fac-tail-helper-2 n 1))))
有一個在define
時間沒有錯誤,但執行結果爲:
#;> (fac-tail-2 4)
Error: undefined variable 'fac-tail-helper-2'.
{warning: printing of stack trace not supported}
如何使let
版本正常工作?
方案版本是SISC v 1.16.6
[1]基於對factorial
在節中的迭代版本SICP 1.2.1 http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.1
很高興知道有人在那裏攻擊計劃...... :) – galambalazs 2010-07-02 22:26:28