;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname structures) (read-case-sensitive #t) (teachpacks ((lib "universe.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.ss" "teachpack" "2htdp"))))) ;; Helene Martin, Garfield APCS++ 2010 ;; Structure examples ;; A shoe is (make-shoe string string number number) (define-struct shoe (brand color size price)) ;; Some constants for testing (define MY-SHOE (make-shoe "Nike" "red" 8.5 60)) (define YOUR-SHOE (make-shoe "Nike" "purple" 10 100)) ;; Selectors access a single field (check-expect (shoe-color MY-SHOE) "red") (check-expect (shoe-size YOUR-SHOE) 10) ;; Template for a function over a shoe ;(define (shoe-func a-shoe) ; ... (shoe-brand a-shoe) ... ; ... (shoe-color a-shoe) ... ; ... (shoe-size a-shoe) ... ; ... (shoe-price a-shoe) ... ;; is-red? shoe -> boolean ;; Takes in a shoe and returns whether that shoe is red (define (is-red? a-shoe) (string=? (shoe-color a-shoe) "red")) (check-expect (is-red? MY-SHOE) true) (check-expect (is-red? YOUR-SHOE) false) ;; discount-shoe shoe -> shoe ;; Takes in a shoe and returns one with a 50% discount (define (discount-shoe a-shoe) (make-shoe (shoe-brand a-shoe) (shoe-color a-shoe) (shoe-size a-shoe) (* .5 (shoe-price a-shoe)))) (check-expect (discount-shoe MY-SHOE) (make-shoe "Nike" "red" 8.5 30)) ;; same-size? shoe shoe -> boolean ;; Takes in two shoes and returns whether they are of the same size (define (same-size? a-shoe another-shoe) (= (shoe-size MY-SHOE) (shoe-size YOUR-SHOE))) (check-expect (same-size? MY-SHOE YOUR-SHOE) false) ;; An outfit is (make-outfit string shoe) (define-struct outfit (color shoe)) (define MY-OUTFIT (make-outfit "red" MY-SHOE)) (define YOUR-OUTFIT (make-outfit "red" YOUR-SHOE)) ;; matches? outfit -> boolean ;; Takes in an outfit and returns whether the color matches the shoes (define (matches? an-outfit) (string=? (outfit-color an-outfit) (shoe-color (outfit-shoe an-outfit)))) (check-expect (matches? MY-OUTFIT) true) (check-expect (matches? YOUR-OUTFIT) false)