Untitled
unknown
plain_text
3 years ago
3.1 kB
5
Indexable
Never
;;;-------------------------------- ;;;Exercise 7 (define-struct photos [top bottom]) ; ImageCollection is one of: ; – #false ; – (make-photos Image ImageCollection) ; interpretation: represents a collection of photos where #false is a ; collection with no photos and make-photos has at least one photo. ; - photos-top is the photo on the top of the stack of photos ; - photos-bottom represents the remaining photos (define p0 #false) (define p1 (make-photos . p0)) (define p2 (make-photos . p1)) (define p3 (make-photos . p2)) (define p4 (make-photos . p3)) (define p5 (make-photos . p4)) (define (photos-temp p) (... (cond [(boolean? p) ...] [(photos? p) ... (photos-top p) ... (photos-temp (photos-bottom p))]))) ;;;p-size: ImageCollection -> Nat ;;;Returns the total number of images in a ImageCollection (check-expect (p-size p1) 1) (check-expect (p-size p3) 3) (check-expect (p-size p5) 5) (define (p-size p) (cond [(boolean? p) 0] [(photos? p) (add1 (p-size (photos-bottom p)))])) ;---------------------------------------------------- (define-struct cr (index images)) ; A CR (Camera Roll) is a (make-cr PositiveNumber ImageCollection) ; interpretation: ; The index represents the position of the current image. ; The index must be between 0 and the number of images in ImageCollection. (define cr0 (make-cr 0 p0)) (define cr1 (make-cr 0 p1)) (define cr2 (make-cr 1 p2)) (define cr3 (make-cr 2 p3)) (define cr4 (make-cr 1 p4)) (define cr5 (make-cr 3 p5)) #; (define (cr-temp cr) (... (cr-index cr)) (... (photos-temp (cr-images cr)))) ;;;NEED FUNCTION THAT TAKES IN CAMERA ROLL, LOOKS AT INDEX, GOES THAT MANY DEEP INTO THE IMAGECOLLECTION ;;;how do we go a variable amount deep? (define Blacksquare (square 100 "solid" "black")) ;;;display-photo: CR -> Image ;;;takes in a Camera Roll and returns the current image ;;;returns a black square if no images in camera roll ;(check-expect (display-photo cr0) Blacksquare) (check-expect (display-photo cr2) (photos-top p1)) (check-expect (display-photo cr5) (photos-top p3)) #; (define (display-photo cr) (cond [(boolean? (cr-images cr)) Blacksquare] [(photos? (cr-images cr)) (photos-top (cr-images cr))])) ;;;change-photo: CR a-key -> CR (define (change-photo cr a-key) (cond [(string=? a-key "left") (prev-photo cr)] [(string=? a-key "right") (next-photo cr)] [else cr])) ;;;prev-photo: CR -> CR ;;;Subtracts one from the index of a Camera Roll (check-expect (prev-photo cr5) (make-cr 2 p5)) (check-expect (prev-photo cr3) (make-cr 1 p3)) (define (prev-photo cr) (if (> (cr-index cr) 0) (make-cr (- (cr-index cr) 1) (cr-images cr)) cr)) ;;;next-photo: CR -> CR ;;;Adds one to the index of a Camera Roll (check-expect (next-photo cr5) (make-cr 4 p5)) (check-expect (next-photo cr3) (make-cr 3 p3)) (define (next-photo cr) (if (< (cr-index cr) (p-size (cr-images cr))) (make-cr (+ (cr-index cr) 1) (cr-images cr)) cr))