Example: using helper functions
;; compute the average mpg for all cars in the loa list ;; List-of-Auto -> Number ;; (define (avg-mpg loa) ;; Notice: we will really need two functions: ;; compute the total of the mpg consumption for all cars int the loa list ;; List-of-Auto -> Number ;; (define (total-mpg loa)... ;; count the number of autos in the loa list ;; List-of-Auto -> Number ;; (define (count loa) ... ;; Need to make a decision, how to handle the empty case ;; Decide to produce average to be 0 ;; Examples: ;; (= (avg-mpg mt-loa) 0) ;; (= (avg-mpg loa1) 20) ;; (= (avg-mpg loa2) 15) ;; (= (avg-mpg loa3) 15) ;; Template: ;; (cond ;; [( empty? loa) ...] ;; [else ... (first loa) ... ;; ... (rest loa) ...] ;; Notice, we cannot apply avg-mpg to the (rest loa) ;; we use the wish list helper functions instead ;; Program: (define (avg-mpg loa) (cond [(empty? loa) 0] [else (/ (total-mpg loa) (count loa))])) ;; Tests for the avg-mpg function: (= (avg-mpg mt-loa) 0) (= (avg-mpg loa1) 20) (= (avg-mpg loa2) 15) (= (avg-mpg loa3) 15)