为什么`let`不能为内部递归过程命名? [英] Why won't `let` work for naming internal recursive procedures?
本文介绍了为什么`let`不能为内部递归过程命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下用于计算阶乘的函数的实现:[1]
Consider the following implementation of a function to compute factorial: [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
进行内部定义重写:
I attempted to rewrite using let
for the inner define:
(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
时没有错误,但是执行结果为:
There is no error at define
time, but execution results in:
#;> (fac-tail-2 4)
Error: undefined variable 'fac-tail-helper-2'.
{warning: printing of stack trace not supported}
如何使let
版本正常工作?
方案版本为SISC v 1.16.6
Scheme version is SISC v 1.16.6
查看全文