;; 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 lect08-structs) (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"))))) ; Data definition: ; A book is: ; (make-book [string] [string] [number] [boolean]) ; (define-struct book (title author pages copyR?)) ; Examples of the data: (make-book "The Soul of Wit" "Shorty" 0 false) (define b1 (make-book "The Cat in the Hat" "Seuss" 31 true)) ; Note that 'define-struct' automagically creates ; functions for us: ; Constructor: ; make-book: string, string, number, boolean -> book ; Selectors: ; book-title : book -> string ; book-author : book -> string ; book-pages : book -> number ; book-copyR? : book -> boolean ; Predicate: ; book? : ANY -> boolean ; A dvd is: ; (make-dvd [string] [natNum] [symbol]) ; (define-struct dvd (title time rating)) ; example of the data: (define once (make-dvd "Once" 153 'PG-13)) (dvd-time once) ; watch? : dvd -> boolean ; Given a particular DVD, should we invest our precious time in watching it? ; (define (watch? a-dvd) #f) (check-expect (watch? once) #f) (check-expect (watch? (make-dvd "Bill&Ted's Excellent Adventure" 90 'PG)) #t) ;(watch? (make-dvd "Wolf of Wall Street" 170 'R) #t) ;(watch? (make-dvd "Scary Movie" 105 'R) #t) #; template #;(define (watch? a-dvd) ...(dvd-title a-dvd) ...(dvd-time a-dvd) ...(dvd-rating a-dvd)) (define (watch? a-dvd) (or (< (dvd-time a-dvd) 110) (symbol=? (dvd-rating a-dvd) 'R))) ; read-time: book -> number ; How long does it take to read `a-book`, in hours? ; (define (read-time a-book) -99) (check-expect (read-time (make-book "The Soul of Wit" "Shorty" 0 false)) 0) (check-expect (read-time b1) (+ 1 2/60)) ;;; AFTER making the test cases, we go back and write the code: