階乗を計算する関数の次の実装を考えてみましょう: [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 の反復バージョンに基づく