考虑以下计算阶乘的函数实现:[1]
(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]基于SICP http://mitpress.mit.edu/sicp/full-text/book/book-ZH-11.html#%_sec_1.2.1factorial
1.2.1节的迭代版本