Tail Recursion

A function is tail-recursive if the value it returns is either (a) computed without any further calls to the function, or (b) is exactly the value of a recursive call, without any modification.

Tail-recursive functions are desirable because they can be executed more efficiently.

Our design pattern for auxiliary functions allows tail-recursive functions to be written:


(define (factorial n)
  (factorialb n 1))

(define (factorialb n answer) (if (<= n 0) answer (factorialb (1- n) (* n answer)) ) )

This version of factorial is tail-recursive, while the previous version was not.

Contents    Page-10    Prev    Next    Page+10    Index