;; 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 lect11-union-of-structs) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; a book has info: ;; title, author, # of pages, is copyrighted? ; Data Definiton: ; A book is: ; (make-book [string] [string] [natural] [boolean]) (define-struct book (title author num-pages is-copyrighted?)) ; Examples of the data (define b0 (make-book "Soul and Wit" "Barland" 0 #false)) (define b1 (make-book "The Cat in the Hat" "Seuss" 37 #true)) ; `define-struct` actually introduces additional functions: ; a constructor ; and four getters ("selectors"). ; In this case, the constructor is named ; make-book : string, string, natural, boolean -> book ; and the getters are named ; book-title : book -> string ; book-author ; book-num-pages ; book-is-copyrighted? (book-title (make-book "The Cat in the Hat" "Seuss" 37 #true)) (book-is-copyrighted? b0) ;;; template (we'll come back) ; func-for-book : book -> ... ; (define (func-for-book a-book) (... (book-title a-book) ... (book-author a-book) ... (book-num-pages a-book) ... (book-is-copyrighted? a-book))) ; reading-time : book -> non-negative real ; The amount of time it takes the average reader to read a book (in minutes) ; (define (reading-time a-book) (* (book-num-pages a-book) 2)) (check-expect (reading-time (make-book "The Cat in the Hat" "Seuss" 37 #true)) 74) (check-expect (reading-time b0) 0) ; book->string : book -> string ; Return a string-representation of a book struct, user-friendly. (define (book->string a-book) (string-append (book-title a-book) ", by " (book-author a-book) " (" (number->string (book-num-pages a-book)) "pp)" (if (book-is-copyrighted? a-book) ", ©" ""))) (check-expect (book->string (make-book "The Cat in the Hat" "Seuss" 37 #true)) "The Cat in the Hat, by Seuss (37pp), ©") (check-expect (book->string b0) "Soul and Wit, by Barland (0pp)") ; derive-from ; Create a brand new book, based on an original. ; The result's author will be the original author & the new-author, ; and the result has pages added to the original. ; (check-expect (derive-from b1 "Sauced" 3) (make-book "Son of The Cat in the Hat" "Seuss & Sauced" 40 #false)) (check-expect (derive-from b0 "Freund" 0) (make-book "Son of Soul and Wit" "Barland & Freund" 0 #false)) ; A DVD is: ; Data def'n: ; A lib-item is: ; - a book, OR ; - a dvd ; template: code for ANY function which processes a lib-item. ; func-for-lib-item : lib-item -> ...? ; (define (func-for-lib-item an-item) (cond [(book? an-item) ...] [(dvd? an-item) ...])) ; func-for-lib-item : lib-item -> ...? ; (define (func-for-lib-item an-item) (cond [(book? an-item) (... (book-title an-item) ... (book-author an-item) ... (book-num-pages an-item) ... (book-copyrighted? an-item))] [(dvd? an-item) (... (dvd-title an-item) ... (dvd-length an-item) ... (dvd-rating an-item))])) ; time-to-waste : lib-item -> non-negative real ; Return how much time I can waste from this library item. (define (time-to-waste an-item) ; time-to-waste: a dvd's run time plus 5min to get it going, ; or (for a book) its reading time pluse 1min to read back cover. 9999.99)