;; 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 lect09-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] [natural] [boolean]) (define-struct book (title author num-pages is-copyrighted?)) ; Examples of the data (define b1 (make-book "The Cat in the Hat" "Seuss" 37 #true)) (define b0 (make-book "Soul and Wit" "Barland" 0 #false)) ; `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) ; 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. (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. ; ;;; template (we'll come back)