As a scheme-newbie I'm trying to solve some basic problems.
One of them are continued fractions, which I like to realize both ways, recursively and via tailrecursion.
My recursive soloution (d and n are functions and k is the recursion-depth):
(define (c-fraction d n k)
(if (= k 0) 1
(/ (n k) (+ (d k) (c-fraction d n (- k 1))))))
is working like I want to, but I've no idea, how to makte this tail-recursively.
I do not to recognize, which expression I have to to accumulate in which way.
Remark that your function computes:
If you want to compute
you need to reverse the order of the coefficients:
For a tail-recursive function you just need to keep:
in the accumulator, without the need to reverse the coefficients:
The expressions
(c-fraction2 d n k)
and(c-fraction-tail d n k 1)
give the same results.