#lang scheme (define (sum-all-v1 nums) (cond [(cons? nums) (+ (first nums) (sum-all-v1 (rest nums)))] [(empty? nums) 0])) (define (sum-all-v2 nums) (sum-all-helper nums 0)) (define (sum-all-helper nums-remaining sum-so-far) (cond [(cons? nums-remaining) (sum-all-helper (rest nums-remaining) (+ sum-so-far (first nums-remaining)))] [(empty? nums-remaining) sum-so-far])) ;; foldr: ;; combine: a rule to combine the accumulator ("so-far" variable) ;; with the first thing in the list. ;; base-case: the initial accumulator value. (define (my-foldr combine base-case data) (letrec ([my-foldr-helper (lambda (data-remaining result-so-far) (cond [(cons? data-remaining) (my-foldr-helper (rest data-remaining) (combine result-so-far (first data-remaining)))] [(empty? data-remaining) result-so-far]))]) (my-foldr-helper data base-case))) (define (sum-all-v3 nums) (foldl + 0 nums)) #:(define (grow-fires lof) (foldl (lambda (f rr) (cons (grow-fire f) rr)) empty lof))