Design Pattern: Tail Recursion on List

We can specialize our previous tail-recursive design pattern for use with lists: (define (myfun lst)
         (myfunb lst initanswer))

(define (myfunb lst answer)
         (if (pair? lst)
                 (myfunb (cdr lst)
                         (combine (car lst) answer ))
                 answer ))

This pattern can be used for a tail-recursive implementation of length:

(define (length2 lst) (length2b lst 0))

(define (length2b lst n) (if (pair? lst) (length2b (cdr lst) (1+ n)) n) )

Contents    Page-10    Prev    Next    Page+10    Index