Saturday, May 30, 2009

Ex-1.9

First Version:
Its not tail-recursive, process generated is recursive.
(+ 4 5)
(inc (+ 3 5))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9

Second Version:
Its tail-recursive, process generated is iterative.
(+ 4 5)
(+ 3 6)
(+ 2 7)
(+ 1 8)
(+ 0 9)
9


Tail-Recursive Procedure: If there is only one recursive call in the procedure and that is THE LAST call in the procedure body.

No comments:

Post a Comment