exam9.5.3

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

  1. ;; mht created on Nov 15, 2011

  2. ;; dollar-store? : list-of-numbers -> boolean
  3. ;; to check whether all of a-list-of-nums are < 1

  4. (define (dollar-store? a-list-of-nums)
  5.   (cond
  6.     [(empty? a-list-of-nums) true]
  7.     [else
  8.      (and
  9.       (< (first a-list-of-nums) 1)
  10.       (dollar-store? (rest a-list-of-nums)))]))

  11. ;; test
  12. (dollar-store? empty)
  13. (not (dollar-store? (cons .75 (cons 1.95 (cons .25 empty)))))
  14. (dollar-store? (cons .15 (cons .05 (cons .25 empty))))

  15. ;; generalized-dollar-store? : list-of-numbers number -> boolean
  16. ;; to check whether all of a-list-of-nums are below n

  17. (define (generalized-dollar-store? a-list-of-nums n)
  18.   (cond
  19.     [(empty? a-list-of-nums) true]
  20.     [else
  21.      (and
  22.       (< (first a-list-of-nums) n)
  23.       (generalized-dollar-store? (rest a-list-of-nums) n))]))

  24. ;; test
  25. (generalized-dollar-store? empty 1)
  26. (not (generalized-dollar-store? (cons .75 (cons 1.95 (cons .25 empty))) 1))
  27. (generalized-dollar-store? (cons .15 (cons .05 (cons .25 empty))) 1)
上一篇:exam9.5.2
下一篇:exam9.5.4