previous | index | next

Processing Lists: Summing Integers

Q: What is the sum of the integers in an empty list?

A: 0

Q: What is the sum of the integers in a nonempty list?

A: The head of the list plus the sum of the integers in the tail.

     (define sum
       (lambda (lst)
         (if (null? lst)
             0
             (+ (car lst) (sum (cdr lst))))))
Try:
          (sum (integers-from-to 1 2000))

previous | index | next