I recently started reading SICP , and I am very interested in converting a recursive procedure into a tail recursive form.
For "one-dimensional" situations (linear), such as a Fibonacci series or factorial calculations, it is not difficult to make a transformation.
For example, as the book says, we can rewrite the Fibonacci calculation as follows
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
And this form is obviously tail-recursive
However, for a “two-dimensional” situation, such as calculating the Pascal triangle (example 1.12 in SICP), we can still easily write a recursive solution, for example, the following
(define (pascal x y)
(cond ((or (<= x 0) (<= y 0) (< x y )) 0)
((or (= 1 y) (= x y) ) 1)
(else (+ (pascal (- x 1) y) (pascal (- x 1) (- y 1))))))
The question is how to convert this to a tail recursive form?