The following bit of code does the weighted-average algorithm correctly:
;; weighted-average : ListOfNumber ListOfNumber
;; GIVEN: a list of actual scores, a list of maximum possible
;; scores, and a list of relative weights
;; RETURNS: the weighted average.
;; EXAMPLEs:
;; (weighted-average (list 5 10) (list 10 10) (list 1 1)) = .75
;; (weighted-average (list 5 10) (list 10 10) (list 0 1)) = 1.0
;; strategy: function composition
(define (weighted-average scores maxes weights)
(let ((weighted-sum
(sum
(map
(lambda (score max weight) (* (/ score max) weight))
scores maxes weights)))
(total-weight (sum weights)))
(/ weighted-sum total-weight)))
;; sum : ListOfNumber -> Number
;; GIVEN a list of numbers,
;; RETURNS: their sum
;; Example:
;; (sum (list 3 4 1)) = 8
;; (sum empty) = 0
;; strategy: higher-order function composition
(define sum
(lambda (l)
(foldr + 0 l)))
Last modified: Sun Jan 10 2016