;; 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 lect10-dist-struct-intro) (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] [natnum] [boolean]) (define-struct book (title author num-pages copyrighted?)) ; Examples of the data (define b0 (make-book "Brevity 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 ; and the selectors (getters) are named ; book-title ; book-author ; book-num-pages ; book-copyrighted? (book-title b0) (book-num-pages (make-book "hello" "hmm" 1000 #f)) ; time-to-read : book -> non-negative real ; Return the amount of time to read `a-book` (in minutes). ; (define (time-to-read a-book) (* (book-num-pages a-book) 2)) (check-expect (time-to-read b1) 74) (check-expect (time-to-read b0) 0) (check-expect (time-to-read (make-book "php for all" "amy" 1003 #true)) 2006) ; book->string : book -> string ; Return a user-friendly string representing `a-book`. ; (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-copyrighted? a-book) " (c)" ""))) (check-expect (book->string b1) "The Cat in the Hat, by Seuss (37pp) (c)") (check-expect (book->string b0) "Brevity and Wit, by Barland (0pp)") ; func-for-book : book -> ... ; (define (func-for-book a-book) (... ... (book-title a-book) ... (book-author a-book) ... (book-num-pages a-book) ... (book-copyrighted? a-book))) ; derive : book, string, natnum -> book ; Create a new, derived work with additional pages. ; (define (derive original new-author added-pages) (make-book (string-append "Son of " (book-title original)) (string-append (book-author original) " & " new-author) (+ (book-num-pages original) added-pages) #false)) (check-expect (derive b1 "Barland" 5) (make-book "Son of The Cat in the Hat" "Seuss & Barland" 42 #false)) (check-expect (derive b0 "anon" 0) (make-book "Son of Brevity and Wit" "Barland & anon" 0 #false)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; TASK: come up with a data def'n for representing DVDs ; (where we care about the movie's title, its length, and rating) ; ; Hint: it should look like some of the other data def'ns we've made! ; (is it a union("OR") type, or a compound("AND") type? ; And what did we do for that type, previously?) ; ; You should be able to make it through step#3 of design recipe. ; ; Data Definition: ; A dvd is: ; (make-dvd [string] [natnum] [symbol]) (define-struct dvd (title length rating)) ; where the length is the run-time, in minutes. (make-dvd "Spiderman 5" 92 'PG) (make-dvd "" 0 'NC-17) ; TEMPLATE ; func-for-dvd : dvd -> ... ; (define (func-for-dvd a-dvd) (... (dvd-title a-dvd) (dvd-length a-dvd) (dvd-rating a-dvd))) ; ; ; ;;; ; Data definition: ; A lib-item is: ; - a book, OR ; - a dvd. ; ; Examples of data: (make-dvd "Spiderman 5" 92 'PG) (make-book "The Cat in the Hat" "Seuss" 37 #true) ; 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) (... (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. (cond [(book? an-item) (+ 1 (time-to-read an-item))] [(dvd? an-item) (+ 5 (dvd-length an-item))])) (check-expect (time-to-waste (make-dvd "Spiderman 5" 92 'PG)) 97) (check-expect (time-to-waste (make-book "The Cat in the Hat" "Seuss" 37 #true)) 75)