You should notice that this function is greater or less than sum , which can be defined simply with fold .
(define (adder lst) (fold + 0 lst))
What does a fold do? In principle, it is defined as follows:
(define (fold f initial lst) (if (null? lst) initial (fold f (f (car lst) initial) (cdr lst))))
(In other words, it calls the function f, a function of two arguments, for each element of lst, using the car lst as the first argument, and the accumulated result as the second argument for f.)
The problem you need to handle is that + does not know how to work with non-numeric values. No problem, you have already dealt with it. What happens if a symbol appears instead? Well, you are not adding anything to the overall value, so replace it with 0. Therefore, your solution is as simple as:
(define (adder lst) (fold your-new-protected-+ 0 lst))
source share