;; an example (define camera-roll-1 (cons (triangle 100 "solid" "red") (cons (square 120 "outline" "green") (cons (circle 90 "solid" "blue") (cons (star 120 "solid" "silver") (cons (rhombus 130 20 "solid" "pink") empty)))))) ;; data representation: (define-struct pictures (lefties current righties)) ;; A CR is (make-pictures LOI Image LOI) ;; A LOI is one of: ;; -- empty ;; -- (cons Image LOI) ;; interpretation: ;; (make-pictures one-list-of-images an-image other-list) represents ;; the currently displayed image is 'an-image' ;; one-list-of-images are those to its left in the camera roll ;; and other-list are the images to its right ;; LOI -> CR ;; simulate a camera roll program from a smart phone (define (show an-loi) (big-bang (make-pictures empty (first an-loi) (rest an-loi)) (to-draw show-current) (on-key move-left-right))) ;; ----------------------------------------------------------------------------- ;; wish list ;; CR -> Image ;; pick current image and show (check-expect (show-current (make-pictures empty (circle 100 "solid" "red") empty)) (circle 100 "solid" "red")) (define (show-current a-cr) (pictures-current a-cr)) ;; ----------------------------------------------------------------------------- ;; *** wish list finished after lecture *** ;; CR KeyEvent -> CR ;; move to left or right when given "left" or "right" (define test-data (make-pictures (cons (triangle 100 "solid" "green") empty) (circle 100 "solid" "red") (cons (square 100 "solid" "blue") empty))) (define left-data (make-pictures empty (triangle 100 "solid" "green") (cons (circle 100 "solid" "red") (cons (square 100 "solid" "blue") empty)))) (define right-data (make-pictures (cons (circle 100 "solid" "red") (cons (triangle 100 "solid" "green") empty)) (square 100 "solid" "blue") empty)) (check-expect (move-left-right test-data "left") left-data) (check-expect (move-left-right test-data "right") right-data) (check-expect (move-left-right test-data "up") test-data) (define (move-left-right a-cr a-ke) (cond [(key=? a-ke "left") (move-left a-cr)] [(key=? a-ke "right") (move-right a-cr)] [else a-cr])) ;; CR -> CR ;; move to the right of the current image if possible (check-expect (move-right right-data) right-data) (check-expect (move-right test-data) right-data) (define (move-right a-cr) (if (empty? (pictures-righties a-cr)) a-cr (make-pictures (cons (pictures-current a-cr) (pictures-lefties a-cr)) (first (pictures-righties a-cr)) (rest (pictures-righties a-cr))))) ;; CR -> CR ;; move to the left of the current image if possible (check-expect (move-left left-data) left-data) (check-expect (move-left test-data) left-data) (define (move-left a-cr) (if (empty? (pictures-lefties a-cr)) a-cr (make-pictures (rest (pictures-lefties a-cr)) (first (pictures-lefties a-cr)) (cons (pictures-current a-cr) (pictures-righties a-cr)))))