PasteRack.org
Paste # 59736
2017-09-16 09:12:49

Fork as a new paste.

Paste viewed 48 times.


Embed:

combine-tress abstraction

  1. #lang htdp/isl
  2. (require 2htdp/image)
  3.  
  4. (define-struct dir (name sub-dirs images))
  5. ;; Dir is (make-dir String ListOfDir ListOfImage)
  6. ;; interp. An directory in the organizer, with a name, a list
  7. ;;         of sub-dirs and a list of images.
  8.  
  9. (define I1 (square 10 "solid" "red"))
  10. (define I2 (square 12 "solid" "green"))
  11. (define I3 (rectangle 13 14 "solid" "blue"))
  12. (define D4 (make-dir "D4" empty (list I1 I2)))
  13. (define D5 (make-dir "D5" empty (list I3)))
  14. (define D6 (make-dir "D6" (list D4 D5) empty))
  15.  
  16. ;
  17. ;                       D6
  18. ;                     /    \
  19. ;                    /      \
  20. ;                   D4      D5
  21. ;                 /    \     |
  22. ;                I1    I2    I3
  23. ;
  24.  
  25. ;
  26. ; PROBLEM A:
  27. ;
  28. ; Design an abstract fold function for Dir called fold-dir.
  29. ;
  30.  
  31. ;; (String Y Z -> X) (X Y -> Y) (Image -> Z) Y Z Dir -> X
  32. ;; The abstract function for Dir.
  33.  
  34. ;; Get all dir names as a list.
  35. (check-expect (local
  36.                 [(define (c1 name rlod rloi) (cons name rlod))]
  37.                 (fold-dir c1 append cons empty empty D6))
  38.               (list "D6" "D4" "D5"))
  39.  
  40. ;; Get all images as a list.
  41. (check-expect (local [(define (c1 name rlod rloi) (append rlod rloi))]
  42.                 (fold-dir c1 append cons empty empty D6))
  43.               (list I1 I2 I3))
  44.  
  45. (define (fold-dir combdir comblod combloi bdir bloi d)
  46.   (local [
  47.           ;; Dir -> X
  48.           (define (fn-for-dir d)
  49.             (combdir (dir-name d)
  50.                      (fn-for-lod (dir-sub-dirs d))
  51.                      (fn-for-loi (dir-images d))))
  52.  
  53.           ;; ListOfDir -> Y
  54.           (define (fn-for-lod lod)
  55.             (cond [(empty? lod) bdir]
  56.                   [else
  57.                    (comblod (fn-for-dir (first lod))
  58.                             (fn-for-lod (rest lod)))]))
  59.  
  60.           ;; Image -> Z
  61.           (define (fn-for-loi loi)
  62.             (cond [(empty? loi) bloi]
  63.                   [else
  64.                    (combloi (first loi)
  65.                             (fn-for-loi (rest loi)))]))
  66.           ]
  67.     (fn-for-dir d)))
  68.  
  69.  
  70. ;
  71. ; PROBLEM B:
  72. ;
  73. ; Design a function that consumes a Dir and produces the number of
  74. ; images in the directory and its sub-directories.
  75. ; Use the fold-dir abstract function.
  76. ;
  77.  
  78. ;; Dir -> Natural
  79. ;; Produce number of images in Dir and sub-dirs.
  80. (check-expect  (local [(define (c1 name rlod rloi) (+ rlod rloi))
  81.                        (define (c2 rdir rlod) (+ rdir rlod))
  82.                        (define (c3 i rloi) (+ 1 rloi))]
  83.                  (fold-dir c1 c2 c3 0 0 (make-dir "D0" empty empty)))
  84.                0)
  85.  
  86. (check-expect  (local [(define (c1 name rlod rloi) (+ rlod rloi))
  87.                        (define (c2 rdir rlod) (+ rdir rlod))
  88.                        (define (c3 i rloi) (+ 1 rloi))]
  89.                  (fold-dir c1 c2 c3 0 0 (make-dir "D0" empty (list I1 I2 I3 I1 I2))))
  90.                5)
  91.  
  92. (check-expect  (local [(define (c1 name rlod rloi) (+ rlod rloi))
  93.                        (define (c2 rdir rlod) (+ rdir rlod))
  94.                        (define (c3 i rloi) (+ 1 rloi))]
  95.                  (fold-dir c1 c2 c3 0 0 D6))
  96.                3)
  97.  
  98.  
  99. (define (count-images dir)
  100.   (local [
  101.           (define (c1 name rlod rloi)
  102.             (+ rlod rloi))
  103.  
  104.           (define (c2 rdir rlod)
  105.             (+ rdir rlod))
  106.  
  107.           (define (c3 img rloi)
  108.             (+ 1 rloi))]
  109.     (fold-dir c1 c2 c3 0 0 dir)))

=>

All 5 tests passed!