xxxxxxxxxx
class TailCallOptimization {
public static void main(String[] args) {
}
public static long reFact(int n) {
if( n <= 1)
return 1;
else
return n * reFact(n-1);
}
public static long tailReFact(int n, int a) {
if( n <= 1)
return a;
else
return tailReFact(n-1, n * a);
}
}
xxxxxxxxxx
(define (fact x)
(if (= x 0) 1
(* x (fact (- x 1)))))
(define (fact x)
(define (fact-tail x accum)
(if (= x 0) accum
(fact-tail (- x 1) (* x accum))))
(fact-tail x 1))