12.exercises.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (import
  2. (scheme r5rs)
  3. (aop 2)
  4. (aop 3)
  5. (aop 12))
  6. (define (exercise-12.1)
  7. (define acc-max (accumulator-maker 0 max))
  8. (for-each (lambda (n)
  9. (send acc-max 'update! n))
  10. '(3 7 2 4 10 1 5))
  11. (send acc-max 'show))
  12. (define (exercise-12.2)
  13. (define (double-box-maker item1 item2)
  14. (let ((left-box (box-maker item1))
  15. (right-box (box-maker item2)))
  16. (lambda msg
  17. (case (first msg)
  18. ((show-left) (send left-box 'show))
  19. ((show-right) (send right-box 'show))
  20. ((update-left!)
  21. (send left-box 'update! (second msg)))
  22. ((update-right!)
  23. (send right-box 'update! (second msg)))
  24. ((reset!)
  25. (for-effect-only
  26. (begin
  27. (set! left-box item1)
  28. (set! right-box item2))))))))
  29. (define a (double-box-maker 11 21))
  30. (send a 'show-left))
  31. (define (word-frequency-maker)
  32. (let ((naive-hash-function
  33. (lambda (s)
  34. (remainder (char->integer (string-ref s 0)) 26))))
  35. (let ((h (hash-table-maker 26 naive-hash-function)))
  36. (lambda (string-list)
  37. (for-each
  38. (lambda (s) (send h 'update! s add1 (lambda (s) 1)))
  39. string-list)
  40. h))))
  41. (define test-sample '("this" "hash" "table" "is" "as" "inefficient"
  42. "as" "a" "bucket" "we" "chose" "the" "vector"
  43. "too" "small" "and" "we" "chose" "the"
  44. "hash" "function" "too" "naively" "of" "course"
  45. "this" "would" "never" "be" "done" "in" "practice"
  46. "most" "systems" "discourage" "the" "user" "from"
  47. "worrying" "about" "the" "size" "of" "the"
  48. "hash" "table" "or" "the" "nature" "of" "the"
  49. "hash" "function"))
  50. (define (exercise-12.22)
  51. (define word-frequency (word-frequency-maker))
  52. (word-frequency test-sample))