s-99-04


 (define (my-length l) 
   (if (null? l) 
     0 
     (+ 1 (my-length (cdr list))))) 

The previous code isn't tail recursive, and so uses space linear in the length of l. Swapping the calls to my-length and + using an accumulator argument gives a tail-recursive solution that uses constant space:

 (define (my-length l) 
  
   (let my-length ((len 0) (l l)) 
     (if (null? l) 
         len 
         (my-length (+ len 1) (cdr l))))) 

And, of course, foldl also provides a constant-space solution:

 (define (my-length l) 
  
   (foldl (lambda (e len) (+ len 1)) 0 l))