;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 2-2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) #| ;; A Russian Doll (RD) is one of: ;; -- (cons "body" empty) ;; -- (list Dress RD) ;; A Dress is a string (that describes a layer of dressing). |# ;; ----------------------------------------------------------------------------- ;; since this is a given data definition, I am creating some data examples (define plain-doll '("body")) (define russian-1 (list "lace" plain-doll)) (define russ-2-lrs (list "velvet" russian-1)) ;; ----------------------------------------------------------------------------- ;; a list of dresses, which is used in two places ;; An LoD is one of: ;; -- empty ;; -- (cons Dress LoD) ;; ----------------------------------------------------------------------------- ;; RD -> LoD ;; determine the list of dresses around a Russian Doll (check-expect (unwrap russ-2-lrs) (list "velvet" "lace")) (define (unwrap rd) (cond [(empty? (rest rd)) '()] [else (cons (first rd) (unwrap (second rd)))])) ;; ----------------------------------------------------------------------------- ;; LoD -> RD ;; wrap a body with the given list of dresses (check-expect (wrap '("velvet" "lace")) russ-2-lrs) (define (wrap lod) (cond [(empty? lod) plain-doll] [else (list (first lod) (wrap (rest lod)))])) ;; ----------------------------------------------------------------------------- ;; RD -> Image ;; create an image from the Russian doll (require htdp/image) ;; I should have disallowed universe.ss ;; constants: (define SIZE 100) ;; feel free to change (define FTSZ 11) ;; feel free to change (define GAP (* FTSZ 2.5)) ;; graphical constants: (define BODY (triangle SIZE "solid" "red")) ;; NOTE: define auxilaries first so you can use them to define expected result ;; It is okay to do so becaue I am testing each auxiliary separately. -> LOCAL ;; Number Number String -> Image ;; creat a w by h frame, labeled in the upper-left corner with the string (check-expect (labeled-box 60 60 "lace") (overlay (text "lace" FTSZ "black") (put-pinhole (rectangle 60 60 "outline" "black") 2 2))) (define (labeled-box w h txt) (overlay (text txt FTSZ "black") (put-pinhole (rectangle w h "outline" "black") 2 2))) ;; Image String -> Image ;; surround the given image with a labeled box (check-expect (frame BODY "lace") (local ((define w (+ (image-width BODY) GAP)) (define h (+ (image-height BODY) GAP))) (overlay BODY (put-pinhole (labeled-box w h "lace") (/ w 2) (/ h 2))))) (define (frame img label) (local ((define w (+ (image-width img) GAP)) (define h (+ (image-height img) GAP))) (overlay img (put-pinhole (labeled-box w h label) (/ w 2) (/ h 2))))) ;; --- main function --- (check-expect (image russ-2-lrs) (frame (frame BODY "lace") "velvet")) (define (image rd) (cond [(empty? (rest rd)) BODY] [else (frame (image (second rd)) (first rd))])) (check-expect (image.v2 russ-2-lrs) (frame (frame BODY "lace") "velvet")) (define (image.v2 rd) (local (;; RD -> Image (define (image rd) (cond [(empty? (rest rd)) BODY] [else (frame (image (second rd)) (first rd))])) ;; Nat Nat String -> Image (define (labeled-box w h txt) (overlay (text txt FTSZ "black") (put-pinhole (rectangle w h "outline" "black") 2 2))) ;; Image String -> Image (define (frame img label) (local ((define w (+ (image-width img) GAP)) (define h (+ (image-height img) GAP))) (overlay img (put-pinhole (labeled-box w h label) (/ w 2) (/ h 2)))))) ;; --- in --- (image rd)))