More Examples

Sum of a list of numbers:


(define (sum lst) (sumb lst 0))

(define (sumb lst sum) (if (pair? lst) (sumb (cdr lst) (+ (car lst) sum)) sum))

Average of a list of numbers:


(define (average lst) (averageb lst 0 0))

(define (averageb lst sum n) (if (pair? lst) (averageb (cdr lst) (+ (car lst) sum) (+ 1 n)) (/ sum n) ))

Contents    Page-10    Prev    Next    Page+10    Index