drracket UBC CPSC 110
(require 2htdp/image)
(define-struct dir (name sub-dirs images))
;; Dir is (make-dir String (listof Dir) (listof Image))
;; interp. a directory in the organizer, with a name, a list
;; of sub-dirs and a list of images.
(define I1 (square 20 “solid” “red”))
(define I2 (square 15 “solid” “green”))
(define I3 (square 10 “solid” “blue”))
(define I4 (square 25 “solid” “black”))
(define I5 (square 30 “solid” “purple”))
(define D4 (make-dir “D4” empty (list I1 I2)))
(define D5 (make-dir “D5” empty (list I3)))
(define D6 (make-dir “D6” (list D4 D5) (list I4 I5)))
#;
(define (fn-for-dir d)
(local [(define (fn-for-dir d)
(… (dir-name d)
(fn-for-lod (dir-sub-dirs d))
(fn-for-loi (dir-images d))))
(define (fn-for-lod lod)
(cond [(empty? lod) (…)]
[else
(… (fn-for-dir (first lod))
(fn-for-lod (rest lod)))]))
(define (fn-for-loi loi)
(cond [(empty? loi) (…)]
[else
(… (first loi)
(fn-for-loi (rest loi)))]))]
(fn-for-dir d)))
;; abstract fold for Dir
(check-expect (fold-dir make-dir cons cons empty empty D6) D6)
(define (fold-dir c1 c2 c3 b1 b2 d)
(local [(define (fn-for-dir d)
(c1 (dir-name d)
(fn-for-lod (dir-sub-dirs d))
(fn-for-loi (dir-images d))))
(define (fn-for-lod lod)
(cond [(empty? lod) b1]
[else
(c2 (fn-for-dir (first lod))
(fn-for-lod (rest lod)))]))
(define (fn-for-loi loi)
(cond [(empty? loi) b2]
[else
(c3 (first loi)
(fn-for-loi (rest loi)))]))]
(fn-for-dir d)))
;;
;; Complete the design of the function below.
;; The body of your function MUST USE the abstract fold function for Dir.
;; Solutions that do not use fold-dir will NOT be graded.
;; produce the count of images in dir with image-height > n
(check-expect (count-imgs-with-height>n D5 10) 0)
(check-expect (count-imgs-with-height>n D6 15) 3)
(check-expect (count-imgs-with-height>n D6 20) 2)
(define (cout-imgs-lst lst n)
(if (eq? lst ‘()) 0
(+ (if (> (image-height (car lst)) n) 1 0)
(cout-imgs-lst (rest lst) n))))
(define (count-imgs-with-height-lst lst n)
(if (eq? lst ‘()) 0
(+ (count-imgs-with-height>n (first lst) n)
(count-imgs-with-height-lst (rest lst) n))))
(define (count-imgs-with-height>n dir n)
(+ (cout-imgs-lst (dir-images dir) n)
(count-imgs-with-height-lst (dir-sub-dirs dir) n)))