末尾再帰(Tail Recursion)
> (define (fac-tail n multiplier)
    (if (= n 0)
        multiplier
        (fac-tail (- n 1) (* multiplier n))))
> (trace fac-tail)
(fac-tail)
> (define (fac2 n) (fac-tail n 1))
> (fac2 4)
|(fac-tail 4 1)
|(fac-tail 3 4)
|(fac-tail 2 12)
|(fac-tail 1 24)
|(fac-tail 0 24)
|24
24

一覧 前へ 次へ