previous | index | next

Processing Lists: List Length

Q: What is the length of the empty list?

A: 0

Q: What is the length of a nonempty list?

A: One plus the length of the tail

     (define length
       (lambda (lst)
         (if (null? lst)
             0
             (+ 1 (length (cdr lst))))))
Traversing a given list like this is called cdr-ing down a list.

previous | index | next