exam9.5.7

238阅读 0评论2011-11-18 maunix
分类:LINUX

  1. ;; mht created on Nov 15, 2011

  2. ;; average-price : list-of-numbers -> number
  3. (define (average-price a-list-of-nums)
  4.   (cond
  5.     [(empty? a-list-of-nums) 0]
  6.     [else
  7.      (/ (sum-price a-list-of-nums)
  8.         (count-price a-list-of-nums))]))

  9. ;; sum-price : list-of-nums -> number
  10. (define (sum-price a-list-of-nums)
  11.   (cond
  12.     [(empty? a-list-of-nums) 0]
  13.     [else
  14.      (+ (first a-list-of-nums)
  15.         (sum-price (rest a-list-of-nums)))]))

  16. ;; count-price : list-of-nums -> number
  17. (define (count-price a-list-of-nums)
  18.   (cond
  19.     [(empty? a-list-of-nums) 0]
  20.     [else
  21.      (+ (count-price (rest a-list-of-nums))
  22.         1)]))

  23. ;; test
  24. (average-price (cons 2 (cons 3 (cons 1 empty))))
  25. (average-price (cons 2 empty))
上一篇:exam9.5.6
下一篇:exam9.5.8